Skip to content
Open
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
118 changes: 118 additions & 0 deletions test/e2e/certman_operator_tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,20 @@ import (
configv1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
"github.com/openshift/osde2e-common/pkg/clients/openshift"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/log"
)

var _ = Describe("Certman Operator", Ordered, func() {
var (
logger = log.Log
k8s *openshift.Client
clientset *kubernetes.Clientset
secretName string

dynamicClient dynamic.Interface
)
const (
pollingDuration = 15 * time.Minute
Expand All @@ -35,6 +40,10 @@ var _ = Describe("Certman Operator", Ordered, func() {
Expect(err).ShouldNot(HaveOccurred(), "Unable to setup k8s client")
clientset, err = kubernetes.NewForConfig(k8s.GetConfig())
Expect(err).ShouldNot(HaveOccurred(), "Unable to setup Config client")

dynamicClient, err = dynamic.NewForConfig(k8s.GetConfig())
Expect(err).ShouldNot(HaveOccurred(), "Unable to create dynamic client")
Expect(dynamicClient).ShouldNot(BeNil(), "dynamic client is nil")
})

It("certificate secret exists under openshift-config namespace", func(ctx context.Context) {
Expand Down Expand Up @@ -62,4 +71,113 @@ var _ = Describe("Certman Operator", Ordered, func() {
return apiserver.Spec.ServingCerts.NamedCertificates[0].ServingCertificate.Name == secretName
}, pollingDuration, 30*time.Second).Should(BeTrue(), "Certificate secret should be applied to apiserver object")
})

It("Delete the Cluster Deployment", func(ctx context.Context) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Delete clusterdeployment is not part of the functionality of the certman-operator.

So we should not test like this. Instead, we can update it like following test scenarios.

- certman-opreator finalizer should not block clusterdeployment being deleted
- certificaterequest should be deleted when the clusterdeployment being deleted
- primary-cert-bundle-secret should be deleted accordingly when the clusterdeployment being deleted

logger.Info("Test - Delete Cluster Deployment")
clusterDeploymentGVR := schema.GroupVersionResource{
Group: "hive.openshift.io",
Version: "v1",
Resource: "clusterdeployments",
}
certRequestGVR := schema.GroupVersionResource{
Group: "certman.managed.openshift.io",
Version: "v1alpha1",
Resource: "certificaterequests",
}

Eventually(func() bool {
logger.Info("Checking if ClusterDeployment exist or not")
cdList, err := dynamicClient.Resource(clusterDeploymentGVR).Namespace("certman-operator").List(ctx, metav1.ListOptions{})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed in the sync meet, please try to create the clusterdeployment is a separated namespace rather than the certman-operator

if err != nil {
logger.Error(err, "Failed to list ClusterDeployments")
return false
}
if len(cdList.Items) == 0 {
logger.Info("No ClusterDeployment found in certman-operator namespace.")
return false
}

cd := cdList.Items[0]
cdName := cd.GetName()
finalizers := cd.GetFinalizers()
logger.Info("Found ClusterDeployment", "name", cdName, "finalizers", finalizers)

hasCertFinalizer := false
for _, f := range finalizers {
if f == "certificaterequests.certman.managed.openshift.io" {
hasCertFinalizer = true
break
}
}

if !hasCertFinalizer {
logger.Info("ClusterDeployment does not have the certman finalizer", "name", cdName)
return false
}

logger.Info("Found the specified finalizer. Deleting ClusterDeployment", "name", cdName)
err = dynamicClient.Resource(clusterDeploymentGVR).Namespace("certman-operator").Delete(ctx, cdName, metav1.DeleteOptions{})
if err != nil {
logger.Error(err, "Failed to delete ClusterDeployment", "name", cdName)
return false
}

time.Sleep(2 * time.Second)

logger.Info("Checking if CertificateRequests are deleted")

crList, err := dynamicClient.Resource(certRequestGVR).Namespace("certman-operator").List(ctx, metav1.ListOptions{})
if err != nil {
logger.Error(err, "Failed to list CertificateRequests")
return false
}

if len(crList.Items) > 0 {
for _, cr := range crList.Items {
crName := cr.GetName()
finalizers := cr.GetFinalizers()

if len(finalizers) > 0 {
logger.Info("CertificateRequest not deleted due to finalizers. Removing finalizers", "name", crName)
cr.SetFinalizers([]string{})
_, err := dynamicClient.Resource(certRequestGVR).Namespace("certman-operator").Update(ctx, &cr, metav1.UpdateOptions{})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not remove the finalizer manually here. It needs to be handled by the operator/controller.

if err != nil {
logger.Error(err, "Failed to remove finalizers from CertificateRequest", "name", crName)
return false
}

}

logger.Info("Rechecking CertificateRequest deletion ", "name", crName)
crList, err = dynamicClient.Resource(certRequestGVR).Namespace("certman-operator").List(ctx, metav1.ListOptions{})
if err != nil {
logger.Error(err, "Failed to re-list CertificateRequests")
return false
}
if len(crList.Items) > 0 {
logger.Info("CertificateRequests still present")
return false
}
}
}

logger.Info("All CertificateRequests successfully deleted")

logger.Info("Checking if primary-cert-bundle-secret is deleted or not")

secretList, err := clientset.CoreV1().Secrets("certman-operator").List(ctx, metav1.ListOptions{})
if err != nil {
logger.Error(err, "Failed to list Secrets in certman-operator")
return false
}
for _, s := range secretList.Items {
if s.Name == "primary-cert-bundle-secret" {
Fail("primary-cert-bundle-secret still exists.")
}
}
logger.Info("primary-cert-bundle-secret successfully deleted")

return true
}, pollingDuration, 15*time.Second).Should(BeTrue(), "Delete the Cluster Deployment")
})
})