Skip to content

Commit

Permalink
Merge pull request #1030 from camilamacedo86/OSDK-471
Browse files Browse the repository at this point in the history
Using MonitorService to setup Prometheus
  • Loading branch information
k8s-ci-robot authored Oct 14, 2019
2 parents 6b37d47 + bec8316 commit c0a784a
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 90 deletions.
4 changes: 3 additions & 1 deletion pkg/scaffold/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"sigs.k8s.io/kubebuilder/pkg/scaffold/v2/certmanager"
managerv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2/manager"
metricsauthv2 "sigs.k8s.io/kubebuilder/pkg/scaffold/v2/metricsauth"
"sigs.k8s.io/kubebuilder/pkg/scaffold/v2/prometheus"
"sigs.k8s.io/kubebuilder/pkg/scaffold/v2/webhook"
)

Expand Down Expand Up @@ -216,7 +217,6 @@ func (p *V2Project) Scaffold() error {
p.buildUniverse(),
input.Options{ProjectPath: projectInput.Path, BoilerplatePath: bpInput.Path},
&project.GitIgnore{},
&metricsauthv2.KustomizePrometheusMetricsPatch{},
&metricsauthv2.KustomizeAuthProxyPatch{},
&scaffoldv2.AuthProxyService{},
&project.AuthProxyRole{},
Expand All @@ -237,6 +237,8 @@ func (p *V2Project) Scaffold() error {
&webhook.KustomizeConfigWebhook{},
&webhook.Service{},
&webhook.InjectCAPatch{},
&prometheus.Kustomization{},
&prometheus.PrometheusServiceMonitor{},
&certmanager.CertManager{},
&certmanager.Kustomization{},
&certmanager.KustomizeConfig{})
Expand Down
4 changes: 0 additions & 4 deletions pkg/scaffold/v2/authproxyservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,6 @@ func (r *AuthProxyService) GetInput() (input.Input, error) {
var AuthProxyServiceTemplate = `apiVersion: v1
kind: Service
metadata:
annotations:
prometheus.io/port: "8443"
prometheus.io/scheme: https
prometheus.io/scrape: "true"
labels:
control-plane: controller-manager
name: controller-manager-metrics-service
Expand Down
2 changes: 2 additions & 0 deletions pkg/scaffold/v2/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ bases:
#- ../webhook
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
#- ../certmanager
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
#- ../prometheus
patchesStrategicMerge:
# Protect the /metrics endpoint by putting it behind auth.
Expand Down
62 changes: 0 additions & 62 deletions pkg/scaffold/v2/metricsauth/kustomize_metrics_patch.go

This file was deleted.

41 changes: 41 additions & 0 deletions pkg/scaffold/v2/prometheus/kustomize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2019 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 prometheus

import (
"path/filepath"
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
)

// Kustomization scaffolds the kustomizaiton in the prometheus folder
type Kustomization struct {
input.Input
}

// GetInput implements input.File
func (p *Kustomization) GetInput() (input.Input, error) {
if p.Path == "" {
p.Path = filepath.Join("config", "prometheus", "kustomization.yaml")
}
p.TemplateBody = kustomizationTemplate
return p.Input, nil
}

var kustomizationTemplate = `resources:
- monitor.yaml
`

37 changes: 37 additions & 0 deletions pkg/scaffold/v2/prometheus/monitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package prometheus

import (
"path/filepath"
"sigs.k8s.io/kubebuilder/pkg/scaffold/input"
)

// PrometheusMetricsService scaffolds an issuer CR and a certificate CR
type PrometheusServiceMonitor struct {
input.Input
}

// GetInput implements input.File
func (p *PrometheusServiceMonitor) GetInput() (input.Input, error) {
if p.Path == "" {
p.Path = filepath.Join("config", "prometheus", "monitor.yaml")
}
p.TemplateBody = monitorTemplate
return p.Input, nil
}

var monitorTemplate = `
# Prometheus Monitor Service (Metrics)
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
labels:
control-plane: controller-manager
name: controller-manager-metrics-monitor
namespace: system
spec:
endpoints:
- path: /metrics
port: https
selector:
control-plane: controller-manager
`
14 changes: 14 additions & 0 deletions test/e2e/utils/test_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
)

const certmanagerVersion = "v0.10.1"
const prometheusOperatorVersion= "0.33"

// KBTestContext specified to run e2e tests
type KBTestContext struct {
Expand Down Expand Up @@ -94,6 +95,19 @@ func (kc *KBTestContext) InstallCertManager() error {
return err
}

// InstallPrometheusOperManager installs the prometheus manager bundle.
func (kc *KBTestContext) InstallPrometheusOperManager() error {
_, err := kc.Kubectl.Apply(false, "-f", fmt.Sprintf("https://raw.githubusercontent.com/coreos/prometheus-operator/release-%s/bundle.yaml", prometheusOperatorVersion))
return err
}

// UninstallPrometheusOperManager uninstalls the prometheus manager bundle.
func (kc *KBTestContext) UninstallPrometheusOperManager() {
if _, err := kc.Kubectl.Delete(false, "-f", fmt.Sprintf("https://github.com/coreos/prometheus-operator/blob/release-%s/bundle.yaml", prometheusOperatorVersion)); err != nil {
fmt.Fprintf(GinkgoWriter, "error when running kubectl delete during cleaning up prometheus bundle: %v\n", err)
}
}

// UninstallCertManager uninstalls the cert manager bundle.
func (kc *KBTestContext) UninstallCertManager() {
if _, err := kc.Kubectl.Delete(false, "-f", fmt.Sprintf("https://github.com/jetstack/cert-manager/releases/download/%s/cert-manager.yaml", certmanagerVersion)); err != nil {
Expand Down
23 changes: 23 additions & 0 deletions test/e2e/v2/e2e_suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,18 @@ var _ = Describe("kubebuilder", func() {

By("installing cert manager bundle")
Expect(kbc.InstallCertManager()).To(Succeed())

By("installing prometheus operator")
Expect(kbc.InstallPrometheusOperManager()).To(Succeed())
})

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

By("uninstalling prometheus manager bundle")
kbc.UninstallPrometheusOperManager()

By("uninstalling cert manager bundle")
kbc.UninstallCertManager()

Expand Down Expand Up @@ -104,6 +110,9 @@ var _ = Describe("kubebuilder", func() {
Expect(utils.UncommentCode(
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
"#- ../certmanager", "#")).To(Succeed())
Expect(utils.UncommentCode(
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
"#- ../prometheus", "#")).To(Succeed())
Expect(utils.UncommentCode(
filepath.Join(kbc.Dir, "config", "default", "kustomization.yaml"),
"#- manager_webhook_patch.yaml", "#")).To(Succeed())
Expand Down Expand Up @@ -189,6 +198,20 @@ var _ = Describe("kubebuilder", func() {
return err
}, time.Minute, time.Second).Should(Succeed())

By("validate prometheus manager has provisioned the Service")
Eventually(func() error {
_, err := kbc.Kubectl.Get(
false,
"Service", "prometheus-operator")
return err
}, time.Minute, time.Second).Should(Succeed())

By("validate Service Monitor for Prometheus is applied in the namespace")
_, err = kbc.Kubectl.Get(
true,
"ServiceMonitor")
Expect(err).NotTo(HaveOccurred())

By("validate the mutating|validating webhooks have the CA injected")
verifyCAInjection := func() error {
mwhOutput, err := kbc.Kubectl.Get(
Expand Down
2 changes: 2 additions & 0 deletions testdata/project-v2/config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ bases:
#- ../webhook
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
#- ../certmanager
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
#- ../prometheus

patchesStrategicMerge:
# Protect the /metrics endpoint by putting it behind auth.
Expand Down

This file was deleted.

2 changes: 2 additions & 0 deletions testdata/project-v2/config/prometheus/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
resources:
- monitor.yaml
15 changes: 15 additions & 0 deletions testdata/project-v2/config/prometheus/monitor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@

# Prometheus Monitor Service (Metrics)
apiVersion: monitoring.coreos.com/v1
kind: ServiceMonitor
metadata:
labels:
control-plane: controller-manager
name: controller-manager-metrics-monitor
namespace: system
spec:
endpoints:
- path: /metrics
port: https
selector:
control-plane: controller-manager
4 changes: 0 additions & 4 deletions testdata/project-v2/config/rbac/auth_proxy_service.yaml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
apiVersion: v1
kind: Service
metadata:
annotations:
prometheus.io/port: "8443"
prometheus.io/scheme: https
prometheus.io/scrape: "true"
labels:
control-plane: controller-manager
name: controller-manager-metrics-service
Expand Down

0 comments on commit c0a784a

Please sign in to comment.