Skip to content

Commit

Permalink
Add controller logic for custom response headers
Browse files Browse the repository at this point in the history
adding unit test for custom response headers
  • Loading branch information
ruixiansong committed Aug 26, 2022
1 parent 09c54ff commit a63e41d
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 0 deletions.
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

0 comments on commit a63e41d

Please sign in to comment.