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

Add test for configpolicy cleanup during uninstall #541

Merged
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 Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ e2e-debug-kind: e2e-debug
-@for APP in $(KIND_COMPONENTS); do\
for CONTAINER in $$(kubectl get pod -l $(KIND_COMPONENT_SELECTOR)=$${APP} -n $(KIND_MANAGED_NAMESPACE) -o jsonpath={.items[*].spec.containers[*].name} --kubeconfig=$(PWD)/kubeconfig_$(MANAGED_CLUSTER_NAME)); do\
echo "* Logs for Label: $(KIND_COMPONENT_SELECTOR)=$${APP}, Container: $${CONTAINER}" > $(DEBUG_DIR)/managed_logs_$${CONTAINER}.log;\
kubectl logs -l $(KIND_COMPONENT_SELECTOR)=$${APP} -n $(KIND_MANAGED_NAMESPACE) -c $${CONTAINER} --kubeconfig=$(PWD)/kubeconfig_$(MANAGED_CLUSTER_NAME) >> $(DEBUG_DIR)/managed_logs_$${CONTAINER}.log;\
kubectl logs -l $(KIND_COMPONENT_SELECTOR)=$${APP} -n $(KIND_MANAGED_NAMESPACE) -c $${CONTAINER} --kubeconfig=$(PWD)/kubeconfig_$(MANAGED_CLUSTER_NAME) --tail=-1 >> $(DEBUG_DIR)/managed_logs_$${CONTAINER}.log;\
done;\
done

Expand Down
134 changes: 114 additions & 20 deletions test/configuration_policy_prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package test

import (
"errors"
"os"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand All @@ -20,6 +21,24 @@ func ConfigPruneBehavior(labels ...string) bool {
pruneConfigMapYaml string = "../resources/configuration_policy_prune/configmap-only.yaml"
)

cleanPolicy := func(policyName, policyYaml string) func() {
return func() {
By("Cleaning up policy " + policyName + ", ignoring if not found")

outHub, err := OcHub("delete", "-f", policyYaml, "-n", UserNamespace, "--ignore-not-found")
GinkgoWriter.Printf("cleanPolicy OcHub output: %v\n", outHub)
Expect(err).To(BeNil())

outManaged, err := OcManaged(
"delete", "events", "-n", ClusterNamespace,
"--field-selector=involvedObject.name="+UserNamespace+"."+policyName,
"--ignore-not-found",
)
GinkgoWriter.Printf("cleanPolicy OcManaged output: %v\n", outManaged)
Expect(err).To(BeNil())
}
}

pruneTestCreatedByPolicy := func(policyName, policyYaml string, cmShouldBeDeleted bool) {
clientManagedDynamic := NewKubeClientDynamic("", KubeconfigManaged, "")

Expand Down Expand Up @@ -270,26 +289,6 @@ func ConfigPruneBehavior(labels ...string) bool {
BeforeEach(cleanConfigMap)
AfterAll(cleanConfigMap)

cleanPolicy := func(policyName, policyYaml string) func() {
return func() {
_, err := OcHub(
"delete", "-f", policyYaml,
"--ignore-not-found",
)
Expect(err).To(BeNil())
_, err = OcManaged(
"delete",
"events",
"-n",
ClusterNamespace,
"--field-selector=involvedObject.name="+
UserNamespace+"."+policyName,
"--ignore-not-found",
)
Expect(err).To(BeNil())
}
}

Describe("Test DeleteAll pruning", func() {
policyName := "cm-policy-prune-all"
policyYaml := "../resources/configuration_policy_prune/cm-policy-prune-all.yaml"
Expand Down Expand Up @@ -361,5 +360,100 @@ func ConfigPruneBehavior(labels ...string) bool {
})
})

Describe("GRC: [P1][Sev1][policy-grc] Test cleanup during controller removal", Ordered, func() {
var configpolicyDeploymentYaml string
var configpolicyCRDYaml string

clientManagedDynamic := NewKubeClientDynamic("", KubeconfigManaged, "")
policyName := "cm-policy-prune-all"
policyYaml := "../resources/configuration_policy_prune/cm-policy-prune-all.yaml"
pruneFinalizer := "policy.open-cluster-management.io/delete-related-objects"

BeforeAll(func() {
var err error

configpolicyDeploymentYaml, err = OcManaged("get", "deployment", "-o=yaml",
"--namespace=open-cluster-management-agent-addon", "config-policy-controller")
Expect(err).To(BeNil())

configpolicyCRDYaml, err = OcManaged("get", "crd", "-o=yaml",
"configurationpolicies.policy.open-cluster-management.io")
Expect(err).To(BeNil())
})

AfterAll(func() {
By("Re-applying the config policy controller deployment")
tmpDeployFile, err := os.CreateTemp("", "config-policy-controller-*.yaml")
Expect(err).To(BeNil())

defer os.Remove(tmpDeployFile.Name())

_, err = tmpDeployFile.WriteString(configpolicyDeploymentYaml)
Expect(err).To(BeNil())

Expect(tmpDeployFile.Close()).To(BeNil())

_, err = OcManaged("apply", "-f", tmpDeployFile.Name())
Expect(err).To(BeNil())

By("Re-applying the configuration policy CRD")
tmpCRDFile, err := os.CreateTemp("", "config-policy-crd-*.yaml")
Expect(err).To(BeNil())

defer os.Remove(tmpCRDFile.Name())

_, err = tmpCRDFile.WriteString(configpolicyCRDYaml)
Expect(err).To(BeNil())

Expect(tmpCRDFile.Close()).To(BeNil())

_, err = OcManaged("apply", "-f", tmpCRDFile.Name())
Expect(err).To(BeNil())
})

AfterAll(cleanPolicy(policyName, policyYaml))

It("Should have the finalizer on the deployment", func() {
DoCreatePolicyTest(policyYaml, GvrConfigurationPolicy)

By("Checking for the finalizer")
Eventually(func(g Gomega) {
deployment := utils.GetWithTimeout(
clientManagedDynamic,
GvrDeployment,
"config-policy-controller",
"open-cluster-management-agent-addon",
true,
DefaultTimeoutSeconds,
)

g.Expect(deployment.GetFinalizers()).Should(ContainElement(pruneFinalizer))
}, DefaultTimeoutSeconds, 1).Should(Succeed())
})

It("Should eventually remove the deployment despite the finalizer", func() {
// Ignoring error because it probably should take more than 1 second,
// but we check whether it eventually succeeds separately.
// nolint: errcheck
OcManaged("delete", "deployment", "--timeout=1s",
"--namespace=open-cluster-management-agent-addon", "config-policy-controller")

utils.GetWithTimeout(
clientManagedDynamic,
GvrDeployment,
"config-policy-controller",
"open-cluster-management-agent-addon",
false,
DefaultTimeoutSeconds,
)
})

It("Should remove the CRD in a timely manner", func() {
_, err := OcManaged("delete", "crd", "--timeout=15s",
"configurationpolicies.policy.open-cluster-management.io")
Expect(err).To(BeNil())
})
})

return true
}