Skip to content

Commit 196e8e9

Browse files
remove: Prometheus install/check helpers from e2e utils
1 parent 1b82a27 commit 196e8e9

File tree

9 files changed

+159
-334
lines changed

9 files changed

+159
-334
lines changed

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

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -63,50 +63,6 @@ func Run(cmd *exec.Cmd) (string, error) {
6363
return string(output), nil
6464
}
6565

66-
// InstallPrometheusOperator installs the prometheus Operator to be used to export the enabled metrics.
67-
func InstallPrometheusOperator() error {
68-
url := fmt.Sprintf(prometheusOperatorURL, prometheusOperatorVersion)
69-
cmd := exec.Command("kubectl", "create", "-f", url)
70-
_, err := Run(cmd)
71-
return err
72-
}
73-
74-
// UninstallPrometheusOperator uninstalls the prometheus
75-
func UninstallPrometheusOperator() {
76-
url := fmt.Sprintf(prometheusOperatorURL, prometheusOperatorVersion)
77-
cmd := exec.Command("kubectl", "delete", "-f", url)
78-
if _, err := Run(cmd); err != nil {
79-
warnError(err)
80-
}
81-
}
82-
83-
// IsPrometheusCRDsInstalled checks if any Prometheus CRDs are installed
84-
// by verifying the existence of key CRDs related to Prometheus.
85-
func IsPrometheusCRDsInstalled() bool {
86-
// List of common Prometheus CRDs
87-
prometheusCRDs := []string{
88-
"prometheuses.monitoring.coreos.com",
89-
"prometheusrules.monitoring.coreos.com",
90-
"prometheusagents.monitoring.coreos.com",
91-
}
92-
93-
cmd := exec.Command("kubectl", "get", "crds", "-o", "custom-columns=NAME:.metadata.name")
94-
output, err := Run(cmd)
95-
if err != nil {
96-
return false
97-
}
98-
crdList := GetNonEmptyLines(output)
99-
for _, crd := range prometheusCRDs {
100-
for _, line := range crdList {
101-
if strings.Contains(line, crd) {
102-
return true
103-
}
104-
}
105-
}
106-
107-
return false
108-
}
109-
11066
// UninstallCertManager uninstalls the cert manager
11167
func UninstallCertManager() {
11268
url := fmt.Sprintf(certmanagerURLTmpl, certmanagerVersion)
@@ -168,6 +124,50 @@ func IsCertManagerCRDsInstalled() bool {
168124
return false
169125
}
170126

127+
// InstallPrometheusOperator installs the prometheus Operator to be used to export the enabled metrics.
128+
func InstallPrometheusOperator() error {
129+
url := fmt.Sprintf(prometheusOperatorURL, prometheusOperatorVersion)
130+
cmd := exec.Command("kubectl", "create", "-f", url)
131+
_, err := Run(cmd)
132+
return err
133+
}
134+
135+
// UninstallPrometheusOperator uninstalls the prometheus
136+
func UninstallPrometheusOperator() {
137+
url := fmt.Sprintf(prometheusOperatorURL, prometheusOperatorVersion)
138+
cmd := exec.Command("kubectl", "delete", "-f", url)
139+
if _, err := Run(cmd); err != nil {
140+
warnError(err)
141+
}
142+
}
143+
144+
// IsPrometheusCRDsInstalled checks if any Prometheus CRDs are installed
145+
// by verifying the existence of key CRDs related to Prometheus.
146+
func IsPrometheusCRDsInstalled() bool {
147+
// List of common Prometheus CRDs
148+
prometheusCRDs := []string{
149+
"prometheuses.monitoring.coreos.com",
150+
"prometheusrules.monitoring.coreos.com",
151+
"prometheusagents.monitoring.coreos.com",
152+
}
153+
154+
cmd := exec.Command("kubectl", "get", "crds", "-o", "custom-columns=NAME:.metadata.name")
155+
output, err := Run(cmd)
156+
if err != nil {
157+
return false
158+
}
159+
crdList := GetNonEmptyLines(output)
160+
for _, crd := range prometheusCRDs {
161+
for _, line := range crdList {
162+
if strings.Contains(line, crd) {
163+
return true
164+
}
165+
}
166+
}
167+
168+
return false
169+
}
170+
171171
// LoadImageToKindClusterWithName loads a local docker image to the kind cluster
172172
func LoadImageToKindClusterWithName(name string) error {
173173
cluster := defaultKindCluster

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

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ const (
3333

3434
defaultKindBinary = "kind"
3535
defaultKindCluster = "kind"
36-
37-
prometheusOperatorVersion = "v0.77.1"
38-
prometheusOperatorURL = "https://github.com/prometheus-operator/prometheus-operator/" +
39-
"releases/download/%s/bundle.yaml"
4036
)
4137

4238
func warnError(err error) {
@@ -63,50 +59,6 @@ func Run(cmd *exec.Cmd) (string, error) {
6359
return string(output), nil
6460
}
6561

66-
// InstallPrometheusOperator installs the prometheus Operator to be used to export the enabled metrics.
67-
func InstallPrometheusOperator() error {
68-
url := fmt.Sprintf(prometheusOperatorURL, prometheusOperatorVersion)
69-
cmd := exec.Command("kubectl", "create", "-f", url)
70-
_, err := Run(cmd)
71-
return err
72-
}
73-
74-
// UninstallPrometheusOperator uninstalls the prometheus
75-
func UninstallPrometheusOperator() {
76-
url := fmt.Sprintf(prometheusOperatorURL, prometheusOperatorVersion)
77-
cmd := exec.Command("kubectl", "delete", "-f", url)
78-
if _, err := Run(cmd); err != nil {
79-
warnError(err)
80-
}
81-
}
82-
83-
// IsPrometheusCRDsInstalled checks if any Prometheus CRDs are installed
84-
// by verifying the existence of key CRDs related to Prometheus.
85-
func IsPrometheusCRDsInstalled() bool {
86-
// List of common Prometheus CRDs
87-
prometheusCRDs := []string{
88-
"prometheuses.monitoring.coreos.com",
89-
"prometheusrules.monitoring.coreos.com",
90-
"prometheusagents.monitoring.coreos.com",
91-
}
92-
93-
cmd := exec.Command("kubectl", "get", "crds", "-o", "custom-columns=NAME:.metadata.name")
94-
output, err := Run(cmd)
95-
if err != nil {
96-
return false
97-
}
98-
crdList := GetNonEmptyLines(output)
99-
for _, crd := range prometheusCRDs {
100-
for _, line := range crdList {
101-
if strings.Contains(line, crd) {
102-
return true
103-
}
104-
}
105-
}
106-
107-
return false
108-
}
109-
11062
// UninstallCertManager uninstalls the cert manager
11163
func UninstallCertManager() {
11264
url := fmt.Sprintf(certmanagerURLTmpl, certmanagerVersion)

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

Lines changed: 44 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -63,50 +63,6 @@ func Run(cmd *exec.Cmd) (string, error) {
6363
return string(output), nil
6464
}
6565

66-
// InstallPrometheusOperator installs the prometheus Operator to be used to export the enabled metrics.
67-
func InstallPrometheusOperator() error {
68-
url := fmt.Sprintf(prometheusOperatorURL, prometheusOperatorVersion)
69-
cmd := exec.Command("kubectl", "create", "-f", url)
70-
_, err := Run(cmd)
71-
return err
72-
}
73-
74-
// UninstallPrometheusOperator uninstalls the prometheus
75-
func UninstallPrometheusOperator() {
76-
url := fmt.Sprintf(prometheusOperatorURL, prometheusOperatorVersion)
77-
cmd := exec.Command("kubectl", "delete", "-f", url)
78-
if _, err := Run(cmd); err != nil {
79-
warnError(err)
80-
}
81-
}
82-
83-
// IsPrometheusCRDsInstalled checks if any Prometheus CRDs are installed
84-
// by verifying the existence of key CRDs related to Prometheus.
85-
func IsPrometheusCRDsInstalled() bool {
86-
// List of common Prometheus CRDs
87-
prometheusCRDs := []string{
88-
"prometheuses.monitoring.coreos.com",
89-
"prometheusrules.monitoring.coreos.com",
90-
"prometheusagents.monitoring.coreos.com",
91-
}
92-
93-
cmd := exec.Command("kubectl", "get", "crds", "-o", "custom-columns=NAME:.metadata.name")
94-
output, err := Run(cmd)
95-
if err != nil {
96-
return false
97-
}
98-
crdList := GetNonEmptyLines(output)
99-
for _, crd := range prometheusCRDs {
100-
for _, line := range crdList {
101-
if strings.Contains(line, crd) {
102-
return true
103-
}
104-
}
105-
}
106-
107-
return false
108-
}
109-
11066
// UninstallCertManager uninstalls the cert manager
11167
func UninstallCertManager() {
11268
url := fmt.Sprintf(certmanagerURLTmpl, certmanagerVersion)
@@ -168,6 +124,50 @@ func IsCertManagerCRDsInstalled() bool {
168124
return false
169125
}
170126

127+
// InstallPrometheusOperator installs the prometheus Operator to be used to export the enabled metrics.
128+
func InstallPrometheusOperator() error {
129+
url := fmt.Sprintf(prometheusOperatorURL, prometheusOperatorVersion)
130+
cmd := exec.Command("kubectl", "create", "-f", url)
131+
_, err := Run(cmd)
132+
return err
133+
}
134+
135+
// UninstallPrometheusOperator uninstalls the prometheus
136+
func UninstallPrometheusOperator() {
137+
url := fmt.Sprintf(prometheusOperatorURL, prometheusOperatorVersion)
138+
cmd := exec.Command("kubectl", "delete", "-f", url)
139+
if _, err := Run(cmd); err != nil {
140+
warnError(err)
141+
}
142+
}
143+
144+
// IsPrometheusCRDsInstalled checks if any Prometheus CRDs are installed
145+
// by verifying the existence of key CRDs related to Prometheus.
146+
func IsPrometheusCRDsInstalled() bool {
147+
// List of common Prometheus CRDs
148+
prometheusCRDs := []string{
149+
"prometheuses.monitoring.coreos.com",
150+
"prometheusrules.monitoring.coreos.com",
151+
"prometheusagents.monitoring.coreos.com",
152+
}
153+
154+
cmd := exec.Command("kubectl", "get", "crds", "-o", "custom-columns=NAME:.metadata.name")
155+
output, err := Run(cmd)
156+
if err != nil {
157+
return false
158+
}
159+
crdList := GetNonEmptyLines(output)
160+
for _, crd := range prometheusCRDs {
161+
for _, line := range crdList {
162+
if strings.Contains(line, crd) {
163+
return true
164+
}
165+
}
166+
}
167+
168+
return false
169+
}
170+
171171
// LoadImageToKindClusterWithName loads a local docker image to the kind cluster
172172
func LoadImageToKindClusterWithName(name string) error {
173173
cluster := defaultKindCluster

hack/docs/internal/cronjob-tutorial/e2e_implementation.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,54 @@ const serviceMonitorE2e = `
5353
cmd = exec.Command("kubectl", "get", "ServiceMonitor", "-n", namespace)
5454
_, err = utils.Run(cmd)
5555
Expect(err).NotTo(HaveOccurred(), "ServiceMonitor should exist")`
56+
57+
const prometheusUtilities = `// InstallPrometheusOperator installs the prometheus Operator to be used to export the enabled metrics.
58+
func InstallPrometheusOperator() error {
59+
url := fmt.Sprintf(prometheusOperatorURL, prometheusOperatorVersion)
60+
cmd := exec.Command("kubectl", "create", "-f", url)
61+
_, err := Run(cmd)
62+
return err
63+
}
64+
65+
// UninstallPrometheusOperator uninstalls the prometheus
66+
func UninstallPrometheusOperator() {
67+
url := fmt.Sprintf(prometheusOperatorURL, prometheusOperatorVersion)
68+
cmd := exec.Command("kubectl", "delete", "-f", url)
69+
if _, err := Run(cmd); err != nil {
70+
warnError(err)
71+
}
72+
}
73+
74+
// IsPrometheusCRDsInstalled checks if any Prometheus CRDs are installed
75+
// by verifying the existence of key CRDs related to Prometheus.
76+
func IsPrometheusCRDsInstalled() bool {
77+
// List of common Prometheus CRDs
78+
prometheusCRDs := []string{
79+
"prometheuses.monitoring.coreos.com",
80+
"prometheusrules.monitoring.coreos.com",
81+
"prometheusagents.monitoring.coreos.com",
82+
}
83+
84+
cmd := exec.Command("kubectl", "get", "crds", "-o", "custom-columns=NAME:.metadata.name")
85+
output, err := Run(cmd)
86+
if err != nil {
87+
return false
88+
}
89+
crdList := GetNonEmptyLines(output)
90+
for _, crd := range prometheusCRDs {
91+
for _, line := range crdList {
92+
if strings.Contains(line, crd) {
93+
return true
94+
}
95+
}
96+
}
97+
98+
return false
99+
}
100+
`
101+
102+
const prometheusVersionURL = `
103+
104+
prometheusOperatorVersion = "v0.77.1"
105+
prometheusOperatorURL = "https://github.com/prometheus-operator/prometheus-operator/" +
106+
"releases/download/%s/bundle.yaml"`

hack/docs/internal/cronjob-tutorial/generate_cronjob.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -662,22 +662,36 @@ func (sp *Sample) addControllerTest() {
662662
func (sp *Sample) updateE2E() {
663663
cronjobE2ESuite := filepath.Join(sp.ctx.Dir, "test", "e2e", "e2e_suite_test.go")
664664
cronjobE2ETest := filepath.Join(sp.ctx.Dir, "test", "e2e", "e2e_test.go")
665+
cronjobE2EUtils := filepath.Join(sp.ctx.Dir, "test", "utils", "utils.go")
665666
var err error
666667

667668
err = pluginutil.InsertCode(cronjobE2ESuite, `isCertManagerAlreadyInstalled = false`, isPrometheusInstalledVar)
668-
hackutils.CheckError("fixing test/e2e/e2e_suite_test.go", err)
669+
hackutils.CheckError("fixing test/e2e/e2e_suite_test.go by adding isPrometheusInstalledVar", err)
669670

670671
err = pluginutil.InsertCode(cronjobE2ESuite, `var _ = BeforeSuite(func() {`, beforeSuitePrometheus)
671-
hackutils.CheckError("fixing test/e2e/e2e_suite_test.go", err)
672+
hackutils.CheckError("fixing test/e2e/e2e_suite_test.go by adding prometheus code in the before suite", err)
672673

673674
err = pluginutil.InsertCode(cronjobE2ESuite,
674675
`// The tests-e2e are intended to run on a temporary cluster that is created and destroyed for testing.`,
675676
checkPrometheusInstalled)
676-
hackutils.CheckError("fixing test/e2e/e2e_suite_test.go", err)
677+
hackutils.CheckError("fixing test/e2e/e2e_suite_test.go by adding code check if has prometheus", err)
678+
679+
err = pluginutil.InsertCode(cronjobE2EUtils,
680+
`defaultKindCluster = "kind"`,
681+
prometheusVersionURL)
682+
hackutils.CheckError("fixing test/e2e/e2e_suite_test.go by adding prometheus version and URL", err)
683+
684+
err = pluginutil.InsertCode(cronjobE2EUtils,
685+
`return false
686+
}
687+
`,
688+
prometheusUtilities)
689+
hackutils.CheckError("fixing test/e2e/e2e_suite_test.go by adding prometheus version and URL", err)
677690

678691
err = pluginutil.InsertCode(cronjobE2ESuite, `var _ = AfterSuite(func() {`, afterSuitePrometheus)
679-
hackutils.CheckError("fixing test/e2e/e2e_suite_test.go", err)
692+
hackutils.CheckError("fixing test/e2e/e2e_suite_test.go by adding prometheus code after suite", err)
680693

681-
err = pluginutil.InsertCode(cronjobE2ETest, `Expect(err).NotTo(HaveOccurred(), "Metrics service should exist")`, serviceMonitorE2e)
682-
hackutils.CheckError("fixing test/e2e/e2e_test.go", err)
694+
err = pluginutil.InsertCode(cronjobE2ETest, `Expect(err).NotTo(HaveOccurred(), "Metrics service should exist")`,
695+
serviceMonitorE2e)
696+
hackutils.CheckError("fixing test/e2e/e2e_test.go by adding ServiceMonitor should exist", err)
683697
}

0 commit comments

Comments
 (0)