Skip to content

Commit 4b23466

Browse files
(fix): ensure full cert-manager uninstall/teardown for e2e tests
1 parent 58a7cd0 commit 4b23466

File tree

8 files changed

+465
-94
lines changed
  • docs/book/src
    • cronjob-tutorial/testdata/project/test/utils
    • getting-started/testdata/project/test/utils
    • multiversion-tutorial/testdata/project/test/utils
  • pkg/plugins/golang/v4/scaffolds/internal/templates/test/utils
  • testdata
  • test/e2e/utils

8 files changed

+465
-94
lines changed

docs/book/src/cronjob-tutorial/testdata/project/test/utils/utils.go

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ const (
3939
"releases/download/%s/bundle.yaml"
4040
)
4141

42+
// certManagerCRDs defines the list of cert-manager CRDs to uninstall.
43+
var certManagerCRDs = []string{
44+
"certificates.cert-manager.io",
45+
"certificaterequests.cert-manager.io",
46+
"challenges.acme.cert-manager.io",
47+
"clusterissuers.cert-manager.io",
48+
"issuers.cert-manager.io",
49+
"orders.acme.cert-manager.io",
50+
"signers.config.cert-manager.io",
51+
}
52+
4253
func warnError(err error) {
4354
_, _ = fmt.Fprintf(GinkgoWriter, "warning: %v\n", err)
4455
}
@@ -66,9 +77,53 @@ func Run(cmd *exec.Cmd) (string, error) {
6677
// UninstallCertManager uninstalls the cert manager
6778
func UninstallCertManager() {
6879
url := fmt.Sprintf(certmanagerURLTmpl, certmanagerVersion)
69-
cmd := exec.Command("kubectl", "delete", "-f", url)
70-
if _, err := Run(cmd); err != nil {
71-
warnError(err)
80+
cmd := exec.Command("kubectl", "delete", "-f", url, "--ignore-not-found")
81+
_, _ = Run(cmd)
82+
83+
// Step 2: Delete webhook configurations
84+
webhooks := []string{"cert-manager-webhook"}
85+
for _, webhook := range webhooks {
86+
cmd = exec.Command("kubectl", "delete", "mutatingwebhookconfiguration", webhook,
87+
"--ignore-not-found", "--force", "--grace-period=0")
88+
_, _ = Run(cmd)
89+
90+
cmd = exec.Command("kubectl", "delete", "validatingwebhookconfiguration", webhook,
91+
"--ignore-not-found", "--force", "--grace-period=0")
92+
_, _ = Run(cmd)
93+
}
94+
95+
// Step 3: Delete cert-manager CRDs
96+
for _, crd := range certManagerCRDs {
97+
cmd = exec.Command("kubectl", "delete", "crd", crd,
98+
"--ignore-not-found", "--force", "--grace-period=0")
99+
_, _ = Run(cmd)
100+
}
101+
102+
// Step 4: Delete leader election leases in cert-manager namespace
103+
leases := []string{
104+
"cert-manager-cainjector-leader-election",
105+
"cert-manager-controller",
106+
}
107+
for _, lease := range leases {
108+
cmd = exec.Command("kubectl", "delete", "lease", lease,
109+
"-n", "cert-manager", "--ignore-not-found", "--force", "--grace-period=0")
110+
_, _ = Run(cmd)
111+
}
112+
113+
// Step 5: Delete the cert-manager namespace
114+
cmd = exec.Command("kubectl", "delete", "namespace", "cert-manager",
115+
"--ignore-not-found", "--force", "--grace-period=0")
116+
_, _ = Run(cmd)
117+
118+
// Step 6: Delete leftover leases in kube-system (not cleaned by default)
119+
kubeSystemLeases := []string{
120+
"cert-manager-cainjector-leader-election",
121+
"cert-manager-controller",
122+
}
123+
for _, lease := range kubeSystemLeases {
124+
cmd = exec.Command("kubectl", "delete", "lease", lease,
125+
"-n", "kube-system", "--ignore-not-found", "--force", "--grace-period=0")
126+
_, _ = Run(cmd)
72127
}
73128
}
74129

@@ -94,16 +149,6 @@ func InstallCertManager() error {
94149
// IsCertManagerCRDsInstalled checks if any Cert Manager CRDs are installed
95150
// by verifying the existence of key CRDs related to Cert Manager.
96151
func IsCertManagerCRDsInstalled() bool {
97-
// List of common Cert Manager CRDs
98-
certManagerCRDs := []string{
99-
"certificates.cert-manager.io",
100-
"issuers.cert-manager.io",
101-
"clusterissuers.cert-manager.io",
102-
"certificaterequests.cert-manager.io",
103-
"orders.acme.cert-manager.io",
104-
"challenges.acme.cert-manager.io",
105-
}
106-
107152
// Execute the kubectl command to get all CRDs
108153
cmd := exec.Command("kubectl", "get", "crds")
109154
output, err := Run(cmd)

docs/book/src/getting-started/testdata/project/test/utils/utils.go

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ const (
3535
defaultKindCluster = "kind"
3636
)
3737

38+
// certManagerCRDs defines the list of cert-manager CRDs to uninstall.
39+
var certManagerCRDs = []string{
40+
"certificates.cert-manager.io",
41+
"certificaterequests.cert-manager.io",
42+
"challenges.acme.cert-manager.io",
43+
"clusterissuers.cert-manager.io",
44+
"issuers.cert-manager.io",
45+
"orders.acme.cert-manager.io",
46+
"signers.config.cert-manager.io",
47+
}
48+
3849
func warnError(err error) {
3950
_, _ = fmt.Fprintf(GinkgoWriter, "warning: %v\n", err)
4051
}
@@ -62,9 +73,53 @@ func Run(cmd *exec.Cmd) (string, error) {
6273
// UninstallCertManager uninstalls the cert manager
6374
func UninstallCertManager() {
6475
url := fmt.Sprintf(certmanagerURLTmpl, certmanagerVersion)
65-
cmd := exec.Command("kubectl", "delete", "-f", url)
66-
if _, err := Run(cmd); err != nil {
67-
warnError(err)
76+
cmd := exec.Command("kubectl", "delete", "-f", url, "--ignore-not-found")
77+
_, _ = Run(cmd)
78+
79+
// Step 2: Delete webhook configurations
80+
webhooks := []string{"cert-manager-webhook"}
81+
for _, webhook := range webhooks {
82+
cmd = exec.Command("kubectl", "delete", "mutatingwebhookconfiguration", webhook,
83+
"--ignore-not-found", "--force", "--grace-period=0")
84+
_, _ = Run(cmd)
85+
86+
cmd = exec.Command("kubectl", "delete", "validatingwebhookconfiguration", webhook,
87+
"--ignore-not-found", "--force", "--grace-period=0")
88+
_, _ = Run(cmd)
89+
}
90+
91+
// Step 3: Delete cert-manager CRDs
92+
for _, crd := range certManagerCRDs {
93+
cmd = exec.Command("kubectl", "delete", "crd", crd,
94+
"--ignore-not-found", "--force", "--grace-period=0")
95+
_, _ = Run(cmd)
96+
}
97+
98+
// Step 4: Delete leader election leases in cert-manager namespace
99+
leases := []string{
100+
"cert-manager-cainjector-leader-election",
101+
"cert-manager-controller",
102+
}
103+
for _, lease := range leases {
104+
cmd = exec.Command("kubectl", "delete", "lease", lease,
105+
"-n", "cert-manager", "--ignore-not-found", "--force", "--grace-period=0")
106+
_, _ = Run(cmd)
107+
}
108+
109+
// Step 5: Delete the cert-manager namespace
110+
cmd = exec.Command("kubectl", "delete", "namespace", "cert-manager",
111+
"--ignore-not-found", "--force", "--grace-period=0")
112+
_, _ = Run(cmd)
113+
114+
// Step 6: Delete leftover leases in kube-system (not cleaned by default)
115+
kubeSystemLeases := []string{
116+
"cert-manager-cainjector-leader-election",
117+
"cert-manager-controller",
118+
}
119+
for _, lease := range kubeSystemLeases {
120+
cmd = exec.Command("kubectl", "delete", "lease", lease,
121+
"-n", "kube-system", "--ignore-not-found", "--force", "--grace-period=0")
122+
_, _ = Run(cmd)
68123
}
69124
}
70125

@@ -90,16 +145,6 @@ func InstallCertManager() error {
90145
// IsCertManagerCRDsInstalled checks if any Cert Manager CRDs are installed
91146
// by verifying the existence of key CRDs related to Cert Manager.
92147
func IsCertManagerCRDsInstalled() bool {
93-
// List of common Cert Manager CRDs
94-
certManagerCRDs := []string{
95-
"certificates.cert-manager.io",
96-
"issuers.cert-manager.io",
97-
"clusterissuers.cert-manager.io",
98-
"certificaterequests.cert-manager.io",
99-
"orders.acme.cert-manager.io",
100-
"challenges.acme.cert-manager.io",
101-
}
102-
103148
// Execute the kubectl command to get all CRDs
104149
cmd := exec.Command("kubectl", "get", "crds")
105150
output, err := Run(cmd)

docs/book/src/multiversion-tutorial/testdata/project/test/utils/utils.go

Lines changed: 58 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ const (
3939
"releases/download/%s/bundle.yaml"
4040
)
4141

42+
// certManagerCRDs defines the list of cert-manager CRDs to uninstall.
43+
var certManagerCRDs = []string{
44+
"certificates.cert-manager.io",
45+
"certificaterequests.cert-manager.io",
46+
"challenges.acme.cert-manager.io",
47+
"clusterissuers.cert-manager.io",
48+
"issuers.cert-manager.io",
49+
"orders.acme.cert-manager.io",
50+
"signers.config.cert-manager.io",
51+
}
52+
4253
func warnError(err error) {
4354
_, _ = fmt.Fprintf(GinkgoWriter, "warning: %v\n", err)
4455
}
@@ -66,9 +77,53 @@ func Run(cmd *exec.Cmd) (string, error) {
6677
// UninstallCertManager uninstalls the cert manager
6778
func UninstallCertManager() {
6879
url := fmt.Sprintf(certmanagerURLTmpl, certmanagerVersion)
69-
cmd := exec.Command("kubectl", "delete", "-f", url)
70-
if _, err := Run(cmd); err != nil {
71-
warnError(err)
80+
cmd := exec.Command("kubectl", "delete", "-f", url, "--ignore-not-found")
81+
_, _ = Run(cmd)
82+
83+
// Step 2: Delete webhook configurations
84+
webhooks := []string{"cert-manager-webhook"}
85+
for _, webhook := range webhooks {
86+
cmd = exec.Command("kubectl", "delete", "mutatingwebhookconfiguration", webhook,
87+
"--ignore-not-found", "--force", "--grace-period=0")
88+
_, _ = Run(cmd)
89+
90+
cmd = exec.Command("kubectl", "delete", "validatingwebhookconfiguration", webhook,
91+
"--ignore-not-found", "--force", "--grace-period=0")
92+
_, _ = Run(cmd)
93+
}
94+
95+
// Step 3: Delete cert-manager CRDs
96+
for _, crd := range certManagerCRDs {
97+
cmd = exec.Command("kubectl", "delete", "crd", crd,
98+
"--ignore-not-found", "--force", "--grace-period=0")
99+
_, _ = Run(cmd)
100+
}
101+
102+
// Step 4: Delete leader election leases in cert-manager namespace
103+
leases := []string{
104+
"cert-manager-cainjector-leader-election",
105+
"cert-manager-controller",
106+
}
107+
for _, lease := range leases {
108+
cmd = exec.Command("kubectl", "delete", "lease", lease,
109+
"-n", "cert-manager", "--ignore-not-found", "--force", "--grace-period=0")
110+
_, _ = Run(cmd)
111+
}
112+
113+
// Step 5: Delete the cert-manager namespace
114+
cmd = exec.Command("kubectl", "delete", "namespace", "cert-manager",
115+
"--ignore-not-found", "--force", "--grace-period=0")
116+
_, _ = Run(cmd)
117+
118+
// Step 6: Delete leftover leases in kube-system (not cleaned by default)
119+
kubeSystemLeases := []string{
120+
"cert-manager-cainjector-leader-election",
121+
"cert-manager-controller",
122+
}
123+
for _, lease := range kubeSystemLeases {
124+
cmd = exec.Command("kubectl", "delete", "lease", lease,
125+
"-n", "kube-system", "--ignore-not-found", "--force", "--grace-period=0")
126+
_, _ = Run(cmd)
72127
}
73128
}
74129

@@ -94,16 +149,6 @@ func InstallCertManager() error {
94149
// IsCertManagerCRDsInstalled checks if any Cert Manager CRDs are installed
95150
// by verifying the existence of key CRDs related to Cert Manager.
96151
func IsCertManagerCRDsInstalled() bool {
97-
// List of common Cert Manager CRDs
98-
certManagerCRDs := []string{
99-
"certificates.cert-manager.io",
100-
"issuers.cert-manager.io",
101-
"clusterissuers.cert-manager.io",
102-
"certificaterequests.cert-manager.io",
103-
"orders.acme.cert-manager.io",
104-
"challenges.acme.cert-manager.io",
105-
}
106-
107152
// Execute the kubectl command to get all CRDs
108153
cmd := exec.Command("kubectl", "get", "crds")
109154
output, err := Run(cmd)

pkg/plugins/golang/v4/scaffolds/internal/templates/test/utils/utils.go

Lines changed: 59 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ const (
6363
defaultKindCluster = "kind"
6464
)
6565
66+
// certManagerCRDs defines the list of cert-manager CRDs to uninstall.
67+
var certManagerCRDs = []string{
68+
"certificates.cert-manager.io",
69+
"certificaterequests.cert-manager.io",
70+
"challenges.acme.cert-manager.io",
71+
"clusterissuers.cert-manager.io",
72+
"issuers.cert-manager.io",
73+
"orders.acme.cert-manager.io",
74+
"signers.config.cert-manager.io",
75+
}
76+
6677
func warnError(err error) {
6778
_, _ = fmt.Fprintf(GinkgoWriter, "warning: %v\n", err)
6879
}
@@ -87,12 +98,57 @@ func Run(cmd *exec.Cmd) (string, error) {
8798
return string(output), nil
8899
}
89100
101+
90102
// UninstallCertManager uninstalls the cert manager
91103
func UninstallCertManager() {
92104
url := fmt.Sprintf(certmanagerURLTmpl, certmanagerVersion)
93-
cmd := exec.Command("kubectl", "delete", "-f", url)
94-
if _, err := Run(cmd); err != nil {
95-
warnError(err)
105+
cmd := exec.Command("kubectl", "delete", "-f", url, "--ignore-not-found")
106+
_, _ = Run(cmd)
107+
108+
// Step 2: Delete webhook configurations
109+
webhooks := []string{"cert-manager-webhook"}
110+
for _, webhook := range webhooks {
111+
cmd = exec.Command("kubectl", "delete", "mutatingwebhookconfiguration", webhook,
112+
"--ignore-not-found", "--force", "--grace-period=0")
113+
_, _ = Run(cmd)
114+
115+
cmd = exec.Command("kubectl", "delete", "validatingwebhookconfiguration", webhook,
116+
"--ignore-not-found", "--force", "--grace-period=0")
117+
_, _ = Run(cmd)
118+
}
119+
120+
// Step 3: Delete cert-manager CRDs
121+
for _, crd := range certManagerCRDs {
122+
cmd = exec.Command("kubectl", "delete", "crd", crd,
123+
"--ignore-not-found", "--force", "--grace-period=0")
124+
_, _ = Run(cmd)
125+
}
126+
127+
// Step 4: Delete leader election leases in cert-manager namespace
128+
leases := []string{
129+
"cert-manager-cainjector-leader-election",
130+
"cert-manager-controller",
131+
}
132+
for _, lease := range leases {
133+
cmd = exec.Command("kubectl", "delete", "lease", lease,
134+
"-n", "cert-manager", "--ignore-not-found", "--force", "--grace-period=0")
135+
_, _ = Run(cmd)
136+
}
137+
138+
// Step 5: Delete the cert-manager namespace
139+
cmd = exec.Command("kubectl", "delete", "namespace", "cert-manager",
140+
"--ignore-not-found", "--force", "--grace-period=0")
141+
_, _ = Run(cmd)
142+
143+
// Step 6: Delete leftover leases in kube-system (not cleaned by default)
144+
kubeSystemLeases := []string{
145+
"cert-manager-cainjector-leader-election",
146+
"cert-manager-controller",
147+
}
148+
for _, lease := range kubeSystemLeases {
149+
cmd = exec.Command("kubectl", "delete", "lease", lease,
150+
"-n", "kube-system", "--ignore-not-found", "--force", "--grace-period=0")
151+
_, _ = Run(cmd)
96152
}
97153
}
98154
@@ -118,16 +174,6 @@ func InstallCertManager() error {
118174
// IsCertManagerCRDsInstalled checks if any Cert Manager CRDs are installed
119175
// by verifying the existence of key CRDs related to Cert Manager.
120176
func IsCertManagerCRDsInstalled() bool {
121-
// List of common Cert Manager CRDs
122-
certManagerCRDs := []string{
123-
"certificates.cert-manager.io",
124-
"issuers.cert-manager.io",
125-
"clusterissuers.cert-manager.io",
126-
"certificaterequests.cert-manager.io",
127-
"orders.acme.cert-manager.io",
128-
"challenges.acme.cert-manager.io",
129-
}
130-
131177
// Execute the kubectl command to get all CRDs
132178
cmd := exec.Command("kubectl", "get", "crds")
133179
output, err := Run(cmd)

0 commit comments

Comments
 (0)