Skip to content

Commit ebe0325

Browse files
committed
Read userdata from bootstrap secret.
1 parent 2260356 commit ebe0325

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

api/v1alpha1/metalstackmachine_types.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ const (
2929
TagInfraMachineID = "metal-stack.infrastructure.cluster.x-k8s.io/machine-id"
3030

3131
ProviderMachineCreated clusterv1.ConditionType = "MachineCreated"
32+
ProviderMachineReady clusterv1.ConditionType = "MachineReady"
3233
ProviderMachineHealthy clusterv1.ConditionType = "MachineHealthy"
3334
)
3435

config/samples/example.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ apiVersion: controlplane.cluster.x-k8s.io/v1beta1
3434
metadata:
3535
name: metal-test-controlplane
3636
spec:
37+
kubeadmConfigSpec:
38+
format: ignition
39+
initConfiguration:
40+
nodeRegistration: {}
41+
joinConfiguration:
42+
controlPlane: {}
43+
nodeRegistration: {}
3744
machineTemplate:
3845
nodeDrainTimeout: 10m
3946
infrastructureRef:

internal/controller/metalstackcluster_controller.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ func (r *MetalStackClusterReconciler) Reconcile(ctx context.Context, req ctrl.Re
113113
statusErr := reconciler.status()
114114
if statusErr != nil {
115115
err = errors.Join(err, fmt.Errorf("unable to update status: %w", statusErr))
116+
} else if !reconciler.infraCluster.Status.Ready {
117+
err = errors.New("cluster is not yet ready, requeueing")
116118
}
117119
}()
118120

internal/controller/metalstackmachine_controller.go

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ func (r *MetalStackMachineReconciler) Reconcile(ctx context.Context, req ctrl.Re
143143
statusErr := reconciler.status()
144144
if statusErr != nil {
145145
err = errors.Join(err, fmt.Errorf("unable to update status: %w", statusErr))
146+
} else if !reconciler.infraMachine.Status.Ready {
147+
err = errors.New("machine is not yet ready, requeueing")
146148
}
147149
}()
148150

@@ -188,6 +190,10 @@ func (r *MetalStackMachineReconciler) Reconcile(ctx context.Context, req ctrl.Re
188190
return ctrl.Result{}, errors.New("waiting until control plane ip was set to cluster spec")
189191
}
190192

193+
if machine.Spec.Bootstrap.DataSecretName == nil {
194+
return ctrl.Result{}, errors.New("waiting until bootstrap data secret was created")
195+
}
196+
191197
err = reconciler.reconcile()
192198

193199
return ctrl.Result{}, err // remember to return err here and not nil because the defer func can influence this
@@ -256,6 +262,17 @@ func (r *machineReconciler) delete() error {
256262
}
257263

258264
func (r *machineReconciler) create() (*models.V1MachineResponse, error) {
265+
bootstrapSecret := &corev1.Secret{
266+
ObjectMeta: metav1.ObjectMeta{
267+
Name: *r.clusterMachine.Spec.Bootstrap.DataSecretName,
268+
Namespace: r.infraMachine.Namespace,
269+
},
270+
}
271+
err := r.client.Get(r.ctx, client.ObjectKeyFromObject(bootstrapSecret), bootstrapSecret)
272+
if err != nil {
273+
return nil, fmt.Errorf("unable to fetch bootstrap secret: %w", err)
274+
}
275+
259276
var (
260277
ips []string
261278
nws = []*models.V1MachineAllocationNetwork{
@@ -292,7 +309,8 @@ func (r *machineReconciler) create() (*models.V1MachineResponse, error) {
292309
Description: fmt.Sprintf("%s/%s for cluster %s/%s", r.infraMachine.Namespace, r.infraMachine.Name, r.infraCluster.Namespace, r.infraCluster.Name),
293310
Networks: nws,
294311
Ips: ips,
295-
// TODO: UserData, SSHPubKeys, ...
312+
UserData: string(bootstrapSecret.Data["value"]),
313+
// TODO: SSHPubKeys, ...
296314
}), nil)
297315
if err != nil {
298316
return nil, fmt.Errorf("failed to allocate machine: %w", err)
@@ -330,9 +348,11 @@ func (r *machineReconciler) status() error {
330348
case err != nil && !errors.Is(err, errProviderMachineNotFound):
331349
conditions.MarkFalse(r.infraMachine, v1alpha1.ProviderMachineCreated, "InternalError", clusterv1.ConditionSeverityError, "%s", err.Error())
332350
conditions.MarkFalse(r.infraMachine, v1alpha1.ProviderMachineHealthy, "NotHealthy", clusterv1.ConditionSeverityWarning, "machine not created")
351+
conditions.MarkFalse(r.infraMachine, v1alpha1.ProviderMachineReady, "NotReady", clusterv1.ConditionSeverityWarning, "machine not created")
333352
case err != nil && errors.Is(err, errProviderMachineNotFound):
334353
conditions.MarkFalse(r.infraMachine, v1alpha1.ProviderMachineCreated, "NotCreated", clusterv1.ConditionSeverityError, "%s", err.Error())
335354
conditions.MarkFalse(r.infraMachine, v1alpha1.ProviderMachineHealthy, "NotHealthy", clusterv1.ConditionSeverityWarning, "machine not created")
355+
conditions.MarkFalse(r.infraMachine, v1alpha1.ProviderMachineReady, "NotReady", clusterv1.ConditionSeverityWarning, "machine not created")
336356
default:
337357
if r.infraMachine.Spec.ProviderID == *m.ID {
338358
conditions.MarkTrue(r.infraMachine, v1alpha1.ProviderMachineCreated)
@@ -357,6 +377,12 @@ func (r *machineReconciler) status() error {
357377
}
358378
}
359379

380+
if m.Events != nil && len(m.Events.Log) > 0 && ptr.Deref(m.Events.Log[0].Event, "") == "Phoned Home" {
381+
conditions.MarkTrue(r.infraMachine, v1alpha1.ProviderMachineReady)
382+
} else {
383+
conditions.MarkFalse(r.infraMachine, v1alpha1.ProviderMachineReady, "NotReady", clusterv1.ConditionSeverityWarning, "machine is not in phoned home state")
384+
}
385+
360386
if len(errs) == 0 {
361387
conditions.MarkTrue(r.infraMachine, v1alpha1.ProviderMachineHealthy)
362388
} else {

0 commit comments

Comments
 (0)