Skip to content

Commit

Permalink
Create a single entrypoint for the prometheus-operator
Browse files Browse the repository at this point in the history
Make sure that all kubevirt components which provide logs are selected
by the "kubevirt-prometheus-metrics" endpoint. The openshift-operator
can look up the registered endpoints behind it and create a scrape
config.
  • Loading branch information
rmohr committed Sep 24, 2018
1 parent 8aceaaa commit 4ae085e
Show file tree
Hide file tree
Showing 9 changed files with 141 additions and 44 deletions.
10 changes: 8 additions & 2 deletions cmd/virt-handler/virt-handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"time"

"github.com/golang/glog"
"github.com/prometheus/client_golang/prometheus/promhttp"
flag "github.com/spf13/pflag"
k8sv1 "k8s.io/api/core/v1"
Expand All @@ -35,6 +36,8 @@ import (
"k8s.io/client-go/tools/cache"
"k8s.io/client-go/tools/record"

"kubevirt.io/kubevirt/pkg/certificates"

"kubevirt.io/kubevirt/pkg/api/v1"
"kubevirt.io/kubevirt/pkg/controller"
inotifyinformer "kubevirt.io/kubevirt/pkg/inotify-informer"
Expand Down Expand Up @@ -148,11 +151,14 @@ func (app *virtHandlerApp) Run() {
// Bootstrapping. From here on the startup order matters
stop := make(chan struct{})
defer close(stop)

certStore, err := certificates.GenerateSelfSignedCert("virt-handler", certificates.GetNamespace())
if err != nil {
glog.Fatalf("unable to generate certificates: %v", err)
}
go vmController.Run(3, stop)

http.Handle("/metrics", promhttp.Handler())
err = http.ListenAndServe(app.ServiceListen.Address(), nil)
err = http.ListenAndServeTLS(app.ServiceListen.Address(), certStore.CurrentPath(), certStore.CurrentPath(), nil)
if err != nil {
log.Log.Reason(err).Error("Serving prometheus failed.")
panic(err)
Expand Down
16 changes: 16 additions & 0 deletions manifests/dev/prometheus.yaml.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: v1
kind: Service
metadata:
name: kubevirt-prometheus-metrics
namespace: {{.Namespace}}
labels:
prometheus.kubevirt.io: ""
kubevirt.io: ""
spec:
ports:
- name: metrics
port: 443
targetPort: metrics
protocol: TCP
selector:
prometheus.kubevirt.io: ""
4 changes: 4 additions & 0 deletions manifests/dev/virt-api.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ spec:
]
labels:
kubevirt.io: virt-api
prometheus.kubevirt.io: ""
spec:
serviceAccountName: kubevirt-apiserver
containers:
Expand All @@ -50,6 +51,9 @@ spec:
- containerPort: 8443
name: "virt-api"
protocol: "TCP"
- containerPort: 8443
name: "metrics"
protocol: "TCP"
readinessProbe:
tcpSocket:
port: 8443
Expand Down
28 changes: 10 additions & 18 deletions manifests/dev/virt-controller.yaml.in
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
apiVersion: v1
kind: Service
metadata:
name: virt-controller
namespace: {{.Namespace}}
labels:
kubevirt.io: "virt-controller"
spec:
ports:
- port: 8182
targetPort: virt-controller
selector:
kubevirt.io: virt-controller
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
Expand All @@ -34,6 +20,7 @@ spec:
]
labels:
kubevirt.io: virt-controller
prometheus.kubevirt.io: ""
spec:
serviceAccountName: kubevirt-controller
containers:
Expand All @@ -45,21 +32,26 @@ spec:
- "--launcher-image"
- "{{.DockerPrefix}}/virt-launcher:{{.DockerTag}}"
- "--port"
- "8182"
- "8443"
ports:
- containerPort: 8182
- containerPort: 8443
name: "virt-controller"
protocol: "TCP"
- containerPort: 8443
name: "metrics"
protocol: "TCP"
livenessProbe:
failureThreshold: 8
httpGet:
port: 8182
scheme: HTTPS
port: 8443
path: /healthz
initialDelaySeconds: 15
timeoutSeconds: 10
readinessProbe:
httpGet:
port: 8182
scheme: HTTPS
port: 8443
path: /leader
initialDelaySeconds: 15
timeoutSeconds: 10
Expand Down
8 changes: 6 additions & 2 deletions manifests/dev/virt-handler.yaml.in
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,24 @@ spec:
name: virt-handler
labels:
kubevirt.io: virt-handler
prometheus.kubevirt.io: ""
spec:
serviceAccountName: kubevirt-privileged
hostPID: true
containers:
- name: virt-handler
ports:
- containerPort: 8185
hostPort: 8185
- containerPort: 8443
name: "metrics"
protocol: "TCP"
image: {{.DockerPrefix}}/virt-handler:{{.DockerTag}}
imagePullPolicy: {{.ImagePullPolicy}}
command:
- "virt-handler"
- "-v"
- "3"
- "--port"
- "8443"
- "--hostname-override"
- "$(NODE_NAME)"
securityContext:
Expand Down
43 changes: 36 additions & 7 deletions manifests/release/kubevirt.yaml.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
# Monitoring
apiVersion: v1
kind: Service
metadata:
name: kubevirt-prometheus-metrics
namespace: {{.Namespace}}
labels:
prometheus.kubevirt.io: ""
kubevirt.io: ""
spec:
ports:
- name: metrics
port: 443
targetPort: metrics
protocol: TCP
selector:
prometheus.kubevirt.io: ""
---
# RBAC
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
Expand Down Expand Up @@ -409,6 +427,7 @@ spec:
]
labels:
kubevirt.io: virt-api
prometheus.kubevirt.io: ""
spec:
serviceAccountName: kubevirt-apiserver
containers:
Expand All @@ -424,6 +443,9 @@ spec:
- containerPort: 8443
name: "virt-api"
protocol: "TCP"
- containerPort: 8443
name: "metrics"
protocol: "TCP"
readinessProbe:
tcpSocket:
port: 8443
Expand Down Expand Up @@ -455,6 +477,7 @@ spec:
]
labels:
kubevirt.io: virt-controller
prometheus.kubevirt.io: ""
spec:
serviceAccountName: kubevirt-controller
containers:
Expand All @@ -466,21 +489,23 @@ spec:
- "--launcher-image"
- "{{.DockerPrefix}}/virt-launcher:{{.DockerTag}}"
- "--port"
- "8182"
- "8443"
ports:
- containerPort: 8182
name: "virt-controller"
- containerPort: 8443
name: "metrics"
protocol: "TCP"
livenessProbe:
failureThreshold: 8
httpGet:
port: 8182
scheme: HTTPS
port: 8443
path: /healthz
initialDelaySeconds: 15
timeoutSeconds: 10
readinessProbe:
httpGet:
port: 8182
scheme: HTTPS
port: 8443
path: /leader
initialDelaySeconds: 15
timeoutSeconds: 10
Expand Down Expand Up @@ -512,20 +537,24 @@ spec:
]
labels:
kubevirt.io: virt-handler
prometheus.kubevirt.io: ""
spec:
serviceAccountName: kubevirt-privileged
hostPID: true
containers:
- name: virt-handler
ports:
- containerPort: 8185
hostPort: 8185
- containerPort: 8443
name: "metrics"
protocol: "TCP"
image: {{.DockerPrefix}}/virt-handler:{{.DockerTag}}
imagePullPolicy: {{.ImagePullPolicy}}
command:
- "virt-handler"
- "-v"
- "3"
- "--port"
- "8443"
- "--hostname-override"
- "$(NODE_NAME)"
securityContext:
Expand Down
47 changes: 47 additions & 0 deletions pkg/certificates/certificates.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package certificates

