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

🐛 KubeadmControlPlane should adopt v1alpha2 kubeconfig secrets #4050

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
2 changes: 1 addition & 1 deletion controlplane/kubeadm/controllers/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ func (r *KubeadmControlPlaneReconciler) reconcile(ctx context.Context, cluster *
}

// Generate Cluster Kubeconfig if needed
if err := r.reconcileKubeconfig(ctx, util.ObjectKey(cluster), cluster.Spec.ControlPlaneEndpoint, kcp); err != nil {
if err := r.reconcileKubeconfig(ctx, cluster, kcp); err != nil {
logger.Error(err, "failed to reconcile Kubeconfig")
return ctrl.Result{}, err
}
Expand Down
33 changes: 31 additions & 2 deletions controlplane/kubeadm/controllers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ import (
"sigs.k8s.io/cluster-api/util/kubeconfig"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/cluster-api/util/secret"
"sigs.k8s.io/controller-runtime/pkg/client"
)

func (r *KubeadmControlPlaneReconciler) reconcileKubeconfig(ctx context.Context, clusterName client.ObjectKey, endpoint clusterv1.APIEndpoint, kcp *controlplanev1.KubeadmControlPlane) error {
func (r *KubeadmControlPlaneReconciler) reconcileKubeconfig(ctx context.Context, cluster *clusterv1.Cluster, kcp *controlplanev1.KubeadmControlPlane) error {
endpoint := cluster.Spec.ControlPlaneEndpoint
if endpoint.IsZero() {
return nil
}

controllerOwnerRef := *metav1.NewControllerRef(kcp, controlplanev1.GroupVersion.WithKind("KubeadmControlPlane"))
clusterName := util.ObjectKey(cluster)
configSecret, err := secret.GetFromNamespacedName(ctx, r.Client, clusterName, secret.Kubeconfig)
switch {
case apierrors.IsNotFound(errors.Cause(err)):
Expand All @@ -68,6 +69,14 @@ func (r *KubeadmControlPlaneReconciler) reconcileKubeconfig(ctx context.Context,
return errors.Wrap(err, "failed to retrieve kubeconfig Secret")
}

// check if the kubeconfig secret was created by v1alpha2 controllers, and thus it has the Cluster as the owner instead of KCP;
// if yes, adopt it.
if util.IsOwnedByObject(configSecret, cluster) && !util.IsControlledBy(configSecret, kcp) {
if err := r.adoptKubeconfigSecret(ctx, cluster, configSecret, controllerOwnerRef); err != nil {
return err
}
}

// only do rotation on owned secrets
if !util.IsControlledBy(configSecret, kcp) {
return nil
Expand All @@ -88,6 +97,26 @@ func (r *KubeadmControlPlaneReconciler) reconcileKubeconfig(ctx context.Context,
return nil
}

func (r *KubeadmControlPlaneReconciler) adoptKubeconfigSecret(ctx context.Context, cluster *clusterv1.Cluster, configSecret *corev1.Secret, controllerOwnerRef metav1.OwnerReference) error {
r.Log.Info("Adopting KubeConfig secret created by v1alpha2 controllers", "Name", configSecret.Name)

patchHelper, err := patch.NewHelper(configSecret, r.Client)
if err != nil {
return errors.Wrap(err, "failed to create patch helper for the kubeconfig secret")
}
configSecret.OwnerReferences = util.RemoveOwnerRef(configSecret.OwnerReferences, metav1.OwnerReference{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Cluster",
Name: cluster.Name,
UID: cluster.UID,
})
configSecret.OwnerReferences = util.EnsureOwnerRef(configSecret.OwnerReferences, controllerOwnerRef)
if err := patchHelper.Patch(ctx, configSecret); err != nil {
return errors.Wrap(err, "failed to patch the kubeconfig secret")
}
return nil
}

func (r *KubeadmControlPlaneReconciler) reconcileExternalReference(ctx context.Context, cluster *clusterv1.Cluster, ref corev1.ObjectReference) error {
if !strings.HasSuffix(ref.Kind, external.TemplateSuffix) {
return nil
Expand Down
253 changes: 114 additions & 139 deletions controlplane/kubeadm/controllers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,48 +34,32 @@ import (
"sigs.k8s.io/cluster-api/controllers/external"
controlplanev1 "sigs.k8s.io/cluster-api/controlplane/kubeadm/api/v1alpha3"
"sigs.k8s.io/cluster-api/controlplane/kubeadm/internal"
"sigs.k8s.io/cluster-api/util"
"sigs.k8s.io/cluster-api/util/kubeconfig"
"sigs.k8s.io/cluster-api/util/secret"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/log"
)

func TestReconcileKubeconfigEmptyAPIEndpoints(t *testing.T) {
g := NewWithT(t)

kcp := &controlplanev1.KubeadmControlPlane{
func TestReconcileKubeconfig(t *testing.T) {
cluster := &clusterv1.Cluster{
TypeMeta: metav1.TypeMeta{
Kind: "Cluster",
APIVersion: clusterv1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "test",
},
Spec: controlplanev1.KubeadmControlPlaneSpec{
Version: "v1.16.6",
Spec: clusterv1.ClusterSpec{
ControlPlaneEndpoint: clusterv1.APIEndpoint{Host: "test.local", Port: 8443},
},
}
clusterName := client.ObjectKey{Namespace: "test", Name: "foo"}

fakeClient := newFakeClient(g, kcp.DeepCopy())
r := &KubeadmControlPlaneReconciler{
Client: fakeClient,
Log: log.Log,
recorder: record.NewFakeRecorder(32),
}

g.Expect(r.reconcileKubeconfig(context.Background(), clusterName, clusterv1.APIEndpoint{}, kcp)).To(Succeed())

kubeconfigSecret := &corev1.Secret{}
secretName := client.ObjectKey{
Namespace: "test",
Name: secret.Name(clusterName.Name, secret.Kubeconfig),
}
g.Expect(r.Client.Get(context.Background(), secretName, kubeconfigSecret)).To(MatchError(ContainSubstring("not found")))
}

func TestReconcileKubeconfigMissingCACertificate(t *testing.T) {
g := NewWithT(t)

kcp := &controlplanev1.KubeadmControlPlane{
TypeMeta: metav1.TypeMeta{
Kind: "KubeadmControlPlane",
APIVersion: controlplanev1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: "test",
Expand All @@ -84,122 +68,113 @@ func TestReconcileKubeconfigMissingCACertificate(t *testing.T) {
Version: "v1.16.6",
},
}
clusterName := client.ObjectKey{Namespace: "test", Name: "foo"}
endpoint := clusterv1.APIEndpoint{Host: "test.local", Port: 8443}

fakeClient := newFakeClient(g, kcp.DeepCopy())
r := &KubeadmControlPlaneReconciler{
Client: fakeClient,
Log: log.Log,
recorder: record.NewFakeRecorder(32),
}

g.Expect(r.reconcileKubeconfig(context.Background(), clusterName, endpoint, kcp)).NotTo(Succeed())

kubeconfigSecret := &corev1.Secret{}
secretName := client.ObjectKey{
Namespace: "test",
Name: secret.Name(clusterName.Name, secret.Kubeconfig),
}
g.Expect(r.Client.Get(context.Background(), secretName, kubeconfigSecret)).To(MatchError(ContainSubstring("not found")))
}

func TestReconcileKubeconfigSecretAlreadyExists(t *testing.T) {
g := NewWithT(t)

cluster := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
t.Run("Empty API Endpoints", func(t *testing.T) {
g := NewWithT(t)
fakeClient := newFakeClient(g, kcp.DeepCopy())
r := &KubeadmControlPlaneReconciler{
Client: fakeClient,
Log: log.Log,
recorder: record.NewFakeRecorder(32),
}

c := cluster.DeepCopy()
c.Spec.ControlPlaneEndpoint = clusterv1.APIEndpoint{}
g.Expect(r.reconcileKubeconfig(context.Background(), c, kcp)).To(Succeed())

kubeconfigSecret := &corev1.Secret{}
secretName := client.ObjectKey{
Namespace: "test",
},
}

kcp := &controlplanev1.KubeadmControlPlane{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Name: secret.Name(c.Name, secret.Kubeconfig),
}
g.Expect(r.Client.Get(context.Background(), secretName, kubeconfigSecret)).To(MatchError(ContainSubstring("not found")))
})

t.Run("Missing CA Certificate", func(t *testing.T) {
g := NewWithT(t)
fakeClient := newFakeClient(g, kcp.DeepCopy())
r := &KubeadmControlPlaneReconciler{
Client: fakeClient,
Log: log.Log,
recorder: record.NewFakeRecorder(32),
}

g.Expect(r.reconcileKubeconfig(context.Background(), cluster, kcp)).NotTo(Succeed())

kubeconfigSecret := &corev1.Secret{}
secretName := client.ObjectKey{
Namespace: "test",
},
Spec: controlplanev1.KubeadmControlPlaneSpec{
Version: "v1.16.6",
},
}
clusterName := util.ObjectKey(cluster)
endpoint := clusterv1.APIEndpoint{Host: "test.local", Port: 8443}

existingKubeconfigSecret := kubeconfig.GenerateSecretWithOwner(
client.ObjectKey{Name: "foo", Namespace: "test"},
[]byte{},
*metav1.NewControllerRef(cluster, clusterv1.GroupVersion.WithKind("Cluster")),
)

fakeClient := newFakeClient(g, kcp.DeepCopy(), existingKubeconfigSecret.DeepCopy())
r := &KubeadmControlPlaneReconciler{
Client: fakeClient,
Log: log.Log,
recorder: record.NewFakeRecorder(32),
}

g.Expect(r.reconcileKubeconfig(context.Background(), clusterName, endpoint, kcp)).To(Succeed())

kubeconfigSecret := &corev1.Secret{}
secretName := client.ObjectKey{
Namespace: "test",
Name: secret.Name(clusterName.Name, secret.Kubeconfig),
}
g.Expect(r.Client.Get(context.Background(), secretName, kubeconfigSecret)).To(Succeed())
g.Expect(kubeconfigSecret.Labels).To(Equal(existingKubeconfigSecret.Labels))
g.Expect(kubeconfigSecret.Data).To(Equal(existingKubeconfigSecret.Data))
g.Expect(kubeconfigSecret.OwnerReferences).NotTo(ContainElement(*metav1.NewControllerRef(kcp, controlplanev1.GroupVersion.WithKind("KubeadmControlPlane"))))

}

func TestKubeadmControlPlaneReconciler_reconcileKubeconfig(t *testing.T) {
g := NewWithT(t)

cluster := &clusterv1.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Name: secret.Name(cluster.Name, secret.Kubeconfig),
}
g.Expect(r.Client.Get(context.Background(), secretName, kubeconfigSecret)).To(MatchError(ContainSubstring("not found")))
})

t.Run("Adopts v1alpha2 Secrets", func(t *testing.T) {
g := NewWithT(t)
existingKubeconfigSecret := kubeconfig.GenerateSecretWithOwner(
client.ObjectKey{Name: "foo", Namespace: "test"},
[]byte{},
metav1.OwnerReference{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Cluster",
Name: cluster.Name,
UID: cluster.UID,
}, // the Cluster ownership defines v1alpha2 controlled secrets
)

fakeClient := newFakeClient(g, kcp.DeepCopy(), existingKubeconfigSecret.DeepCopy())
r := &KubeadmControlPlaneReconciler{
Client: fakeClient,
Log: log.Log,
recorder: record.NewFakeRecorder(32),
}

g.Expect(r.reconcileKubeconfig(context.Background(), cluster, kcp)).To(Succeed())

kubeconfigSecret := &corev1.Secret{}
secretName := client.ObjectKey{
Namespace: "test",
},
}

kcp := &controlplanev1.KubeadmControlPlane{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Name: secret.Name(cluster.Name, secret.Kubeconfig),
}
g.Expect(r.Client.Get(context.Background(), secretName, kubeconfigSecret)).To(Succeed())
g.Expect(kubeconfigSecret.Labels).To(Equal(existingKubeconfigSecret.Labels))
g.Expect(kubeconfigSecret.Data).To(Equal(existingKubeconfigSecret.Data))
g.Expect(kubeconfigSecret.OwnerReferences).ToNot(ContainElement(metav1.OwnerReference{
APIVersion: clusterv1.GroupVersion.String(),
Kind: "Cluster",
Name: cluster.Name,
UID: cluster.UID,
}))
g.Expect(kubeconfigSecret.OwnerReferences).To(ContainElement(*metav1.NewControllerRef(kcp, controlplanev1.GroupVersion.WithKind("KubeadmControlPlane"))))
})

t.Run("Does not adopt user secrets", func(t *testing.T) {
g := NewWithT(t)
existingKubeconfigSecret := kubeconfig.GenerateSecretWithOwner(
client.ObjectKey{Name: "foo", Namespace: "test"},
[]byte{},
metav1.OwnerReference{},
)

fakeClient := newFakeClient(g, kcp.DeepCopy(), existingKubeconfigSecret.DeepCopy())
r := &KubeadmControlPlaneReconciler{
Client: fakeClient,
Log: log.Log,
recorder: record.NewFakeRecorder(32),
}

g.Expect(r.reconcileKubeconfig(context.Background(), cluster, kcp)).To(Succeed())

kubeconfigSecret := &corev1.Secret{}
secretName := client.ObjectKey{
Namespace: "test",
},
Spec: controlplanev1.KubeadmControlPlaneSpec{
Version: "v1.16.6",
},
}
clusterName := util.ObjectKey(cluster)
endpoint := clusterv1.APIEndpoint{Host: "test.local", Port: 8443}

clusterCerts := secret.NewCertificatesForInitialControlPlane(&kubeadmv1.ClusterConfiguration{})
g.Expect(clusterCerts.Generate()).To(Succeed())
caCert := clusterCerts.GetByPurpose(secret.ClusterCA)
existingCACertSecret := caCert.AsSecret(
client.ObjectKey{Namespace: "test", Name: "foo"},
*metav1.NewControllerRef(kcp, controlplanev1.GroupVersion.WithKind("KubeadmControlPlane")),
)

fakeClient := newFakeClient(g, kcp.DeepCopy(), existingCACertSecret.DeepCopy())
r := &KubeadmControlPlaneReconciler{
Client: fakeClient,
Log: log.Log,
recorder: record.NewFakeRecorder(32),
}
g.Expect(r.reconcileKubeconfig(context.Background(), clusterName, endpoint, kcp)).To(Succeed())

kubeconfigSecret := &corev1.Secret{}
secretName := client.ObjectKey{
Namespace: "test",
Name: secret.Name(clusterName.Name, secret.Kubeconfig),
}
g.Expect(r.Client.Get(context.Background(), secretName, kubeconfigSecret)).To(Succeed())
g.Expect(kubeconfigSecret.OwnerReferences).NotTo(BeEmpty())
g.Expect(kubeconfigSecret.OwnerReferences).To(ContainElement(*metav1.NewControllerRef(kcp, controlplanev1.GroupVersion.WithKind("KubeadmControlPlane"))))
g.Expect(kubeconfigSecret.Labels).To(HaveKeyWithValue(clusterv1.ClusterLabelName, clusterName.Name))
Name: secret.Name(cluster.Name, secret.Kubeconfig),
}
g.Expect(r.Client.Get(context.Background(), secretName, kubeconfigSecret)).To(Succeed())
g.Expect(kubeconfigSecret.Labels).To(Equal(existingKubeconfigSecret.Labels))
g.Expect(kubeconfigSecret.Data).To(Equal(existingKubeconfigSecret.Data))
g.Expect(kubeconfigSecret.OwnerReferences).ToNot(ContainElement(*metav1.NewControllerRef(kcp, controlplanev1.GroupVersion.WithKind("KubeadmControlPlane"))))
})
}

func TestCloneConfigsAndGenerateMachine(t *testing.T) {
Expand Down