From a84f419ddc9f83f927a4564dd0cd8fe416f11f06 Mon Sep 17 00:00:00 2001 From: Adam Dyess Date: Wed, 1 May 2024 13:36:05 -0500 Subject: [PATCH] on join, if microcluster.Status errors, node won't be apart of the cluster --- src/k8s/pkg/k8sd/api/cluster_join.go | 34 +++++++--------------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/src/k8s/pkg/k8sd/api/cluster_join.go b/src/k8s/pkg/k8sd/api/cluster_join.go index 011b8eb2d..8c71c80ac 100644 --- a/src/k8s/pkg/k8sd/api/cluster_join.go +++ b/src/k8s/pkg/k8sd/api/cluster_join.go @@ -5,10 +5,8 @@ import ( "net/http" apiv1 "github.com/canonical/k8s/api/v1" - databaseutil "github.com/canonical/k8s/pkg/k8sd/database/util" "github.com/canonical/k8s/pkg/k8sd/types" "github.com/canonical/k8s/pkg/utils" - nodeutil "github.com/canonical/k8s/pkg/utils/node" "github.com/canonical/lxd/lxd/response" "github.com/canonical/microcluster/state" ) @@ -24,42 +22,26 @@ func (e *Endpoints) postClusterJoin(s *state.State, r *http.Request) response.Re return response.BadRequest(fmt.Errorf("invalid hostname %q: %w", req.Name, err)) } + context := r.Context() + if _, err := e.provider.MicroCluster().Status(context); err == nil { + return NodeInUse(fmt.Errorf("node %q is part of the cluster", hostname)) + } + config := map[string]string{} internalToken := types.InternalWorkerNodeToken{} // Check if token is worker token - - workerToken := internalToken.Decode(req.Token) == nil - - if workerToken { - isWorker, err := databaseutil.IsWorkerNode(r.Context(), s, hostname) - if err != nil { - return response.InternalError(fmt.Errorf("failed to check if node is worker: %w", err)) - } - if isWorker { - return NodeInUse(fmt.Errorf("node %q is part of the cluster", hostname)) - } - } else { - isControlPlane, err := nodeutil.IsControlPlaneNode(r.Context(), s, hostname) - if err != nil { - return response.InternalError(fmt.Errorf("failed to check if node is control-plane: %w", err)) - } - if isControlPlane { - return NodeInUse(fmt.Errorf("node %q is part of the cluster", hostname)) - } - } - - if workerToken { + if internalToken.Decode(req.Token) == nil { // valid worker node token - let's join the cluster // The validation of the token is done when fetching the cluster information. config["workerToken"] = req.Token config["workerJoinConfig"] = req.Config - if err := e.provider.MicroCluster().NewCluster(r.Context(), hostname, req.Address, config); err != nil { + if err := e.provider.MicroCluster().NewCluster(context, hostname, req.Address, config); err != nil { return response.InternalError(fmt.Errorf("failed to join k8sd cluster as worker: %w", err)) } } else { // Is not a worker token. let microcluster check if it is a valid control-plane token. config["controlPlaneJoinConfig"] = req.Config - if err := e.provider.MicroCluster().JoinCluster(r.Context(), hostname, req.Address, req.Token, config); err != nil { + if err := e.provider.MicroCluster().JoinCluster(context, hostname, req.Address, req.Token, config); err != nil { return response.InternalError(fmt.Errorf("failed to join k8sd cluster as control plane: %w", err)) } }