Skip to content

Commit

Permalink
[BOP-912] manage only resources(certs,issuers) that are created by BOP (
Browse files Browse the repository at this point in the history
#79)

* manage only resources(certs,issuers) that are created by BOP

* fix e2e tests
  • Loading branch information
sakshisharma84 authored Jul 12, 2024
1 parent b6e495c commit 2e231e7
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 4 deletions.
9 changes: 9 additions & 0 deletions controllers/blueprint_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ func issuerObject(issuer boundlessv1alpha1.Issuer) client.Object {
ObjectMeta: metav1.ObjectMeta{
Name: issuer.Name,
Namespace: issuer.Namespace,
Labels: map[string]string{
"app.kubernetes.io/managed-by": "blueprint-operator",
},
},
Spec: issuer.Spec,
}
Expand All @@ -248,6 +251,9 @@ func clusterIssuerObject(issuer boundlessv1alpha1.ClusterIssuer) client.Object {
},
ObjectMeta: metav1.ObjectMeta{
Name: issuer.Name,
Labels: map[string]string{
"app.kubernetes.io/managed-by": "blueprint-operator",
},
},
Spec: issuer.Spec,
}
Expand All @@ -262,6 +268,9 @@ func certificateObject(certificate boundlessv1alpha1.Certificate) client.Object
ObjectMeta: metav1.ObjectMeta{
Name: certificate.Name,
Namespace: certificate.Namespace,
Labels: map[string]string{
"app.kubernetes.io/managed-by": "blueprint-operator",
},
},
Spec: certificate.Spec,
}
Expand Down
10 changes: 10 additions & 0 deletions controllers/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,16 @@ func listInstalledObjects(ctx context.Context, logger logr.Logger, apiClient cli

func deleteObjects(ctx context.Context, logger logr.Logger, apiClient client.Client, objectsToUninstall map[string]client.Object) error {
for _, o := range objectsToUninstall {
// Only delete the resources(cert/issuer) that are managed by BOP.
// This check can be removed once we add the label in all
// the objects created by BOP (https://mirantis.jira.com/browse/BOP-919).
if o.GetObjectKind().GroupVersionKind().Kind == "Certificate" || o.GetObjectKind().GroupVersionKind().Kind == "Issuer" {
if o.GetLabels()["app.kubernetes.io/managed-by"] != "blueprint-operator" {
logger.Info("Skipping deletion of ", "Kind", o.GetObjectKind().GroupVersionKind().Kind)
continue
}
}

logger.Info("Removing object", "Name", o.GetName(), "Namespace", o.GetNamespace())
if err := apiClient.Delete(ctx, o, client.PropagationPolicy(metav1.DeletePropagationBackground)); client.IgnoreNotFound(err) != nil {
logger.Error(err, "Failed to remove object", "Name", o.GetName())
Expand Down
2 changes: 2 additions & 0 deletions test/e2e/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func newIssuer(i metav1.ObjectMeta) *certmanager.Issuer {
ObjectMeta: metav1.ObjectMeta{
Name: i.Name,
Namespace: i.Namespace,
Labels: i.Labels,
},
}
}
Expand All @@ -65,6 +66,7 @@ func newCertificate(cert metav1.ObjectMeta) *certmanager.Certificate {
ObjectMeta: metav1.ObjectMeta{
Name: cert.Name,
Namespace: cert.Namespace,
Labels: cert.Labels,
},
}
}
Expand Down
32 changes: 28 additions & 4 deletions test/e2e/install_certs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,30 @@ import (
func TestInstallCerts(t *testing.T) {
dir := filepath.Join(curDir, "manifests", "certs")

i1 := newIssuer(metav1.ObjectMeta{Name: "test-issuer-1", Namespace: "test-issuer-ns-1"})
i2 := newIssuer(metav1.ObjectMeta{Name: "test-issuer-2", Namespace: "test-issuer-ns-1"})
i1 := newIssuer(metav1.ObjectMeta{
Name: "test-issuer-1",
Namespace: "test-issuer-ns-1",
Labels: map[string]string{
"app.kubernetes.io/managed-by": "blueprint-operator",
},
})
i2 := newIssuer(metav1.ObjectMeta{
Name: "test-issuer-2",
Namespace: "test-issuer-ns-1",
Labels: map[string]string{
"app.kubernetes.io/managed-by": "blueprint-operator",
},
})

ci1 := newClusterIssuer(metav1.ObjectMeta{Name: "test-cluster-issuer-1"})

cert1 := newCertificate(metav1.ObjectMeta{Name: "test-cert-1", Namespace: "test-issuer-ns-1"})
cert1 := newCertificate(metav1.ObjectMeta{
Name: "test-cert-1",
Namespace: "test-issuer-ns-1",
Labels: map[string]string{
"app.kubernetes.io/managed-by": "blueprint-operator",
},
})
cert1Specs := certmanager.CertificateSpec{
CommonName: "test-cert-1",
IsCA: true,
Expand All @@ -35,7 +53,13 @@ func TestInstallCerts(t *testing.T) {
Kind: "Issuer",
},
}
cert2 := newCertificate(metav1.ObjectMeta{Name: "test-cert-2", Namespace: "test-cert-ns-1"})
cert2 := newCertificate(metav1.ObjectMeta{
Name: "test-cert-2",
Namespace: "test-cert-ns-1",
Labels: map[string]string{
"app.kubernetes.io/managed-by": "blueprint-operator",
},
})
cert2Specs := certmanager.CertificateSpec{
CommonName: "test-cert-2",
IsCA: false,
Expand Down

0 comments on commit 2e231e7

Please sign in to comment.