-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
defaults.go
260 lines (228 loc) · 8.26 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
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package cloudup
import (
"context"
"fmt"
"net"
"strings"
"k8s.io/klog/v2"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/azure"
"k8s.io/kops/upup/pkg/fi/cloudup/gce"
"k8s.io/kops/util/pkg/vfs"
kopsversion "k8s.io/kops"
)
// PerformAssignments populates values that are required and immutable
// For example, it assigns stable Keys to InstanceGroups & Masters, and
// it assigns CIDRs to subnets
// We also assign KubernetesVersion, because we want it to be explicit
//
// PerformAssignments is called on create, as well as an update. In fact
// any time Run() is called in apply_cluster.go we will reach this function.
// Please do all after-market logic here.
//
func PerformAssignments(c *kops.Cluster, cloud fi.Cloud) error {
ctx := context.TODO()
// Topology support
// TODO Kris: Unsure if this needs to be here, or if the API conversion code will handle it
if c.Spec.Topology == nil {
c.Spec.Topology = &kops.TopologySpec{Masters: kops.TopologyPublic, Nodes: kops.TopologyPublic}
}
if cloud == nil {
return fmt.Errorf("cloud cannot be nil")
}
if cloud.ProviderID() == kops.CloudProviderGCE {
if err := gce.PerformNetworkAssignments(ctx, c, cloud); err != nil {
return err
}
}
setNetworkCIDR := (cloud.ProviderID() == kops.CloudProviderAWS) || (cloud.ProviderID() == kops.CloudProviderALI) || (cloud.ProviderID() == kops.CloudProviderAzure)
if setNetworkCIDR && c.Spec.NetworkCIDR == "" {
if c.SharedVPC() {
var vpcInfo *fi.VPCInfo
var err error
if cloud.ProviderID() == kops.CloudProviderAzure {
if c.Spec.CloudConfig == nil || c.Spec.CloudConfig.Azure == nil || c.Spec.CloudConfig.Azure.ResourceGroupName == "" {
return fmt.Errorf("missing required --azure-resource-group-name when specifying Network ID")
}
vpcInfo, err = cloud.(azure.AzureCloud).FindVNetInfo(c.Spec.NetworkID, c.Spec.CloudConfig.Azure.ResourceGroupName)
if err != nil {
return err
}
} else {
vpcInfo, err = cloud.FindVPCInfo(c.Spec.NetworkID)
if err != nil {
return err
}
}
if vpcInfo == nil {
return fmt.Errorf("unable to find Network ID %q", c.Spec.NetworkID)
}
c.Spec.NetworkCIDR = vpcInfo.CIDR
if c.Spec.NetworkCIDR == "" {
return fmt.Errorf("unable to infer NetworkCIDR from Network ID, please specify --network-cidr")
}
} else {
if cloud.ProviderID() == kops.CloudProviderAWS {
// TODO: Choose non-overlapping networking CIDRs for VPCs, using vpcInfo
c.Spec.NetworkCIDR = "172.20.0.0/16"
} else if cloud.ProviderID() == kops.CloudProviderALI {
c.Spec.NetworkCIDR = "192.168.0.0/16"
}
}
// Amazon VPC CNI uses the same network
if c.Spec.Networking != nil && c.Spec.Networking.AmazonVPC != nil {
c.Spec.NonMasqueradeCIDR = c.Spec.NetworkCIDR
}
}
if c.Spec.NonMasqueradeCIDR == "" {
if c.Spec.Networking != nil && c.Spec.Networking.GCE != nil {
// Don't set NonMasqueradeCIDR
} else {
c.Spec.NonMasqueradeCIDR = "100.64.0.0/10"
}
}
// TODO: Unclear this should be here - it isn't too hard to change
if c.Spec.MasterPublicName == "" && c.ObjectMeta.Name != "" {
c.Spec.MasterPublicName = "api." + c.ObjectMeta.Name
}
// We only assign subnet CIDRs on AWS, OpenStack, Ali, and Azure.
pd := cloud.ProviderID()
if pd == kops.CloudProviderAWS || pd == kops.CloudProviderOpenstack || pd == kops.CloudProviderALI || pd == kops.CloudProviderAzure {
// TODO: Use vpcInfo
err := assignCIDRsToSubnets(c, cloud)
if err != nil {
return err
}
}
proxy, err := assignProxy(c)
if err != nil {
return err
}
c.Spec.EgressProxy = proxy
return ensureKubernetesVersion(c)
}
// ensureKubernetesVersion populates KubernetesVersion, if it is not already set
// It will be populated with the latest stable kubernetes version, or the version from the channel
func ensureKubernetesVersion(c *kops.Cluster) error {
if c.Spec.KubernetesVersion == "" {
if c.Spec.Channel != "" {
channel, err := kops.LoadChannel(c.Spec.Channel)
if err != nil {
return err
}
kubernetesVersion := kops.RecommendedKubernetesVersion(channel, kopsversion.Version)
if kubernetesVersion != nil {
c.Spec.KubernetesVersion = kubernetesVersion.String()
klog.Infof("Using KubernetesVersion %q from channel %q", c.Spec.KubernetesVersion, c.Spec.Channel)
} else {
klog.Warningf("Cannot determine recommended kubernetes version from channel %q", c.Spec.Channel)
}
} else {
klog.Warningf("Channel is not set; cannot determine KubernetesVersion from channel")
}
}
if c.Spec.KubernetesVersion == "" {
latestVersion, err := FindLatestKubernetesVersion()
if err != nil {
return err
}
klog.Infof("Using kubernetes latest stable version: %s", latestVersion)
c.Spec.KubernetesVersion = latestVersion
}
return nil
}
// FindLatestKubernetesVersion returns the latest kubernetes version,
// as stored at https://storage.googleapis.com/kubernetes-release/release/stable.txt
// This shouldn't be used any more; we prefer reading the stable channel
func FindLatestKubernetesVersion() (string, error) {
stableURL := "https://storage.googleapis.com/kubernetes-release/release/stable.txt"
klog.Warningf("Loading latest kubernetes version from %q", stableURL)
b, err := vfs.Context.ReadFile(stableURL)
if err != nil {
return "", fmt.Errorf("KubernetesVersion not specified, and unable to download latest version from %q: %v", stableURL, err)
}
latestVersion := strings.TrimSpace(string(b))
return latestVersion, nil
}
func assignProxy(cluster *kops.Cluster) (*kops.EgressProxySpec, error) {
egressProxy := cluster.Spec.EgressProxy
// Add default no_proxy values if we are using a http proxy
if egressProxy != nil {
var egressSlice []string
if egressProxy.ProxyExcludes != "" {
egressSlice = strings.Split(egressProxy.ProxyExcludes, ",")
}
ip, _, err := net.ParseCIDR(cluster.Spec.NonMasqueradeCIDR)
if err != nil {
return nil, fmt.Errorf("unable to parse Non Masquerade CIDR")
}
firstIP, err := incrementIP(ip, cluster.Spec.NonMasqueradeCIDR)
if err != nil {
return nil, fmt.Errorf("unable to get first ip address in Non Masquerade CIDR")
}
// run through the basic list
for _, exclude := range []string{
"127.0.0.1",
"localhost",
cluster.Spec.ClusterDNSDomain, // TODO we may want this for public loadbalancers
cluster.Spec.MasterPublicName,
cluster.ObjectMeta.Name,
firstIP,
cluster.Spec.NonMasqueradeCIDR,
} {
if exclude == "" {
continue
}
if !strings.Contains(egressProxy.ProxyExcludes, exclude) {
egressSlice = append(egressSlice, exclude)
}
}
awsNoProxy := "169.254.169.254"
if cluster.Spec.CloudProvider == "aws" && !strings.Contains(cluster.Spec.EgressProxy.ProxyExcludes, awsNoProxy) {
egressSlice = append(egressSlice, awsNoProxy)
}
// the kube-apiserver will need to talk to kubelets on their node IP addresses port 10250
// for pod logs to be available via the api
if cluster.Spec.NetworkCIDR != "" {
if !strings.Contains(cluster.Spec.EgressProxy.ProxyExcludes, cluster.Spec.NetworkCIDR) {
egressSlice = append(egressSlice, cluster.Spec.NetworkCIDR)
}
} else {
klog.Warningf("No NetworkCIDR defined (yet), not adding to egressProxy.excludes")
}
egressProxy.ProxyExcludes = strings.Join(egressSlice, ",")
klog.V(8).Infof("Completed setting up Proxy excludes as follows: %q", egressProxy.ProxyExcludes)
} else {
klog.V(8).Info("Not setting up Proxy Excludes")
}
return egressProxy, nil
}
func incrementIP(ip net.IP, cidr string) (string, error) {
_, ipNet, err := net.ParseCIDR(cidr)
if err != nil {
return "", err
}
for i := len(ip) - 1; i >= 0; i-- {
ip[i]++
if ip[i] != 0 {
break
}
}
if !ipNet.Contains(ip) {
return "", fmt.Errorf("overflowed CIDR while incrementing IP")
}
return ip.String(), nil
}