From 2055e2183c33ee9236562825bf35e8b1b5575d96 Mon Sep 17 00:00:00 2001 From: Delyan Raychev <49918230+draychev@users.noreply.github.com> Date: Wed, 26 Jun 2019 12:22:46 -0700 Subject: [PATCH] cache: Sort HTTP Settings and Backend Pool slices (#319) --- pkg/appgw/backendaddresspools.go | 6 +++++- pkg/appgw/backendhttpsettings.go | 5 +++++ pkg/controller/controller.go | 1 + pkg/controller/helpers.go | 8 ++++---- pkg/controller/helpers_test.go | 6 ++++-- pkg/sorter/http_settings.go | 26 ++++++++++++++++++++++++++ pkg/sorter/pools.go | 26 ++++++++++++++++++++++++++ 7 files changed, 71 insertions(+), 7 deletions(-) create mode 100644 pkg/sorter/http_settings.go create mode 100644 pkg/sorter/pools.go diff --git a/pkg/appgw/backendaddresspools.go b/pkg/appgw/backendaddresspools.go index cbd289ff5..edfb3c0d7 100644 --- a/pkg/appgw/backendaddresspools.go +++ b/pkg/appgw/backendaddresspools.go @@ -47,7 +47,11 @@ func (c *appGwConfigBuilder) BackendAddressPools(cbCtx *ConfigBuilderContext) er addressPools[*pool.Name] = pool } } - c.appGwConfig.BackendAddressPools = getBackendPoolMapValues(&addressPools) + pools := getBackendPoolMapValues(&addressPools) + if pools != nil { + sort.Sort(sorter.ByBackendPoolName(*pools)) + } + c.appGwConfig.BackendAddressPools = pools return nil } diff --git a/pkg/appgw/backendhttpsettings.go b/pkg/appgw/backendhttpsettings.go index b576669c1..4bb8f7372 100644 --- a/pkg/appgw/backendhttpsettings.go +++ b/pkg/appgw/backendhttpsettings.go @@ -8,6 +8,7 @@ package appgw import ( "errors" "fmt" + "sort" n "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-12-01/network" "github.com/Azure/go-autorest/autorest/to" @@ -18,6 +19,7 @@ import ( "github.com/Azure/application-gateway-kubernetes-ingress/pkg/annotations" "github.com/Azure/application-gateway-kubernetes-ingress/pkg/events" + "github.com/Azure/application-gateway-kubernetes-ingress/pkg/sorter" ) const ( @@ -200,6 +202,9 @@ func (c *appGwConfigBuilder) getBackendsAndSettingsMap(ingressList []*v1beta1.In func (c *appGwConfigBuilder) BackendHTTPSettingsCollection(cbCtx *ConfigBuilderContext) error { httpSettings, _, _, err := c.getBackendsAndSettingsMap(cbCtx.IngressList, cbCtx.ServiceList) + if httpSettings != nil { + sort.Sort(sorter.BySettingsName(*httpSettings)) + } c.appGwConfig.BackendHTTPSettingsCollection = httpSettings return err } diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index 134f2afc8..035242372 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -49,6 +49,7 @@ func NewAppGwIngressController(appGwClient n.ApplicationGatewaysClient, appGwIde k8sContext: k8sContext, k8sUpdateChannel: k8sContext.UpdateChannel, recorder: recorder, + configCache: to.ByteSlicePtr([]byte{}), } controller.eventQueue = NewEventQueue(controller) diff --git a/pkg/controller/helpers.go b/pkg/controller/helpers.go index bd71a25de..4419a5d7a 100644 --- a/pkg/controller/helpers.go +++ b/pkg/controller/helpers.go @@ -33,7 +33,7 @@ func (c *AppGwIngressController) updateCache(appGw *n.ApplicationGateway) { c.configCache = nil return } - c.configCache = &sanitized + *c.configCache = sanitized } // configIsSame compares the newly created App Gwy configuration with a cache to determine whether anything has changed. @@ -68,14 +68,14 @@ func (c *AppGwIngressController) dumpSanitizedJSON(appGw *n.ApplicationGateway) keysToDelete := []string{ "sslCertificates", } - var stripped []byte - if stripped, err = deleteKeyFromJSON(jsonConfig, keysToDelete...); err != nil { + var sanitized []byte + if sanitized, err = deleteKeyFromJSON(jsonConfig, keysToDelete...); err != nil { return nil, err } // Unmarshal and Marshall again with Indent so it is human readable var config interface{} - _ = json.Unmarshal(stripped, &config) + _ = json.Unmarshal(sanitized, &config) return json.MarshalIndent(config, "-- App Gwy config --", " ") } diff --git a/pkg/controller/helpers_test.go b/pkg/controller/helpers_test.go index b464b3e57..fc9decdfd 100644 --- a/pkg/controller/helpers_test.go +++ b/pkg/controller/helpers_test.go @@ -64,8 +64,10 @@ var _ = Describe("configure App Gateway", func() { }) Context("ensure configIsSame works as expected", func() { - It("should deal with nil cache and store stuff in it", func() { - c := AppGwIngressController{} + It("should deal with empty cache and store stuff in it", func() { + c := AppGwIngressController{ + configCache: to.ByteSlicePtr([]byte{}), + } config := n.ApplicationGateway{ ID: to.StringPtr("something"), } diff --git a/pkg/sorter/http_settings.go b/pkg/sorter/http_settings.go new file mode 100644 index 000000000..4b56ef5f9 --- /dev/null +++ b/pkg/sorter/http_settings.go @@ -0,0 +1,26 @@ +// ------------------------------------------------------------------------------------------- +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. +// -------------------------------------------------------------------------------------------- + +package sorter + +import ( + n "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-12-01/network" +) + +// BySettingsName is a facility to sort slices of ApplicationGatewayBackendHTTPSettings by Name +type BySettingsName []n.ApplicationGatewayBackendHTTPSettings + +func (a BySettingsName) Len() int { return len(a) } +func (a BySettingsName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a BySettingsName) Less(i, j int) bool { + return getSettingsName(a[i]) < getSettingsName(a[j]) +} + +func getSettingsName(setting n.ApplicationGatewayBackendHTTPSettings) string { + if setting.Name == nil { + return "" + } + return *setting.Name +} diff --git a/pkg/sorter/pools.go b/pkg/sorter/pools.go new file mode 100644 index 000000000..9907b77ff --- /dev/null +++ b/pkg/sorter/pools.go @@ -0,0 +1,26 @@ +// ------------------------------------------------------------------------------------------- +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. See License.txt in the project root for license information. +// -------------------------------------------------------------------------------------------- + +package sorter + +import ( + n "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-12-01/network" +) + +// ByBackendPoolName is a facility to sort slices of ApplicationGatewayBackendAddressPool by Name +type ByBackendPoolName []n.ApplicationGatewayBackendAddressPool + +func (a ByBackendPoolName) Len() int { return len(a) } +func (a ByBackendPoolName) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a ByBackendPoolName) Less(i, j int) bool { + return getPoolName(a[i]) < getPoolName(a[j]) +} + +func getPoolName(pool n.ApplicationGatewayBackendAddressPool) string { + if pool.Name == nil { + return "" + } + return *pool.Name +}