-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
utils.go
258 lines (214 loc) · 6.71 KB
/
utils.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
/*
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 (
"fmt"
"strings"
"k8s.io/klog/v2"
"k8s.io/kops/dnsprovider/pkg/dnsprovider"
"k8s.io/kops/dnsprovider/pkg/dnsprovider/providers/aws/route53"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"k8s.io/kops/upup/pkg/fi/cloudup/azure"
"k8s.io/kops/upup/pkg/fi/cloudup/do"
"k8s.io/kops/upup/pkg/fi/cloudup/gce"
"k8s.io/kops/upup/pkg/fi/cloudup/hetzner"
"k8s.io/kops/upup/pkg/fi/cloudup/openstack"
"k8s.io/kops/upup/pkg/fi/cloudup/scaleway"
)
func BuildCloud(cluster *kops.Cluster) (fi.Cloud, error) {
var cloud fi.Cloud
region := ""
project := ""
switch cluster.Spec.GetCloudProvider() {
case kops.CloudProviderGCE:
{
for _, subnet := range cluster.Spec.Networking.Subnets {
if subnet.Region != "" {
region = subnet.Region
}
}
if region == "" {
return nil, fmt.Errorf("on GCE, subnets must include Regions")
}
project = cluster.Spec.CloudProvider.GCE.Project
if project == "" {
return nil, fmt.Errorf("project is required for GCE - try gcloud config get-value project")
}
labels := map[string]string{gce.GceLabelNameKubernetesCluster: gce.SafeClusterName(cluster.ObjectMeta.Name)}
gceCloud, err := gce.NewGCECloud(region, project, labels)
if err != nil {
return nil, err
}
cloud = gceCloud
}
case kops.CloudProviderAWS:
{
region, err := awsup.FindRegion(cluster)
if err != nil {
return nil, err
}
err = awsup.ValidateRegion(region)
if err != nil {
return nil, err
}
cloudTags := map[string]string{awsup.TagClusterName: cluster.ObjectMeta.Name}
awsCloud, err := awsup.NewAWSCloud(region, cloudTags)
if err != nil {
return nil, err
}
var zoneNames []string
for _, subnet := range cluster.Spec.Networking.Subnets {
zoneNames = append(zoneNames, subnet.Zone)
}
err = awsup.ValidateZones(zoneNames, awsCloud)
if err != nil {
return nil, err
}
cloud = awsCloud
}
case kops.CloudProviderDO:
{
// for development purposes we're going to assume
// single region setups for DO. Reconsider this logic
// when setting up multi-region kubernetes clusters on DO
region := cluster.Spec.Networking.Subnets[0].Zone
doCloud, err := do.NewDOCloud(region)
if err != nil {
return nil, fmt.Errorf("error initializing digitalocean cloud: %s", err)
}
cloud = doCloud
}
case kops.CloudProviderHetzner:
{
region, err := hetzner.FindRegion(cluster)
if err != nil {
return nil, err
}
hetznerCloud, err := hetzner.NewHetznerCloud(region)
if err != nil {
return nil, fmt.Errorf("error initializing hetzner cloud: %s", err)
}
cloud = hetznerCloud
}
case kops.CloudProviderOpenstack:
{
osc, err := openstack.NewOpenstackCloud(cluster, "build-cloud")
if err != nil {
return nil, err
}
var zoneNames []string
for _, subnet := range cluster.Spec.Networking.Subnets {
if !fi.ArrayContains(zoneNames, subnet.Zone) {
zoneNames = append(zoneNames, subnet.Zone)
}
}
osc.UseZones(zoneNames)
cloud = osc
}
case kops.CloudProviderAzure:
{
for _, subnet := range cluster.Spec.Networking.Subnets {
if subnet.Region != "" {
region = subnet.Region
}
}
if region == "" {
return nil, fmt.Errorf("on Azure, subnets must include Regions")
}
cloudTags := map[string]string{azure.TagClusterName: cluster.ObjectMeta.Name}
azureCloud, err := azure.NewAzureCloud(cluster.Spec.CloudProvider.Azure.SubscriptionID, region, cloudTags)
if err != nil {
return nil, err
}
cloud = azureCloud
}
case kops.CloudProviderScaleway:
{
cloudTags := map[string]string{scaleway.TagClusterName: cluster.ObjectMeta.Name}
scwCloud, err := scaleway.NewScwCloud(cloudTags)
if err != nil {
return nil, fmt.Errorf("error initializing scaleway cloud: %s", err)
}
cloud = scwCloud
}
default:
return nil, fmt.Errorf("unknown CloudProvider %q", cluster.Spec.GetCloudProvider())
}
return cloud, nil
}
func FindDNSHostedZone(dns dnsprovider.Interface, clusterDNSName string, dnsType kops.DNSType) (string, error) {
klog.V(2).Infof("Querying for all DNS zones to find match for %q", clusterDNSName)
clusterDNSName = "." + strings.TrimSuffix(clusterDNSName, ".")
zonesProvider, ok := dns.Zones()
if !ok {
return "", fmt.Errorf("dns provider %T does not support zones", dns)
}
allZones, err := zonesProvider.List()
if err != nil {
return "", fmt.Errorf("error querying zones: %v", err)
}
var zones []dnsprovider.Zone
for _, z := range allZones {
zoneName := "." + strings.TrimSuffix(z.Name(), ".")
if !strings.HasSuffix(clusterDNSName, zoneName) {
continue
}
if dnsType != "" {
if awsZone, ok := z.(*route53.Zone); ok {
hostedZone := awsZone.Route53HostedZone()
if hostedZone.Config != nil {
zoneDNSType := kops.DNSTypePublic
if fi.ValueOf(hostedZone.Config.PrivateZone) {
zoneDNSType = kops.DNSTypePrivate
}
if zoneDNSType != dnsType {
klog.Infof("Found matching hosted zone %q, but it was %q and we require %q", zoneName, zoneDNSType, dnsType)
continue
}
}
}
}
zones = append(zones, z)
}
// Find the longest zones
maxLength := -1
maxLengthZones := []dnsprovider.Zone{}
for _, z := range zones {
zoneName := "." + strings.TrimSuffix(z.Name(), ".")
n := len(zoneName)
if n < maxLength {
continue
}
if n > maxLength {
maxLength = n
maxLengthZones = []dnsprovider.Zone{}
}
maxLengthZones = append(maxLengthZones, z)
}
if len(maxLengthZones) == 0 {
// We make this an error because you have to set up DNS delegation anyway
tokens := strings.Split(clusterDNSName, ".")
suffix := strings.Join(tokens[len(tokens)-2:], ".")
// klog.Warningf("No matching hosted zones found; will created %q", suffix)
// return suffix, nil
return "", fmt.Errorf("No matching hosted zones found for %q; please create one (e.g. %q) first", clusterDNSName, suffix)
}
if len(maxLengthZones) == 1 {
id := maxLengthZones[0].ID()
id = strings.TrimPrefix(id, "/hostedzone/")
return id, nil
}
return "", fmt.Errorf("Found multiple hosted zones matching cluster %q; please specify the ID of the zone to use", clusterDNSName)
}