Skip to content

Commit 5016b30

Browse files
committed
fix: improve k8s-dqlite node removal handling
`dqlite.Client.RemoveNodeByAddress` now returns `ErrNotFound` error if the member is not found. This error is now being handled properly in `removeNodeFromK8sDqlite` (`cluster_remove.go`). By doing so, we're making the k8s-dqlite node removal operation idempotent on subsequent calls. Signed-off-by: Claudiu Belu <cbelu@cloudbasesolutions.com>
1 parent baff70f commit 5016b30

3 files changed

Lines changed: 11 additions & 1 deletion

File tree

src/k8s/pkg/client/dqlite/client.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@ import (
44
"context"
55
"crypto/tls"
66
"crypto/x509"
7+
"errors"
78
"fmt"
89
"os"
910

1011
"github.com/canonical/go-dqlite/v2/app"
1112
"github.com/canonical/go-dqlite/v2/client"
1213
)
1314

15+
// ErrNotFound is returned when a dqlite member is not found.
16+
var ErrNotFound = errors.New("dqlite member not found")
17+
1418
type ClientOpts struct {
1519
// ClusterYAML is the path cluster.yaml, containing the list of known dqlite nodes.
1620
ClusterYAML string

src/k8s/pkg/client/dqlite/remove.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (c *Client) RemoveNodeByAddress(ctx context.Context, address string) error
4646
}
4747

4848
if !memberExists {
49-
return fmt.Errorf("cluster does not have a node with address %v", address)
49+
return fmt.Errorf("%w: %s", ErrNotFound, address)
5050
}
5151

5252
if !clusterHasOtherVoters {

src/k8s/pkg/k8sd/api/cluster_remove.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package api
33
import (
44
"context"
55
"database/sql"
6+
"errors"
67
"fmt"
78
"net"
89
"net/http"
@@ -11,6 +12,7 @@ import (
1112

1213
apiv1 "github.com/canonical/k8s-snap-api/api/v1"
1314
apiv1_annotations "github.com/canonical/k8s-snap-api/api/v1/annotations"
15+
dqliteclient "github.com/canonical/k8s/pkg/client/dqlite"
1416
databaseutil "github.com/canonical/k8s/pkg/k8sd/database/util"
1517
"github.com/canonical/k8s/pkg/k8sd/types"
1618
"github.com/canonical/k8s/pkg/log"
@@ -207,6 +209,10 @@ func removeNodeFromK8sDqlite(ctx context.Context, s state.State, snap snap.Snap,
207209

208210
log.Info("Removing node from k8s-dqlite using address", "address", nodeAddress)
209211
if err := client.RemoveNodeByAddress(ctx, nodeAddress); err != nil {
212+
if errors.Is(err, dqliteclient.ErrNotFound) {
213+
log.Info("Node not found in k8s-dqlite cluster, nothing to remove", "address", nodeAddress)
214+
return nil
215+
}
210216
return fmt.Errorf("failed to remove node from k8s-dqlite cluster: %w", err)
211217
}
212218
return nil

0 commit comments

Comments
 (0)