Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Custom Response Header Logic and the Unit Tests for it #1772

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions pkg/backends/features/customresponseheaders.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copyright 2022 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 features

import (
"reflect"

"k8s.io/ingress-gce/pkg/composite"
"k8s.io/ingress-gce/pkg/utils"
"k8s.io/klog/v2"
)

// EnsureCustomResponseHeaders reads the CustomResponseHeaders configuration specified in the ServicePort.BackendConfig
// and applies it to the BackendService. It returns true if there was a difference between
// current settings on the BackendService and ServicePort.BackendConfig.

func EnsureCustomResponseHeaders(sp utils.ServicePort, be *composite.BackendService) bool {
desiredHeaders := []string{}
if sp.BackendConfig.Spec.CustomResponseHeaders != nil {
desiredHeaders = sp.BackendConfig.Spec.CustomResponseHeaders.Headers
}
currentHeaders := []string{}
if be.CustomResponseHeaders != nil {
currentHeaders = be.CustomResponseHeaders
}

if !reflect.DeepEqual(desiredHeaders, currentHeaders) {
be.CustomResponseHeaders = desiredHeaders
klog.V(2).Infof("Updated Custom Response Headers for service %v/%v.", sp.ID.Service.Namespace, sp.ID.Service.Name)
return true
}

return false
}
99 changes: 99 additions & 0 deletions pkg/backends/features/customresponseheaders_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
Copyright 2022 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 features

import (
"testing"

backendconfigv1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1"
"k8s.io/ingress-gce/pkg/composite"
"k8s.io/ingress-gce/pkg/utils"
)

var testCustomResponseHeader = []string{"X-TEST-HEADER:{test-value}"}

func TestEnsureCustomResponseHeaders(t *testing.T) {
testCases := []struct {
desc string
sp utils.ServicePort
be *composite.BackendService
updateExpected bool
}{
{
desc: "Custom Response Headers missing from both ends, no update needed",
sp: utils.ServicePort{BackendConfig: &backendconfigv1.BackendConfig{}},
be: &composite.BackendService{},
updateExpected: false,
},
{
desc: "No header in current setting, new headers in backendConfig, update needed",
sp: utils.ServicePort{BackendConfig: &backendconfigv1.BackendConfig{
Spec: backendconfigv1.BackendConfigSpec{
CustomResponseHeaders: &backendconfigv1.CustomResponseHeadersConfig{
Headers: testCustomResponseHeader,
},
},
}},
be: &composite.BackendService{},
updateExpected: true,
},
{
desc: "Having headers in current setting, no header in backendConfig, update needed",
sp: utils.ServicePort{BackendConfig: &backendconfigv1.BackendConfig{}},
be: &composite.BackendService{
CustomResponseHeaders: testCustomResponseHeader,
},
updateExpected: true,
},
{
desc: "Having headers in current setting, new headers in backendConfig, update needed",
sp: utils.ServicePort{BackendConfig: &backendconfigv1.BackendConfig{
Spec: backendconfigv1.BackendConfigSpec{
CustomResponseHeaders: &backendconfigv1.CustomResponseHeadersConfig{
Headers: append(testCustomResponseHeader, "X-TEST-HEADER2:{test-value2}"),
},
},
}},
be: &composite.BackendService{
CustomResponseHeaders: testCustomResponseHeader,
},
updateExpected: true,
},
{
desc: "Identical headers, no update",
sp: utils.ServicePort{BackendConfig: &backendconfigv1.BackendConfig{
Spec: backendconfigv1.BackendConfigSpec{
CustomResponseHeaders: &backendconfigv1.CustomResponseHeadersConfig{
Headers: testCustomResponseHeader,
},
},
}},
be: &composite.BackendService{
CustomResponseHeaders: testCustomResponseHeader,
},
updateExpected: false,
},
}
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
result := EnsureCustomResponseHeaders(tc.sp, tc.be)
if result != tc.updateExpected {
t.Errorf("Expected %v but got %v", tc.updateExpected, result)
}
})
}
}
1 change: 1 addition & 0 deletions pkg/backends/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (s *backendSyncer) ensureBackendService(sp utils.ServicePort) error {
needUpdate = features.EnsureDraining(sp, be) || needUpdate
needUpdate = features.EnsureAffinity(sp, be) || needUpdate
needUpdate = features.EnsureCustomRequestHeaders(sp, be) || needUpdate
needUpdate = features.EnsureCustomResponseHeaders(sp, be) || needUpdate
needUpdate = features.EnsureLogging(sp, be) || needUpdate
}

Expand Down