Skip to content

Commit

Permalink
Support setting node Hostname to match Serverclaim name
Browse files Browse the repository at this point in the history
  • Loading branch information
defo89 committed Sep 4, 2024
1 parent 62d325a commit f6cd29b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ RUN --mount=type=cache,target=/root/.cache/go-build \
# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM gcr.io/distroless/static:nonroot AS manager
LABEL source_repository="https://github.com/ironcore-dev/cluster-api-provider-metal"
WORKDIR /
COPY --from=builder /workspace/manager .
USER 65532:65532
Expand Down
21 changes: 17 additions & 4 deletions internal/controller/metalmachine_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package controller
import (
"context"
"fmt"
"strings"

"github.com/go-logr/logr"
"github.com/ironcore-dev/cluster-api-provider-metal/internal/scope"
Expand Down Expand Up @@ -220,7 +221,7 @@ func (r *MetalMachineReconciler) reconcileNormal(ctx context.Context, machineSco
}

machineScope.Info("Creating IgnitionSecret", "Secret", machineScope.MetalMachine.Name)
ignitionSecret, err := r.applyIgnitionSecret(ctx, machineScope.Logger, bootstrapSecret)
ignitionSecret, err := r.applyIgnitionSecret(ctx, machineScope.Logger, machineScope.MetalMachine, bootstrapSecret)
if err != nil {
machineScope.Error(err, "failed to create or patch ignition secret")
return ctrl.Result{}, err
Expand Down Expand Up @@ -253,7 +254,10 @@ func (r *MetalMachineReconciler) reconcileNormal(ctx context.Context, machineSco
return reconcile.Result{}, nil
}

func (r *MetalMachineReconciler) applyIgnitionSecret(ctx context.Context, log *logr.Logger, capidatasecret *corev1.Secret) (*corev1.Secret, error) {
func (r *MetalMachineReconciler) applyIgnitionSecret(ctx context.Context, log *logr.Logger, metalmachine *infrav1alpha1.MetalMachine, capidatasecret *corev1.Secret) (*corev1.Secret, error) {
dataSecret := capidatasecret
findAndReplaceIgnition(metalmachine, dataSecret)

secretObj := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("ignition-%s", capidatasecret.Name),
Expand All @@ -264,8 +268,7 @@ func (r *MetalMachineReconciler) applyIgnitionSecret(ctx context.Context, log *l
APIVersion: corev1.SchemeGroupVersion.String(),
},
Data: map[string][]byte{
// TODO: Make Metal Specific changes in the Ignition if necessary
DefaultIgnitionSecretKeyName: capidatasecret.Data["value"],
DefaultIgnitionSecretKeyName: dataSecret.Data["value"],
},
}

Expand Down Expand Up @@ -341,3 +344,13 @@ func (r *MetalMachineReconciler) ensureServerClaimBound(ctx context.Context, ser
}
return true, nil
}

func findAndReplaceIgnition(metalmachine *infrav1alpha1.MetalMachine, capidatasecret *corev1.Secret) {
data := capidatasecret.Data["value"]

// replace $${METAL_HOSTNAME} with machine name
hostname := "%24%24%7BMETAL_HOSTNAME%7D"
modifiedData := strings.ReplaceAll(string(data), hostname, metalmachine.Name)

capidatasecret.Data["value"] = []byte(modifiedData)
}
4 changes: 2 additions & 2 deletions templates/test/cluster_v1beta1_cluster.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ metadata:
name: cluster-sample
spec:
controlPlaneRef:
apiVersion: controlplane.cluster.x-k8s.io/v1alpha1
apiVersion: controlplane.cluster.x-k8s.io/v1beta1
kind: KubeadmControlPlane
name: kubeadmcontrolplane-sample-cp
infrastructureRef:
apiVersion: infrastructure.cluster.x-k8s.io/v1beta1
apiVersion: infrastructure.cluster.x-k8s.io/v1alpha1
kind: MetalCluster
name: metalcluster-sample
3 changes: 1 addition & 2 deletions templates/test/cluster_v1beta1_kubeadmcontrolplane.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,5 @@ spec:
[TEST]
test=1
preKubeadmCommands:
- envsubst < /etc/kubeadm.yml > /etc/kubeadm.yml.tmp
- mv /etc/kubeadm.yml.tmp /etc/kubeadm.yml
- hostnamectl set-hostname $${METAL_HOSTNAME}
version: 1.29.4

0 comments on commit f6cd29b

Please sign in to comment.