Skip to content

Commit 2d76020

Browse files
authored
Merge pull request #468 from droot/feature/enable-metrics
enable prometheus metrics integration
2 parents 86180a2 + 12ad30b commit 2d76020

File tree

6 files changed

+103
-1
lines changed

6 files changed

+103
-1
lines changed

cmd/init_project.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ Writes the following files:
5252
- a Gopkg.toml with project dependencies
5353
- a Kustomization.yaml for customizating manifests
5454
- a Patch file for customizing image for manager manifests
55+
- a Patch file for enabling prometheus metrics
5556
- a cmd/manager/main.go to run
5657
5758
project will prompt the user to run 'dep ensure' after writing the project files.
@@ -141,7 +142,8 @@ func (o *projectOptions) runInit() {
141142
&manager.Config{Image: imgName},
142143
&project.GitIgnore{},
143144
&project.Kustomize{},
144-
&project.KustomizeImagePatch{})
145+
&project.KustomizeImagePatch{},
146+
&project.KustomizePrometheusMetricsPatch{})
145147
if err != nil {
146148
log.Fatal(err)
147149
}

pkg/scaffold/project/kustomize.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ resources:
7777
7878
patches:
7979
- manager_image_patch.yaml
80+
- manager_prometheus_metrics_patch.yaml
8081
8182
vars:
8283
- name: WEBHOOK_SECRET_NAME
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright 2018 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 project
18+
19+
import (
20+
"path/filepath"
21+
22+
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
23+
)
24+
25+
var _ input.File = &KustomizePrometheusMetricsPatch{}
26+
27+
// KustomizePrometheusMetricsPatch scaffolds the patch file for enabling
28+
// prometheus metrics for manager Pod.
29+
type KustomizePrometheusMetricsPatch struct {
30+
input.Input
31+
}
32+
33+
// GetInput implements input.File
34+
func (c *KustomizePrometheusMetricsPatch) GetInput() (input.Input, error) {
35+
if c.Path == "" {
36+
c.Path = filepath.Join("config", "default", "manager_prometheus_metrics_patch.yaml")
37+
}
38+
c.TemplateBody = kustomizePrometheusMetricsPatchTemplate
39+
c.Input.IfExistsAction = input.Error
40+
return c.Input, nil
41+
}
42+
43+
var kustomizePrometheusMetricsPatchTemplate = `# This patch enables Prometheus scraping for the manager pod.
44+
apiVersion: apps/v1
45+
kind: StatefulSet
46+
metadata:
47+
name: controller-manager
48+
namespace: system
49+
spec:
50+
template:
51+
metadata:
52+
annotations:
53+
prometheus.io/scrape: 'true'
54+
spec:
55+
containers:
56+
# Expose the prometheus metrics on default port
57+
- name: manager
58+
ports:
59+
- containerPort: 8080
60+
name: metrics
61+
protocol: TCP
62+
`

pkg/scaffold/project/project_test.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,23 @@ Copyright 2019 Example Owners.
218218
})
219219
})
220220

221+
Describe("scaffolding a Kustomize prometheus metrics patch", func() {
222+
BeforeEach(func() {
223+
goldenPath = filepath.Join("config", "default", "manager_prometheus_metrics_patch.yaml")
224+
writeToPath = goldenPath
225+
})
226+
Context("with defaults ", func() {
227+
It("should match the golden file", func() {
228+
instance := &KustomizePrometheusMetricsPatch{}
229+
instance.Repo = "sigs.k8s.io/kubebuilder/test/project"
230+
Expect(s.Execute(input.Options{}, instance)).NotTo(HaveOccurred())
231+
232+
// Verify the contents matches the golden file.
233+
Expect(result.Actual.String()).To(BeEquivalentTo(result.Golden))
234+
})
235+
})
236+
})
237+
221238
Describe("scaffolding a .gitignore", func() {
222239
BeforeEach(func() {
223240
goldenPath = filepath.Join(".gitignore")

test/project/config/default/kustomization.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ resources:
2424

2525
patches:
2626
- manager_image_patch.yaml
27+
- manager_prometheus_metrics_patch.yaml
2728

2829
vars:
2930
- name: WEBHOOK_SECRET_NAME
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# This patch enables Prometheus scraping for the manager pod.
2+
apiVersion: apps/v1
3+
kind: StatefulSet
4+
metadata:
5+
name: controller-manager
6+
namespace: system
7+
spec:
8+
template:
9+
metadata:
10+
annotations:
11+
prometheus.io/scrape: 'true'
12+
spec:
13+
containers:
14+
# Expose the prometheus metrics on default port
15+
- name: manager
16+
ports:
17+
- containerPort: 8080
18+
name: metrics
19+
protocol: TCP

0 commit comments

Comments
 (0)