Skip to content

Commit c0a784a

Browse files
authored
Merge pull request #1030 from camilamacedo86/OSDK-471
Using MonitorService to setup Prometheus
2 parents 6b37d47 + bec8316 commit c0a784a

File tree

13 files changed

+139
-90
lines changed

13 files changed

+139
-90
lines changed

pkg/scaffold/project.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import (
3434
"sigs.k8s.io/kubebuilder/pkg/scaffold/v2/certmanager"
3535
managerv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2/manager"
3636
metricsauthv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2/metricsauth"
37+
"sigs.k8s.io/kubebuilder/pkg/scaffold/v2/prometheus"
3738
"sigs.k8s.io/kubebuilder/pkg/scaffold/v2/webhook"
3839
)
3940

@@ -216,7 +217,6 @@ func (p *V2Project) Scaffold() error {
216217
p.buildUniverse(),
217218
input.Options{ProjectPath: projectInput.Path, BoilerplatePath: bpInput.Path},
218219
&project.GitIgnore{},
219-
&metricsauthv2.KustomizePrometheusMetricsPatch{},
220220
&metricsauthv2.KustomizeAuthProxyPatch{},
221221
&scaffoldv2.AuthProxyService{},
222222
&project.AuthProxyRole{},
@@ -237,6 +237,8 @@ func (p *V2Project) Scaffold() error {
237237
&webhook.KustomizeConfigWebhook{},
238238
&webhook.Service{},
239239
&webhook.InjectCAPatch{},
240+
&prometheus.Kustomization{},
241+
&prometheus.PrometheusServiceMonitor{},
240242
&certmanager.CertManager{},
241243
&certmanager.Kustomization{},
242244
&certmanager.KustomizeConfig{})

pkg/scaffold/v2/authproxyservice.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,6 @@ func (r *AuthProxyService) GetInput() (input.Input, error) {
4141
var AuthProxyServiceTemplate = `apiVersion: v1
4242
kind: Service
4343
metadata:
44-
annotations:
45-
prometheus.io/port: "8443"
46-
prometheus.io/scheme: https
47-
prometheus.io/scrape: "true"
4844
labels:
4945
control-plane: controller-manager
5046
name: controller-manager-metrics-service

pkg/scaffold/v2/kustomize.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,8 @@ bases:
7373
#- ../webhook
7474
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
7575
#- ../certmanager
76+
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
77+
#- ../prometheus
7678
7779
patchesStrategicMerge:
7880
# Protect the /metrics endpoint by putting it behind auth.

pkg/scaffold/v2/metricsauth/kustomize_metrics_patch.go

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright 2019 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package prometheus
18+
19+
import (
20+
"path/filepath"
21+
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
22+
)
23+
24+
// Kustomization scaffolds the kustomizaiton in the prometheus folder
25+
type Kustomization struct {
26+
input.Input
27+
}
28+
29+
// GetInput implements input.File
30+
func (p *Kustomization) GetInput() (input.Input, error) {
31+
if p.Path == "" {
32+
p.Path = filepath.Join("config", "prometheus", "kustomization.yaml")
33+
}
34+
p.TemplateBody = kustomizationTemplate
35+
return p.Input, nil
36+
}
37+
38+
var kustomizationTemplate = `resources:
39+
- monitor.yaml
40+
`
41+

pkg/scaffold/v2/prometheus/monitor.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package prometheus
2+
3+
import (
4+
"path/filepath"
5+
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
6+
)
7+
8+
// PrometheusMetricsService scaffolds an issuer CR and a certificate CR
9+
type PrometheusServiceMonitor struct {
10+
input.Input
11+
}
12+
13+
// GetInput implements input.File
14+
func (p *PrometheusServiceMonitor) GetInput() (input.Input, error) {
15+
if p.Path == "" {
16+
p.Path = filepath.Join("config", "prometheus", "monitor.yaml")
17+
}
18+
p.TemplateBody = monitorTemplate
19+
return p.Input, nil
20+
}
21+
22+
var monitorTemplate = `
23+
# Prometheus Monitor Service (Metrics)
24+
apiVersion: monitoring.coreos.com/v1
25+
kind: ServiceMonitor
26+
metadata:
27+
labels:
28+
control-plane: controller-manager
29+
name: controller-manager-metrics-monitor
30+
namespace: system
31+
spec:
32+
endpoints:
33+
- path: /metrics
34+
port: https
35+
selector:
36+
control-plane: controller-manager
37+
`

test/e2e/utils/test_context.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
)
2828

2929
const certmanagerVersion = "v0.10.1"
30+
const prometheusOperatorVersion= "0.33"
3031

3132
// KBTestContext specified to run e2e tests
3233
type KBTestContext struct {
@@ -94,6 +95,19 @@ func (kc *KBTestContext) InstallCertManager() error {
9495
return err
9596
}
9697

98+
// InstallPrometheusOperManager installs the prometheus manager bundle.
99+
func (kc *KBTestContext) InstallPrometheusOperManager() error {
100+
_, err := kc.Kubectl.Apply(false, "-f", fmt.Sprintf("https://raw.githubusercontent.com/coreos/prometheus-operator/release-%s/bundle.yaml", prometheusOperatorVersion))
101+
return err
102+
}
103+
104+
// UninstallPrometheusOperManager uninstalls the prometheus manager bundle.
105+
func (kc *KBTestContext) UninstallPrometheusOperManager() {
106+
if _, err := kc.Kubectl.Delete(false, "-f", fmt.Sprintf("https://github.com/coreos/prometheus-operator/blob/release-%s/bundle.yaml", prometheusOperatorVersion)); err != nil {
107+
fmt.Fprintf(GinkgoWriter, "error when running kubectl delete during cleaning up prometheus bundle: %v\n", err)
108+
}
109+
}
110+
97111
// UninstallCertManager uninstalls the cert manager bundle.
98112
func (kc *KBTestContext) UninstallCertManager() {
99113
if _, err := kc.Kubectl.Delete(false, "-f", fmt.Sprintf("https://github.com/jetstack/cert-manager/releases/download/%s/cert-manager.yaml", certmanagerVersion)); err != nil {

test/e2e/v2/e2e_suite.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,18 @@ var _ = Describe("kubebuilder", func() {
4040

4141
By("installing cert manager bundle")
4242
Expect(kbc.InstallCertManager()).To(Succeed())
43+
44+
By("installing prometheus operator")
45+
Expect(kbc.InstallPrometheusOperManager()).To(Succeed())
4346
})
4447

4548
AfterEach(func() {
4649
By("clean up created API objects during test process")
4750
kbc.CleanupManifests(filepath.Join("config", "default"))
4851

52+
By("uninstalling prometheus manager bundle")
53+
kbc.UninstallPrometheusOperManager()
54+
4955
By("uninstalling cert manager bundle")
5056
kbc.UninstallCertManager()
5157

@@ -104,6 +110,9 @@ var _ = Describe("kubebuilder", func() {
104110
Expect(utils.UncommentCode(
105111
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
106112
"#- ../certmanager", "#")).To(Succeed())
113+
Expect(utils.UncommentCode(
114+
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
115+
"#- ../prometheus", "#")).To(Succeed())
107116
Expect(utils.UncommentCode(
108117
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
109118
"#- manager_webhook_patch.yaml", "#")).To(Succeed())
@@ -189,6 +198,20 @@ var _ = Describe("kubebuilder", func() {
189198
return err
190199
}, time.Minute, time.Second).Should(Succeed())
191200

201+
By("validate prometheus manager has provisioned the Service")
202+
Eventually(func() error {
203+
_, err := kbc.Kubectl.Get(
204+
false,
205+
"Service", "prometheus-operator")
206+
return err
207+
}, time.Minute, time.Second).Should(Succeed())
208+
209+
By("validate Service Monitor for Prometheus is applied in the namespace")
210+
_, err = kbc.Kubectl.Get(
211+
true,
212+
"ServiceMonitor")
213+
Expect(err).NotTo(HaveOccurred())
214+
192215
By("validate the mutating|validating webhooks have the CA injected")
193216
verifyCAInjection := func() error {
194217
mwhOutput, err := kbc.Kubectl.Get(

testdata/project-v2/config/default/kustomization.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ bases:
2020
#- ../webhook
2121
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
2222
#- ../certmanager
23+
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
24+
#- ../prometheus
2325

2426
patchesStrategicMerge:
2527
# Protect the /metrics endpoint by putting it behind auth.

testdata/project-v2/config/default/manager_prometheus_metrics_patch.yaml

Lines changed: 0 additions & 19 deletions
This file was deleted.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
resources:
2+
- monitor.yaml
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
# Prometheus Monitor Service (Metrics)
3+
apiVersion: monitoring.coreos.com/v1
4+
kind: ServiceMonitor
5+
metadata:
6+
labels:
7+
control-plane: controller-manager
8+
name: controller-manager-metrics-monitor
9+
namespace: system
10+
spec:
11+
endpoints:
12+
- path: /metrics
13+
port: https
14+
selector:
15+
control-plane: controller-manager

testdata/project-v2/config/rbac/auth_proxy_service.yaml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
apiVersion: v1
22
kind: Service
33
metadata:
4-
annotations:
5-
prometheus.io/port: "8443"
6-
prometheus.io/scheme: https
7-
prometheus.io/scrape: "true"
84
labels:
95
control-plane: controller-manager
106
name: controller-manager-metrics-service

0 commit comments

Comments
 (0)