Skip to content

Commit

Permalink
sort node roles before creating the node join command
Browse files Browse the repository at this point in the history
  • Loading branch information
laverya committed Nov 15, 2023
1 parent 84dab6d commit 10ab562
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 14 deletions.
46 changes: 46 additions & 0 deletions pkg/embeddedcluster/roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package embeddedcluster
import (
"context"
"fmt"
"sort"

embeddedclusterv1beta1 "github.com/replicatedhq/embedded-cluster-operator/api/v1beta1"
)
Expand Down Expand Up @@ -37,3 +38,48 @@ func GetRoles(ctx context.Context) ([]string, error) {

return roles, nil
}

// ControllerRoleName determines the name for the 'controller' role
// this might be part of the config, or it might be the default
func ControllerRoleName(ctx context.Context) (string, error) {
conf, err := ClusterConfig(ctx)
if err != nil {
return "", fmt.Errorf("failed to get cluster config: %w", err)
}

if conf != nil && conf.Controller.Name != "" {
return conf.Controller.Name, nil
}
return DEFAULT_CONTROLLER_ROLE_NAME, nil
}

// sort roles by name, but put controller first
func SortRoles(controllerRole string, inputRoles []string) []string {
roles := inputRoles

// determine if the controller role is present
hasController := false
controllerIdx := 0
for idx, role := range roles {
if role == controllerRole {
hasController = true
controllerIdx = idx
break
}
}

// if the controller role is present, remove it
if hasController {
roles = append(roles[:controllerIdx], roles[controllerIdx+1:]...)
}

// sort the roles
sort.Strings(roles)

// if the controller role was present, add it back to the front
if hasController {
roles = append([]string{controllerRole}, roles...)
}

return roles
}
72 changes: 72 additions & 0 deletions pkg/embeddedcluster/roles_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package embeddedcluster

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestSortRoles(t *testing.T) {
tests := []struct {
name string
controllerRole string
inputRoles []string
want []string
}{
{
name: "no controller",
controllerRole: "",
inputRoles: []string{"c", "b", "a"},
want: []string{"a", "b", "c"},
},
{
name: "controller in front",
controllerRole: "controller",
inputRoles: []string{"controller", "c", "b"},
want: []string{"controller", "b", "c"},
},
{
name: "controller in middle",
controllerRole: "controller",
inputRoles: []string{"c", "controller", "b"},
want: []string{"controller", "b", "c"},
},
{
name: "controller at end",
controllerRole: "controller",
inputRoles: []string{"c", "b", "controller"},
want: []string{"controller", "b", "c"},
},
{
name: "controller not present",
controllerRole: "controller",
inputRoles: []string{"c", "b", "d"},
want: []string{"b", "c", "d"},
},
{
name: "other controller name",
controllerRole: "other-controller",
inputRoles: []string{"c", "b", "a"},
want: []string{"a", "b", "c"},
},
{
name: "other controller name in list",
controllerRole: "other-controller",
inputRoles: []string{"c", "b", "other-controller"},
want: []string{"other-controller", "b", "c"},
},
{
name: "more items",
controllerRole: "controller",
inputRoles: []string{"a", "b", "c", "controller", "e", "f", "x", "y", "z", "g", "h", "i", "j", "k", "l", "m", "n"},
want: []string{"controller", "a", "b", "c", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "x", "y", "z"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req := require.New(t)
got := SortRoles(tt.controllerRole, tt.inputRoles)
req.Equal(tt.want, got)
})
}
}
14 changes: 0 additions & 14 deletions pkg/embeddedcluster/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,3 @@ func ClusterConfig(ctx context.Context) (*embeddedclusterv1beta1.ConfigSpec, err
latest := installationList.Items[0]
return latest.Spec.Config, nil
}

// ControllerRoleName determines the name for the 'controller' role
// this might be part of the config, or it might be the default
func ControllerRoleName(ctx context.Context) (string, error) {
conf, err := ClusterConfig(ctx)
if err != nil {
return "", fmt.Errorf("failed to get cluster config: %w", err)
}

if conf != nil && conf.Controller.Name != "" {
return conf.Controller.Name, nil
}
return DEFAULT_CONTROLLER_ROLE_NAME, nil
}
3 changes: 3 additions & 0 deletions pkg/handlers/embedded_cluster_node_join_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ func (h *Handler) GetEmbeddedClusterNodeJoinCommand(w http.ResponseWriter, r *ht
}
}

// sort roles by name, but put controller first
roles = embeddedcluster.SortRoles(controllerRoleName, roles)

k0sToken, err := embeddedcluster.GenerateAddNodeToken(r.Context(), client, k0sRole)
if err != nil {
logger.Error(fmt.Errorf("failed to generate add node token: %w", err))
Expand Down

0 comments on commit 10ab562

Please sign in to comment.