Skip to content

Commit 21bff7a

Browse files
committed
Add new metric to expose GKE component version
1 parent f315629 commit 21bff7a

File tree

149 files changed

+30685
-42
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

149 files changed

+30685
-42
lines changed

cmd/gce-pd-csi-driver/main.go

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
gce "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/compute"
2929
metadataservice "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-cloud-provider/metadata"
3030
driver "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/gce-pd-csi-driver"
31+
"sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/metrics"
3132
mountmanager "sigs.k8s.io/gcp-compute-persistent-disk-csi-driver/pkg/mount-manager"
3233
)
3334

@@ -36,6 +37,8 @@ var (
3637
endpoint = flag.String("endpoint", "unix:/tmp/csi.sock", "CSI endpoint")
3738
runControllerService = flag.Bool("run-controller-service", true, "If set to false then the CSI driver does not activate its controller service (default: true)")
3839
runNodeService = flag.Bool("run-node-service", true, "If set to false then the CSI driver does not activate its node service (default: true)")
40+
httpEndpoint = flag.String("http-endpoint", "", "The TCP network address where the prometheus metrics endpoint will listen (example: `:8080`). The default is empty string, which means metrics endpoint is disabled.")
41+
metricsPath = flag.String("metrics-path", "/metrics", "The HTTP path where prometheus metrics will be exposed. Default is `/metrics`.")
3942
extraVolumeLabels map[string]string
4043
version string
4144
)
@@ -70,6 +73,12 @@ func handle() {
7073
}
7174
klog.V(2).Infof("Driver vendor version %v", version)
7275

76+
if *runControllerService && *httpEndpoint != "" && metrics.IsGKEComponentVersionAvailable() {
77+
mm := metrics.NewMetricsManager()
78+
mm.InitializeHttpHandler(*httpEndpoint, *metricsPath)
79+
mm.EmitGKEComponentVersion()
80+
}
81+
7382
gceDriver := driver.GetGCEDriver()
7483

7584
//Initialize GCE Driver

go.sum

+7-42
Large diffs are not rendered by default.

pkg/metrics/metrics.go

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/*
2+
Copyright 2020 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 metrics
18+
19+
import (
20+
"fmt"
21+
"net/http"
22+
"os"
23+
24+
"k8s.io/component-base/metrics"
25+
"k8s.io/klog"
26+
)
27+
28+
const (
29+
// envGKEPDCSIVersion is an environment variable set in the PDCSI controller manifest
30+
// with the current version of the GKE component.
31+
envGKEPDCSIVersion = "GKE_PDCSI_VERSION"
32+
)
33+
34+
var (
35+
// This metric is exposed only from the controller driver component when GKE_PDCSI_VERSION env variable is set.
36+
gkeComponentVersion = metrics.NewGaugeVec(&metrics.GaugeOpts{
37+
Name: "component_version",
38+
Help: "Metric to expose the version of the PDCSI GKE component.",
39+
}, []string{"component_version"})
40+
)
41+
42+
type metricsManager struct {
43+
registry metrics.KubeRegistry
44+
}
45+
46+
func NewMetricsManager() metricsManager {
47+
mm := metricsManager{
48+
registry: metrics.NewKubeRegistry(),
49+
}
50+
return mm
51+
}
52+
53+
func (mm *metricsManager) GetRegistry() metrics.KubeRegistry {
54+
return mm.registry
55+
}
56+
57+
func (mm *metricsManager) registerComponentVersionMetric() {
58+
mm.registry.MustRegister(gkeComponentVersion)
59+
}
60+
61+
func (mm *metricsManager) recordComponentVersionMetric() error {
62+
v := getEnvVar(envGKEPDCSIVersion)
63+
if v == "" {
64+
klog.V(2).Info("Skip emitting component version metric")
65+
return fmt.Errorf("Failed to register GKE component version metric, env variable %v not defined", envGKEPDCSIVersion)
66+
}
67+
68+
gkeComponentVersion.WithLabelValues(v).Set(1.0)
69+
return nil
70+
}
71+
72+
func (mm *metricsManager) EmitGKEComponentVersion() error {
73+
mm.registerComponentVersionMetric()
74+
if err := mm.recordComponentVersionMetric(); err != nil {
75+
return err
76+
}
77+
78+
return nil
79+
}
80+
81+
// Server represents any type that could serve HTTP requests for the metrics
82+
// endpoint.
83+
type Server interface {
84+
Handle(pattern string, handler http.Handler)
85+
}
86+
87+
// RegisterToServer registers an HTTP handler for this metrics manager to the
88+
// given server at the specified address/path.
89+
func (mm *metricsManager) registerToServer(s Server, metricsPath string) {
90+
s.Handle(metricsPath, metrics.HandlerFor(
91+
mm.GetRegistry(),
92+
metrics.HandlerOpts{
93+
ErrorHandling: metrics.ContinueOnError}))
94+
}
95+
96+
// InitializeHttpHandler sets up a server and creates a handler for metrics.
97+
func (mm *metricsManager) InitializeHttpHandler(address, path string) {
98+
mux := http.NewServeMux()
99+
mm.registerToServer(mux, path)
100+
go func() {
101+
klog.Infof("Metric server listening at %q", address)
102+
if err := http.ListenAndServe(address, mux); err != nil {
103+
klog.Fatalf("Failed to start metric server at specified address (%q) and path (%q): %s", address, path, err)
104+
}
105+
}()
106+
}
107+
108+
func getEnvVar(envVarName string) string {
109+
v, ok := os.LookupEnv(envVarName)
110+
if !ok {
111+
klog.Warningf("%q env not set", envVarName)
112+
return ""
113+
}
114+
115+
return v
116+
}
117+
118+
func IsGKEComponentVersionAvailable() bool {
119+
if getEnvVar(envGKEPDCSIVersion) == "" {
120+
return false
121+
}
122+
123+
return true
124+
}

vendor/github.com/beorn7/perks/LICENSE

+20
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)