Skip to content

Commit

Permalink
cache: Sort HTTP Settings and Backend Pool slices (#319)
Browse files Browse the repository at this point in the history
  • Loading branch information
draychev authored and akshaysngupta committed Jun 26, 2019
1 parent 6971d51 commit 2055e21
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 7 deletions.
6 changes: 5 additions & 1 deletion pkg/appgw/backendaddresspools.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 5 additions & 0 deletions pkg/appgw/backendhttpsettings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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 (
Expand Down Expand Up @@ -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
}
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions pkg/controller/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 --", " ")
}

Expand Down
6 changes: 4 additions & 2 deletions pkg/controller/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/sorter/http_settings.go
Original file line number Diff line number Diff line change
@@ -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
}
26 changes: 26 additions & 0 deletions pkg/sorter/pools.go
Original file line number Diff line number Diff line change
@@ -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
}

0 comments on commit 2055e21

Please sign in to comment.