Merge pull request #2114 from dgrisonnet/webhook-services

feat: Add webhooks client config service metrics
This commit is contained in:
Kubernetes Prow Robot 2023-07-26 03:42:18 -07:00 committed by GitHub
commit 61c9de04cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 118 additions and 0 deletions

View File

@ -5,3 +5,4 @@
| kube_mutatingwebhookconfiguration_info | Gauge | `mutatingwebhookconfiguration`=&lt;mutatingwebhookconfiguration-name&gt; <br> `namespace`=&lt;mutatingwebhookconfiguration-namespace&gt; | EXPERIMENTAL | | 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_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_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 |

View File

@ -5,3 +5,4 @@
| kube_validatingwebhookconfiguration_info | Gauge | `validatingwebhookconfiguration`=&lt;validatingwebhookconfiguration-name&gt; <br> `namespace`=&lt;validatingwebhookconfiguration-namespace&gt; | EXPERIMENTAL | | 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_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_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 |

View File

@ -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,
}
}),
),
} }
) )

View File

@ -28,6 +28,7 @@ import (
func TestMutatingWebhookConfigurationStore(t *testing.T) { func TestMutatingWebhookConfigurationStore(t *testing.T) {
startTime := 1501569018 startTime := 1501569018
metav1StartTime := metav1.Unix(int64(startTime), 0) metav1StartTime := metav1.Unix(int64(startTime), 0)
externalURL := "example.com"
cases := []generateMetricsTestCase{ cases := []generateMetricsTestCase{
{ {
@ -69,6 +70,37 @@ func TestMutatingWebhookConfigurationStore(t *testing.T) {
`, `,
MetricNames: []string{"kube_mutatingwebhookconfiguration_created", "kube_mutatingwebhookconfiguration_info", "kube_mutatingwebhookconfiguration_metadata_resource_version"}, 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 { for i, c := range cases {
c.Func = generator.ComposeMetricGenFuncs(mutatingWebhookConfigurationMetricFamilies) c.Func = generator.ComposeMetricGenFuncs(mutatingWebhookConfigurationMetricFamilies)

View File

@ -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,
}
}),
),
} }
) )

View File

@ -28,6 +28,7 @@ import (
func TestValidatingWebhookConfigurationStore(t *testing.T) { func TestValidatingWebhookConfigurationStore(t *testing.T) {
startTime := 1501569018 startTime := 1501569018
metav1StartTime := metav1.Unix(int64(startTime), 0) metav1StartTime := metav1.Unix(int64(startTime), 0)
externalURL := "example.com"
cases := []generateMetricsTestCase{ cases := []generateMetricsTestCase{
{ {
@ -69,6 +70,37 @@ func TestValidatingWebhookConfigurationStore(t *testing.T) {
`, `,
MetricNames: []string{"kube_validatingwebhookconfiguration_created", "kube_validatingwebhookconfiguration_info", "kube_validatingwebhookconfiguration_metadata_resource_version"}, 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 { for i, c := range cases {
c.Func = generator.ComposeMetricGenFuncs(validatingWebhookConfigurationMetricFamilies) c.Func = generator.ComposeMetricGenFuncs(validatingWebhookConfigurationMetricFamilies)