Skip to content

Commit ab58834

Browse files
authored
Merge pull request #3805 from nawazkh/fix_authIP_reconcile_1.10
[release-1.10] fix AuthorizedIPRanges disable -> enable bug
2 parents a5f7214 + 934e1bd commit ab58834

File tree

2 files changed

+92
-1
lines changed

2 files changed

+92
-1
lines changed

azure/services/managedclusters/spec.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/base64"
2222
"fmt"
2323
"net"
24+
"reflect"
2425
"time"
2526

2627
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2022-03-01/containerservice"
@@ -266,6 +267,9 @@ func buildAutoScalerProfile(autoScalerProfile *AutoScalerProfile) *containerserv
266267
}
267268

268269
// Parameters returns the parameters for the managed clusters.
270+
// TODO: remove nolint after refactoring this function
271+
//
272+
//nolint:gocyclo // Function requires a lot of nil checks that raise complexity.
269273
func (s *ManagedClusterSpec) Parameters(ctx context.Context, existing interface{}) (params interface{}, err error) {
270274
ctx, log, done := tele.StartSpanWithLogger(ctx, "managedclusters.Service.Parameters")
271275
defer done()
@@ -377,7 +381,7 @@ func (s *ManagedClusterSpec) Parameters(ctx context.Context, existing interface{
377381
EnablePrivateClusterPublicFQDN: s.APIServerAccessProfile.EnablePrivateClusterPublicFQDN,
378382
}
379383

380-
if len(s.APIServerAccessProfile.AuthorizedIPRanges) > 0 {
384+
if s.APIServerAccessProfile.AuthorizedIPRanges != nil {
381385
managedCluster.APIServerAccessProfile.AuthorizedIPRanges = &s.APIServerAccessProfile.AuthorizedIPRanges
382386
}
383387
}
@@ -427,6 +431,16 @@ func (s *ManagedClusterSpec) Parameters(ctx context.Context, existing interface{
427431
// AgentPool changes are managed through AMMP.
428432
managedCluster.AgentPoolProfiles = existingMC.AgentPoolProfiles
429433

434+
// if the AuthorizedIPRanges is nil in the user-updated spec, but not nil in the existing spec, then
435+
// we need to set the AuthorizedIPRanges to empty array (&[]string{}) once so that the Azure API will
436+
// update the existing authorized IP ranges to nil.
437+
if !isAuthIPRangesNilOrEmpty(existingMC) && isAuthIPRangesNilOrEmpty(managedCluster) {
438+
log.V(4).Info("managed cluster spec has nil AuthorizedIPRanges, updating existing authorized IP ranges to an empty list")
439+
managedCluster.APIServerAccessProfile = &containerservice.ManagedClusterAPIServerAccessProfile{
440+
AuthorizedIPRanges: &[]string{},
441+
}
442+
}
443+
430444
diff := computeDiffOfNormalizedClusters(managedCluster, existingMC)
431445
if diff == "" {
432446
log.V(4).Info("no changes found between user-updated spec and existing spec")
@@ -662,3 +676,11 @@ func getIdentity(identity *infrav1.Identity) (managedClusterIdentity *containers
662676
}
663677
return
664678
}
679+
680+
// isAuthIPRangesNilOrEmpty returns true if the managed cluster's APIServerAccessProfile or AuthorizedIPRanges is nil or if AuthorizedIPRanges is empty.
681+
func isAuthIPRangesNilOrEmpty(managedCluster containerservice.ManagedCluster) bool {
682+
if managedCluster.APIServerAccessProfile == nil || managedCluster.APIServerAccessProfile.AuthorizedIPRanges == nil || reflect.DeepEqual(managedCluster.APIServerAccessProfile.AuthorizedIPRanges, &[]string{}) {
683+
return true
684+
}
685+
return false
686+
}

azure/services/managedclusters/spec_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,67 @@ func TestParameters(t *testing.T) {
186186
g.Expect(result).To(BeNil())
187187
},
188188
},
189+
{
190+
name: "update authorized IP ranges with empty struct if spec does not have authorized IP ranges but existing cluster has authorized IP ranges",
191+
existing: getExistingClusterWithAuthorizedIPRanges(),
192+
spec: &ManagedClusterSpec{
193+
Name: "test-managedcluster",
194+
ResourceGroup: "test-rg",
195+
Location: "test-location",
196+
Tags: map[string]string{
197+
"test-tag": "test-value",
198+
},
199+
Version: "v1.22.0",
200+
LoadBalancerSKU: "Standard",
201+
},
202+
expect: func(g *WithT, result interface{}) {
203+
g.Expect(result).To(BeAssignableToTypeOf(containerservice.ManagedCluster{}))
204+
g.Expect(result.(containerservice.ManagedCluster).APIServerAccessProfile).To(Not(BeNil()))
205+
g.Expect(result.(containerservice.ManagedCluster).APIServerAccessProfile.AuthorizedIPRanges).To(Equal(&[]string{}))
206+
},
207+
},
208+
{
209+
name: "update authorized IP ranges with authorized IPs spec has authorized IP ranges but existing cluster does not have authorized IP ranges",
210+
existing: getExistingCluster(),
211+
spec: &ManagedClusterSpec{
212+
Name: "test-managedcluster",
213+
ResourceGroup: "test-rg",
214+
Location: "test-location",
215+
Tags: map[string]string{
216+
"test-tag": "test-value",
217+
},
218+
Version: "v1.22.0",
219+
LoadBalancerSKU: "Standard",
220+
APIServerAccessProfile: &APIServerAccessProfile{
221+
AuthorizedIPRanges: []string{"192.168.0.1/32, 192.168.0.2/32, 192.168.0.3/32"},
222+
},
223+
},
224+
expect: func(g *WithT, result interface{}) {
225+
g.Expect(result).To(BeAssignableToTypeOf(containerservice.ManagedCluster{}))
226+
g.Expect(result.(containerservice.ManagedCluster).APIServerAccessProfile).To(Not(BeNil()))
227+
g.Expect(result.(containerservice.ManagedCluster).APIServerAccessProfile.AuthorizedIPRanges).To(Equal(&[]string{"192.168.0.1/32, 192.168.0.2/32, 192.168.0.3/32"}))
228+
},
229+
},
230+
{
231+
name: "no update needed when authorized IP ranges when both clusters have the same authorized IP ranges",
232+
existing: getExistingClusterWithAuthorizedIPRanges(),
233+
spec: &ManagedClusterSpec{
234+
Name: "test-managedcluster",
235+
ResourceGroup: "test-rg",
236+
Location: "test-location",
237+
Tags: map[string]string{
238+
"test-tag": "test-value",
239+
},
240+
Version: "v1.22.0",
241+
LoadBalancerSKU: "Standard",
242+
APIServerAccessProfile: &APIServerAccessProfile{
243+
AuthorizedIPRanges: []string{"192.168.0.1/32, 192.168.0.2/32, 192.168.0.3/32"},
244+
},
245+
},
246+
expect: func(g *WithT, result interface{}) {
247+
g.Expect(result).To(BeNil())
248+
},
249+
},
189250
}
190251
for _, tc := range testcases {
191252
tc := tc
@@ -336,3 +397,11 @@ func getSampleManagedCluster() containerservice.ManagedCluster {
336397
})),
337398
}
338399
}
400+
401+
func getExistingClusterWithAuthorizedIPRanges() containerservice.ManagedCluster {
402+
mc := getExistingCluster()
403+
mc.APIServerAccessProfile = &containerservice.ManagedClusterAPIServerAccessProfile{
404+
AuthorizedIPRanges: &[]string{"192.168.0.1/32, 192.168.0.2/32, 192.168.0.3/32"},
405+
}
406+
return mc
407+
}

0 commit comments

Comments
 (0)