diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/BUILD b/staging/src/k8s.io/legacy-cloud-providers/azure/BUILD index 11fc4b92d13..14ba5e38c44 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/BUILD +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/BUILD @@ -100,6 +100,7 @@ go_test( "//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/types:go_default_library", "//staging/src/k8s.io/apimachinery/pkg/util/sets:go_default_library", + "//staging/src/k8s.io/client-go/tools/record:go_default_library", "//staging/src/k8s.io/cloud-provider:go_default_library", "//staging/src/k8s.io/cloud-provider/service/helpers:go_default_library", "//staging/src/k8s.io/legacy-cloud-providers/azure/auth:go_default_library", diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_cache.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_cache.go index fd69eadbff2..ac959515a84 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_cache.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_cache.go @@ -122,3 +122,12 @@ func (t *timedCache) Delete(key string) error { key: key, }) } + +// Set sets the data cache for the key. +// It is only used for testing. +func (t *timedCache) Set(key string, data interface{}) { + t.store.Add(&cacheEntry{ + key: key, + data: data, + }) +} diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_fakes.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_fakes.go index 17bbb8f99ac..2ce921be1bb 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_fakes.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_fakes.go @@ -36,6 +36,10 @@ import ( "github.com/Azure/go-autorest/autorest/to" ) +var ( + errPreconditionFailedEtagMismatch = fmt.Errorf("PreconditionFailedEtagMismatch") +) + type fakeAzureLBClient struct { mutex *sync.Mutex FakeStore map[string]map[string]network.LoadBalancer @@ -417,13 +421,21 @@ func newFakeAzureNSGClient() *fakeAzureNSGClient { return fNSG } -func (fNSG *fakeAzureNSGClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, networkSecurityGroupName string, parameters network.SecurityGroup) (resp *http.Response, err error) { +func (fNSG *fakeAzureNSGClient) CreateOrUpdate(ctx context.Context, resourceGroupName string, networkSecurityGroupName string, parameters network.SecurityGroup, etag string) (resp *http.Response, err error) { fNSG.mutex.Lock() defer fNSG.mutex.Unlock() if _, ok := fNSG.FakeStore[resourceGroupName]; !ok { fNSG.FakeStore[resourceGroupName] = make(map[string]network.SecurityGroup) } + + if nsg, ok := fNSG.FakeStore[resourceGroupName][networkSecurityGroupName]; ok { + if etag != "" && to.String(nsg.Etag) != "" && etag != to.String(nsg.Etag) { + return &http.Response{ + StatusCode: http.StatusPreconditionFailed, + }, errPreconditionFailedEtagMismatch + } + } fNSG.FakeStore[resourceGroupName][networkSecurityGroupName] = parameters return nil, nil diff --git a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_test.go b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_test.go index aae754303bb..efcfbad5602 100644 --- a/staging/src/k8s.io/legacy-cloud-providers/azure/azure_test.go +++ b/staging/src/k8s.io/legacy-cloud-providers/azure/azure_test.go @@ -30,6 +30,7 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" "k8s.io/apimachinery/pkg/util/sets" + "k8s.io/client-go/tools/record" servicehelpers "k8s.io/cloud-provider/service/helpers" "k8s.io/legacy-cloud-providers/azure/auth" @@ -855,6 +856,25 @@ func TestReconcileSecurityWithSourceRanges(t *testing.T) { validateSecurityGroup(t, sg, svc) } +func TestReconcileSecurityGroupEtagMismatch(t *testing.T) { + az := getTestCloud() + + sg := getTestSecurityGroup(az) + cachedSG := *sg + cachedSG.Etag = to.StringPtr("1111111-0000-0000-0000-000000000000") + az.nsgCache.Set(to.String(sg.Name), &cachedSG) + + svc1 := getTestService("servicea", v1.ProtocolTCP, 80) + clusterResources := getClusterResources(az, 1, 1) + lb, _ := az.reconcileLoadBalancer(testClusterName, &svc1, clusterResources.nodes, true) + lbStatus, _ := az.getServiceLoadBalancerStatus(&svc1, lb) + + newSG, err := az.reconcileSecurityGroup(testClusterName, &svc1, &lbStatus.Ingress[0].IP, true /* wantLb */) + assert.Nil(t, newSG) + assert.NotNil(t, err) + assert.Equal(t, err, errPreconditionFailedEtagMismatch) +} + func TestReconcilePublicIPWithNewService(t *testing.T) { az := getTestCloud() svc := getTestService("servicea", v1.ProtocolTCP, 80, 443) @@ -958,6 +978,7 @@ func getTestCloud() (az *Cloud) { nodeResourceGroups: map[string]string{}, unmanagedNodes: sets.NewString(), routeCIDRs: map[string]string{}, + eventRecorder: &record.FakeRecorder{}, } az.DisksClient = newFakeDisksClient() az.InterfacesClient = newFakeAzureInterfacesClient() @@ -1186,6 +1207,7 @@ func getTestSecurityGroup(az *Cloud, services ...v1.Service) *network.SecurityGr sg := network.SecurityGroup{ Name: &az.SecurityGroupName, + Etag: to.StringPtr("0000000-0000-0000-0000-000000000000"), SecurityGroupPropertiesFormat: &network.SecurityGroupPropertiesFormat{ SecurityRules: &rules, }, @@ -1197,7 +1219,8 @@ func getTestSecurityGroup(az *Cloud, services ...v1.Service) *network.SecurityGr ctx, az.ResourceGroup, az.SecurityGroupName, - sg) + sg, + "") return &sg }