Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add webhooks client config service metrics #2114

Merged
merged 1 commit into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/mutatingwebhookconfiguration-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
| kube_mutatingwebhookconfiguration_info | Gauge | `mutatingwebhookconfiguration`=&lt;mutatingwebhookconfiguration-name&gt; <br> `namespace`=&lt;mutatingwebhookconfiguration-namespace&gt; | EXPERIMENTAL |
| kube_mutatingwebhookconfiguration_created | Gauge | `mutatingwebhookconfiguration`=&lt;mutatingwebhookconfiguration-name&gt; <br> `namespace`=&lt;mutatingwebhookconfiguration-namespace&gt; | EXPERIMENTAL |
| kube_mutatingwebhookconfiguration_metadata_resource_version | Gauge | `mutatingwebhookconfiguration`=&lt;mutatingwebhookconfiguration-name&gt; <br> `namespace`=&lt;mutatingwebhookconfiguration-namespace&gt; | EXPERIMENTAL |
| kube_mutatingwebhookconfiguration_webhook_clientconfig_service | Gauge | `mutatingwebhookconfiguration`=&lt;mutatingwebhookconfiguration-name&gt; <br> `namespace`=&lt;mutatingwebhookconfiguration-namespace&gt; <br> `webhook_name`=&lt;webhook-name&gt; <br> `service_name`=&lt;webhook-service-name&gt; <br> `service_namespace`=&lt;webhook-service-namespace&gt;| EXPERIMENTAL |
1 change: 1 addition & 0 deletions docs/validatingwebhookconfiguration-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@
| kube_validatingwebhookconfiguration_info | Gauge | `validatingwebhookconfiguration`=&lt;validatingwebhookconfiguration-name&gt; <br> `namespace`=&lt;validatingwebhookconfiguration-namespace&gt; | EXPERIMENTAL |
| kube_validatingwebhookconfiguration_created | Gauge | `validatingwebhookconfiguration`=&lt;validatingwebhookconfiguration-name&gt; <br> `namespace`=&lt;validatingwebhookconfiguration-namespace&gt; | EXPERIMENTAL |
| kube_validatingwebhookconfiguration_metadata_resource_version | Gauge | `validatingwebhookconfiguration`=&lt;validatingwebhookconfiguration-name&gt; <br> `namespace`=&lt;validatingwebhookconfiguration-namespace&gt; | EXPERIMENTAL |
| kube_validatingwebhookconfiguration_webhook_clientconfig_service | Gauge | `validatingwebhookconfiguration`=&lt;validatingwebhookconfiguration-name&gt; <br> `namespace`=&lt;validatingwebhookconfiguration-namespace&gt; <br> `webhook_name`=&lt;webhook-name&gt; <br> `service_name`=&lt;webhook-service-name&gt; <br> `service_namespace`=&lt;webhook-service-namespace&gt;| EXPERIMENTAL |
26 changes: 26 additions & 0 deletions internal/store/mutatingwebhookconfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,32 @@ var (
}
}),
),
*generator.NewFamilyGeneratorWithStability(
"kube_mutatingwebhookconfiguration_webhook_clientconfig_service",
"Service used by the apiserver to connect to a mutating webhook.",
metric.Gauge,
basemetrics.ALPHA,
"",
wrapMutatingWebhookConfigurationFunc(func(mwc *admissionregistrationv1.MutatingWebhookConfiguration) *metric.Family {
ms := []*metric.Metric{}
for _, webhook := range mwc.Webhooks {
var serviceName, serviceNamespace string
if webhook.ClientConfig.Service != nil {
serviceName = webhook.ClientConfig.Service.Name
serviceNamespace = webhook.ClientConfig.Service.Namespace
}

ms = append(ms, &metric.Metric{
LabelKeys: []string{"webhook_name", "service_name", "service_namespace"},
LabelValues: []string{webhook.Name, serviceName, serviceNamespace},
Value: 1,
})
}
return &metric.Family{
Metrics: ms,
}
}),
),
}
)

