forked from Azure/acs-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
defaults.go
273 lines (236 loc) · 10.2 KB
/
defaults.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package acsengine
import (
"fmt"
"net"
"github.com/Azure/acs-engine/pkg/api"
)
var (
//AzureCloudSpec is the default configurations for global azure.
AzureCloudSpec = AzureEnvironmentSpecConfig{
//DockerSpecConfig specify the docker engine download repo
DockerSpecConfig: DockerSpecConfig{
DockerEngineRepo: "https://aptdocker.azureedge.net/repo",
},
//KubernetesSpecConfig is the default kubernetes container image url.
KubernetesSpecConfig: KubernetesSpecConfig{
KubernetesImageBase: "gcrio.azureedge.net/google_containers/",
KubeBinariesSASURLBase: "https://acs-mirror.azureedge.net/wink8s/",
},
DCOSSpecConfig: DCOSSpecConfig{
DCOS173_BootstrapDownloadURL: fmt.Sprintf(MsecndDCOSBootstrapDownloadURL, "testing", "df308b6fc3bd91e1277baa5a3db928ae70964722"),
DCOS184_BootstrapDownloadURL: fmt.Sprintf(AzureEdgeDCOSBootstrapDownloadURL, "testing", "5b4aa43610c57ee1d60b4aa0751a1fb75824c083"),
DCOS187_BootstrapDownloadURL: fmt.Sprintf(AzureEdgeDCOSBootstrapDownloadURL, "stable", "e73ba2b1cd17795e4dcb3d6647d11a29b9c35084"),
DCOS188_BootstrapDownloadURL: fmt.Sprintf(AzureEdgeDCOSBootstrapDownloadURL, "stable", "5df43052907c021eeb5de145419a3da1898c58a5"),
DCOS190_BootstrapDownloadURL: fmt.Sprintf(AzureEdgeDCOSBootstrapDownloadURL, "stable", "58fd0833ce81b6244fc73bf65b5deb43217b0bd7"),
},
}
//AzureChinaCloudSpec is the configurations for Azure China (Mooncake)
AzureChinaCloudSpec = AzureEnvironmentSpecConfig{
//DockerSpecConfig specify the docker engine download repo
DockerSpecConfig: DockerSpecConfig{
DockerEngineRepo: "https://mirror.azure.cn/docker-engine/apt/repo/",
},
//KubernetesSpecConfig - Due to Chinese firewall issue, the default containers from google is blocked, use the Chinese local mirror instead
KubernetesSpecConfig: KubernetesSpecConfig{
KubernetesImageBase: "mirror.azure.cn:5000/google_containers/",
KubeBinariesSASURLBase: "https://acs-mirror.azureedge.net/wink8s/",
},
DCOSSpecConfig: DCOSSpecConfig{
DCOS173_BootstrapDownloadURL: fmt.Sprintf(AzureChinaCloudDCOSBootstrapDownloadURL, "df308b6fc3bd91e1277baa5a3db928ae70964722"),
DCOS184_BootstrapDownloadURL: fmt.Sprintf(AzureChinaCloudDCOSBootstrapDownloadURL, "5b4aa43610c57ee1d60b4aa0751a1fb75824c083"),
DCOS187_BootstrapDownloadURL: fmt.Sprintf(AzureChinaCloudDCOSBootstrapDownloadURL, "e73ba2b1cd17795e4dcb3d6647d11a29b9c35084"),
DCOS188_BootstrapDownloadURL: fmt.Sprintf(AzureChinaCloudDCOSBootstrapDownloadURL, "5df43052907c021eeb5de145419a3da1898c58a5"),
},
}
)
// SetPropertiesDefaults for the container Properties, returns true if certs are generated
func SetPropertiesDefaults(cs *api.ContainerService) (bool, error) {
properties := cs.Properties
setOrchestratorDefaults(cs)
setMasterNetworkDefaults(properties)
setAgentNetworkDefaults(properties)
setStorageDefaults(properties)
certsGenerated, e := setDefaultCerts(properties)
if e != nil {
return false, e
}
return certsGenerated, nil
}
// setOrchestratorDefaults for orchestrators
func setOrchestratorDefaults(cs *api.ContainerService) {
location := cs.Location
a := cs.Properties
cloudSpecConfig := GetCloudSpecConfig(location)
if a.OrchestratorProfile.OrchestratorType == api.Kubernetes {
if a.OrchestratorProfile.KubernetesConfig == nil {
a.OrchestratorProfile.KubernetesConfig = &api.KubernetesConfig{}
}
a.OrchestratorProfile.KubernetesConfig.KubernetesImageBase = cloudSpecConfig.KubernetesSpecConfig.KubernetesImageBase
if a.OrchestratorProfile.KubernetesConfig.NetworkPolicy == "" {
a.OrchestratorProfile.KubernetesConfig.NetworkPolicy = DefaultNetworkPolicy
}
if a.OrchestratorProfile.KubernetesConfig.ClusterSubnet == "" {
if a.OrchestratorProfile.IsVNETIntegrated() {
// When VNET integration is enabled, all masters, agents and pods share the same large subnet.
a.OrchestratorProfile.KubernetesConfig.ClusterSubnet = DefaultKubernetesSubnet
} else {
a.OrchestratorProfile.KubernetesConfig.ClusterSubnet = DefaultKubernetesClusterSubnet
}
}
}
}
// SetMasterNetworkDefaults for masters
func setMasterNetworkDefaults(a *api.Properties) {
if !a.MasterProfile.IsCustomVNET() {
if a.OrchestratorProfile.OrchestratorType == api.Kubernetes {
if a.OrchestratorProfile.IsVNETIntegrated() {
// When VNET integration is enabled, all masters, agents and pods share the same large subnet.
a.MasterProfile.Subnet = a.OrchestratorProfile.KubernetesConfig.ClusterSubnet
a.MasterProfile.FirstConsecutiveStaticIP = getFirstConsecutiveStaticIPAddress(a.MasterProfile.Subnet)
} else {
a.MasterProfile.Subnet = DefaultKubernetesMasterSubnet
a.MasterProfile.FirstConsecutiveStaticIP = DefaultFirstConsecutiveKubernetesStaticIP
}
} else if a.HasWindows() {
a.MasterProfile.Subnet = DefaultSwarmWindowsMasterSubnet
a.MasterProfile.FirstConsecutiveStaticIP = DefaultSwarmWindowsFirstConsecutiveStaticIP
} else {
a.MasterProfile.Subnet = DefaultMasterSubnet
a.MasterProfile.FirstConsecutiveStaticIP = DefaultFirstConsecutiveStaticIP
}
}
// Allocate IP addresses for containers if VNET integration is enabled.
// A custom count specified by the user overrides this value.
if a.MasterProfile.IPAddressCount == 0 {
if a.OrchestratorProfile.IsVNETIntegrated() {
a.MasterProfile.IPAddressCount = DefaultAgentMultiIPAddressCount
} else {
a.MasterProfile.IPAddressCount = DefaultAgentIPAddressCount
}
}
}
// SetAgentNetworkDefaults for agents
func setAgentNetworkDefaults(a *api.Properties) {
// configure the subnets if not in custom VNET
if !a.MasterProfile.IsCustomVNET() {
subnetCounter := 0
for _, profile := range a.AgentPoolProfiles {
if a.OrchestratorProfile.OrchestratorType == api.Kubernetes {
profile.Subnet = a.MasterProfile.Subnet
} else {
profile.Subnet = fmt.Sprintf(DefaultAgentSubnetTemplate, subnetCounter)
}
subnetCounter++
}
}
for _, profile := range a.AgentPoolProfiles {
// set default OSType to Linux
if profile.OSType == "" {
profile.OSType = api.Linux
}
// Allocate IP addresses for containers if VNET integration is enabled.
// A custom count specified by the user overrides this value.
if profile.IPAddressCount == 0 {
if a.OrchestratorProfile.IsVNETIntegrated() {
profile.IPAddressCount = DefaultAgentMultiIPAddressCount
} else {
profile.IPAddressCount = DefaultAgentIPAddressCount
}
}
}
}
// setStorageDefaults for agents
func setStorageDefaults(a *api.Properties) {
for _, profile := range a.AgentPoolProfiles {
if len(profile.StorageProfile) == 0 {
profile.StorageProfile = api.StorageAccount
}
if len(profile.AvailabilityProfile) == 0 {
profile.AvailabilityProfile = api.VirtualMachineScaleSets
}
}
}
func setDefaultCerts(a *api.Properties) (bool, error) {
if !certGenerationRequired(a) {
return false, nil
}
masterExtraFQDNs := FormatAzureProdFQDNs(a.MasterProfile.DNSPrefix)
firstMasterIP := net.ParseIP(a.MasterProfile.FirstConsecutiveStaticIP).To4()
if firstMasterIP == nil {
return false, fmt.Errorf("MasterProfile.FirstConsecutiveStaticIP '%s' is an invalid IP address", a.MasterProfile.FirstConsecutiveStaticIP)
}
ips := []net.IP{firstMasterIP}
// Add the Internal Loadbalancer IP which is always at at a known offset from the firstMasterIP
ips = append(ips, net.IP{firstMasterIP[0], firstMasterIP[1], firstMasterIP[2], firstMasterIP[3] + byte(DefaultInternalLbStaticIPOffset)})
// Include the Internal load balancer as well
for i := 1; i < a.MasterProfile.Count; i++ {
ip := net.IP{firstMasterIP[0], firstMasterIP[1], firstMasterIP[2], firstMasterIP[3] + byte(i)}
ips = append(ips, ip)
}
if a.CertificateProfile == nil {
a.CertificateProfile = &api.CertificateProfile{}
}
// use the specified Certificate Authority pair, or generate a new pair
var caPair *PkiKeyCertPair
if len(a.CertificateProfile.CaCertificate) != 0 && len(a.CertificateProfile.GetCAPrivateKey()) != 0 {
caPair = &PkiKeyCertPair{CertificatePem: a.CertificateProfile.CaCertificate, PrivateKeyPem: a.CertificateProfile.GetCAPrivateKey()}
} else {
caCertificate, caPrivateKey, err := createCertificate("ca", nil, nil, false, nil, nil)
if err != nil {
return false, err
}
caPair = &PkiKeyCertPair{CertificatePem: string(certificateToPem(caCertificate.Raw)), PrivateKeyPem: string(privateKeyToPem(caPrivateKey))}
a.CertificateProfile.CaCertificate = caPair.CertificatePem
a.CertificateProfile.SetCAPrivateKey(caPair.PrivateKeyPem)
}
apiServerPair, clientPair, kubeConfigPair, err := CreatePki(masterExtraFQDNs, ips, DefaultKubernetesClusterDomain, caPair)
if err != nil {
return false, err
}
a.CertificateProfile.APIServerCertificate = apiServerPair.CertificatePem
a.CertificateProfile.APIServerPrivateKey = apiServerPair.PrivateKeyPem
a.CertificateProfile.ClientCertificate = clientPair.CertificatePem
a.CertificateProfile.ClientPrivateKey = clientPair.PrivateKeyPem
a.CertificateProfile.KubeConfigCertificate = kubeConfigPair.CertificatePem
a.CertificateProfile.KubeConfigPrivateKey = kubeConfigPair.PrivateKeyPem
return true, nil
}
func certGenerationRequired(a *api.Properties) bool {
if a.CertificateProfile != nil &&
(len(a.CertificateProfile.APIServerCertificate) > 0 || len(a.CertificateProfile.APIServerPrivateKey) > 0 ||
len(a.CertificateProfile.ClientCertificate) > 0 || len(a.CertificateProfile.ClientPrivateKey) > 0) {
return false
}
switch a.OrchestratorProfile.OrchestratorType {
case api.DCOS:
return false
case api.Swarm:
return false
case api.SwarmMode:
return false
case api.Kubernetes:
return true
default:
return false
}
}
// getFirstConsecutiveStaticIPAddress returns the first static IP address of the given subnet.
func getFirstConsecutiveStaticIPAddress(subnetStr string) string {
_, subnet, err := net.ParseCIDR(subnetStr)
if err != nil {
return DefaultFirstConsecutiveKubernetesStaticIP
}
// Round up the prefix length to the nearest octet boundary.
ones, bits := subnet.Mask.Size()
if ones%8 != 0 {
ones += 8 - ones%8
}
// Fill the intermediate octets with 1s and last octet with offset. This is done so to match
// the existing behavior of allocating static IP addresses from the last /24 of the subnet.
lastOctet := bits/8 - 1
for i := ones / 8; i < lastOctet; i++ {
subnet.IP[i] = 255
}
subnet.IP[lastOctet] = DefaultKubernetesFirstConsecutiveStaticIPOffset
return subnet.IP.String()
}