Skip to content

Commit bfd8610

Browse files
authored
Merge pull request kubernetes#81786 from sttts/sttts-openapi-log-why
aggregator/apiextensions: logs & metrics why OpenAPI spec is regenerated
2 parents 419dccb + beee72e commit bfd8610

File tree

7 files changed

+136
-3
lines changed

7 files changed

+136
-3
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: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,11 +168,13 @@ func (c *Controller) sync(name string) error {
168168
return nil
169169
}
170170
delete(c.crdSpecs, name)
171+
klog.V(2).Infof("Updating CRD OpenAPI spec because %s was removed", name)
172+
regenerationCounter.With(map[string]string{"crd": name, "reason": "remove"})
171173
return c.updateSpecLocked()
172174
}
173175

174176
// compute CRD spec and see whether it changed
175-
oldSpecs := c.crdSpecs[crd.Name]
177+
oldSpecs, updated := c.crdSpecs[crd.Name]
176178
newSpecs, changed, err := buildVersionSpecs(crd, oldSpecs)
177179
if err != nil {
178180
return err
@@ -183,6 +185,12 @@ func (c *Controller) sync(name string) error {
183185

184186
// update specs of this CRD
185187
c.crdSpecs[crd.Name] = newSpecs
188+
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})
186194
return c.updateSpecLocked()
187195
}
188196

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+
}

