Skip to content

Commit 63dbb23

Browse files
committed
apiextension: add metric for openapi regeneration
1 parent de020ec commit 63dbb23

File tree

3 files changed

+50
-2
lines changed

3 files changed

+50
-2
lines changed

staging/src/k8s.io/apiextensions-apiserver/pkg/controller/openapi/BUILD

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library")
22

33
go_library(
44
name = "go_default_library",
5-
srcs = ["controller.go"],
5+
srcs = [
6+
"controller.go",
7+
"metrics.go",
8+
],
69
importmap = "k8s.io/kubernetes/vendor/k8s.io/apiextensions-apiserver/pkg/controller/openapi",
710
importpath = "k8s.io/apiextensions-apiserver/pkg/controller/openapi",
811
visibility = ["//visibility:public"],
@@ -17,6 +20,8 @@ go_library(
1720
"//staging/src/k8s.io/apimachinery/pkg/util/wait:go_default_library",
1821
"//staging/src/k8s.io/client-go/tools/cache:go_default_library",
1922
"//staging/src/k8s.io/client-go/util/workqueue:go_default_library",
23+
"//staging/src/k8s.io/component-base/metrics:go_default_library",
24+
"//staging/src/k8s.io/component-base/metrics/legacyregistry:go_default_library",
2025
"//vendor/github.com/go-openapi/spec:go_default_library",
2126
"//vendor/k8s.io/klog:go_default_library",
2227
"//vendor/k8s.io/kube-openapi/pkg/handler:go_default_library",

staging/src/k8s.io/apiextensions-apiserver/pkg/controller/openapi/controller.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,11 +169,12 @@ func (c *Controller) sync(name string) error {
169169
}
170170
delete(c.crdSpecs, name)
171171
klog.V(2).Infof("Updating CRD OpenAPI spec because %s was removed", name)
172+
regenerationCounter.With(map[string]string{"crd": name, "reason": "remove"})
172173
return c.updateSpecLocked()
173174
}
174175

175176
// compute CRD spec and see whether it changed
176-
oldSpecs := c.crdSpecs[crd.Name]
177+
oldSpecs, updated := c.crdSpecs[crd.Name]
177178
newSpecs, changed, err := buildVersionSpecs(crd, oldSpecs)
178179
if err != nil {
179180
return err
@@ -185,6 +186,11 @@ func (c *Controller) sync(name string) error {
185186
// update specs of this CRD
186187
c.crdSpecs[crd.Name] = newSpecs
187188
klog.V(2).Infof("Updating CRD OpenAPI spec because %s changed", name)
189+
reason := "add"
190+
if updated {
191+
reason = "update"
192+
}
193+
regenerationCounter.With(map[string]string{"crd": name, "reason": reason})
188194
return c.updateSpecLocked()
189195
}
190196

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright 2019 The Kubernetes 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 openapi
18+
19+
import (
20+
"k8s.io/component-base/metrics"
21+
"k8s.io/component-base/metrics/legacyregistry"
22+
)
23+
24+
var (
25+
regenerationCounter = metrics.NewCounterVec(
26+
&metrics.CounterOpts{
27+
Name: "apiextensions_openapi_v2_regeneration_count",
28+
Help: "Counter of OpenAPI v2 spec regeneration count broken down by causing CRD name and reason.",
29+
StabilityLevel: metrics.ALPHA,
30+
},
31+
[]string{"crd", "reason"},
32+
)
33+
)
34+
35+
func init() {
36+
legacyregistry.MustRegister(regenerationCounter)
37+
}

0 commit comments

Comments
 (0)