-
Notifications
You must be signed in to change notification settings - Fork 20
/
sks_create.go
289 lines (252 loc) · 10.8 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
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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package cmd
import (
"errors"
"fmt"
"strings"
egoscale "github.com/exoscale/egoscale/v2"
exoapi "github.com/exoscale/egoscale/v2/api"
"github.com/spf13/cobra"
)
var (
defaultSKSClusterCNI = "calico"
defaultSKSClusterServiceLevel = "pro"
sksClusterAddonExoscaleCCM = "exoscale-cloud-controller"
sksClusterAddonMetricsServer = "metrics-server"
)
type sksCreateCmd struct {
cliCommandSettings `cli-cmd:"-"`
_ bool `cli-cmd:"create"`
Name string `cli-arg:"#" cli-usage:"NAME"`
AutoUpgrade bool `cli-usage:"enable automatic upgrading of the SKS cluster control plane Kubernetes version"`
CNI string `cli-usage:"CNI plugin to deploy. e.g. 'calico', or 'cilium'"`
Description string `cli-usage:"SKS cluster description"`
KubernetesVersion string `cli-usage:"SKS cluster control plane Kubernetes version"`
Labels map[string]string `cli-flag:"label" cli-usage:"SKS cluster label (format: key=value)"`
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"`
NodepoolLabels map[string]string `cli-flag:"nodepool-label" cli-usage:"default Nodepool label (format: key=value)"`
NodepoolName string `cli-usage:"default Nodepool name"`
NodepoolPrivateNetworks []string `cli-flag:"nodepool-private-network" cli-usage:"default Nodepool Private Network NAME|ID (can be specified multiple times)"`
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."`
NodepoolTaints []string `cli-flag:"nodepool-taint" cli-usage:"Kubernetes taint to apply to default Nodepool Nodes (format: KEY=VALUE:EFFECT, can be specified multiple times)"`
OIDCClientID string `cli-flag:"oidc-client-id" cli-usage:"OpenID client ID"`
OIDCGroupsClaim string `cli-flag:"oidc-groups-claim" cli-usage:"OpenID JWT claim to use as the user's group"`
OIDCGroupsPrefix string `cli-flag:"oidc-groups-prefix" cli-usage:"OpenID prefix prepended to group claims"`
OIDCIssuerURL string `cli-flag:"oidc-issuer-url" cli-usage:"OpenID provider URL"`
OIDCRequiredClaim map[string]string `cli-flag:"oidc-required-claim" cli-usage:"OpenID token required claim (format: key=value)"`
OIDCUsernameClaim string `cli-flag:"oidc-username-claim" cli-usage:"OpenID JWT claim to use as the user name"`
OIDCUsernamePrefix string `cli-flag:"oidc-username-prefix" cli-usage:"OpenID prefix prepended to username claims"`
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 := &egoscale.SKSCluster{
AutoUpgrade: &c.AutoUpgrade,
CNI: &c.CNI,
Description: nonEmptyStringPtr(c.Description),
Labels: func() (v *map[string]string) {
if len(c.Labels) > 0 {
return &c.Labels
}
return
}(),
Name: &c.Name,
ServiceLevel: &c.ServiceLevel,
Version: &c.KubernetesVersion,
}
ctx := exoapi.WithEndpoint(gContext, exoapi.NewReqEndpoint(gCurrentAccount.Environment, c.Zone))
if c.NoCNI {
cluster.CNI = nil
}
addOns := map[string]struct{}{
sksClusterAddonExoscaleCCM: {},
sksClusterAddonMetricsServer: {},
}
cluster.AddOns = func() (v *[]string) {
if c.NoExoscaleCCM {
delete(addOns, sksClusterAddonExoscaleCCM)
}
if c.NoMetricsServer {
delete(addOns, sksClusterAddonMetricsServer)
}
if len(addOns) > 0 {
list := make([]string, 0)
for k := range addOns {
list = append(list, k)
}
v = &list
}
return
}()
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: %w", err)
}
cluster.Version = &versions[0]
}
var opts []egoscale.CreateSKSClusterOpt
if c.OIDCClientID != "" {
opts = append(opts, egoscale.CreateSKSClusterWithOIDC(&egoscale.SKSClusterOIDCConfig{
ClientID: &c.OIDCClientID,
GroupsClaim: nonEmptyStringPtr(c.OIDCGroupsClaim),
GroupsPrefix: nonEmptyStringPtr(c.OIDCGroupsPrefix),
IssuerURL: &c.OIDCIssuerURL,
RequiredClaim: func() (v *map[string]string) {
if len(c.OIDCRequiredClaim) > 0 {
v = &c.OIDCRequiredClaim
}
return
}(),
UsernameClaim: nonEmptyStringPtr(c.OIDCUsernameClaim),
UsernamePrefix: nonEmptyStringPtr(c.OIDCUsernamePrefix),
}))
}
var err error
decorateAsyncOperation(fmt.Sprintf("Creating SKS cluster %q...", *cluster.Name), func() {
cluster, err = cs.CreateSKSCluster(ctx, c.Zone, cluster, opts...)
})
if err != nil {
return err
}
if c.NodepoolSize > 0 {
nodepool := &egoscale.SKSNodepool{
Description: nonEmptyStringPtr(c.NodepoolDescription),
DiskSize: &c.NodepoolDiskSize,
InstancePrefix: nonEmptyStringPtr(c.NodepoolInstancePrefix),
Labels: func() (v *map[string]string) {
if len(c.NodepoolLabels) > 0 {
return &c.NodepoolLabels
}
return
}(),
Name: nonEmptyStringPtr(func() string {
if c.NodepoolName != "" {
return c.NodepoolName
}
return c.Name
}()),
Size: &c.NodepoolSize,
}
if l := len(c.NodepoolAntiAffinityGroups); l > 0 {
nodepoolAntiAffinityGroupIDs := make([]string, l)
for i, v := range c.NodepoolAntiAffinityGroups {
antiAffinityGroup, err := cs.FindAntiAffinityGroup(ctx, c.Zone, v)
if err != nil {
return fmt.Errorf("error retrieving Anti-Affinity Group: %w", err)
}
nodepoolAntiAffinityGroupIDs[i] = *antiAffinityGroup.ID
}
nodepool.AntiAffinityGroupIDs = &nodepoolAntiAffinityGroupIDs
}
if c.NodepoolDeployTarget != "" {
deployTarget, err := cs.FindDeployTarget(ctx, c.Zone, c.NodepoolDeployTarget)
if err != nil {
return fmt.Errorf("error retrieving Deploy Target: %w", err)
}
nodepool.DeployTargetID = deployTarget.ID
}
nodepoolInstanceType, err := cs.FindInstanceType(ctx, c.Zone, c.NodepoolInstanceType)
if err != nil {
return fmt.Errorf("error retrieving instance type: %w", err)
}
nodepool.InstanceTypeID = nodepoolInstanceType.ID
if l := len(c.NodepoolPrivateNetworks); l > 0 {
nodepoolPrivateNetworkIDs := make([]string, l)
for i, v := range c.NodepoolPrivateNetworks {
privateNetwork, err := cs.FindPrivateNetwork(ctx, c.Zone, v)
if err != nil {
return fmt.Errorf("error retrieving Private Network: %w", err)
}
nodepoolPrivateNetworkIDs[i] = *privateNetwork.ID
}
nodepool.PrivateNetworkIDs = &nodepoolPrivateNetworkIDs
}
if l := len(c.NodepoolSecurityGroups); l > 0 {
nodepoolSecurityGroupIDs := make([]string, l)
for i, v := range c.NodepoolSecurityGroups {
securityGroup, err := cs.FindSecurityGroup(ctx, c.Zone, v)
if err != nil {
return fmt.Errorf("error retrieving Security Group: %w", err)
}
nodepoolSecurityGroupIDs[i] = *securityGroup.ID
}
nodepool.SecurityGroupIDs = &nodepoolSecurityGroupIDs
}
if len(c.NodepoolTaints) > 0 {
taints := make(map[string]*egoscale.SKSNodepoolTaint)
for _, t := range c.NodepoolTaints {
key, taint, err := parseSKSNodepoolTaint(t)
if err != nil {
return fmt.Errorf("invalid taint value %q: %w", t, err)
}
taints[key] = taint
}
nodepool.Taints = &taints
}
decorateAsyncOperation(fmt.Sprintf("Adding Nodepool %q...", *nodepool.Name), func() {
_, err = cs.CreateSKSNodepool(ctx, c.Zone, cluster, nodepool)
})
if err != nil {
return err
}
}
if !gQuiet {
return (&sksShowCmd{
cliCommandSettings: c.cliCommandSettings,
Cluster: *cluster.ID,
Zone: c.Zone,
}).cmdRun(nil, nil)
}
return nil
}
func init() {
cobra.CheckErr(registerCLICommand(sksCmd, &sksCreateCmd{
cliCommandSettings: defaultCLICmdSettings(),
CNI: defaultSKSClusterCNI,
KubernetesVersion: "latest",
NodepoolDiskSize: 50,
NodepoolInstanceType: defaultServiceOffering,
ServiceLevel: defaultSKSClusterServiceLevel,
}))
// FIXME: remove this someday.
cobra.CheckErr(registerCLICommand(deprecatedSKSCmd, &sksCreateCmd{
cliCommandSettings: defaultCLICmdSettings(),
CNI: defaultSKSClusterCNI,
KubernetesVersion: "latest",
NodepoolDiskSize: 50,
NodepoolInstanceType: defaultServiceOffering,
ServiceLevel: defaultSKSClusterServiceLevel,
}))
}