Skip to content

Commit 937688e

Browse files
Add support to Prometheus by Operator
1 parent 6b37d47 commit 937688e

File tree

6 files changed

+83
-67
lines changed

6 files changed

+83
-67
lines changed

pkg/scaffold/project.go

+3-1
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

-4
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

+2
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

-62
This file was deleted.
+41
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

+37
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+
`

0 commit comments

Comments
 (0)