Expand Down
32 changes: 32 additions & 0 deletions internal/store/mutatingwebhookconfiguration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
func TestMutatingWebhookConfigurationStore(t *testing.T) {
startTime := 1501569018
metav1StartTime := metav1.Unix(int64(startTime), 0)
externalURL := "example.com"

cases := []generateMetricsTestCase{
{
Expand Down Expand Up @@ -69,6 +70,37 @@ func TestMutatingWebhookConfigurationStore(t *testing.T) {
`,
MetricNames: []string{"kube_mutatingwebhookconfiguration_created", "kube_mutatingwebhookconfiguration_info", "kube_mutatingwebhookconfiguration_metadata_resource_version"},
},
{
Obj: &admissionregistrationv1.MutatingWebhookConfiguration{
ObjectMeta: metav1.ObjectMeta{
Name: "mutatingwebhookconfiguration3",
Namespace: "ns3",
CreationTimestamp: metav1StartTime,
ResourceVersion: "abcdef",
},
Webhooks: []admissionregistrationv1.MutatingWebhook{
{
Name: "webhook_with_service",
ClientConfig: admissionregistrationv1.WebhookClientConfig{
Service: &admissionregistrationv1.ServiceReference{Name: "svc", Namespace: "ns"},
},
},
{
Name: "webhook_with_external_url",
ClientConfig: admissionregistrationv1.WebhookClientConfig{
URL: &externalURL,
},
},
},
},
Want: `
# HELP kube_mutatingwebhookconfiguration_webhook_clientconfig_service Service used by the apiserver to connect to a mutating webhook.
# TYPE kube_mutatingwebhookconfiguration_webhook_clientconfig_service gauge
kube_mutatingwebhookconfiguration_webhook_clientconfig_service{webhook_name="webhook_with_external_url",namespace="ns3",service_name="",service_namespace="",mutatingwebhookconfiguration="mutatingwebhookconfiguration3"} 1
kube_mutatingwebhookconfiguration_webhook_clientconfig_service{webhook_name="webhook_with_service",namespace="ns3",service_name="svc",service_namespace="ns",mutatingwebhookconfiguration="mutatingwebhookconfiguration3"} 1
`,
MetricNames: []string{"kube_mutatingwebhookconfiguration_webhook_clientconfig_service"},
},
}
for i, c := range cases {
c.Func = generator.ComposeMetricGenFuncs(mutatingWebhookConfigurationMetricFamilies)
Expand Down
26 changes: 26 additions & 0 deletions internal/store/validatingwebhookconfiguration.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,32 @@ var (
}
}),
),
*generator.NewFamilyGeneratorWithStability(
"kube_validatingwebhookconfiguration_webhook_clientconfig_service",
"Service used by the apiserver to connect to a validating webhook.",
metric.Gauge,
basemetrics.ALPHA,
"",
wrapValidatingWebhookConfigurationFunc(func(vwc *admissionregistrationv1.ValidatingWebhookConfiguration) *metric.Family {
ms := []*metric.Metric{}
for _, webhook := range vwc.Webhooks {
var serviceName, serviceNamespace string
if webhook.ClientConfig.Service != nil {
serviceName = webhook.ClientConfig.Service.Name
serviceNamespace = webhook.ClientConfig.Service.Namespace
}

ms = append(ms, &metric.Metric{
LabelKeys: []string{"webhook_name", "service_name", "service_namespace"},
LabelValues: []string{webhook.Name, serviceName, serviceNamespace},
Value: 1,
})
}
return &metric.Family{
Metrics: ms,
}
}),
),
}
)

Expand Down
32 changes: 32 additions & 0 deletions internal/store/validatingwebhookconfiguration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
func TestValidatingWebhookConfigurationStore(t *testing.T) {
startTime := 1501569018
metav1StartTime := metav1.Unix(int64(startTime), 0)
externalURL := "example.com"

cases := []generateMetricsTestCase{
{
Expand Down Expand Up @@ -69,6 +70,37 @@ func TestValidatingWebhookConfigurationStore(t *testing.T) {
`,
MetricNames: []string{"kube_validatingwebhookconfiguration_created", "kube_validatingwebhookconfiguration_info", "kube_validatingwebhookconfiguration_metadata_resource_version"},
},
{
Obj: &admissionregistrationv1.ValidatingWebhookConfiguration{
ObjectMeta: metav1.ObjectMeta{
Name: "validatingwebhookconfiguration3",
Namespace: "ns3",
CreationTimestamp: metav1StartTime,
ResourceVersion: "abcdef",
},
Webhooks: []admissionregistrationv1.ValidatingWebhook{
{
Name: "webhook_with_service",
ClientConfig: admissionregistrationv1.WebhookClientConfig{
Service: &admissionregistrationv1.ServiceReference{Name: "svc", Namespace: "ns"},
},
},
{
Name: "webhook_with_external_url",
ClientConfig: admissionregistrationv1.WebhookClientConfig{
URL: &externalURL,
},
},
},
},
Want: `
# HELP kube_validatingwebhookconfiguration_webhook_clientconfig_service Service used by the apiserver to connect to a validating webhook.
# TYPE kube_validatingwebhookconfiguration_webhook_clientconfig_service gauge
kube_validatingwebhookconfiguration_webhook_clientconfig_service{webhook_name="webhook_with_external_url",namespace="ns3",service_name="",service_namespace="",validatingwebhookconfiguration="validatingwebhookconfiguration3"} 1
kube_validatingwebhookconfiguration_webhook_clientconfig_service{webhook_name="webhook_with_service",namespace="ns3",service_name="svc",service_namespace="ns",validatingwebhookconfiguration="validatingwebhookconfiguration3"} 1
`,
MetricNames: []string{"kube_validatingwebhookconfiguration_webhook_clientconfig_service"},
},
}
for i, c := range cases {
c.Func = generator.ComposeMetricGenFuncs(validatingWebhookConfigurationMetricFamilies)
Expand Down
Loading