From 688247f4f93b6f5b9ec3a170c8da2aaefa65a4c7 Mon Sep 17 00:00:00 2001 From: Jun <108045855+2456868764@users.noreply.github.com> Date: Mon, 25 Dec 2023 11:55:04 +0800 Subject: [PATCH] fix disableOnEtagHeader can't work in configmap gzip envoyfiler (#731) --- pkg/ingress/kube/configmap/gzip.go | 24 ++++++----- pkg/ingress/kube/configmap/gzip_test.go | 53 +++++++++++++++++++------ 2 files changed, 54 insertions(+), 23 deletions(-) diff --git a/pkg/ingress/kube/configmap/gzip.go b/pkg/ingress/kube/configmap/gzip.go index 48106c27f..ce308b5e0 100644 --- a/pkg/ingress/kube/configmap/gzip.go +++ b/pkg/ingress/kube/configmap/gzip.go @@ -15,7 +15,6 @@ package configmap import ( - "encoding/json" "errors" "fmt" "reflect" @@ -130,12 +129,17 @@ func compareGzip(old *Gzip, new *Gzip) (Result, error) { func deepCopyGzip(gzip *Gzip) (*Gzip, error) { newGzip := NewDefaultGzip() - bytes, err := json.Marshal(gzip) - if err != nil { - return nil, err - } - err = json.Unmarshal(bytes, newGzip) - return newGzip, err + newGzip.Enable = gzip.Enable + newGzip.MinContentLength = gzip.MinContentLength + newGzip.ContentType = make([]string, 0, len(gzip.ContentType)) + newGzip.ContentType = append(newGzip.ContentType, gzip.ContentType...) + newGzip.DisableOnEtagHeader = gzip.DisableOnEtagHeader + newGzip.MemoryLevel = gzip.MemoryLevel + newGzip.WindowBits = gzip.WindowBits + newGzip.ChunkSize = gzip.ChunkSize + newGzip.CompressionLevel = gzip.CompressionLevel + newGzip.CompressionStrategy = gzip.CompressionStrategy + return newGzip, nil } func NewDefaultGzip() *Gzip { @@ -305,9 +309,9 @@ func (g *GzipController) constructGzipStruct(gzip *Gzip, namespace string) strin "response_direction_config": { "common_config": { "min_content_length": %d, - "content_type": [%s], - "disable_on_etag_header": %t - } + "content_type": [%s] + }, + "disable_on_etag_header": %t }, "request_direction_config": { "common_config": { diff --git a/pkg/ingress/kube/configmap/gzip_test.go b/pkg/ingress/kube/configmap/gzip_test.go index 3400ea126..c8a5c0871 100644 --- a/pkg/ingress/kube/configmap/gzip_test.go +++ b/pkg/ingress/kube/configmap/gzip_test.go @@ -277,27 +277,54 @@ func Test_deepCopyGzip(t *testing.T) { wantErr error }{ { - name: "deep copy", + name: "deep copy case 1", gzip: &Gzip{ Enable: false, - MinContentLength: 1024, - ContentType: []string{"text/html", "text/css", "text/plain", "text/xml", "application/json", "application/javascript", "application/xhtml+xml", "image/svg+xml"}, - DisableOnEtagHeader: true, - MemoryLevel: 5, - WindowBits: 12, + MinContentLength: 102, + ContentType: []string{"text/css", "text/plain", "text/xml", "application/json", "application/javascript", "application/xhtml+xml", "image/svg+xml"}, + DisableOnEtagHeader: false, + MemoryLevel: 6, + WindowBits: 11, ChunkSize: 4096, - CompressionLevel: "BEST_COMPRESSION", + CompressionLevel: "BEST_SPEED", CompressionStrategy: "DEFAULT_STRATEGY", }, wantGzip: &Gzip{ Enable: false, - MinContentLength: 1024, - ContentType: []string{"text/html", "text/css", "text/plain", "text/xml", "application/json", "application/javascript", "application/xhtml+xml", "image/svg+xml"}, - DisableOnEtagHeader: true, - MemoryLevel: 5, - WindowBits: 12, + MinContentLength: 102, + ContentType: []string{"text/css", "text/plain", "text/xml", "application/json", "application/javascript", "application/xhtml+xml", "image/svg+xml"}, + DisableOnEtagHeader: false, + MemoryLevel: 6, + WindowBits: 11, ChunkSize: 4096, - CompressionLevel: "BEST_COMPRESSION", + CompressionLevel: "BEST_SPEED", + CompressionStrategy: "DEFAULT_STRATEGY", + }, + wantErr: nil, + }, + + { + name: "deep copy case 2", + gzip: &Gzip{ + Enable: true, + MinContentLength: 102, + ContentType: []string{"text/css", "text/plain", "text/xml", "application/json", "application/javascript", "application/xhtml+xml", "image/svg+xml"}, + DisableOnEtagHeader: true, + MemoryLevel: 6, + WindowBits: 11, + ChunkSize: 4096, + CompressionLevel: "BEST_SPEED", + CompressionStrategy: "DEFAULT_STRATEGY", + }, + wantGzip: &Gzip{ + Enable: true, + MinContentLength: 102, + ContentType: []string{"text/css", "text/plain", "text/xml", "application/json", "application/javascript", "application/xhtml+xml", "image/svg+xml"}, + DisableOnEtagHeader: true, + MemoryLevel: 6, + WindowBits: 11, + ChunkSize: 4096, + CompressionLevel: "BEST_SPEED", CompressionStrategy: "DEFAULT_STRATEGY", }, wantErr: nil,