Skip to content

Commit 71bfb73

Browse files
authored
Merge pull request kubernetes#92842 from liggitt/deprecated-annotation
Deprecated API request audit annotation
2 parents d2f4cc0 + 9550f50 commit 71bfb73

File tree

3 files changed

+32
-9
lines changed

3 files changed

+32
-9
lines changed

staging/src/k8s.io/apiextensions-apiserver/pkg/apiserver/customresource_handler.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ type crdInfo struct {
142142
spec *apiextensionsv1.CustomResourceDefinitionSpec
143143
acceptedNames *apiextensionsv1.CustomResourceDefinitionNames
144144

145+
// Deprecated per version
146+
deprecated map[string]bool
147+
145148
// Warnings per version
146149
warnings map[string][]string
147150

@@ -329,10 +332,9 @@ func (r *crdHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
329332
return
330333
}
331334

332-
if utilfeature.DefaultFeatureGate.Enabled(features.WarningHeaders) {
333-
for _, w := range crdInfo.warnings[requestInfo.APIVersion] {
334-
warning.AddWarning(req.Context(), "", w)
335-
}
335+
deprecated := crdInfo.deprecated[requestInfo.APIVersion]
336+
for _, w := range crdInfo.warnings[requestInfo.APIVersion] {
337+
warning.AddWarning(req.Context(), "", w)
336338
}
337339

338340
verb := strings.ToUpper(requestInfo.Verb)
@@ -372,7 +374,7 @@ func (r *crdHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
372374
}
373375

374376
if handlerFunc != nil {
375-
handlerFunc = metrics.InstrumentHandlerFunc(verb, requestInfo.APIGroup, requestInfo.APIVersion, resource, subresource, scope, metrics.APIServerComponent, false, "", handlerFunc)
377+
handlerFunc = metrics.InstrumentHandlerFunc(verb, requestInfo.APIGroup, requestInfo.APIVersion, resource, subresource, scope, metrics.APIServerComponent, deprecated, "", handlerFunc)
376378
handler := genericfilters.WithWaitGroup(handlerFunc, longRunningFilter, crdInfo.waitGroup)
377379
handler.ServeHTTP(w, req)
378380
return
@@ -622,6 +624,7 @@ func (r *crdHandler) getOrCreateServingInfoFor(uid types.UID, name string) (*crd
622624
storages := map[string]customresource.CustomResourceStorage{}
623625
statusScopes := map[string]*handlers.RequestScope{}
624626
scaleScopes := map[string]*handlers.RequestScope{}
627+
deprecated := map[string]bool{}
625628
warnings := map[string][]string{}
626629

627630
equivalentResourceRegistry := runtime.NewEquivalentResourceRegistry()
@@ -883,10 +886,13 @@ func (r *crdHandler) getOrCreateServingInfoFor(uid types.UID, name string) (*crd
883886
statusScopes[v.Name] = &statusScope
884887

885888
if v.Deprecated {
886-
if v.DeprecationWarning != nil {
887-
warnings[v.Name] = append(warnings[v.Name], *v.DeprecationWarning)
888-
} else {
889-
warnings[v.Name] = append(warnings[v.Name], defaultDeprecationWarning(v.Name, crd.Spec))
889+
deprecated[v.Name] = true
890+
if utilfeature.DefaultFeatureGate.Enabled(features.WarningHeaders) {
891+
if v.DeprecationWarning != nil {
892+
warnings[v.Name] = append(warnings[v.Name], *v.DeprecationWarning)
893+
} else {
894+
warnings[v.Name] = append(warnings[v.Name], defaultDeprecationWarning(v.Name, crd.Spec))
895+
}
890896
}
891897
}
892898
}
@@ -898,6 +904,7 @@ func (r *crdHandler) getOrCreateServingInfoFor(uid types.UID, name string) (*crd
898904
requestScopes: requestScopes,
899905
scaleRequestScopes: scaleScopes,
900906
statusRequestScopes: statusScopes,
907+
deprecated: deprecated,
901908
warnings: warnings,
902909
storageVersion: storageVersion,
903910
waitGroup: &utilwaitgroup.SafeWaitGroup{},

staging/src/k8s.io/apiserver/pkg/endpoints/metrics/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ go_library(
1515
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/validation:go_default_library",
1616
"//staging/src/k8s.io/apimachinery/pkg/types:go_default_library",
1717
"//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library",
18+
"//staging/src/k8s.io/apiserver/pkg/audit:go_default_library",
1819
"//staging/src/k8s.io/apiserver/pkg/endpoints/request:go_default_library",
1920
"//staging/src/k8s.io/apiserver/pkg/features:go_default_library",
2021
"//staging/src/k8s.io/apiserver/pkg/util/feature:go_default_library",

staging/src/k8s.io/apiserver/pkg/endpoints/metrics/metrics.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"k8s.io/apimachinery/pkg/apis/meta/v1/validation"
3232
"k8s.io/apimachinery/pkg/types"
3333
utilsets "k8s.io/apimachinery/pkg/util/sets"
34+
"k8s.io/apiserver/pkg/audit"
3435
"k8s.io/apiserver/pkg/endpoints/request"
3536
"k8s.io/apiserver/pkg/features"
3637
utilfeature "k8s.io/apiserver/pkg/util/feature"
@@ -232,6 +233,16 @@ const (
232233
MutatingKind = "mutating"
233234
)
234235

236+
const (
237+
// deprecatedAnnotationKey is a key for an audit annotation set to
238+
// "true" on requests made to deprecated API versions
239+
deprecatedAnnotationKey = "k8s.io/deprecated"
240+
// removedReleaseAnnotationKey is a key for an audit annotation set to
241+
// the target removal release, in "<major>.<minor>" format,
242+
// on requests made to deprecated API versions with a target removal release
243+
removedReleaseAnnotationKey = "k8s.io/removed-release"
244+
)
245+
235246
var registerMetrics sync.Once
236247

237248
// Register all metrics.
@@ -315,6 +326,10 @@ func MonitorRequest(req *http.Request, verb, group, version, resource, subresour
315326
requestCounter.WithLabelValues(reportedVerb, dryRun, group, version, resource, subresource, scope, component, cleanContentType, codeToString(httpCode)).Inc()
316327
if deprecated {
317328
deprecatedRequestGauge.WithLabelValues(group, version, resource, subresource, removedRelease).Set(1)
329+
audit.AddAuditAnnotation(req.Context(), deprecatedAnnotationKey, "true")
330+
if len(removedRelease) > 0 {
331+
audit.AddAuditAnnotation(req.Context(), removedReleaseAnnotationKey, removedRelease)
332+
}
318333
}
319334
requestLatencies.WithLabelValues(reportedVerb, dryRun, group, version, resource, subresource, scope, component).Observe(elapsedSeconds)
320335
// We are only interested in response sizes of read requests.

0 commit comments

Comments
 (0)