import (
"io/ioutil"
"strings"

"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/util/cert"
"k8s.io/client-go/util/cert/triple"
"k8s.io/client-go/util/certificate"
)

func GenerateSelfSignedCert(name string, namespace string) (certificate.FileStore, error) {
caKeyPair, _ := triple.NewCA("kubevirt.io")
keyPair, _ := triple.NewServerKeyPair(
caKeyPair,
name+"."+namespace+".pod.cluster.local",
name,
namespace,
"cluster.local",
nil,
nil,
)

certsDirectory, err := ioutil.TempDir("", "certsdir")
if err != nil {
return nil, err
}
store, err := certificate.NewFileStore(name, certsDirectory, certsDirectory, "", "")
if err != nil {
return nil, err
}
_, err = store.Update(cert.EncodeCertPEM(keyPair.Cert), cert.EncodePrivateKeyPEM(keyPair.Key))
if err != nil {
return nil, err
}
return store, nil
}

func GetNamespace() string {
if data, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"); err == nil {
if ns := strings.TrimSpace(string(data)); len(ns) > 0 {
return ns
}
}
return v1.NamespaceSystem
}
20 changes: 6 additions & 14 deletions pkg/virt-api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (
"net/http"
"os"
"path/filepath"
"strings"

"github.com/emicklei/go-restful"
"github.com/emicklei/go-restful-openapi"
Expand All @@ -45,6 +44,8 @@ import (
apiregistrationv1beta1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1beta1"
aggregatorclient "k8s.io/kube-aggregator/pkg/client/clientset_generated/clientset"

"kubevirt.io/kubevirt/pkg/certificates"

"kubevirt.io/kubevirt/pkg/api/v1"
"kubevirt.io/kubevirt/pkg/feature-gates"
"kubevirt.io/kubevirt/pkg/healthz"
Expand Down Expand Up @@ -440,19 +441,10 @@ func (app *virtAPIApp) getClientCert() error {
return nil
}

func getNamespace() string {
if data, err := ioutil.ReadFile("/var/run/secrets/kubernetes.io/serviceaccount/namespace"); err == nil {
if ns := strings.TrimSpace(string(data)); len(ns) > 0 {
return ns
}
}
return metav1.NamespaceSystem
}

func (app *virtAPIApp) getSelfSignedCert() error {
var ok bool

namespace := getNamespace()
namespace := certificates.GetNamespace()
generateCerts := false
secret, err := app.virtCli.CoreV1().Secrets(namespace).Get(virtApiCertSecretName, metav1.GetOptions{})
if err != nil {
Expand Down Expand Up @@ -531,7 +523,7 @@ func (app *virtAPIApp) createWebhook() error {
}

func (app *virtAPIApp) createValidatingWebhook() error {
namespace := getNamespace()
namespace := certificates.GetNamespace()
registerWebhook := false
vmiPathCreate := vmiCreateValidatePath
vmiPathUpdate := vmiUpdateValidatePath
Expand Down Expand Up @@ -709,7 +701,7 @@ func (app *virtAPIApp) createValidatingWebhook() error {
}

func (app *virtAPIApp) createMutatingWebhook() error {
namespace := getNamespace()
namespace := certificates.GetNamespace()
registerWebhook := false
vmiPath := vmiMutatePath

Expand Down Expand Up @@ -783,7 +775,7 @@ func (app *virtAPIApp) createMutatingWebhook() error {
}

func (app *virtAPIApp) createSubresourceApiservice() error {
namespace := getNamespace()
namespace := certificates.GetNamespace()
config, err := kubecli.GetConfig()
if err != nil {
return err
Expand Down
Loading

0 comments on commit 4ae085e

Please sign in to comment.