|
| 1 | +/* |
| 2 | +Copyright 2020 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package v1alpha3 |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | +) |
| 22 | + |
| 23 | +const ( |
| 24 | + // DefaultVnetCIDR is the default Vnet CIDR |
| 25 | + DefaultVnetCIDR = "10.0.0.0/8" |
| 26 | + // DefaultControlPlaneSubnetCIDR is the default Control Plane Subnet CIDR |
| 27 | + DefaultControlPlaneSubnetCIDR = "10.0.0.0/16" |
| 28 | + // DefaultNodeSubnetCIDR is the default Node Subnet CIDR |
| 29 | + DefaultNodeSubnetCIDR = "10.1.0.0/16" |
| 30 | +) |
| 31 | + |
| 32 | +func (c *AzureCluster) setDefaults() { |
| 33 | + c.setNetworkSpecDefaults() |
| 34 | +} |
| 35 | + |
| 36 | +func (c *AzureCluster) setNetworkSpecDefaults() { |
| 37 | + c.setVnetDefaults() |
| 38 | + c.setSubnetDefaults() |
| 39 | +} |
| 40 | + |
| 41 | +func (c *AzureCluster) setVnetDefaults() { |
| 42 | + if c.Spec.NetworkSpec.Vnet.ResourceGroup == "" { |
| 43 | + c.Spec.NetworkSpec.Vnet.ResourceGroup = c.Spec.ResourceGroup |
| 44 | + } |
| 45 | + if c.Spec.NetworkSpec.Vnet.Name == "" { |
| 46 | + c.Spec.NetworkSpec.Vnet.Name = generateVnetName(c.ObjectMeta.Name) |
| 47 | + } |
| 48 | + if c.Spec.NetworkSpec.Vnet.CidrBlock == "" { |
| 49 | + c.Spec.NetworkSpec.Vnet.CidrBlock = DefaultVnetCIDR |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func (c *AzureCluster) setSubnetDefaults() { |
| 54 | + cpSubnet := c.Spec.NetworkSpec.GetControlPlaneSubnet() |
| 55 | + if cpSubnet == nil { |
| 56 | + cpSubnet = &SubnetSpec{Role: SubnetControlPlane} |
| 57 | + c.Spec.NetworkSpec.Subnets = append(c.Spec.NetworkSpec.Subnets, cpSubnet) |
| 58 | + } |
| 59 | + |
| 60 | + nodeSubnet := c.Spec.NetworkSpec.GetNodeSubnet() |
| 61 | + if nodeSubnet == nil { |
| 62 | + nodeSubnet = &SubnetSpec{Role: SubnetNode} |
| 63 | + c.Spec.NetworkSpec.Subnets = append(c.Spec.NetworkSpec.Subnets, nodeSubnet) |
| 64 | + } |
| 65 | + |
| 66 | + if cpSubnet.Name == "" { |
| 67 | + cpSubnet.Name = generateControlPlaneSubnetName(c.ObjectMeta.Name) |
| 68 | + } |
| 69 | + if cpSubnet.CidrBlock == "" { |
| 70 | + cpSubnet.CidrBlock = DefaultControlPlaneSubnetCIDR |
| 71 | + } |
| 72 | + if cpSubnet.SecurityGroup.Name == "" { |
| 73 | + cpSubnet.SecurityGroup.Name = generateControlPlaneSecurityGroupName(c.ObjectMeta.Name) |
| 74 | + } |
| 75 | + if cpSubnet.RouteTable.Name == "" { |
| 76 | + cpSubnet.RouteTable.Name = generateRouteTableName(c.ObjectMeta.Name) |
| 77 | + } |
| 78 | + |
| 79 | + if nodeSubnet.Name == "" { |
| 80 | + nodeSubnet.Name = generateNodeSubnetName(c.ObjectMeta.Name) |
| 81 | + } |
| 82 | + if nodeSubnet.CidrBlock == "" { |
| 83 | + nodeSubnet.CidrBlock = DefaultNodeSubnetCIDR |
| 84 | + } |
| 85 | + if nodeSubnet.SecurityGroup.Name == "" { |
| 86 | + nodeSubnet.SecurityGroup.Name = generateNodeSecurityGroupName(c.ObjectMeta.Name) |
| 87 | + } |
| 88 | + if nodeSubnet.RouteTable.Name == "" { |
| 89 | + nodeSubnet.RouteTable.Name = generateRouteTableName(c.ObjectMeta.Name) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +// generateVnetName generates a virtual network name, based on the cluster name. |
| 94 | +func generateVnetName(clusterName string) string { |
| 95 | + return fmt.Sprintf("%s-%s", clusterName, "vnet") |
| 96 | +} |
| 97 | + |
| 98 | +// generateControlPlaneSubnetName generates a node subnet name, based on the cluster name. |
| 99 | +func generateControlPlaneSubnetName(clusterName string) string { |
| 100 | + return fmt.Sprintf("%s-%s", clusterName, "controlplane-subnet") |
| 101 | +} |
| 102 | + |
| 103 | +// generateNodeSubnetName generates a node subnet name, based on the cluster name. |
| 104 | +func generateNodeSubnetName(clusterName string) string { |
| 105 | + return fmt.Sprintf("%s-%s", clusterName, "node-subnet") |
| 106 | +} |
| 107 | + |
| 108 | +// generateControlPlaneSecurityGroupName generates a control plane security group name, based on the cluster name. |
| 109 | +func generateControlPlaneSecurityGroupName(clusterName string) string { |
| 110 | + return fmt.Sprintf("%s-%s", clusterName, "controlplane-nsg") |
| 111 | +} |
| 112 | + |
| 113 | +// generateNodeSecurityGroupName generates a node security group name, based on the cluster name. |
| 114 | +func generateNodeSecurityGroupName(clusterName string) string { |
| 115 | + return fmt.Sprintf("%s-%s", clusterName, "node-nsg") |
| 116 | +} |
| 117 | + |
| 118 | +// generateRouteTableName generates a route table name, based on the cluster name. |
| 119 | +func generateRouteTableName(clusterName string) string { |
| 120 | + return fmt.Sprintf("%s-%s", clusterName, "node-routetable") |
| 121 | +} |
0 commit comments