Skip to content

Commit

Permalink
Create new response API codes for specific Leave/Join failures (#388)
Browse files Browse the repository at this point in the history
  • Loading branch information
addyess authored May 8, 2024
1 parent 4e9d343 commit 0dd0ba2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src/k8s/pkg/k8sd/api/cluster_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func (e *Endpoints) postClusterJoin(s *state.State, r *http.Request) response.Re
return response.BadRequest(fmt.Errorf("invalid hostname %q: %w", req.Name, err))
}

if _, err := e.provider.MicroCluster().Status(r.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
Expand Down
4 changes: 2 additions & 2 deletions src/k8s/pkg/k8sd/api/cluster_remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (e *Endpoints) postClusterRemove(s *state.State, r *http.Request) response.

isWorker, err := databaseutil.IsWorkerNode(r.Context(), s, req.Name)
if err != nil {
return response.InternalError(fmt.Errorf("failed to check if node is control-plane: %w", err))
return response.InternalError(fmt.Errorf("failed to check if node is worker: %w", err))
}
if isWorker {
// For worker nodes, we need to manually clean up the kubernetes node and db entry.
Expand All @@ -57,7 +57,7 @@ func (e *Endpoints) postClusterRemove(s *state.State, r *http.Request) response.
}

if !isWorker && !isControlPlane {
return response.InternalError(fmt.Errorf("node %q is not part of the cluster", req.Name))
return NodeUnavailable(fmt.Errorf("node %q is not part of the cluster", req.Name))
}
return response.SyncResponse(true, nil)
}
20 changes: 20 additions & 0 deletions src/k8s/pkg/k8sd/api/response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package api

import (
"github.com/canonical/lxd/lxd/response"
)

const (
// StatusNodeUnavailable is the Http status code that the API returns if the node isn't in the cluster
StatusNodeUnavailable = 520
// StatusNodeInUse is the Http status code that the API returns if the node is already in the cluster
StatusNodeInUse = 521
)

func NodeUnavailable(err error) response.Response {
return response.ErrorResponse(StatusNodeUnavailable, err.Error())
}

func NodeInUse(err error) response.Response {
return response.ErrorResponse(StatusNodeInUse, err.Error())
}

0 comments on commit 0dd0ba2

Please sign in to comment.