Skip to content

Commit 4efc2ed

Browse files
damianot98cheina97
authored andcommitted
Add metrics for virtual-kubelet
Co-authored-by: Francesco Cheinasso <[email protected]>
1 parent 1a9ff8e commit 4efc2ed

File tree

13 files changed

+189
-0
lines changed

13 files changed

+189
-0
lines changed

cmd/virtual-kubelet/root/flag.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ func InstallFlags(flags *pflag.FlagSet, o *Opts) {
7272
flags.BoolVar(&o.EnableStorage, "enable-storage", false, "Enable the Liqo storage reflection")
7373
flags.StringVar(&o.VirtualStorageClassName, "virtual-storage-class-name", "liqo", "Name of the virtual storage class")
7474
flags.StringVar(&o.RemoteRealStorageClassName, "remote-real-storage-class-name", "", "Name of the real storage class to use for the actual volumes")
75+
flags.BoolVar(&o.EnableMetrics, "enable-metrics", false, "Enable the metrics server")
7576
flags.StringVar(&o.HomeAPIServerHost, "home-api-server-host", "",
7677
"Home cluster API server HOST, this parameter is optional and required only to override the default values")
7778
flags.StringVar(&o.HomeAPIServerPort, "home-api-server-port", "",

cmd/virtual-kubelet/root/opts.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ type Opts struct {
9696
EnableStorage bool
9797
VirtualStorageClassName string
9898
RemoteRealStorageClassName string
99+
EnableMetrics bool
99100

100101
HomeAPIServerHost string
101102
HomeAPIServerPort string

cmd/virtual-kubelet/root/root.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import (
3636
"github.com/liqotech/liqo/pkg/utils"
3737
"github.com/liqotech/liqo/pkg/utils/restcfg"
3838
nodeprovider "github.com/liqotech/liqo/pkg/virtualKubelet/liqoNodeProvider"
39+
metrics "github.com/liqotech/liqo/pkg/virtualKubelet/metrics"
3940
podprovider "github.com/liqotech/liqo/pkg/virtualKubelet/provider"
4041
)
4142

@@ -110,6 +111,7 @@ func runRootCommand(ctx context.Context, c *Opts) error {
110111
EnableStorage: c.EnableStorage,
111112
VirtualStorageClassName: c.VirtualStorageClassName,
112113
RemoteRealStorageClassName: c.RemoteRealStorageClassName,
114+
EnableMetrics: c.EnableMetrics,
113115

114116
HomeAPIServerHost: c.HomeAPIServerHost,
115117
HomeAPIServerPort: c.HomeAPIServerPort,
@@ -194,6 +196,10 @@ func runRootCommand(ctx context.Context, c *Opts) error {
194196
return errors.Wrap(err, "error while setting up HTTPS server")
195197
}
196198

199+
if c.EnableMetrics {
200+
metrics.SetupMetricHandler()
201+
}
202+
197203
go func() {
198204
if err := nodeRunner.Run(ctx); err != nil {
199205
klog.Error(err, "error in pod controller running")

deployments/liqo/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,11 @@
134134
| virtualKubelet.extra.labels | object | `{}` | virtual kubelet pod extra labels |
135135
| virtualKubelet.extra.resources | object | `{"limits":{},"requests":{}}` | virtual kubelet pod containers' resource requests and limits (https://kubernetes.io/docs/user-guide/compute-resources/) |
136136
| virtualKubelet.imageName | string | `"ghcr.io/liqotech/virtual-kubelet"` | virtual kubelet image repository |
137+
| virtualKubelet.metrics.enabled | bool | `false` | expose metrics about virtual kubelet resources. |
138+
| virtualKubelet.metrics.podMonitor.enabled | bool | `false` | |
139+
| virtualKubelet.metrics.podMonitor.interval | string | `""` | |
140+
| virtualKubelet.metrics.podMonitor.scrapeTimeout | string | `""` | |
141+
| virtualKubelet.metrics.port | int | `9090` | port used to expose metrics. |
137142
| virtualKubelet.virtualNode.extra.annotations | object | `{}` | virtual node extra annotations |
138143
| virtualKubelet.virtualNode.extra.labels | object | `{}` | virtual node extra labels |
139144
| webhook.failurePolicy | string | `"Fail"` | the webhook failure policy, among Ignore and Fail |

deployments/liqo/templates/liqo-controller-manager-deployment.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
{{- $vkargs = append $vkargs "--certificate-type=aws" }}
2727
{{- end }}
2828
{{- end }}
29+
{{- if not (or (has "--enable-metrics" $vkargs ) (has "--enable-metrics=true" $vkargs ) (has "--enable-metrics=false" $vkargs )) }}
30+
{{- $vkargs = append $vkargs "--enable-metrics=true" }}
31+
{{- end}}
2932

3033
apiVersion: apps/v1
3134
kind: Deployment
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{{- $kubeletMetricsConfig := (merge (dict "name" "virtual-kubelet" "module" "virtual-kubelet") .) -}}
2+
{{- if .Values.virtualKubelet.metrics.podMonitor.enabled }}
3+
4+
apiVersion: monitoring.coreos.com/v1
5+
kind: PodMonitor
6+
metadata:
7+
name: {{ include "liqo.prefixedName" $kubeletMetricsConfig }}
8+
labels:
9+
{{- include "liqo.labels" $kubeletMetricsConfig | nindent 4 }}
10+
spec:
11+
namespaceSelector:
12+
any: true
13+
selector:
14+
matchLabels:
15+
app.kubernetes.io/name: "virtual-kubelet"
16+
app.kubernetes.io/component: "virtual-kubelet"
17+
podMetricsEndpoints:
18+
- port: metrics
19+
interval: {{ .Values.virtualKubelet.metrics.podMonitor.interval }}
20+
scrapeTimeout: {{ .Values.virtualKubelet.metrics.podMonitor.scrapeTimeout }}
21+
{{- end }}
22+

deployments/liqo/values.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,20 @@ virtualKubelet:
315315
annotations: {}
316316
# -- virtual node extra labels
317317
labels: {}
318+
metrics:
319+
# -- expose metrics about virtual kubelet resources.
320+
enabled: false
321+
# -- port used to expose metrics.
322+
port: 9090
323+
podMonitor:
324+
# # -- create a prometheus podmonitor.
325+
enabled: false
326+
# # -- setup pod monitor requests interval. If empty, Prometheus uses the global scrape interval.
327+
# # ref: https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#endpoint
328+
interval: ""
329+
# # -- setup pod monitor scrape timeout. If empty, Prometheus uses the global scrape timeout.
330+
# # ref: https://github.com/prometheus-operator/prometheus-operator/blob/main/Documentation/api.md#endpoint
331+
scrapeTimeout: ""
318332

319333
uninstaller:
320334
pod:

pkg/liqoctl/install/handler.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,15 @@ func (o *Options) values() map[string]interface{} {
335335
},
336336
},
337337

338+
"virtualKubelet": map[string]interface{}{
339+
"metrics": map[string]interface{}{
340+
"enabled": o.EnableMetrics,
341+
"podMonitor": map[string]interface{}{
342+
"enabled": o.EnableMetrics,
343+
},
344+
},
345+
},
346+
338347
"telemetry": map[string]interface{}{
339348
"enable": !o.DisableTelemetry,
340349
},

pkg/virtualKubelet/metrics/doc.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright 2019-2023 The Liqo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
// Package metrics provides a set of metrics for Virtual Kubelet component.
16+
package metrics
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2019-2023 The Liqo Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package metrics
16+
17+
import (
18+
"net/http"
19+
"os"
20+
"time"
21+
22+
"github.com/prometheus/client_golang/prometheus"
23+
"github.com/prometheus/client_golang/prometheus/promhttp"
24+
"k8s.io/klog/v2"
25+
)
26+
27+
const (
28+
// MetricsPort is the metrics port constant.
29+
MetricsPort = ":9090"
30+
)
31+
32+
var (
33+
// ErrorsCounter is the counter of the errors occurred during the reflection.
34+
ErrorsCounter *prometheus.CounterVec
35+
// ItemsCounter is the counter of the reflected resources.
36+
// A fast increase of this metric can indicate a race condition between local and remote operators.
37+
ItemsCounter *prometheus.CounterVec
38+
)
39+
40+
// Init initializes the metrics. If no error occurs or no item is processed, the corresponding metric is not exported.
41+
func init() {
42+
var MetricsLabels = []string{"namespace", "reflector_resource"}
43+
44+
ErrorsCounter = prometheus.NewCounterVec(
45+
prometheus.CounterOpts{
46+
Name: "liqo_virtual_kubelet_reflection_error_counter",
47+
Help: "The counter of the transient errors.",
48+
},
49+
MetricsLabels,
50+
)
51+
52+
ItemsCounter = prometheus.NewCounterVec(
53+
prometheus.CounterOpts{
54+
Name: "liqo_virtual_kubelet_reflection_item_counter",
55+
Help: "The counter of the reflected resources. A fast increase of this metric can indicate a race condition between local and remote operators.",
56+
},
57+
MetricsLabels,
58+
)
59+
}
60+
61+
// SetupMetricHandler sets up the metric handler.
62+
func SetupMetricHandler() {
63+
// Register the metrics to the prometheus registry.
64+
prometheus.MustRegister(ErrorsCounter)
65+
// Register the metrics to the prometheus registry.
66+
prometheus.MustRegister(ItemsCounter)
67+
68+
http.Handle("/metrics", promhttp.Handler())
69+
70+
go func() {
71+
klog.Infof("Starting the virtual kubelet Metric Handler listening on %q", MetricsPort)
72+
73+
server := &http.Server{
74+
Addr: ":1234",
75+
ReadHeaderTimeout: 10 * time.Second,
76+
}
77+
78+
// Key and certificate paths are not specified, since already configured as part of the TLSConfig.
79+
if err := server.ListenAndServe(); err != nil {
80+
klog.Errorf("Failed to start the Metric Handler: %v", err)
81+
os.Exit(1)
82+
}
83+
}()
84+
}

0 commit comments

Comments
 (0)