Skip to content

Commit

Permalink
Add worker multinode join implementation (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
bschimke95 authored Jun 20, 2024
1 parent d5c8db6 commit 12c38eb
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 4 deletions.
18 changes: 14 additions & 4 deletions bootstrap/controllers/ck8sconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,10 +298,20 @@ func (r *CK8sConfigReconciler) joinWorker(ctx context.Context, scope *Scope) err
conditions.MarkFalse(scope.Config, bootstrapv1.DataSecretAvailableCondition, bootstrapv1.DataSecretGenerationFailedReason, clusterv1.ConditionSeverityWarning, err.Error())
return err
}
// TODO(neoaggelos): Use authToken to reach the existing k8sd control plane nodes through the k8sd proxy, and generate a token for this node.
_ = authToken
joinToken := "replace me"
// joinToken, err := ck8s.NewControlPlaneJoinToken(ctx, r.Client, authToken, ...)

if authToken == nil {
return fmt.Errorf("auth token not yet generated")
}

workloadCluster, err := r.managementCluster.GetWorkloadCluster(ctx, util.ObjectKey(scope.Cluster))
if err != nil {
return fmt.Errorf("failed to create remote cluster client: %w", err)
}

joinToken, err := workloadCluster.NewWorkerJoinToken(ctx, *authToken, scope.Config.Spec.ControlPlaneConfig.MicroclusterPort, scope.Config.Name)
if err != nil {
return fmt.Errorf("failed to request join token: %w", err)
}

configStruct := ck8s.GenerateJoinWorkerConfig(ck8s.JoinWorkerConfig{})
joinConfig, err := kubeyaml.Marshal(configStruct)
Expand Down
7 changes: 7 additions & 0 deletions pkg/ck8s/workload_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type WorkloadCluster interface {
UpdateAgentConditions(ctx context.Context, controlPlane *ControlPlane)
UpdateEtcdConditions(ctx context.Context, controlPlane *ControlPlane)
NewControlPlaneJoinToken(ctx context.Context, authToken string, microclusterPort int, name string) (string, error)
NewWorkerJoinToken(ctx context.Context, authToken string, microclusterPort int, name string) (string, error)

// NOTE(neoaggelos): See notes in (*CK8sControlPlaneReconciler).reconcileEtcdMembers
//
Expand Down Expand Up @@ -195,6 +196,12 @@ func (w *Workload) NewControlPlaneJoinToken(ctx context.Context, authToken strin
return w.requestJoinToken(ctx, microclusterPort, authToken, name, false)
}

// NewWorkerJoinToken creates a new join token for a worker node.
// NewWorkerJoinToken reaches out to the control-plane of the workload cluster via k8sd-proxy client.
func (w *Workload) NewWorkerJoinToken(ctx context.Context, authToken string, microclusterPort int, name string) (string, error) {
return w.requestJoinToken(ctx, microclusterPort, authToken, name, true)
}

// requestJoinToken requests a join token from the existing control-plane nodes via the k8sd proxy.
func (w *Workload) requestJoinToken(ctx context.Context, microclusterPort int, authToken string, name string, worker bool) (string, error) {
// FIXME: We need to check the default value for this port in one place, not in multiple places.
Expand Down
1 change: 1 addition & 0 deletions pkg/cloudinit/worker_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func NewJoinWorker(input JoinWorkerInput) (CloudConfig, error) {
config.RunCommands = append(config.RunCommands,
"/capi/scripts/load-images.sh",
"/capi/scripts/join-cluster.sh",
"/capi/scripts/create-sentinel-bootstrap.sh",
)
config.RunCommands = append(config.RunCommands, input.PostRunCommands...)

Expand Down
44 changes: 44 additions & 0 deletions pkg/cloudinit/worker_join_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package cloudinit_test

import (
"testing"

. "github.com/onsi/gomega"

"github.com/canonical/cluster-api-k8s/pkg/cloudinit"
)

func TestNewJoinWorker(t *testing.T) {
g := NewWithT(t)

config, err := cloudinit.NewJoinWorker(cloudinit.JoinWorkerInput{
BaseUserData: cloudinit.BaseUserData{
KubernetesVersion: "v1.30.0",
BootCommands: []string{"bootcmd"},
PreRunCommands: []string{"prerun1", "prerun2"},
PostRunCommands: []string{"postrun1", "postrun2"},
ExtraFiles: []cloudinit.File{{
Path: "/tmp/file",
Content: "test file",
Permissions: "0400",
Owner: "root:root",
}},
ConfigFileContents: "### config file ###",
MicroclusterAddress: ":2380",
},
})

// TODO: add tests for expected files and commands
g.Expect(err).To(BeNil())
g.Expect(config.RunCommands).To(Equal([]string{
"set -x",
"prerun1",
"prerun2",
"/capi/scripts/install.sh",
"/capi/scripts/load-images.sh",
"/capi/scripts/join-cluster.sh",
"/capi/scripts/create-sentinel-bootstrap.sh",
"postrun1",
"postrun2",
}))
}

0 comments on commit 12c38eb

Please sign in to comment.