Skip to content

Commit

Permalink
Fix kubeadmconfig bootstrapsecret ownerRef reconciliation
Browse files Browse the repository at this point in the history
Signed-off-by: killianmuldoon <kmuldoon@vmware.com>
  • Loading branch information
killianmuldoon committed Nov 23, 2022
1 parent 0ba4a7a commit eb25ddf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
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

0 comments on commit eb25ddf

Please sign in to comment.