-
Notifications
You must be signed in to change notification settings - Fork 20
/
sks_create.go
196 lines (166 loc) · 6.57 KB
/
sks_create.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
package cmd
import (
"errors"
"fmt"
"strings"
exov2 "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
const (
defaultSKSClusterCNI = "calico"
defaultSKSClusterServiceLevel = "pro"
sksClusterAddonExoscaleCCM = "exoscale-cloud-controller"
sksClusterAddonMetricsServer = "metrics-server"
)
type sksCreateCmd struct {
_ bool `cli-cmd:"create"`
Name string `cli-arg:"#" cli-usage:"NAME"`
Description string `cli-usage:"SKS cluster description"`
KubernetesVersion string `cli-usage:"SKS cluster control plane Kubernetes version"`
NoCNI bool `cli-usage:"do not deploy a default Container Network Interface plugin in the cluster control plane"`
NoExoscaleCCM bool `cli-usage:"do not deploy the Exoscale Cloud Controller Manager in the cluster control plane"`
NoMetricsServer bool `cli-usage:"do not deploy the Kubernetes Metrics Server in the cluster control plane"`
NodepoolAntiAffinityGroups []string `cli-flag:"nodepool-anti-affinity-group" cli-usage:"default Nodepool Anti-Affinity Group NAME|ID (can be specified multiple times)"`
NodepoolDeployTarget string `cli-usage:"default Nodepool Deploy Target NAME|ID"`
NodepoolDescription string `cli-usage:"default Nodepool description"`
NodepoolDiskSize int64 `cli-usage:"default Nodepool Compute instances disk size"`
NodepoolInstancePrefix string `cli-usage:"string to prefix default Nodepool member names with"`
NodepoolInstanceType string `cli-usage:"default Nodepool Compute instances type"`
NodepoolName string `cli-usage:"default Nodepool name"`
NodepoolSecurityGroups []string `cli-flag:"nodepool-security-group" cli-usage:"default Nodepool Security Group NAME|ID (can be specified multiple times)"`
NodepoolSize int64 `cli-usage:"default Nodepool size. If 0, no default Nodepool will be added to the cluster."`
ServiceLevel string `cli-usage:"SKS cluster control plane service level (starter|pro)"`
Zone string `cli-short:"z" cli-usage:"SKS cluster zone"`
}
func (c *sksCreateCmd) cmdAliases() []string { return gCreateAlias }
func (c *sksCreateCmd) cmdShort() string { return "Create an SKS cluster" }
func (c *sksCreateCmd) cmdLong() string {
return fmt.Sprintf(`This command creates an SKS cluster.
Note: SKS cluster Nodes' kubelet configuration is set to use the Exoscale
Cloud Controller Manager (CCM) as Cloud Provider by default. Cluster Nodes
will remain in the "NotReady" status until the Exoscale CCM is deployed by
cluster operators. Please refer to the Exoscale CCM documentation for more
information:
https://github.com/exoscale/exoscale-cloud-controller-manager
If you do not want to use a Cloud Controller Manager, add the
"--no-exoscale-ccm" option to the command. This cannot be changed once the
cluster has been created.
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&sksShowOutput{}), ", "))
}
func (c *sksCreateCmd) cmdPreRun(cmd *cobra.Command, args []string) error {
cmdSetZoneFlagFromDefault(cmd)
return cliCommandDefaultPreRun(c, cmd, args)
}
func (c *sksCreateCmd) cmdRun(_ *cobra.Command, _ []string) error {
cluster := &exov2.SKSCluster{
CNI: defaultSKSClusterCNI,
Description: c.Description,
Name: c.Name,
ServiceLevel: c.ServiceLevel,
Version: c.KubernetesVersion,
}
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, c.Zone))
if c.NoCNI {
cluster.CNI = ""
}
addOns := map[string]struct{}{
sksClusterAddonExoscaleCCM: {},
sksClusterAddonMetricsServer: {},
}
cluster.AddOns = func() []string {
if c.NoExoscaleCCM {
delete(addOns, sksClusterAddonExoscaleCCM)
}
if c.NoMetricsServer {
delete(addOns, sksClusterAddonMetricsServer)
}
list := make([]string, 0)
for k := range addOns {
list = append(list, k)
}
return list
}()
if cluster.Version == "latest" {
versions, err := cs.ListSKSClusterVersions(ctx)
if err != nil || len(versions) == 0 {
if len(versions) == 0 {
err = errors.New("no version returned by the API")
}
return fmt.Errorf("unable to retrieve SKS versions: %s", err)
}
cluster.Version = versions[0]
}
var err error
decorateAsyncOperation(fmt.Sprintf("Creating SKS cluster %q...", c.Name), func() {
cluster, err = cs.CreateSKSCluster(ctx, c.Zone, cluster)
})
if err != nil {
return err
}
if c.NodepoolSize > 0 {
nodepool := &exov2.SKSNodepool{
Description: c.NodepoolDescription,
DiskSize: c.NodepoolDiskSize,
InstancePrefix: c.NodepoolInstancePrefix,
Name: func() string {
if c.NodepoolName != "" {
return c.NodepoolName
}
return c.Name
}(),
Size: c.NodepoolSize,
}
if l := len(c.NodepoolAntiAffinityGroups); l > 0 {
nodepool.AntiAffinityGroupIDs = make([]string, l)
for i := range c.NodepoolAntiAffinityGroups {
antiAffinityGroup, err := cs.FindAntiAffinityGroup(ctx, c.Zone, c.NodepoolAntiAffinityGroups[i])
if err != nil {
return fmt.Errorf("error retrieving Anti-Affinity Group: %s", err)
}
nodepool.AntiAffinityGroupIDs[i] = antiAffinityGroup.ID
}
}
if c.NodepoolDeployTarget != "" {
deployTarget, err := cs.FindDeployTarget(ctx, c.Zone, c.NodepoolDeployTarget)
if err != nil {
return fmt.Errorf("error retrieving Deploy Target: %s", err)
}
nodepool.DeployTargetID = deployTarget.ID
}
nodepoolInstanceType, err := cs.FindInstanceType(ctx, c.Zone, c.NodepoolInstanceType)
if err != nil {
return fmt.Errorf("error retrieving instance type: %s", err)
}
nodepool.InstanceTypeID = nodepoolInstanceType.ID
if l := len(c.NodepoolSecurityGroups); l > 0 {
nodepool.SecurityGroupIDs = make([]string, l)
for i := range c.NodepoolSecurityGroups {
securityGroup, err := cs.FindSecurityGroup(ctx, c.Zone, c.NodepoolSecurityGroups[i])
if err != nil {
return fmt.Errorf("error retrieving Security Group: %s", err)
}
nodepool.SecurityGroupIDs[i] = securityGroup.ID
}
}
decorateAsyncOperation(fmt.Sprintf("Adding Nodepool %q...", nodepool.Name), func() {
_, err = cluster.AddNodepool(ctx, nodepool)
})
if err != nil {
return err
}
}
if !gQuiet {
return output(showSKSCluster(c.Zone, cluster.ID))
}
return nil
}
func init() {
cobra.CheckErr(registerCLICommand(sksCmd, &sksCreateCmd{
KubernetesVersion: "latest",
NodepoolDiskSize: 50,
NodepoolInstanceType: defaultServiceOffering,
ServiceLevel: defaultSKSClusterServiceLevel,
}))
}