Skip to content

Commit

Permalink
enable prometheus metrics integration
Browse files Browse the repository at this point in the history
  • Loading branch information
droot committed Nov 1, 2018
1 parent 86180a2 commit 12ad30b
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cmd/init_project.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Writes the following files:
- a Gopkg.toml with project dependencies
- a Kustomization.yaml for customizating manifests
- a Patch file for customizing image for manager manifests
- a Patch file for enabling prometheus metrics
- a cmd/manager/main.go to run
project will prompt the user to run 'dep ensure' after writing the project files.
Expand Down Expand Up @@ -141,7 +142,8 @@ func (o *projectOptions) runInit() {
&manager.Config{Image: imgName},
&project.GitIgnore{},
&project.Kustomize{},
&project.KustomizeImagePatch{})
&project.KustomizeImagePatch{},
&project.KustomizePrometheusMetricsPatch{})
if err != nil {
log.Fatal(err)
}
Expand Down
1 change: 1 addition & 0 deletions pkg/scaffold/project/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ resources:
patches:
- manager_image_patch.yaml
- manager_prometheus_metrics_patch.yaml
vars:
- name: WEBHOOK_SECRET_NAME
Expand Down
62 changes: 62 additions & 0 deletions pkg/scaffold/project/kustomize_metrics_patch.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package project

import (
"path/filepath"

"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
)

var _ input.File = &KustomizePrometheusMetricsPatch{}

// KustomizePrometheusMetricsPatch scaffolds the patch file for enabling
// prometheus metrics for manager Pod.
type KustomizePrometheusMetricsPatch struct {
input.Input
}

// GetInput implements input.File
func (c *KustomizePrometheusMetricsPatch) GetInput() (input.Input, error) {
if c.Path == "" {
c.Path = filepath.Join("config", "default", "manager_prometheus_metrics_patch.yaml")
}
c.TemplateBody = kustomizePrometheusMetricsPatchTemplate
c.Input.IfExistsAction = input.Error
return c.Input, nil
}

var kustomizePrometheusMetricsPatchTemplate = `# This patch enables Prometheus scraping for the manager pod.
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: controller-manager
namespace: system
spec:
template:
metadata:
annotations:
prometheus.io/scrape: 'true'
spec:
containers:
# Expose the prometheus metrics on default port
- name: manager
ports:
- containerPort: 8080
name: metrics
protocol: TCP
`
17 changes: 17 additions & 0 deletions pkg/scaffold/project/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,23 @@ Copyright 2019 Example Owners.
})
})

Describe("scaffolding a Kustomize prometheus metrics patch", func() {
BeforeEach(func() {
goldenPath = filepath.Join("config", "default", "manager_prometheus_metrics_patch.yaml")
writeToPath = goldenPath
})
Context("with defaults ", func() {
It("should match the golden file", func() {
instance := &KustomizePrometheusMetricsPatch{}
instance.Repo = "sigs.k8s.io/kubebuilder/test/project"
Expect(s.Execute(input.Options{}, instance)).NotTo(HaveOccurred())

// Verify the contents matches the golden file.
Expect(result.Actual.String()).To(BeEquivalentTo(result.Golden))
})
})
})

Describe("scaffolding a .gitignore", func() {
BeforeEach(func() {
goldenPath = filepath.Join(".gitignore")
Expand Down
1 change: 1 addition & 0 deletions test/project/config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ resources:

patches:
- manager_image_patch.yaml
- manager_prometheus_metrics_patch.yaml

vars:
- name: WEBHOOK_SECRET_NAME
Expand Down
19 changes: 19 additions & 0 deletions test/project/config/default/manager_prometheus_metrics_patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This patch enables Prometheus scraping for the manager pod.
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: controller-manager
namespace: system
spec:
template:
metadata:
annotations:
prometheus.io/scrape: 'true'
spec:
containers:
# Expose the prometheus metrics on default port
- name: manager
ports:
- containerPort: 8080
name: metrics
protocol: TCP

0 comments on commit 12ad30b

Please sign in to comment.