-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
droplets.go
117 lines (95 loc) · 3.65 KB
/
droplets.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
/*
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 domodel
import (
"strconv"
"strings"
"k8s.io/kops/pkg/model"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/do"
"k8s.io/kops/upup/pkg/fi/cloudup/dotasks"
)
// DropletBuilder configures droplets for the cluster
type DropletBuilder struct {
*DOModelContext
BootstrapScriptBuilder *model.BootstrapScriptBuilder
Lifecycle fi.Lifecycle
}
var _ fi.CloudupModelBuilder = &DropletBuilder{}
func (d *DropletBuilder) Build(c *fi.CloudupModelBuilderContext) error {
var sshKey *dotasks.SSHKey
if len(d.SSHPublicKeys) != 0 {
sshPublicKey := d.SSHPublicKeys[0]
sshKeyName, err := d.SSHKeyName()
if err != nil {
return err
}
splitSSHKeyName := strings.Split(sshKeyName, "-")
sshKeyFingerPrint := splitSSHKeyName[len(splitSSHKeyName)-1]
sshKey = &dotasks.SSHKey{
Name: &sshKeyName,
Lifecycle: d.Lifecycle,
PublicKey: fi.NewBytesResource(sshPublicKey),
KeyFingerprint: &sshKeyFingerPrint,
}
c.AddTask(sshKey)
}
// replace "." with "-" since DO API does not accept "."
clusterName := do.SafeClusterName(d.ClusterName())
clusterTag := do.TagKubernetesClusterNamePrefix + ":" + clusterName
clusterMasterTag := do.TagKubernetesClusterMasterPrefix + ":" + clusterName
masterIndexCount := 0
// In the future, DigitalOcean will use Machine API to manage groups,
// for now create d.InstanceGroups.Spec.MinSize amount of droplets
for _, ig := range d.InstanceGroups {
name := d.AutoscalingGroupName(ig)
droplet := dotasks.Droplet{
Count: int(fi.ValueOf(ig.Spec.MinSize)),
Name: fi.PtrTo(name),
Lifecycle: d.Lifecycle,
// kops do supports allow only 1 region
Region: fi.PtrTo(d.Cluster.Spec.Networking.Subnets[0].Region),
Size: fi.PtrTo(ig.Spec.MachineType),
Image: fi.PtrTo(ig.Spec.Image),
SSHKey: sshKey,
Tags: []string{clusterTag},
}
if ig.IsControlPlane() {
masterIndexCount++
// create tag based on etcd name. etcd name is now prefixed with etcd-
// Ref: https://github.com/kubernetes/kops/commit/31f8cbd571964f19d3c31024ddba918998d29929
clusterTagIndex := do.TagKubernetesClusterIndex + ":" + "etcd-" + strconv.Itoa(masterIndexCount)
droplet.Tags = append(droplet.Tags, clusterTagIndex)
droplet.Tags = append(droplet.Tags, clusterMasterTag)
droplet.Tags = append(droplet.Tags, do.TagKubernetesInstanceGroup+":"+ig.Name)
} else {
droplet.Tags = append(droplet.Tags, do.TagKubernetesInstanceGroup+":"+ig.Name)
}
if d.Cluster.Spec.Networking.NetworkID != "" {
droplet.VPCUUID = fi.PtrTo(d.Cluster.Spec.Networking.NetworkID)
} else if d.Cluster.Spec.Networking.NetworkCIDR != "" {
// since networkCIDR specified as part of the request, it is made sure that vpc with this cidr exist before
// creating the droplet, so you can associate with vpc uuid for this droplet.
vpcName := "vpc-" + clusterName
droplet.VPCName = fi.PtrTo(vpcName)
droplet.NetworkCIDR = fi.PtrTo(d.Cluster.Spec.Networking.NetworkCIDR)
}
userData, err := d.BootstrapScriptBuilder.ResourceNodeUp(c, ig)
if err != nil {
return err
}
droplet.UserData = userData
c.AddTask(&droplet)
}
return nil
}