Skip to content

Commit 2055e21

Browse files
draychevakshaysngupta
authored andcommitted
cache: Sort HTTP Settings and Backend Pool slices (#319)
1 parent 6971d51 commit 2055e21

File tree

7 files changed

+71
-7
lines changed

7 files changed

+71
-7
lines changed

pkg/appgw/backendaddresspools.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,11 @@ func (c *appGwConfigBuilder) BackendAddressPools(cbCtx *ConfigBuilderContext) er
4747
addressPools[*pool.Name] = pool
4848
}
4949
}
50-
c.appGwConfig.BackendAddressPools = getBackendPoolMapValues(&addressPools)
50+
pools := getBackendPoolMapValues(&addressPools)
51+
if pools != nil {
52+
sort.Sort(sorter.ByBackendPoolName(*pools))
53+
}
54+
c.appGwConfig.BackendAddressPools = pools
5155
return nil
5256
}
5357

pkg/appgw/backendhttpsettings.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package appgw
88
import (
99
"errors"
1010
"fmt"
11+
"sort"
1112

1213
n "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-12-01/network"
1314
"github.com/Azure/go-autorest/autorest/to"
@@ -18,6 +19,7 @@ import (
1819

1920
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/annotations"
2021
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/events"
22+
"github.com/Azure/application-gateway-kubernetes-ingress/pkg/sorter"
2123
)
2224

2325
const (
@@ -200,6 +202,9 @@ func (c *appGwConfigBuilder) getBackendsAndSettingsMap(ingressList []*v1beta1.In
200202

201203
func (c *appGwConfigBuilder) BackendHTTPSettingsCollection(cbCtx *ConfigBuilderContext) error {
202204
httpSettings, _, _, err := c.getBackendsAndSettingsMap(cbCtx.IngressList, cbCtx.ServiceList)
205+
if httpSettings != nil {
206+
sort.Sort(sorter.BySettingsName(*httpSettings))
207+
}
203208
c.appGwConfig.BackendHTTPSettingsCollection = httpSettings
204209
return err
205210
}

pkg/controller/controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ func NewAppGwIngressController(appGwClient n.ApplicationGatewaysClient, appGwIde
4949
k8sContext: k8sContext,
5050
k8sUpdateChannel: k8sContext.UpdateChannel,
5151
recorder: recorder,
52+
configCache: to.ByteSlicePtr([]byte{}),
5253
}
5354

5455
controller.eventQueue = NewEventQueue(controller)

pkg/controller/helpers.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (c *AppGwIngressController) updateCache(appGw *n.ApplicationGateway) {
3333
c.configCache = nil
3434
return
3535
}
36-
c.configCache = &sanitized
36+
*c.configCache = sanitized
3737
}
3838

3939
// 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)
6868
keysToDelete := []string{
6969
"sslCertificates",
7070
}
71-
var stripped []byte
72-
if stripped, err = deleteKeyFromJSON(jsonConfig, keysToDelete...); err != nil {
71+
var sanitized []byte
72+
if sanitized, err = deleteKeyFromJSON(jsonConfig, keysToDelete...); err != nil {
7373
return nil, err
7474
}
7575

7676
// Unmarshal and Marshall again with Indent so it is human readable
7777
var config interface{}
78-
_ = json.Unmarshal(stripped, &config)
78+
_ = json.Unmarshal(sanitized, &config)
7979
return json.MarshalIndent(config, "-- App Gwy config --", " ")
8080
}
8181

pkg/controller/helpers_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,10 @@ var _ = Describe("configure App Gateway", func() {
6464
})
6565

6666
Context("ensure configIsSame works as expected", func() {
67-
It("should deal with nil cache and store stuff in it", func() {
68-
c := AppGwIngressController{}
67+
It("should deal with empty cache and store stuff in it", func() {
68+
c := AppGwIngressController{
69+
configCache: to.ByteSlicePtr([]byte{}),
70+
}
6971
config := n.ApplicationGateway{
7072
ID: to.StringPtr("something"),
7173
}

pkg/sorter/http_settings.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// -------------------------------------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License. See License.txt in the project root for license information.
4+
// --------------------------------------------------------------------------------------------
5+
6+
package sorter
7+
8+
import (
9+
n "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-12-01/network"
10+
)
11+
12+
// BySettingsName is a facility to sort slices of ApplicationGatewayBackendHTTPSettings by Name
13+
type BySettingsName []n.ApplicationGatewayBackendHTTPSettings
14+
15+
func (a BySettingsName) Len() int { return len(a) }
16+
func (a BySettingsName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
17+
func (a BySettingsName) Less(i, j int) bool {
18+
return getSettingsName(a[i]) < getSettingsName(a[j])
19+
}
20+
21+
func getSettingsName(setting n.ApplicationGatewayBackendHTTPSettings) string {
22+
if setting.Name == nil {
23+
return ""
24+
}
25+
return *setting.Name
26+
}

pkg/sorter/pools.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// -------------------------------------------------------------------------------------------
2+
// Copyright (c) Microsoft Corporation. All rights reserved.
3+
// Licensed under the MIT License. See License.txt in the project root for license information.
4+
// --------------------------------------------------------------------------------------------
5+
6+
package sorter
7+
8+
import (
9+
n "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-12-01/network"
10+
)
11+
12+
// ByBackendPoolName is a facility to sort slices of ApplicationGatewayBackendAddressPool by Name
13+
type ByBackendPoolName []n.ApplicationGatewayBackendAddressPool
14+
15+
func (a ByBackendPoolName) Len() int { return len(a) }
16+
func (a ByBackendPoolName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
17+
func (a ByBackendPoolName) Less(i, j int) bool {
18+
return getPoolName(a[i]) < getPoolName(a[j])
19+
}
20+
21+
func getPoolName(pool n.ApplicationGatewayBackendAddressPool) string {
22+
if pool.Name == nil {
23+
return ""
24+
}
25+
return *pool.Name
26+
}

0 commit comments

Comments
 (0)