Repository: kubernetes-sigs/cluster-api-provider-azure
Pull request #1491 (2021-07-06 19:02:07)

set natgateway ip name in defaults
 <!-- If this is your first PR, welcome! Please make sure you read the [contributing guidelines](../CONTRIBUTING.md). -->

 <!-- Please label this pull request according to what type of issue you are addressing (see ../CONTRIBUTING.md) -->
**What type of PR is this?**
/kind cleanup
<!--
Add one of the following kinds:
/kind feature
/kind bug
/kind api-change
/kind cleanup
/kind deprecation
/kind design
/kind documentation
/kind failing-test
/kind flake
/kind other
-->

**What this PR does / why we need it**:

**Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*:
Fixes #

**Special notes for your reviewer**:

_Please confirm that if this PR changes any image versions, then that's the sole change this PR makes._

**TODOs**:
<!-- Put an "X" character inside the brackets of each completed task. Some may be optional depending on the PR. -->

- [x] squashed commits
- [ ] includes documentation
- [x] adds unit tests

**Release note**:
<!--  Write your release note:
1. Enter your extended release note in the below block. If the PR requires additional action from users switching to the new release, include the string "action required".
2. If no release note is required, just write "NONE".
-->
```release-note

```


k8s-ci-robot: @shysank: Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our [release note process](https://git.k8s.io/community/contributors/guide/release-notes.md) to remove it.

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository.
</details>
k8s-ci-robot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by:
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **detiber** after the PR has been reviewed.
You can assign the PR to them by writing `/assign @detiber` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubernetes-sigs%2Fcluster-api-provider-azure).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/kubernetes-sigs/cluster-api-provider-azure/blob/master/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["detiber"]} -->
k8s-ci-robot: @shysank: The following test **failed**, say `/retest` to rerun all failed tests:

Test name | Commit | Details | Rerun command
--- | --- | --- | ---
pull-cluster-api-provider-azure-apidiff | 12ad9474179d4eda020943ff08bee9e7ed612615 | [link](https://prow.k8s.io/view/gs/kubernetes-jenkins/pr-logs/pull/kubernetes-sigs_cluster-api-provider-azure/1491/pull-cluster-api-provider-azure-apidiff/1412487043967094784) | `/test pull-cluster-api-provider-azure-apidiff`

[Full PR test history](https://prow.k8s.io/pr-history?org=kubernetes-sigs&repo=cluster-api-provider-azure&pr=1491). [Your PR dashboard](https://prow.k8s.io/pr?query=is%3Apr%20state%3Aopen%20author%3Ashysank). Please help us cut down on flakes by [linking to](https://git.k8s.io/community/contributors/devel/sig-testing/flaky-tests.md#filing-issues-for-flaky-tests) an [open issue](https://github.com/kubernetes-sigs/cluster-api-provider-azure/issues?q=is:issue+is:open) when you hit one in your PR.

<details>

Instructions for interacting with me using PR comments are available [here](https://git.k8s.io/community/contributors/guide/pull-requests.md).  If you have questions or suggestions related to my behavior, please file an issue against the [kubernetes/test-infra](https://github.com/kubernetes/test-infra/issues/new?title=Prow%20issue:) repository. I understand the commands that are listed [here](https://go.k8s.io/bot-commands).
</details>
<!-- test report -->

Patch:
diff --git a/Tiltfile b/Tiltfile
index 5c1facda9..65088fce4 100644
--- a/Tiltfile
+++ b/Tiltfile
@@ -138,7 +138,7 @@ def capz():
     local_resource(
         "manager",
         cmd = 'mkdir -p .tiltbuild;CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags \'-extldflags "-static"\' -o .tiltbuild/manager',
-        deps = ["api", "azure", "config", "controllers", "exp", "feature", "pkg", "util", "go.mod", "go.sum", "main.go"]
+        deps = ["api", "cloud", "config", "controllers", "exp", "feature", "pkg", "go.mod", "go.sum", "main.go"]
     )
 
     dockerfile_contents = "\n".join([
diff --git a/api/v1alpha4/azurecluster_default.go b/api/v1alpha4/azurecluster_default.go
index 8969e80e4..ea2b17098 100644
--- a/api/v1alpha4/azurecluster_default.go
+++ b/api/v1alpha4/azurecluster_default.go
@@ -118,6 +118,12 @@ func (c *AzureCluster) setSubnetDefaults() {
 		nodeSubnet.RouteTable.Name = generateNodeRouteTableName(c.ObjectMeta.Name)
 	}
 
+	if nodeSubnet.NatGateway.Name != "" {
+		if nodeSubnet.NatGateway.NatGatewayIP.Name == "" {
+			nodeSubnet.NatGateway.NatGatewayIP.Name = generateNatGatewayIPName(c.ObjectMeta.Name, nodeSubnet.Name)
+		}
+	}
+
 	c.Spec.NetworkSpec.UpdateControlPlaneSubnet(cpSubnet)
 	c.Spec.NetworkSpec.UpdateNodeSubnet(nodeSubnet)
 }
@@ -356,6 +362,11 @@ func generateControlPlaneOutboundIPName(clusterName string) string {
 	return fmt.Sprintf("pip-%s-controlplane-outbound", clusterName)
 }
 
+// generateNatGatewayIPName generates a nat gateway IP name.
+func generateNatGatewayIPName(clusterName, subnetName string) string {
+	return fmt.Sprintf("pip-%s-%s-natgw", clusterName, subnetName)
+}
+
 // withIndex appends the index as suffix to a generated name.
 func withIndex(name string, n int) string {
 	return fmt.Sprintf("%s-%d", name, n)
diff --git a/azure/scope/cluster.go b/azure/scope/cluster.go
index 7589fab10..02299fa99 100644
--- a/azure/scope/cluster.go
+++ b/azure/scope/cluster.go
@@ -144,7 +144,7 @@ func (s *ClusterScope) PublicIPSpecs() []azure.PublicIPSpec {
 	nodeNatGateway := s.NodeNatGateway()
 	if nodeNatGateway.Name != "" {
 		nodeNatGatewayIPSpecs = append(nodeNatGatewayIPSpecs, azure.PublicIPSpec{
-			Name:    s.GetNodeNatGatewayIPName(),
+			Name:    nodeNatGateway.NatGatewayIP.Name,
 			DNSName: nodeNatGateway.NatGatewayIP.DNSName,
 		})
 	}
@@ -220,17 +220,6 @@ func (s *ClusterScope) RouteTableSpecs() []azure.RouteTableSpec {
 	return routetables
 }
 
-// GetNodeNatGatewayIPName returns the node nat gateway IP name if defined, or a generated name if it's not.
-func (s *ClusterScope) GetNodeNatGatewayIPName() string {
-	natGateway := s.NodeNatGateway()
-	natGatewayIPName := azure.GenerateNatGatewayIPName(s.ClusterName(), s.NodeSubnet().Name)
-	if natGateway.NatGatewayIP.Name != "" {
-		natGatewayIPName = natGateway.NatGatewayIP.Name
-	}
-
-	return natGatewayIPName
-}
-
 // NatGatewaySpecs returns the node nat gateway.
 func (s *ClusterScope) NatGatewaySpecs() []azure.NatGatewaySpec {
 	natGateways := []azure.NatGatewaySpec{}
@@ -241,7 +230,7 @@ func (s *ClusterScope) NatGatewaySpecs() []azure.NatGatewaySpec {
 		natGateways = append(natGateways, azure.NatGatewaySpec{
 			Name: natGateway.Name,
 			NatGatewayIP: infrav1.PublicIPSpec{
-				Name: s.GetNodeNatGatewayIPName(),
+				Name: natGateway.NatGatewayIP.Name,
 			},
 			Subnet: s.NodeSubnet(),
 		})
diff --git a/config/default/credentials.yaml b/config/default/credentials.yaml
index 9be5a73d2..bfd818533 100644
--- a/config/default/credentials.yaml
+++ b/config/default/credentials.yaml
@@ -5,7 +5,7 @@ metadata:
   namespace: system
 type: Opaque
 data:
-  subscription-id: ${AZURE_SUBSCRIPTION_ID_B64:=""}
-  tenant-id: ${AZURE_TENANT_ID_B64:=""}
-  client-id: ${AZURE_CLIENT_ID_B64:=""}
-  client-secret: ${AZURE_CLIENT_SECRET_B64:=""}
+  subscription-id: ${AZURE_SUBSCRIPTION_ID_B64}
+  tenant-id: ${AZURE_TENANT_ID_B64}
+  client-id: ${AZURE_CLIENT_ID_B64}
+  client-secret: ${AZURE_CLIENT_SECRET_B64}
diff --git a/templates/cluster-template-external-cloud-provider.yaml b/templates/cluster-template-external-cloud-provider.yaml
index 5a2b70058..c46eff66d 100644
--- a/templates/cluster-template-external-cloud-provider.yaml
+++ b/templates/cluster-template-external-cloud-provider.yaml
@@ -376,7 +376,7 @@ data:
           effect: NoSchedule
       containers:
         - name: cloud-controller-manager
-          image: ${AZURE_CLOUD_CONTROLLER_MANAGER_IMG:=mcr.microsoft.com/oss/kubernetes/azure-cloud-controller-manager:v1.0.1}
+          image: ${AZURE_CLOUD_CONTROLLER_MANAGER_IMG:=mcr.microsoft.com/oss/kubernetes/azure-cloud-controller-manager:v0.7.4}
           imagePullPolicy: IfNotPresent
           command: ["cloud-controller-manager"]
           args:
@@ -504,7 +504,7 @@ data:
               effect: NoSchedule
           containers:
             - name: cloud-node-manager
-              image: ${AZURE_CLOUD_NODE_MANAGER_IMG:=mcr.microsoft.com/oss/kubernetes/azure-cloud-node-manager:v1.0.1}
+              image: ${AZURE_CLOUD_NODE_MANAGER_IMG:=mcr.microsoft.com/oss/kubernetes/azure-cloud-node-manager:v0.7.4}
               imagePullPolicy: IfNotPresent
               command:
                 - cloud-node-manager
diff --git a/templates/flavors/external-cloud-provider/cloud-controller-manager.yaml b/templates/flavors/external-cloud-provider/cloud-controller-manager.yaml
index dfa1c4ac8..c4e0b7559 100644
--- a/templates/flavors/external-cloud-provider/cloud-controller-manager.yaml
+++ b/templates/flavors/external-cloud-provider/cloud-controller-manager.yaml
@@ -148,7 +148,7 @@ spec:
       effect: NoSchedule
   containers:
     - name: cloud-controller-manager
-      image: ${AZURE_CLOUD_CONTROLLER_MANAGER_IMG:=mcr.microsoft.com/oss/kubernetes/azure-cloud-controller-manager:v1.0.1}
+      image: ${AZURE_CLOUD_CONTROLLER_MANAGER_IMG:=mcr.microsoft.com/oss/kubernetes/azure-cloud-controller-manager:v0.7.4}
       imagePullPolicy: IfNotPresent
       command: ["cloud-controller-manager"]
       args:
diff --git a/templates/flavors/external-cloud-provider/cloud-node-manager.yaml b/templates/flavors/external-cloud-provider/cloud-node-manager.yaml
index 6f09c21ca..6eb476960 100644
--- a/templates/flavors/external-cloud-provider/cloud-node-manager.yaml
+++ b/templates/flavors/external-cloud-provider/cloud-node-manager.yaml
@@ -66,7 +66,7 @@ spec:
           effect: NoSchedule
       containers:
         - name: cloud-node-manager
-          image: ${AZURE_CLOUD_NODE_MANAGER_IMG:=mcr.microsoft.com/oss/kubernetes/azure-cloud-node-manager:v1.0.1}
+          image: ${AZURE_CLOUD_NODE_MANAGER_IMG:=mcr.microsoft.com/oss/kubernetes/azure-cloud-node-manager:v0.7.4}
           imagePullPolicy: IfNotPresent
           command:
             - cloud-node-manager


Test patch:
diff --git a/api/v1alpha4/azurecluster_default_test.go b/api/v1alpha4/azurecluster_default_test.go
index baba2164d..e23b76785 100644
--- a/api/v1alpha4/azurecluster_default_test.go
+++ b/api/v1alpha4/azurecluster_default_test.go
@@ -301,6 +301,7 @@ func TestSubnetDefaults(t *testing.T) {
 								Role:       SubnetNode,
 								Name:       "my-node-subnet",
 								CIDRBlocks: []string{"10.1.0.16/24"},
+								NatGateway: NatGateway{Name: "foo-natgw"},
 							},
 						},
 					},
@@ -326,6 +327,12 @@ func TestSubnetDefaults(t *testing.T) {
 								CIDRBlocks:    []string{"10.1.0.16/24"},
 								SecurityGroup: SecurityGroup{Name: "cluster-test-node-nsg"},
 								RouteTable:    RouteTable{Name: "cluster-test-node-routetable"},
+								NatGateway: NatGateway{
+									Name: "foo-natgw",
+									NatGatewayIP: PublicIPSpec{
+										Name: "pip-cluster-test-my-node-subnet-natgw",
+									},
+								},
 							},
 						},
 					},
diff --git a/templates/test/ci/cluster-template-prow-external-cloud-provider.yaml b/templates/test/ci/cluster-template-prow-external-cloud-provider.yaml
index 8c52957d8..18f91bfb5 100644
--- a/templates/test/ci/cluster-template-prow-external-cloud-provider.yaml
+++ b/templates/test/ci/cluster-template-prow-external-cloud-provider.yaml
@@ -381,7 +381,7 @@ data:
           effect: NoSchedule
       containers:
         - name: cloud-controller-manager
-          image: ${AZURE_CLOUD_CONTROLLER_MANAGER_IMG:=mcr.microsoft.com/oss/kubernetes/azure-cloud-controller-manager:v1.0.1}
+          image: ${AZURE_CLOUD_CONTROLLER_MANAGER_IMG:=mcr.microsoft.com/oss/kubernetes/azure-cloud-controller-manager:v0.7.4}
           imagePullPolicy: IfNotPresent
           command: ["cloud-controller-manager"]
           args:
@@ -509,7 +509,7 @@ data:
               effect: NoSchedule
           containers:
             - name: cloud-node-manager
-              image: ${AZURE_CLOUD_NODE_MANAGER_IMG:=mcr.microsoft.com/oss/kubernetes/azure-cloud-node-manager:v1.0.1}
+              image: ${AZURE_CLOUD_NODE_MANAGER_IMG:=mcr.microsoft.com/oss/kubernetes/azure-cloud-node-manager:v0.7.4}
               imagePullPolicy: IfNotPresent
               command:
                 - cloud-node-manager