Skip to content

Commit

Permalink
change node join command API to accept a list of roles
Browse files Browse the repository at this point in the history
  • Loading branch information
laverya committed Oct 22, 2023
1 parent 4740d84 commit b4ea073
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 52 deletions.
6 changes: 2 additions & 4 deletions pkg/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,8 @@ func RegisterSessionAuthRoutes(r *mux.Router, kotsStore store.Store, handler KOT

// HelmVM
r.Name("HelmVM").Path("/api/v1/helmvm").HandlerFunc(NotImplemented)
r.Name("GenerateHelmVMNodeJoinCommandSecondary").Path("/api/v1/helmvm/generate-node-join-command-secondary").Methods("POST").
HandlerFunc(middleware.EnforceAccess(policy.ClusterWrite, handler.GenerateHelmVMNodeJoinCommandSecondary))
r.Name("GenerateHelmVMNodeJoinCommandPrimary").Path("/api/v1/helmvm/generate-node-join-command-primary").Methods("POST").
HandlerFunc(middleware.EnforceAccess(policy.ClusterWrite, handler.GenerateHelmVMNodeJoinCommandPrimary))
r.Name("GenerateHelmVMNodeJoinCommand").Path("/api/v1/helmvm/generate-node-join-command").Methods("POST").
HandlerFunc(middleware.EnforceAccess(policy.ClusterWrite, handler.GenerateHelmVMNodeJoinCommand))
r.Name("DrainHelmVMNode").Path("/api/v1/helmvm/nodes/{nodeName}/drain").Methods("POST").
HandlerFunc(middleware.EnforceAccess(policy.ClusterWrite, handler.DrainHelmVMNode))
r.Name("DeleteHelmVMNode").Path("/api/v1/helmvm/nodes/{nodeName}").Methods("DELETE").
Expand Down
34 changes: 17 additions & 17 deletions pkg/handlers/helmvm_node_join_command.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package handlers

import (
"encoding/json"
"net/http"
"time"

Expand All @@ -14,35 +15,34 @@ type GenerateHelmVMNodeJoinCommandResponse struct {
Expiry string `json:"expiry"`
}

func (h *Handler) GenerateHelmVMNodeJoinCommandSecondary(w http.ResponseWriter, r *http.Request) {
client, err := k8sutil.GetClientset()
if err != nil {
logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
type GenerateHelmVMNodeJoinCommandRequest struct {
Roles []string `json:"roles"`
}

command, expiry, err := helmvm.GenerateAddNodeCommand(r.Context(), client, "worker")
if err != nil {
func (h *Handler) GenerateHelmVMNodeJoinCommand(w http.ResponseWriter, r *http.Request) {
generateHelmVMNodeJoinCommandRequest := GenerateHelmVMNodeJoinCommandRequest{}
if err := json.NewDecoder(r.Body).Decode(&generateHelmVMNodeJoinCommandRequest); err != nil {
logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
w.WriteHeader(http.StatusBadRequest)
return
}
JSON(w, http.StatusOK, GenerateHelmVMNodeJoinCommandResponse{
Command: command,
Expiry: expiry.Format(time.RFC3339),
})
}

func (h *Handler) GenerateHelmVMNodeJoinCommandPrimary(w http.ResponseWriter, r *http.Request) {
client, err := k8sutil.GetClientset()
if err != nil {
logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}

command, expiry, err := helmvm.GenerateAddNodeCommand(r.Context(), client, "controller")
k0sRole := "worker"
for _, role := range generateHelmVMNodeJoinCommandRequest.Roles {
if role == "controller" {
k0sRole = "controller"
break
}
}

command, expiry, err := helmvm.GenerateAddNodeCommand(r.Context(), client, k0sRole)
if err != nil {
logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
Expand Down
3 changes: 1 addition & 2 deletions pkg/handlers/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ type KOTSHandler interface {
GetKurlNodes(w http.ResponseWriter, r *http.Request)

// HelmVM
GenerateHelmVMNodeJoinCommandSecondary(w http.ResponseWriter, r *http.Request)
GenerateHelmVMNodeJoinCommandPrimary(w http.ResponseWriter, r *http.Request)
GenerateHelmVMNodeJoinCommand(w http.ResponseWriter, r *http.Request)
DrainHelmVMNode(w http.ResponseWriter, r *http.Request)
DeleteHelmVMNode(w http.ResponseWriter, r *http.Request)
GetHelmVMNodes(w http.ResponseWriter, r *http.Request)
Expand Down
46 changes: 17 additions & 29 deletions pkg/handlers/mock/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b4ea073

Please sign in to comment.