Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 Fix kubeadmconfig bootstrapsecret ownerRef reconciliation #7587

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,10 @@ func (r *KubeadmConfigReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}
}
}()

// Ensure the bootstrap secret associated with this KubeadmConfig has the correct ownerReference.
if err := r.ensureBootstrapSecretOwnersRef(ctx, scope); err != nil {
return ctrl.Result{}, err
}
switch {
// Wait for the infrastructure to be ready.
case !cluster.Status.InfrastructureReady:
Expand Down Expand Up @@ -1022,3 +1025,32 @@ func (r *KubeadmConfigReconciler) storeBootstrapData(ctx context.Context, scope
conditions.MarkTrue(scope.Config, bootstrapv1.DataSecretAvailableCondition)
return nil
}

// Ensure the bootstrap secret has the configOwner as a controller OwnerReference.
func (r *KubeadmConfigReconciler) ensureBootstrapSecretOwnersRef(ctx context.Context, scope *Scope) error {
secret := &corev1.Secret{}
err := r.Client.Get(ctx, client.ObjectKey{Namespace: scope.Config.Namespace, Name: scope.Config.Name}, secret)
if err != nil {
// If the secret has not been created yet return early.
if apierrors.IsNotFound(err) {
return nil
}
return errors.Wrapf(err, "failed to add KubeadmConfig %s as ownerReference to bootstrap Secret %s", scope.ConfigOwner.GetName(), secret.GetName())
}
patchHelper, err := patch.NewHelper(secret, r.Client)
if err != nil {
return errors.Wrapf(err, "failed to add KubeadmConfig %s as ownerReference to bootstrap Secret %s", scope.ConfigOwner.GetName(), secret.GetName())
}
secret.OwnerReferences = util.EnsureOwnerRef(secret.OwnerReferences, metav1.OwnerReference{
APIVersion: scope.ConfigOwner.GetAPIVersion(),
Kind: scope.ConfigOwner.GetKind(),
UID: scope.ConfigOwner.GetUID(),
Name: scope.ConfigOwner.GetName(),
Controller: pointer.Bool(true),
})
err = patchHelper.Patch(ctx, secret)
if err != nil {
return errors.Wrapf(err, "could not add KubeadmConfig %s as ownerReference to bootstrap Secret %s", scope.ConfigOwner.GetName(), secret.GetName())
}
return nil
}
2 changes: 1 addition & 1 deletion bootstrap/kubeadm/internal/controllers/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func getToken(ctx context.Context, c client.Client, token string) (*corev1.Secre
}

if secret.Data == nil {
return nil, errors.Errorf("Invalid bootstrap secret %q, remove the token from the kubadm config to re-create", secretName)
return nil, errors.Errorf("Invalid bootstrap secret %q, remove the token from the kubeadm config to re-create", secretName)
}
return secret, nil
}
Expand Down