Skip to content

Commit 2b084f7

Browse files
committed
feat(metrics): add registry_monitor_registries metric
1 parent 531df0f commit 2b084f7

File tree

6 files changed

+62
-1
lines changed

6 files changed

+62
-1
lines changed

api/kuik/v1alpha1/registrymonitor_types.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package v1alpha1
22

33
import (
4+
"strings"
5+
46
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
57
)
68

@@ -34,6 +36,10 @@ type RegistryMonitorStatus struct {
3436
// RegistryStatus is the status of the registry being monitored
3537
// +kubebuilder:validation:Enum=Up;Down
3638
RegistryStatus RegistryStatus `json:"registryStatus"`
39+
// LastMonitor is the last time the registry health was checked
40+
LastMonitor metav1.Time `json:"lastMonitor,omitempty"`
41+
// LastError is the last error encountered while trying to health check the registry
42+
LastError string `json:"lastError,omitempty"`
3743
}
3844

3945
// +kubebuilder:object:root=true
@@ -66,3 +72,11 @@ type RegistryMonitorList struct {
6672
func init() {
6773
SchemeBuilder.Register(&RegistryMonitor{}, &RegistryMonitorList{})
6874
}
75+
76+
func (r RegistryStatus) ToString() string {
77+
value := string(r)
78+
if value == "" {
79+
value = "unknown"
80+
}
81+
return strings.ToLower(value)
82+
}

api/kuik/v1alpha1/zz_generated.deepcopy.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/crd/bases/kuik.enix.io_registrymonitors.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ spec:
8787
status:
8888
description: RegistryMonitorStatus defines the observed state of RegistryMonitor.
8989
properties:
90+
lastError:
91+
description: LastError is the last error encountered while trying
92+
to health check the registry
93+
type: string
94+
lastMonitor:
95+
description: LastMonitor is the last time the registry health was
96+
checked
97+
format: date-time
98+
type: string
9099
registryStatus:
91100
description: RegistryStatus is the status of the registry being monitored
92101
enum:

helm/kube-image-keeper/crds/kuik.enix.io_registrymonitors.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,15 @@ spec:
8686
status:
8787
description: RegistryMonitorStatus defines the observed state of RegistryMonitor.
8888
properties:
89+
lastError:
90+
description: LastError is the last error encountered while trying
91+
to health check the registry
92+
type: string
93+
lastMonitor:
94+
description: LastMonitor is the last time the registry health was
95+
checked
96+
format: date-time
97+
type: string
8998
registryStatus:
9099
description: RegistryStatus is the status of the registry being monitored
91100
enum:

internal/controller/collector.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,31 @@ func (m *kuikMetrics) Register(elected <-chan struct{}, client client.Client) {
101101
}
102102
}))
103103

104+
m.addCollector(NewGenericCollector(
105+
prometheus.Opts{
106+
Namespace: info.MetricsNamespace,
107+
Subsystem: subsystemRegistryMonitor,
108+
Name: "registries",
109+
Help: "Number of registries monitored up and running, labeled by registry.",
110+
},
111+
prometheus.GaugeValue,
112+
[]string{"registry"},
113+
func(collect func(value float64, labels ...string)) {
114+
registryMonitorList := &kuikv1alpha1.RegistryMonitorList{}
115+
if err := client.List(context.Background(), registryMonitorList); err != nil {
116+
logf.Log.Error(err, "failed to list registry monitors for metrics")
117+
return
118+
}
119+
120+
for _, registry := range registryMonitorList.Items {
121+
if registry.Status.RegistryStatus == kuikv1alpha1.RegistryStatusUp {
122+
collect(1, registry.Name)
123+
} else {
124+
collect(0, registry.Name)
125+
}
126+
}
127+
}))
128+
104129
metrics.Registry.MustRegister(m.collectors...)
105130
}
106131

internal/controller/kuik/registrymonitor_controller.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,13 @@ func (r *RegistryMonitorReconciler) Reconcile(ctx context.Context, req ctrl.Requ
8989
log.Info("found images matching registry", "count", len(images.Items), "monitoredDuringInterval", monitoredDuringInterval)
9090

9191
patch := client.MergeFrom(registryMonitor.DeepCopy())
92+
registryMonitor.Status.LastMonitor = metav1.Now()
9293
registryMonitor.Status.RegistryStatus = kuikv1alpha1.RegistryStatusUp
94+
registryMonitor.Status.LastError = ""
9395
err := registry.HealthCheck(registryMonitor.Spec.Registry, nil, nil)
9496
if err != nil {
9597
registryMonitor.Status.RegistryStatus = kuikv1alpha1.RegistryStatusDown
98+
registryMonitor.Status.LastError = err.Error()
9699
}
97100

98101
if errStatus := r.Status().Patch(ctx, &registryMonitor, patch); errStatus != nil {

0 commit comments

Comments
 (0)