staging/src/k8s.io/kube-aggregator/pkg/controllers/openapi/aggregator/BUILD

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ go_library(
55
srcs = [
66
"aggregator.go",
77
"downloader.go",
8+
"metrics.go",
89
"priority.go",
910
],
1011
importmap = "k8s.io/kubernetes/vendor/k8s.io/kube-aggregator/pkg/controllers/openapi/aggregator",
@@ -14,9 +15,12 @@ go_library(
1415
"//staging/src/k8s.io/apiserver/pkg/authentication/user:go_default_library",
1516
"//staging/src/k8s.io/apiserver/pkg/endpoints/request:go_default_library",
1617
"//staging/src/k8s.io/apiserver/pkg/server:go_default_library",
18+
"//staging/src/k8s.io/component-base/metrics:go_default_library",
19+
"//staging/src/k8s.io/component-base/metrics/legacyregistry:go_default_library",
1720
"//staging/src/k8s.io/kube-aggregator/pkg/apis/apiregistration/v1:go_default_library",
1821
"//vendor/github.com/emicklei/go-restful:go_default_library",
1922
"//vendor/github.com/go-openapi/spec:go_default_library",
23+
"//vendor/k8s.io/klog:go_default_library",
2024
"//vendor/k8s.io/kube-openapi/pkg/aggregator:go_default_library",
2125
"//vendor/k8s.io/kube-openapi/pkg/builder:go_default_library",
2226
"//vendor/k8s.io/kube-openapi/pkg/common:go_default_library",

staging/src/k8s.io/kube-aggregator/pkg/controllers/openapi/aggregator/aggregator.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@ import (
2626
restful "github.com/emicklei/go-restful"
2727
"github.com/go-openapi/spec"
2828

29+
"k8s.io/klog"
30+
2931
"k8s.io/apiserver/pkg/server"
30-
"k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
32+
v1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
3133
"k8s.io/kube-openapi/pkg/aggregator"
3234
"k8s.io/kube-openapi/pkg/builder"
3335
"k8s.io/kube-openapi/pkg/common"
@@ -104,6 +106,14 @@ func BuildAndRegisterAggregator(downloader *Downloader, delegationTarget server.
104106
}
105107

106108
// Build initial spec to serve.
109+
klog.V(2).Infof("Building initial OpenAPI spec")
110+
defer func(start time.Time) {
111+
duration := time.Now().Sub(start)
112+
klog.V(2).Infof("Finished initial OpenAPI spec generation after %v", duration)
113+
114+
regenerationCounter.With(map[string]string{"apiservice": "*", "reason": "startup"})
115+
regenerationDurationGauge.With(map[string]string{"reason": "startup"}).Set(duration.Seconds())
116+
}(time.Now())
107117
specToServe, err := s.buildOpenAPISpec()
108118
if err != nil {
109119
return nil, err
@@ -202,13 +212,27 @@ func (s *specAggregator) tryUpdatingServiceSpecs(specInfo *openAPISpecInfo) erro
202212
if specInfo == nil {
203213
return fmt.Errorf("invalid input: specInfo must be non-nil")
204214
}
215+
_, updated := s.openAPISpecs[specInfo.apiService.Name]
205216
origSpecInfo, existedBefore := s.openAPISpecs[specInfo.apiService.Name]
206217
s.openAPISpecs[specInfo.apiService.Name] = specInfo
207218

208219
// Skip aggregation if OpenAPI spec didn't change
209220
if existedBefore && origSpecInfo != nil && origSpecInfo.etag == specInfo.etag {
210221
return nil
211222
}
223+
klog.V(2).Infof("Updating OpenAPI spec because %s is updated", specInfo.apiService.Name)
224+
defer func(start time.Time) {
225+
duration := time.Now().Sub(start)
226+
klog.V(2).Infof("Finished OpenAPI spec generation after %v", duration)
227+
228+
reason := "add"
229+
if updated {
230+
reason = "update"
231+
}
232+
233+
regenerationCounter.With(map[string]string{"apiservice": specInfo.apiService.Name, "reason": reason})
234+
regenerationDurationGauge.With(map[string]string{"reason": reason}).Set(duration.Seconds())
235+
}(time.Now())
212236
if err := s.updateOpenAPISpec(); err != nil {
213237
if existedBefore {
214238
s.openAPISpecs[specInfo.apiService.Name] = origSpecInfo
@@ -228,6 +252,14 @@ func (s *specAggregator) tryDeleteServiceSpecs(apiServiceName string) error {
228252
return nil
229253
}
230254
delete(s.openAPISpecs, apiServiceName)
255+
klog.V(2).Infof("Updating OpenAPI spec because %s is removed", apiServiceName)
256+
defer func(start time.Time) {
257+
duration := time.Now().Sub(start)
258+
klog.V(2).Infof("Finished OpenAPI spec generation after %v", duration)
259+
260+
regenerationCounter.With(map[string]string{"apiservice": apiServiceName, "reason": "delete"})
261+
regenerationDurationGauge.With(map[string]string{"reason": "delete"}).Set(duration.Seconds())
262+
}(time.Now())
231263
if err := s.updateOpenAPISpec(); err != nil {
232264
s.openAPISpecs[apiServiceName] = orgSpecInfo
233265
return err
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 aggregator
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: "aggregator_openapi_v2_regeneration_count",
28+
Help: "Counter of OpenAPI v2 spec regeneration count broken down by causing APIService name and reason.",
29+
StabilityLevel: metrics.ALPHA,
30+
},
31+
[]string{"apiservice", "reason"},
32+
)
33+
regenerationDurationGauge = metrics.NewGaugeVec(
34+
&metrics.GaugeOpts{
35+
Name: "aggregator_openapi_v2_regeneration_duration",
36+
Help: "Gauge of OpenAPI v2 spec regeneration duration in seconds.",
37+
StabilityLevel: metrics.ALPHA,
38+
},
39+
[]string{"reason"},
40+
)
41+
)
42+
43+
func init() {
44+
legacyregistry.MustRegister(regenerationCounter)
45+
legacyregistry.MustRegister(regenerationDurationGauge)
46+
}

vendor/modules.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1637,6 +1637,7 @@ k8s.io/component-base/featuregate
16371637
k8s.io/component-base/featuregate/testing
16381638
k8s.io/component-base/logs
16391639
k8s.io/component-base/metrics
1640+
k8s.io/component-base/metrics/legacyregistry
16401641
k8s.io/component-base/version
16411642
# k8s.io/cri-api v0.0.0 => ./staging/src/k8s.io/cri-api
16421643
k8s.io/cri-api/pkg/apis

0 commit comments

Comments
 (0)