Skip to content

Commit cb06308

Browse files
authored
feat(metrics): add machine deployment metrics (#1879)
* feat(metrics): add machine deployment metrics Signed-off-by: rajaSahil <[email protected]> * feat(metrics): fix lint Signed-off-by: rajaSahil <[email protected]> --------- Signed-off-by: rajaSahil <[email protected]>
1 parent d6ee7f4 commit cb06308

File tree

3 files changed

+115
-7
lines changed

3 files changed

+115
-7
lines changed

cmd/machine-controller/main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,9 @@ func (bs *controllerBootstrap) Start(ctx context.Context) error {
393393
machineCollector := machinecontroller.NewMachineCollector(ctx, bs.mgr.GetClient())
394394
metrics.Registry.MustRegister(machineCollector)
395395

396+
machineDeploymentCollector := machinedeploymentcontroller.NewCollector(ctx, bs.mgr.GetClient())
397+
metrics.Registry.MustRegister(machineDeploymentCollector)
398+
396399
if err := machinecontroller.Add(
397400
ctx,
398401
bs.opt.log,

pkg/cloudprovider/provider/anexia/types/types.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ package types
1919
import (
2020
"time"
2121

22-
"k8c.io/machine-controller/pkg/apis/cluster/common"
23-
cloudprovidererrors "k8c.io/machine-controller/pkg/cloudprovider/errors"
2422
"k8c.io/machine-controller/pkg/jsonutil"
2523
providerconfigtypes "k8c.io/machine-controller/pkg/providerconfig/types"
2624

@@ -42,11 +40,6 @@ const (
4240
MachinePoweredOn = "poweredOn"
4341
)
4442

45-
var StatusUpdateFailed = cloudprovidererrors.TerminalError{
46-
Reason: common.UpdateMachineError,
47-
Message: "Failed to update the machine status",
48-
}
49-
5043
// RawDisk specifies a single disk, with some values maybe being fetched from secrets.
5144
type RawDisk struct {
5245
Size int `json:"size"`
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
Copyright 2025 The Machine Controller 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 machinedeployment
18+
19+
import (
20+
"context"
21+
22+
"github.com/prometheus/client_golang/prometheus"
23+
"k8c.io/machine-controller/pkg/apis/cluster/v1alpha1"
24+
ctrlruntimeclient "sigs.k8s.io/controller-runtime/pkg/client"
25+
)
26+
27+
const metricsPrefix = "machine_deployment_"
28+
29+
type Collector struct {
30+
ctx context.Context
31+
client ctrlruntimeclient.Client
32+
33+
replicas *prometheus.Desc
34+
availableReplicas *prometheus.Desc
35+
readyReplicas *prometheus.Desc
36+
updatedReplicas *prometheus.Desc
37+
}
38+
39+
// NewCollector creates new machine deployment collector for metrics collection.
40+
func NewCollector(ctx context.Context, client ctrlruntimeclient.Client) *Collector {
41+
return &Collector{
42+
ctx: ctx,
43+
client: client,
44+
replicas: prometheus.NewDesc(
45+
metricsPrefix+"replicas",
46+
"The number of replicas defined for a machine deployment",
47+
[]string{"name", "namespace"}, nil,
48+
),
49+
availableReplicas: prometheus.NewDesc(
50+
metricsPrefix+"available_replicas",
51+
"The number of available replicas for a machine deployment",
52+
[]string{"name", "namespace"}, nil,
53+
),
54+
readyReplicas: prometheus.NewDesc(
55+
metricsPrefix+"ready_replicas",
56+
"The number of ready replicas for a machine deployment",
57+
[]string{"name", "namespace"}, nil,
58+
),
59+
updatedReplicas: prometheus.NewDesc(
60+
metricsPrefix+"updated_replicas",
61+
"The number of replicas updated for a machine deployment",
62+
[]string{"name", "namespace"}, nil,
63+
),
64+
}
65+
}
66+
67+
// Describe implements the prometheus.Describe interface.
68+
func (c *Collector) Describe(desc chan<- *prometheus.Desc) {
69+
desc <- c.replicas
70+
desc <- c.readyReplicas
71+
desc <- c.availableReplicas
72+
desc <- c.readyReplicas
73+
}
74+
75+
// Collect implements the prometheus.Collector interface.
76+
func (c *Collector) Collect(metrics chan<- prometheus.Metric) {
77+
machineDeployments := &v1alpha1.MachineDeploymentList{}
78+
if err := c.client.List(c.ctx, machineDeployments); err != nil {
79+
return
80+
}
81+
82+
for _, machineDeployment := range machineDeployments.Items {
83+
metrics <- prometheus.MustNewConstMetric(
84+
c.replicas,
85+
prometheus.GaugeValue,
86+
float64(machineDeployment.Status.Replicas),
87+
machineDeployment.Name,
88+
machineDeployment.Namespace,
89+
)
90+
metrics <- prometheus.MustNewConstMetric(
91+
c.readyReplicas,
92+
prometheus.GaugeValue,
93+
float64(machineDeployment.Status.ReadyReplicas),
94+
machineDeployment.Name,
95+
machineDeployment.Namespace,
96+
)
97+
metrics <- prometheus.MustNewConstMetric(
98+
c.availableReplicas,
99+
prometheus.GaugeValue,
100+
float64(machineDeployment.Status.AvailableReplicas),
101+
machineDeployment.Name,
102+
machineDeployment.Namespace,
103+
)
104+
metrics <- prometheus.MustNewConstMetric(
105+
c.updatedReplicas,
106+
prometheus.GaugeValue,
107+
float64(machineDeployment.Status.UpdatedReplicas),
108+
machineDeployment.Name,
109+
machineDeployment.Namespace,
110+
)
111+
}
112+
}

0 commit comments

Comments
 (0)