diff --git a/test/integration/suite/010-tls/tls_test.go b/test/integration/suite/010-tls/tls_test.go index b5f1661..846f967 100644 --- a/test/integration/suite/010-tls/tls_test.go +++ b/test/integration/suite/010-tls/tls_test.go @@ -33,6 +33,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" gnmicv1alpha1 "github.com/gnmic/operator/api/v1alpha1" "github.com/gnmic/operator/test/integration/harness" @@ -624,6 +625,23 @@ func TestTLS006_ClientCertRotation(t *testing.T) { waitPodFile(t, cluster, clientTLSCertPath) } +// ownLegacyCertificate makes cluster the controller of a legacy Certificate, as +// the operator that issued it did. Cleanup only deletes what the Cluster owns. +func ownLegacyCertificate(t *testing.T, certName string, cluster *gnmicv1alpha1.Cluster) { + t.Helper() + var cert certmanagerv1.Certificate + if err := s.K8s.Client.Get(s.Ctx, types.NamespacedName{Namespace: s.Namespace, Name: certName}, &cert); err != nil { + t.Fatalf("getting legacy Certificate %s: %v", certName, err) + } + patch := client.MergeFrom(cert.DeepCopy()) + if err := controllerutil.SetControllerReference(cluster, &cert, s.K8s.Client.Scheme()); err != nil { + t.Fatalf("setting owner on %s: %v", certName, err) + } + if err := s.K8s.Client.Patch(s.Ctx, &cert, patch); err != nil { + t.Fatalf("patching owner onto %s: %v", certName, err) + } +} + func conditionTrue(c *gnmicv1alpha1.Cluster, typ string) bool { for _, cond := range c.Status.Conditions { if cond.Type == typ { @@ -695,7 +713,8 @@ func TestTLS008_LegacyPerPodCertificateRemoved(t *testing.T) { const name = "legacy" legacyName := harness.StatefulSetName(name) + "-0-tls" // What the old operator left behind: a per-pod Certificate carrying the - // cluster label, and the Secret cert-manager issued for it. + // cluster label and controlled by the Cluster, and the Secret cert-manager + // issued for it. The owner reference is added once the Cluster exists. s.K8s.ApplyYAML(t, ` apiVersion: cert-manager.io/v1 kind: Certificate @@ -721,6 +740,7 @@ spec: }) applyCluster(t, name, map[string]any{"APIIssuer": issuer}) + ownLegacyCertificate(t, legacyName, s.K8s.Cluster(t, name)) waitCertReady(t, harness.APICertName(name)) waitClusterReady(t, name)