Skip to content

Commit c0ba5ce

Browse files
authored
Merge pull request kubernetes-sigs#4117 from Adembc/e2e-pre-checks
✨ (go/v4):Added checks in the e2e test scaffolds to determine if Prometheus and/or CertManager are already installed on the cluster and avoid re-installation.
2 parents 7e02efb + b87e056 commit c0ba5ce

File tree

16 files changed

+767
-135
lines changed

16 files changed

+767
-135
lines changed

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

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ var (
3636
// re-installation and conflicts.
3737
skipPrometheusInstall = os.Getenv("PROMETHEUS_INSTALL_SKIP") == "true"
3838
skipCertManagerInstall = os.Getenv("CERT_MANAGER_INSTALL_SKIP") == "true"
39+
// isPrometheusOperatorAlreadyInstalled will be set true when prometheus CRDs be found on the cluster
40+
isPrometheusOperatorAlreadyInstalled = false
41+
// isCertManagerAlreadyInstalled will be set true when CertManager CRDs be found on the cluster
42+
isCertManagerAlreadyInstalled = false
3943

4044
// projectImage is the name of the image which will be build and loaded
4145
// with the code source changes to be tested.
@@ -74,24 +78,39 @@ var _ = BeforeSuite(func() {
7478
err = utils.LoadImageToKindClusterWithName(projectImage)
7579
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to load the manager(Operator) image into Kind")
7680

77-
// Setup Prometheus and CertManager before the suite if not skipped
81+
// The tests-e2e are intended to run on a temporary cluster that is created and destroyed for testing.
82+
// To prevent errors when tests run in environments with Prometheus or CertManager already installed,
83+
// we check for their presence before execution.
84+
// Setup Prometheus and CertManager before the suite if not skipped and if not already installed
7885
if !skipPrometheusInstall {
79-
_, _ = fmt.Fprintf(GinkgoWriter, "Installing Prometheus Operator...\n")
80-
Expect(utils.InstallPrometheusOperator()).To(Succeed(), "Failed to install Prometheus Operator")
86+
By("checking if prometheus is installed already")
87+
isPrometheusOperatorAlreadyInstalled = utils.IsPrometheusCRDsInstalled()
88+
if !isPrometheusOperatorAlreadyInstalled {
89+
_, _ = fmt.Fprintf(GinkgoWriter, "Installing Prometheus Operator...\n")
90+
Expect(utils.InstallPrometheusOperator()).To(Succeed(), "Failed to install Prometheus Operator")
91+
} else {
92+
_, _ = fmt.Fprintf(GinkgoWriter, "WARNING: Prometheus Operator is already installed. Skipping installation...\n")
93+
}
8194
}
8295
if !skipCertManagerInstall {
83-
_, _ = fmt.Fprintf(GinkgoWriter, "Installing CertManager...\n")
84-
Expect(utils.InstallCertManager()).To(Succeed(), "Failed to install CertManager")
96+
By("checking if cert manager is installed already")
97+
isCertManagerAlreadyInstalled = utils.IsCertManagerCRDsInstalled()
98+
if !isCertManagerAlreadyInstalled {
99+
_, _ = fmt.Fprintf(GinkgoWriter, "Installing CertManager...\n")
100+
Expect(utils.InstallCertManager()).To(Succeed(), "Failed to install CertManager")
101+
} else {
102+
_, _ = fmt.Fprintf(GinkgoWriter, "WARNING: CertManager is already installed. Skipping installation...\n")
103+
}
85104
}
86105
})
87106

88107
var _ = AfterSuite(func() {
89-
// Teardown Prometheus and CertManager after the suite if not skipped
90-
if !skipPrometheusInstall {
108+
// Teardown Prometheus and CertManager after the suite if not skipped and if they were not already installed
109+
if !skipPrometheusInstall && !isPrometheusOperatorAlreadyInstalled {
91110
_, _ = fmt.Fprintf(GinkgoWriter, "Uninstalling Prometheus Operator...\n")
92111
utils.UninstallPrometheusOperator()
93112
}
94-
if !skipCertManagerInstall {
113+
if !skipCertManagerInstall && !isCertManagerAlreadyInstalled {
95114
_, _ = fmt.Fprintf(GinkgoWriter, "Uninstalling CertManager...\n")
96115
utils.UninstallCertManager()
97116
}

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

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,6 @@ func warnError(err error) {
3838
_, _ = fmt.Fprintf(GinkgoWriter, "warning: %v\n", err)
3939
}
4040

41-
// InstallPrometheusOperator installs the prometheus Operator to be used to export the enabled metrics.
42-
func InstallPrometheusOperator() error {
43-
url := fmt.Sprintf(prometheusOperatorURL, prometheusOperatorVersion)
44-
cmd := exec.Command("kubectl", "create", "-f", url)
45-
_, err := Run(cmd)
46-
return err
47-
}
48-
4941
// Run executes the provided command within this context
5042
func Run(cmd *exec.Cmd) ([]byte, error) {
5143
dir, _ := GetProjectDir()
@@ -66,6 +58,14 @@ func Run(cmd *exec.Cmd) ([]byte, error) {
6658
return output, nil
6759
}
6860

61+
// InstallPrometheusOperator installs the prometheus Operator to be used to export the enabled metrics.
62+
func InstallPrometheusOperator() error {
63+
url := fmt.Sprintf(prometheusOperatorURL, prometheusOperatorVersion)
64+
cmd := exec.Command("kubectl", "create", "-f", url)
65+
_, err := Run(cmd)
66+
return err
67+
}
68+
6969
// UninstallPrometheusOperator uninstalls the prometheus
7070
func UninstallPrometheusOperator() {
7171
url := fmt.Sprintf(prometheusOperatorURL, prometheusOperatorVersion)
@@ -75,6 +75,33 @@ func UninstallPrometheusOperator() {
7575
}
7676
}
7777

78+
// IsPrometheusCRDsInstalled checks if any Prometheus CRDs are installed
79+
// by verifying the existence of key CRDs related to Prometheus.
80+
func IsPrometheusCRDsInstalled() bool {
81+
// List of common Prometheus CRDs
82+
prometheusCRDs := []string{
83+
"prometheuses.monitoring.coreos.com",
84+
"prometheusrules.monitoring.coreos.com",
85+
"prometheusagents.monitoring.coreos.com",
86+
}
87+
88+
cmd := exec.Command("kubectl", "get", "crds", "-o", "custom-columns=NAME:.metadata.name")
89+
output, err := Run(cmd)
90+
if err != nil {
91+
return false
92+
}
93+
crdList := GetNonEmptyLines(string(output))
94+
for _, crd := range prometheusCRDs {
95+
for _, line := range crdList {
96+
if strings.Contains(line, crd) {
97+
return true
98+
}
99+
}
100+
}
101+
102+
return false
103+
}
104+
78105
// UninstallCertManager uninstalls the cert manager
79106
func UninstallCertManager() {
80107
url := fmt.Sprintf(certmanagerURLTmpl, certmanagerVersion)
@@ -103,6 +130,39 @@ func InstallCertManager() error {
103130
return err
104131
}
105132

133+
// IsCertManagerCRDsInstalled checks if any Cert Manager CRDs are installed
134+
// by verifying the existence of key CRDs related to Cert Manager.
135+
func IsCertManagerCRDsInstalled() bool {
136+
// List of common Cert Manager CRDs
137+
certManagerCRDs := []string{
138+
"certificates.cert-manager.io",
139+
"issuers.cert-manager.io",
140+
"clusterissuers.cert-manager.io",
141+
"certificaterequests.cert-manager.io",
142+
"orders.acme.cert-manager.io",
143+
"challenges.acme.cert-manager.io",
144+
}
145+
146+
// Execute the kubectl command to get all CRDs
147+
cmd := exec.Command("kubectl", "get", "crds")
148+
output, err := Run(cmd)
149+
if err != nil {
150+
return false
151+
}
152+
153+
// Check if any of the Cert Manager CRDs are present
154+
crdList := GetNonEmptyLines(string(output))
155+
for _, crd := range certManagerCRDs {
156+
for _, line := range crdList {
157+
if strings.Contains(line, crd) {
158+
return true
159+
}
160+
}
161+
}
162+
163+
return false
164+
}
165+
106166
// LoadImageToKindClusterWithName loads a local docker image to the kind cluster
107167
func LoadImageToKindClusterWithName(name string) error {
108168
cluster := "kind"

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

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ var (
3636
// re-installation and conflicts.
3737
skipPrometheusInstall = os.Getenv("PROMETHEUS_INSTALL_SKIP") == "true"
3838
skipCertManagerInstall = os.Getenv("CERT_MANAGER_INSTALL_SKIP") == "true"
39+
// isPrometheusOperatorAlreadyInstalled will be set true when prometheus CRDs be found on the cluster
40+
isPrometheusOperatorAlreadyInstalled = false
41+
// isCertManagerAlreadyInstalled will be set true when CertManager CRDs be found on the cluster
42+
isCertManagerAlreadyInstalled = false
3943

4044
// projectImage is the name of the image which will be build and loaded
4145
// with the code source changes to be tested.
@@ -74,24 +78,39 @@ var _ = BeforeSuite(func() {
7478
err = utils.LoadImageToKindClusterWithName(projectImage)
7579
ExpectWithOffset(1, err).NotTo(HaveOccurred(), "Failed to load the manager(Operator) image into Kind")
7680

77-
// Setup Prometheus and CertManager before the suite if not skipped
81+
// The tests-e2e are intended to run on a temporary cluster that is created and destroyed for testing.
82+
// To prevent errors when tests run in environments with Prometheus or CertManager already installed,
83+
// we check for their presence before execution.
84+
// Setup Prometheus and CertManager before the suite if not skipped and if not already installed
7885
if !skipPrometheusInstall {
79-
_, _ = fmt.Fprintf(GinkgoWriter, "Installing Prometheus Operator...\n")
80-
Expect(utils.InstallPrometheusOperator()).To(Succeed(), "Failed to install Prometheus Operator")
86+
By("checking if prometheus is installed already")
87+
isPrometheusOperatorAlreadyInstalled = utils.IsPrometheusCRDsInstalled()
88+
if !isPrometheusOperatorAlreadyInstalled {
89+
_, _ = fmt.Fprintf(GinkgoWriter, "Installing Prometheus Operator...\n")
90+
Expect(utils.InstallPrometheusOperator()).To(Succeed(), "Failed to install Prometheus Operator")
91+
} else {
92+
_, _ = fmt.Fprintf(GinkgoWriter, "WARNING: Prometheus Operator is already installed. Skipping installation...\n")
93+
}
8194
}
8295
if !skipCertManagerInstall {
83-
_, _ = fmt.Fprintf(GinkgoWriter, "Installing CertManager...\n")
84-
Expect(utils.InstallCertManager()).To(Succeed(), "Failed to install CertManager")
96+
By("checking if cert manager is installed already")
97+
isCertManagerAlreadyInstalled = utils.IsCertManagerCRDsInstalled()
98+
if !isCertManagerAlreadyInstalled {
99+
_, _ = fmt.Fprintf(GinkgoWriter, "Installing CertManager...\n")
100+
Expect(utils.InstallCertManager()).To(Succeed(), "Failed to install CertManager")
101+
} else {
102+
_, _ = fmt.Fprintf(GinkgoWriter, "WARNING: CertManager is already installed. Skipping installation...\n")
103+
}
85104
}
86105
})
87106

88107
var _ = AfterSuite(func() {
89-
// Teardown Prometheus and CertManager after the suite if not skipped
90-
if !skipPrometheusInstall {
108+
// Teardown Prometheus and CertManager after the suite if not skipped and if they were not already installed
109+
if !skipPrometheusInstall && !isPrometheusOperatorAlreadyInstalled {
91110
_, _ = fmt.Fprintf(GinkgoWriter, "Uninstalling Prometheus Operator...\n")
92111
utils.UninstallPrometheusOperator()
93112
}
94-
if !skipCertManagerInstall {
113+
if !skipCertManagerInstall && !isCertManagerAlreadyInstalled {
95114
_, _ = fmt.Fprintf(GinkgoWriter, "Uninstalling CertManager...\n")
96115
utils.UninstallCertManager()
97116
}

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

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,6 @@ func warnError(err error) {
3838
_, _ = fmt.Fprintf(GinkgoWriter, "warning: %v\n", err)
3939
}
4040

41-
// InstallPrometheusOperator installs the prometheus Operator to be used to export the enabled metrics.
42-
func InstallPrometheusOperator() error {
43-
url := fmt.Sprintf(prometheusOperatorURL, prometheusOperatorVersion)
44-
cmd := exec.Command("kubectl", "create", "-f", url)
45-
_, err := Run(cmd)
46-
return err
47-
}
48-
4941
// Run executes the provided command within this context
5042
func Run(cmd *exec.Cmd) ([]byte, error) {
5143
dir, _ := GetProjectDir()
@@ -66,6 +58,14 @@ func Run(cmd *exec.Cmd) ([]byte, error) {
6658
return output, nil
6759
}
6860

61+
// InstallPrometheusOperator installs the prometheus Operator to be used to export the enabled metrics.
62+
func InstallPrometheusOperator() error {
63+
url := fmt.Sprintf(prometheusOperatorURL, prometheusOperatorVersion)
64+
cmd := exec.Command("kubectl", "create", "-f", url)
65+
_, err := Run(cmd)
66+
return err
67+
}
68+
6969
// UninstallPrometheusOperator uninstalls the prometheus
7070
func UninstallPrometheusOperator() {
7171
url := fmt.Sprintf(prometheusOperatorURL, prometheusOperatorVersion)
@@ -75,6 +75,33 @@ func UninstallPrometheusOperator() {
7575
}
7676
}
7777

78+
// IsPrometheusCRDsInstalled checks if any Prometheus CRDs are installed
79+
// by verifying the existence of key CRDs related to Prometheus.
80+
func IsPrometheusCRDsInstalled() bool {
81+
// List of common Prometheus CRDs
82+
prometheusCRDs := []string{
83+
"prometheuses.monitoring.coreos.com",
84+
"prometheusrules.monitoring.coreos.com",
85+
"prometheusagents.monitoring.coreos.com",
86+
}
87+
88+
cmd := exec.Command("kubectl", "get", "crds", "-o", "custom-columns=NAME:.metadata.name")
89+
output, err := Run(cmd)
90+
if err != nil {
91+
return false
92+
}
93+
crdList := GetNonEmptyLines(string(output))
94+
for _, crd := range prometheusCRDs {
95+
for _, line := range crdList {
96+
if strings.Contains(line, crd) {
97+
return true
98+
}
99+
}
100+
}
101+
102+
return false
103+
}
104+
78105
// UninstallCertManager uninstalls the cert manager
79106
func UninstallCertManager() {
80107
url := fmt.Sprintf(certmanagerURLTmpl, certmanagerVersion)
@@ -103,6 +130,39 @@ func InstallCertManager() error {
103130
return err
104131
}
105132

133+
// IsCertManagerCRDsInstalled checks if any Cert Manager CRDs are installed
134+
// by verifying the existence of key CRDs related to Cert Manager.
135+
func IsCertManagerCRDsInstalled() bool {
136+
// List of common Cert Manager CRDs
137+
certManagerCRDs := []string{
138+
"certificates.cert-manager.io",
139+
"issuers.cert-manager.io",
140+
"clusterissuers.cert-manager.io",
141+
"certificaterequests.cert-manager.io",
142+
"orders.acme.cert-manager.io",
143+
"challenges.acme.cert-manager.io",
144+
}
145+
146+
// Execute the kubectl command to get all CRDs
147+
cmd := exec.Command("kubectl", "get", "crds")
148+
output, err := Run(cmd)
149+
if err != nil {
150+
return false
151+
}
152+
153+
// Check if any of the Cert Manager CRDs are present
154+
crdList := GetNonEmptyLines(string(output))
155+
for _, crd := range certManagerCRDs {
156+
for _, line := range crdList {
157+
if strings.Contains(line, crd) {
158+
return true
159+
}
160+
}
161+
}
162+
163+
return false
164+
}
165+
106166
// LoadImageToKindClusterWithName loads a local docker image to the kind cluster
107167
func LoadImageToKindClusterWithName(name string) error {
108168
cluster := "kind"

0 commit comments

Comments
 (0)