Skip to content
Merged
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
22 changes: 21 additions & 1 deletion test/integration/suite/010-tls/tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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)

Expand Down