Skip to content

Commit

Permalink
Change diffBackends for NEG to not compare the API versions.
Browse files Browse the repository at this point in the history
  • Loading branch information
gauravkghildiyal committed Mar 31, 2023
1 parent 58f3b96 commit 0161032
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
24 changes: 19 additions & 5 deletions pkg/backends/neg_linker.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,15 @@ func diffBackends(old, new []*composite.Backend) *backendDiff {

oldMap := map[string]*composite.Backend{}
for _, be := range old {
d.old.Insert(be.Group)
oldMap[be.Group] = be
beGroup := relativeResourceNameWithDefault(be.Group)
d.old.Insert(beGroup)
oldMap[beGroup] = be
}
for _, be := range new {
d.new.Insert(be.Group)
beGroup := relativeResourceNameWithDefault(be.Group)
d.new.Insert(beGroup)

if oldBe, ok := oldMap[be.Group]; ok {
if oldBe, ok := oldMap[beGroup]; ok {
// Note: if you are comparing a value that has a non-zero default
// value (e.g. CapacityScaler is 1.0), you will need to set that
// value when creating a new Backend to avoid a false positive when
Expand All @@ -183,7 +185,7 @@ func diffBackends(old, new []*composite.Backend) *backendDiff {
changed = changed || oldBe.MaxRatePerEndpoint != be.MaxRatePerEndpoint
changed = changed || oldBe.CapacityScaler != be.CapacityScaler
if changed {
d.changed.Insert(be.Group)
d.changed.Insert(beGroup)
}
}
}
Expand Down Expand Up @@ -264,3 +266,15 @@ func getNegUrlFromSvcneg(key string, zone string, svcNegLister cache.Indexer) (s
}
return "", false
}

// relativeResourceNameWithDefault will attempt to return a RelativeResourceName
// for the provided `selfLink`. In case of a faiure, it will return the
// `selfLink` itself.
func relativeResourceNameWithDefault(selfLink string) string {
result, err := utils.RelativeResourceName(selfLink)
if err != nil {
klog.Warning("Unable to parse resource name from provided selfLink %q", selfLink)
return selfLink
}
return result
}
12 changes: 12 additions & 0 deletions pkg/backends/neg_linker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,18 @@ func TestDiffBackends(t *testing.T) {
new: []*composite.Backend{{Group: "b"}, {Group: "a"}},
isEqual: true,
},
{
name: "same but different api version",
old: []*composite.Backend{
{Group: "https://www.googleapis.com/compute/beta/projects/project-name/zones/us-central1-c/networkEndpointGroups/k8s1-neg-name-1"},
{Group: "https://www.googleapis.com/compute/v1/projects/project-name/zones/us-central1-c/networkEndpointGroups/k8s1-neg-name-2"},
},
new: []*composite.Backend{
{Group: "https://www.googleapis.com/compute/v1/projects/project-name/zones/us-central1-c/networkEndpointGroups/k8s1-neg-name-1"},
{Group: "https://www.googleapis.com/compute/beta/projects/project-name/zones/us-central1-c/networkEndpointGroups/k8s1-neg-name-2"},
},
isEqual: true,
},
{
name: "add backend",
old: []*composite.Backend{{Group: "a"}},
Expand Down

0 comments on commit 0161032

Please sign in to comment.