Skip to content

Commit 0af2c10

Browse files
authored
Merge pull request #7587 from killianmuldoon/fix/bootstrap-secret-ownerref
🐛 Fix kubeadmconfig bootstrapsecret ownerRef reconciliation
2 parents 6c65434 + eb25ddf commit 0af2c10

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

bootstrap/kubeadm/internal/controllers/kubeadmconfig_controller.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,10 @@ func (r *KubeadmConfigReconciler) Reconcile(ctx context.Context, req ctrl.Reques
241241
}
242242
}
243243
}()
244-
244+
// Ensure the bootstrap secret associated with this KubeadmConfig has the correct ownerReference.
245+
if err := r.ensureBootstrapSecretOwnersRef(ctx, scope); err != nil {
246+
return ctrl.Result{}, err
247+
}
245248
switch {
246249
// Wait for the infrastructure to be ready.
247250
case !cluster.Status.InfrastructureReady:
@@ -1022,3 +1025,32 @@ func (r *KubeadmConfigReconciler) storeBootstrapData(ctx context.Context, scope
10221025
conditions.MarkTrue(scope.Config, bootstrapv1.DataSecretAvailableCondition)
10231026
return nil
10241027
}
1028+
1029+
// Ensure the bootstrap secret has the configOwner as a controller OwnerReference.
1030+
func (r *KubeadmConfigReconciler) ensureBootstrapSecretOwnersRef(ctx context.Context, scope *Scope) error {
1031+
secret := &corev1.Secret{}
1032+
err := r.Client.Get(ctx, client.ObjectKey{Namespace: scope.Config.Namespace, Name: scope.Config.Name}, secret)
1033+
if err != nil {
1034+
// If the secret has not been created yet return early.
1035+
if apierrors.IsNotFound(err) {
1036+
return nil
1037+
}
1038+
return errors.Wrapf(err, "failed to add KubeadmConfig %s as ownerReference to bootstrap Secret %s", scope.ConfigOwner.GetName(), secret.GetName())
1039+
}
1040+
patchHelper, err := patch.NewHelper(secret, r.Client)
1041+
if err != nil {
1042+
return errors.Wrapf(err, "failed to add KubeadmConfig %s as ownerReference to bootstrap Secret %s", scope.ConfigOwner.GetName(), secret.GetName())
1043+
}
1044+
secret.OwnerReferences = util.EnsureOwnerRef(secret.OwnerReferences, metav1.OwnerReference{
1045+
APIVersion: scope.ConfigOwner.GetAPIVersion(),
1046+
Kind: scope.ConfigOwner.GetKind(),
1047+
UID: scope.ConfigOwner.GetUID(),
1048+
Name: scope.ConfigOwner.GetName(),
1049+
Controller: pointer.Bool(true),
1050+
})
1051+
err = patchHelper.Patch(ctx, secret)
1052+
if err != nil {
1053+
return errors.Wrapf(err, "could not add KubeadmConfig %s as ownerReference to bootstrap Secret %s", scope.ConfigOwner.GetName(), secret.GetName())
1054+
}
1055+
return nil
1056+
}

bootstrap/kubeadm/internal/controllers/token.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func getToken(ctx context.Context, c client.Client, token string) (*corev1.Secre
8181
}
8282

8383
if secret.Data == nil {
84-
return nil, errors.Errorf("Invalid bootstrap secret %q, remove the token from the kubadm config to re-create", secretName)
84+
return nil, errors.Errorf("Invalid bootstrap secret %q, remove the token from the kubeadm config to re-create", secretName)
8585
}
8686
return secret, nil
8787
}

0 commit comments

Comments
 (0)