Skip to content

Commit

Permalink
Add machine names as etcd name (#778)
Browse files Browse the repository at this point in the history
Signed-off-by: Adrian Pedriza <[email protected]>
Co-authored-by: Adrian Pedriza <[email protected]>
  • Loading branch information
apedriza and AdrianPedriza authored Oct 29, 2024
1 parent 0b786c5 commit e84413c
Showing 1 changed file with 42 additions and 11 deletions.
53 changes: 42 additions & 11 deletions internal/controller/bootstrap/controlplane_bootstrap_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import (
bootstrapv1 "github.com/k0sproject/k0smotron/api/bootstrap/v1beta1"
"github.com/k0sproject/k0smotron/internal/cloudinit"
kutil "github.com/k0sproject/k0smotron/internal/util"
"github.com/k0sproject/version"
)

type ControlPlaneController struct {
Expand All @@ -63,6 +64,8 @@ type ControlPlaneController struct {

const joinTokenFilePath = "/etc/k0s.token"

var minVersionForETCDName = version.MustParse("v1.31.1+k0s.0")

// +kubebuilder:rbac:groups=bootstrap.cluster.x-k8s.io,resources=k0scontrollerconfigs,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=bootstrap.cluster.x-k8s.io,resources=k0scontrollerconfigs/status,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=cluster.x-k8s.io,resources=clusters;clusters/status;machines;machines/status,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -155,6 +158,7 @@ func (c *ControlPlaneController) Reconcile(ctx context.Context, req ctrl.Request
)

if config.Spec.K0s != nil {

nllbEnabled, found, err := unstructured.NestedBool(config.Spec.K0s.Object, "spec", "network", "nodeLocalLoadBalancing", "enabled")
if err != nil {
return ctrl.Result{}, fmt.Errorf("error getting nodeLocalLoadBalancing: %v", err)
Expand Down Expand Up @@ -190,17 +194,6 @@ func (c *ControlPlaneController) Reconcile(ctx context.Context, req ctrl.Request
}
}

k0sConfigBytes, err := config.Spec.K0s.MarshalJSON()
if err != nil {
return ctrl.Result{}, fmt.Errorf("error marshalling k0s config: %v", err)
}
files = append(files, cloudinit.File{
Path: "/etc/k0s.yaml",
Permissions: "0644",
Content: string(k0sConfigBytes),
})
config.Spec.Args = append(config.Spec.Args, "--config", "/etc/k0s.yaml")

// Reconcile the dynamic config
dErr := kutil.ReconcileDynamicConfig(ctx, cluster, c.Client, *config.Spec.K0s)
if dErr != nil {
Expand All @@ -209,6 +202,44 @@ func (c *ControlPlaneController) Reconcile(ctx context.Context, req ctrl.Request
}
}

currentKCPVersion, err := version.NewVersion(config.Spec.Version)
if err != nil {
return ctrl.Result{}, fmt.Errorf("error parsing k0s version: %w", err)
}
if currentKCPVersion.GreaterThanOrEqual(minVersionForETCDName) {
if config.Spec.K0s == nil {
config.Spec.K0s = &unstructured.Unstructured{
Object: make(map[string]interface{}),
}
}
// If it is not explicitly indicated to use Kine storage, we use the machine name to name the ETCD member.
kineStorage, found, err := unstructured.NestedString(config.Spec.K0s.Object, "spec", "storage", "kine", "dataSource")
if err != nil {
return ctrl.Result{}, fmt.Errorf("error retrieving storage.kine.datasource: %w", err)
}
if !found || kineStorage == "" {
err = unstructured.SetNestedMap(config.Spec.K0s.Object, map[string]interface{}{}, "spec", "storage", "etcd", "extraArgs")
if err != nil {
return ctrl.Result{}, fmt.Errorf("error ensuring intermediate maps spec.storage.etcd.extraArgs: %w", err)
}
err = unstructured.SetNestedField(config.Spec.K0s.Object, machine.Name, "spec", "storage", "etcd", "extraArgs", "name")
if err != nil {
return ctrl.Result{}, fmt.Errorf("error setting storage.etcd.extraArgs.name: %w", err)
}
}
}

k0sConfigBytes, err := config.Spec.K0s.MarshalJSON()
if err != nil {
return ctrl.Result{}, fmt.Errorf("error marshalling k0s config: %v", err)
}
files = append(files, cloudinit.File{
Path: "/etc/k0s.yaml",
Permissions: "0644",
Content: string(k0sConfigBytes),
})
config.Spec.Args = append(config.Spec.Args, "--config", "/etc/k0s.yaml")

if config.Status.Ready {
return ctrl.Result{}, nil
}
Expand Down

0 comments on commit e84413c

Please sign in to comment.