From 3f59d2f83781d0a277ff31f1e1e8684e9240b4e6 Mon Sep 17 00:00:00 2001 From: tczekajlo Date: Sat, 11 Mar 2023 18:58:18 +0100 Subject: [PATCH] Add support for Cloud CDN dynamic compression --- pkg/apis/backendconfig/v1/types.go | 1 + pkg/apis/backendconfig/v1beta1/types.go | 5 +++-- pkg/backends/features/cdn.go | 8 ++++++++ pkg/backends/features/cdn_test.go | 16 +++++++++++----- 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/pkg/apis/backendconfig/v1/types.go b/pkg/apis/backendconfig/v1/types.go index ae7f339567..214b503fc7 100644 --- a/pkg/apis/backendconfig/v1/types.go +++ b/pkg/apis/backendconfig/v1/types.go @@ -97,6 +97,7 @@ type CDNConfig struct { ServeWhileStale *int64 `json:"serveWhileStale,omitempty"` SignedUrlCacheMaxAgeSec *int64 `json:"signedUrlCacheMaxAgeSec,omitempty"` SignedUrlKeys []*SignedUrlKey `json:"signedUrlKeys,omitempty"` + CompressionMode *string `json:"compressionMode,omitempty"` } // BypassCacheOnRequestHeader contains configuration for how requests containing specific request diff --git a/pkg/apis/backendconfig/v1beta1/types.go b/pkg/apis/backendconfig/v1beta1/types.go index 0782e1aac2..4d170d7745 100644 --- a/pkg/apis/backendconfig/v1beta1/types.go +++ b/pkg/apis/backendconfig/v1beta1/types.go @@ -81,8 +81,9 @@ type OAuthClientCredentials struct { // CDNConfig contains configuration for CDN-enabled backends. // +k8s:openapi-gen=true type CDNConfig struct { - Enabled bool `json:"enabled"` - CachePolicy *CacheKeyPolicy `json:"cachePolicy,omitempty"` + Enabled bool `json:"enabled"` + CachePolicy *CacheKeyPolicy `json:"cachePolicy,omitempty"` + CompressionMode *string `json:"compressionMode,omitempty"` } // CacheKeyPolicy contains configuration for how requests to a CDN-enabled backend are cached. diff --git a/pkg/backends/features/cdn.go b/pkg/backends/features/cdn.go index a698f436ad..6a4049865b 100644 --- a/pkg/backends/features/cdn.go +++ b/pkg/backends/features/cdn.go @@ -40,6 +40,8 @@ var defaultCdnPolicy = composite.BackendServiceCdnPolicy{ SignedUrlCacheMaxAgeSec: 0, } +var defaultCompressionMode string = "DISABLED" + // EnsureCDN reads the CDN configuration specified in the ServicePort.BackendConfig // and applies it to the BackendService. It returns true if there were existing // settings on the BackendService that were overwritten. @@ -206,6 +208,12 @@ func renderConfig(sp utils.ServicePort) *composite.BackendService { be.CdnPolicy.BypassCacheOnRequestHeaders = bypassCacheOnRequestHeaders } + if cdnConfig.CompressionMode != nil { + be.CompressionMode = *cdnConfig.CompressionMode + } else { + be.CompressionMode = defaultCompressionMode + } + return be } diff --git a/pkg/backends/features/cdn_test.go b/pkg/backends/features/cdn_test.go index a15d21af73..2460af17d9 100644 --- a/pkg/backends/features/cdn_test.go +++ b/pkg/backends/features/cdn_test.go @@ -31,9 +31,11 @@ import ( ) var ( - cacheAllStatic string = "CACHE_ALL_STATIC" - useOriginHeaders string = "USE_ORIGIN_HEADERS" - forceCacheAll string = "FORCE_CACHE_ALL" + cacheAllStatic string = "CACHE_ALL_STATIC" + useOriginHeaders string = "USE_ORIGIN_HEADERS" + forceCacheAll string = "FORCE_CACHE_ALL" + compressionModeAutomatic string = "AUTOMATIC" + compressionModeDisabled string = "DISABLED" ) func init() { @@ -244,12 +246,14 @@ func TestEnsureCDN(t *testing.T) { {HeaderName: "header"}, }, SignedUrlCacheMaxAgeSec: createInt64(3600), + CompressionMode: &compressionModeDisabled, }, }, }, }, be: &composite.BackendService{ - EnableCDN: true, + EnableCDN: true, + CompressionMode: compressionModeAutomatic, CdnPolicy: &composite.BackendServiceCdnPolicy{ CacheKeyPolicy: &composite.CacheKeyPolicy{ IncludeHost: true, @@ -277,7 +281,8 @@ func TestEnsureCDN(t *testing.T) { }, updateExpected: false, beAfter: &composite.BackendService{ - EnableCDN: true, + EnableCDN: true, + CompressionMode: compressionModeAutomatic, CdnPolicy: &composite.BackendServiceCdnPolicy{ CacheKeyPolicy: &composite.CacheKeyPolicy{ IncludeHost: true, @@ -363,6 +368,7 @@ func TestEnsureCDN(t *testing.T) { cdn.ServeWhileStale = createInt64(defCdn.ServeWhileStale) cdn.RequestCoalescing = createBool(defCdn.RequestCoalescing) cdn.SignedUrlCacheMaxAgeSec = createInt64(defCdn.SignedUrlCacheMaxAgeSec) + cdn.CompressionMode = &defaultCompressionMode }).build(), be: newDefaultBackendService().build(), updateExpected: false,