diff --git a/src/k8s/pkg/client/dqlite/client.go b/src/k8s/pkg/client/dqlite/client.go index 051be91d39..b2acea7961 100644 --- a/src/k8s/pkg/client/dqlite/client.go +++ b/src/k8s/pkg/client/dqlite/client.go @@ -4,6 +4,7 @@ import ( "context" "crypto/tls" "crypto/x509" + "errors" "fmt" "os" @@ -11,6 +12,9 @@ import ( "github.com/canonical/go-dqlite/v2/client" ) +// ErrNotFound is returned when a dqlite member is not found. +var ErrNotFound = errors.New("dqlite member not found") + type ClientOpts struct { // ClusterYAML is the path cluster.yaml, containing the list of known dqlite nodes. ClusterYAML string diff --git a/src/k8s/pkg/client/dqlite/remove.go b/src/k8s/pkg/client/dqlite/remove.go index ab266e02f7..b1bdbfccf3 100644 --- a/src/k8s/pkg/client/dqlite/remove.go +++ b/src/k8s/pkg/client/dqlite/remove.go @@ -46,7 +46,7 @@ func (c *Client) RemoveNodeByAddress(ctx context.Context, address string) error } if !memberExists { - return fmt.Errorf("cluster does not have a node with address %v", address) + return fmt.Errorf("%w: %s", ErrNotFound, address) } if !clusterHasOtherVoters { diff --git a/src/k8s/pkg/k8sd/api/cluster_remove.go b/src/k8s/pkg/k8sd/api/cluster_remove.go index 59a0b79b56..0460107f61 100644 --- a/src/k8s/pkg/k8sd/api/cluster_remove.go +++ b/src/k8s/pkg/k8sd/api/cluster_remove.go @@ -3,6 +3,7 @@ package api import ( "context" "database/sql" + "errors" "fmt" "net" "net/http" @@ -11,6 +12,7 @@ import ( apiv1 "github.com/canonical/k8s-snap-api/api/v1" apiv1_annotations "github.com/canonical/k8s-snap-api/api/v1/annotations" + dqliteclient "github.com/canonical/k8s/pkg/client/dqlite" databaseutil "github.com/canonical/k8s/pkg/k8sd/database/util" "github.com/canonical/k8s/pkg/k8sd/types" "github.com/canonical/k8s/pkg/log" @@ -207,6 +209,10 @@ func removeNodeFromK8sDqlite(ctx context.Context, s state.State, snap snap.Snap, log.Info("Removing node from k8s-dqlite using address", "address", nodeAddress) if err := client.RemoveNodeByAddress(ctx, nodeAddress); err != nil { + if errors.Is(err, dqliteclient.ErrNotFound) { + log.Info("Node not found in k8s-dqlite cluster, nothing to remove", "address", nodeAddress) + return nil + } return fmt.Errorf("failed to remove node from k8s-dqlite cluster: %w", err) } return nil