Change:
The kube-apiserver is protected, so it's responses should not be cached. The only known exception is for openapi data which is not sensitive, which is large, and which has etags. /assign
/sig api-machinery

Discussion:
package app import ( <del> "github.com/prometheus/client_golang/prometheus" </del> "net/http" goruntime "runtime" <ins> "github.com/prometheus/client_golang/prometheus" </ins> genericapifilters "k8s.io/apiserver/pkg/endpoints/filters" apirequest "k8s.io/apiserver/pkg/endpoints/request" apiserver "k8s.io/apiserver/pkg/server"

handler = genericapifilters.WithAuthentication(handler, authenticationInfo.Authenticator, failedHandler, nil) } handler = genericapifilters.WithRequestInfo(handler, requestInfoResolver) <ins> handler = genericapifilters.WithCacheControl(handler) </ins> handler = genericfilters.WithPanicRecovery(handler) return handler

handler = genericapifilters.WithAuthorization(handler, authz, legacyscheme.Codecs) handler = genericapifilters.WithAuthentication(handler, authn, failedHandler, nil) handler = genericapifilters.WithRequestInfo(handler, requestInfoResolver) <ins> handler = genericapifilters.WithCacheControl(handler) </ins> handler = genericfilters.WithPanicRecovery(handler) return handler

handler = genericfilters.WithMaxInFlightLimit(handler, c.MaxRequestsInFlight, c.MaxMutatingRequestsInFlight, c.LongRunningFunc) handler = genericfilters.WithWaitGroup(handler, c.LongRunningFunc, c.HandlerChainWaitGroup) handler = genericapifilters.WithRequestInfo(handler, server.NewRequestInfoResolver(c)) <ins> handler = genericapifilters.WithCacheControl(handler) </ins> handler = genericfilters.WithPanicRecovery(handler) return handler

"authentication_test.go", "authn_audit_test.go", "authorization_test.go", <ins> "cachecontrol_test.go", </ins> "impersonation_test.go", "requestinfo_test.go", ],

"authentication.go", "authn_audit.go", "authorization.go", <ins> "cachecontrol.go", </ins> "doc.go", "impersonation.go", "requestinfo.go",

<ins> /* Copyright 2019 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package filters import ( "net/http" ) // WithCacheControl sets the Cache-Control header to "no-cache, private" because all servers are protected by authn/authz. // see https://developers.google.com/web/fundamentals/performance/optimizing-content-efficiency/http-caching#defining_optimal_cache-control_policy func WithCacheControl(handler http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { // Set the cache-control header if it is not already set if _, ok := w.Header()["Cache-Control"]; !ok { w.Header().Set("Cache-Control", "no-cache, private") } handler.ServeHTTP(w, req) }) } </ins>

<ins> /* Copyright 2019 The Kubernetes Authors. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ package filters import ( "net/http" "net/http/httptest" "testing" ) func TestCacheControl(t *testing.T) { tests := []struct { name string path string startingHeader string expectedHeader string }{ { name:           "simple", path:           "/api/v1/namespaces", expectedHeader: "no-cache, private", }, { name:           "openapi", path:           "/openapi/v2", expectedHeader: "no-cache, private", }, { name:           "already-set", path:           "/api/v1/namespaces", startingHeader: "nonsense", expectedHeader: "nonsense", }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { handler := http.HandlerFunc(func(http.ResponseWriter, *http.Request) { //do nothing }) wrapped := WithCacheControl(handler) testRequest, err := http.NewRequest(http.MethodGet, test.path, nil) if err != nil { t.Fatal(err) } w := httptest.NewRecorder() if len(test.startingHeader) > 0 { w.Header().Set("Cache-Control", test.startingHeader) } wrapped.ServeHTTP(w, testRequest) actual := w.Header().Get("Cache-Control") if actual != test.expectedHeader { t.Fatal(actual) } }) } } </ins>

handler = genericfilters.WithTimeoutForNonLongRunningRequests(handler, c.LongRunningFunc, c.RequestTimeout) handler = genericfilters.WithWaitGroup(handler, c.LongRunningFunc, c.HandlerChainWaitGroup) handler = genericapifilters.WithRequestInfo(handler, c.RequestInfoResolver) <ins> handler = genericapifilters.WithCacheControl(handler) </ins> handler = genericfilters.WithPanicRecovery(handler) return handler }