Skip to content

Commit

Permalink
on join, if microcluster.Status errors, node won't be apart of the cl…
Browse files Browse the repository at this point in the history
…uster
  • Loading branch information
addyess committed May 1, 2024
1 parent 5f71d28 commit a84f419
Showing 1 changed file with 8 additions and 26 deletions.
34 changes: 8 additions & 26 deletions src/k8s/pkg/k8sd/api/cluster_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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))
}
}
Expand Down

0 comments on commit a84f419

Please sign in to comment.