diff --git a/staging/src/k8s.io/apiserver/pkg/server/config.go b/staging/src/k8s.io/apiserver/pkg/server/config.go index f9ce8db9b72..ccbc31462a9 100644 --- a/staging/src/k8s.io/apiserver/pkg/server/config.go +++ b/staging/src/k8s.io/apiserver/pkg/server/config.go @@ -678,6 +678,7 @@ func DefaultBuildHandlerChain(apiHandler http.Handler, c *Config) http.Handler { handler = genericfilters.WithProbabilisticGoaway(handler, c.GoawayChance) } handler = genericapifilters.WithAuditAnnotations(handler, c.AuditBackend, c.AuditPolicyChecker) + handler = genericapifilters.WithCacheControl(handler) handler = genericfilters.WithPanicRecovery(handler) return handler } diff --git a/test/integration/apiserver/apiserver_test.go b/test/integration/apiserver/apiserver_test.go index 19b7a8724e4..7d54b6420e6 100644 --- a/test/integration/apiserver/apiserver_test.go +++ b/test/integration/apiserver/apiserver_test.go @@ -215,6 +215,52 @@ func Test4xxStatusCodeInvalidPatch(t *testing.T) { } } +func TestCacheControl(t *testing.T) { + masterConfig := framework.NewIntegrationTestMasterConfigWithOptions(&framework.MasterConfigOptions{}) + masterConfig.GenericConfig.OpenAPIConfig = framework.DefaultOpenAPIConfig() + master, _, closeFn := framework.RunAMaster(masterConfig) + defer closeFn() + + rt, err := restclient.TransportFor(master.GenericAPIServer.LoopbackClientConfig) + if err != nil { + t.Fatal(err) + } + + paths := []string{ + // untyped + "/", + // health + "/healthz", + // openapi + "/openapi/v2", + // discovery + "/api", + "/api/v1", + "/apis", + "/apis/apps", + "/apis/apps/v1", + // apis + "/api/v1/namespaces", + "/apis/apps/v1/deployments", + } + for _, path := range paths { + t.Run(path, func(t *testing.T) { + req, err := http.NewRequest("GET", master.GenericAPIServer.LoopbackClientConfig.Host+path, nil) + if err != nil { + t.Fatal(err) + } + resp, err := rt.RoundTrip(req) + if err != nil { + t.Fatal(err) + } + cc := resp.Header.Get("Cache-Control") + if !strings.Contains(cc, "private") { + t.Errorf("expected private cache-control, got %q", cc) + } + }) + } +} + // Tests that the apiserver returns 202 status code as expected. func Test202StatusCode(t *testing.T) { s, clientSet, closeFn := setup(t)