-
Notifications
You must be signed in to change notification settings - Fork 4
/
create_core_instance_operator.go
431 lines (373 loc) · 15.5 KB
/
create_core_instance_operator.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package coreinstance
import (
"context"
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/spf13/cobra"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/client-go/kubernetes"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp" // register GCP auth provider
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
cloud "github.com/calyptia/api/types"
"github.com/calyptia/cli/cmd/utils"
"github.com/calyptia/cli/cmd/version"
"github.com/calyptia/cli/completer"
cfg "github.com/calyptia/cli/config"
"github.com/calyptia/cli/k8s"
)
func newCmdCreateCoreInstanceOperator(config *cfg.Config, testClientSet kubernetes.Interface) *cobra.Command {
var (
coreInstanceName string
coreCloudURL string
coreFluentBitDockerImage string
coreDockerToCloudImage string
coreDockerFromCloudImage string
noHealthCheckPipeline bool
healthCheckPipelinePort string
healthCheckPipelineServiceType string
enableClusterLogging bool
skipServiceCreation bool
environment string
tags []string
dryRun bool
waitReady bool
waitTimeout time.Duration
noTLSVerify bool
metricsPort string
httpProxy, httpsProxy string
memoryLimit string
annotations string
tolerations string
)
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
configOverrides := &clientcmd.ConfigOverrides{}
completer := completer.Completer{Config: config}
cmd := &cobra.Command{
Use: "operator",
Aliases: []string{"opr", "k8s"},
Short: "Setup a new core instance on top of a operator installation",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
if configOverrides.Context.Namespace == "" {
namespace, err := k8s.GetCurrentContextNamespace()
if err != nil {
if errors.Is(err, k8s.ErrNoContext) {
cmd.Printf("No context is currently set. Using default namespace.\n")
} else {
return err
}
}
if namespace != "" {
configOverrides.Context.Namespace = namespace
} else {
configOverrides.Context.Namespace = apiv1.NamespaceDefault
}
}
var clientSet kubernetes.Interface
var kubeClientConfig *restclient.Config
if testClientSet != nil {
clientSet = testClientSet
} else {
var err error
kubeConfig := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, configOverrides)
kubeClientConfig, err = kubeConfig.ClientConfig()
if err != nil {
return err
}
clientSet, err = kubernetes.NewForConfig(kubeClientConfig)
if err != nil {
return err
}
}
if coreCloudURL == "" {
coreCloudURL = config.BaseURL
}
k8sClient := &k8s.Client{
Interface: clientSet,
Namespace: configOverrides.Context.Namespace,
ProjectToken: config.ProjectToken,
CloudBaseURL: coreCloudURL,
Config: kubeClientConfig,
}
if err := k8sClient.EnsureOwnNamespace(ctx); err != nil {
return fmt.Errorf("could not ensure kubernetes namespace exists: %w", err)
}
var environmentID string
if environment != "" {
var err error
environmentID, err = completer.LoadEnvironmentID(environment)
if err != nil {
return err
}
}
operatorVersion, err := k8sClient.CheckOperatorVersion(ctx)
if errors.Is(err, k8s.ErrCoreOperatorNotFound) {
return fmt.Errorf("calyptia core operator not found running in the cluster. Please install the core operator first (calyptia install operator)")
}
if err != nil {
return err
}
fmt.Printf("Found calyptia core operator installed, version: %s...\n", operatorVersion)
metadata, err := getCoreInstanceMetadata(k8sClient)
if err != nil {
return err
}
metadata.ClusterName, err = getClusterName()
if err != nil {
return err
}
coreInstanceParams := cloud.CreateCoreInstance{
Name: coreInstanceName,
AddHealthCheckPipeline: !noHealthCheckPipeline,
ClusterLogging: enableClusterLogging,
EnvironmentID: environmentID,
Tags: tags,
SkipServiceCreation: skipServiceCreation,
Metadata: metadata,
}
if !noHealthCheckPipeline {
if healthCheckPipelinePort != "" {
p, err := strconv.Atoi(healthCheckPipelinePort)
if err != nil {
return err
}
if !(p > 0 && p <= 65535) {
return fmt.Errorf("invalid provided pipeline port number, must be > 0 < 65535: %w", err)
}
coreInstanceParams.HealthCheckPipelinePort = uint(p)
}
if healthCheckPipelineServiceType != "" {
if !ValidPipelinePortKind(healthCheckPipelineServiceType) {
return fmt.Errorf("invalid health check pipeline service type: %s", healthCheckPipelineServiceType)
}
coreInstanceParams.HealthCheckPipelinePortKind = cloud.PipelinePortKind(healthCheckPipelineServiceType)
}
}
// Only set the version if != latest, otherwise use the default value
// for registering this core instance.
if operatorVersion != utils.LatestVersion {
coreInstanceParams.Version = operatorVersion
}
if coreFluentBitDockerImage != "" {
coreInstanceParams.Image = &coreFluentBitDockerImage
}
created, err := config.Cloud.CreateCoreInstance(ctx, coreInstanceParams)
if err != nil {
return fmt.Errorf("could not create core instance (%q) at calyptia cloud (%q): %w", coreInstanceName, coreCloudURL, err)
}
labelsFunc := func() map[string]string {
return map[string]string{
k8s.LabelVersion: version.Version,
k8s.LabelPartOf: "calyptia",
k8s.LabelComponent: "operator",
k8s.LabelManagedBy: "calyptia-cli",
k8s.LabelCreatedBy: "calyptia-cli",
k8s.LabelProjectID: config.ProjectID,
k8s.LabelAggregatorID: created.ID,
k8s.LabelInstance: coreInstanceName,
}
}
k8sClient.LabelsFunc = labelsFunc
var resourcesCreated []k8s.ResourceRollBack
secret, err := k8sClient.CreateSecretOperatorRSAKey(ctx, created, dryRun)
if err != nil {
fmt.Printf("An error occurred while creating the core operator instance. %s Rolling back created resources.\n", err)
resources, err := k8sClient.DeleteResources(ctx, resourcesCreated)
if err != nil {
return fmt.Errorf("could not delete resources: %w", err)
}
fmt.Printf("Rollback successful. Deleted %d resources.\n", len(resources))
}
if secret != nil {
err = addToRollBack(err, secret.Name, configOverrides.Context.Namespace, "secret", &resourcesCreated)
if err != nil {
return err
}
}
var clusterRoleOpts k8s.ClusterRoleOpt
clusterRole, err := k8sClient.CreateClusterRole(ctx, created, dryRun, clusterRoleOpts)
if err != nil {
fmt.Printf("An error occurred while creating the core operator instance. %s Rolling back created resources.\n", err)
resources, err := k8sClient.DeleteResources(ctx, resourcesCreated)
if err != nil {
return fmt.Errorf("could not delete resources: %w", err)
}
fmt.Printf("Rollback successful. Deleted %d resources.\n", len(resources))
}
if clusterRole != nil {
err = addToRollBack(err, clusterRole.Name, configOverrides.Context.Namespace, "clusterrole", &resourcesCreated)
if err != nil {
return err
}
}
serviceAccount, err := k8sClient.CreateServiceAccount(ctx, created, dryRun)
if err != nil {
fmt.Printf("An error occurred while creating the core operator instance. %s Rolling back created resources.\n", err)
resources, err := k8sClient.DeleteResources(ctx, resourcesCreated)
if err != nil {
return fmt.Errorf("could not delete resources: %w", err)
}
fmt.Printf("Rollback successful. Deleted %d resources.\n", len(resources))
}
if serviceAccount != nil {
err = addToRollBack(err, serviceAccount.Name, configOverrides.Context.Namespace, "serviceaccount", &resourcesCreated)
if err != nil {
return err
}
}
binding, err := k8sClient.CreateClusterRoleBinding(ctx, created, clusterRole, serviceAccount, dryRun)
if err != nil {
fmt.Printf("An error occurred while creating the core operator instance. %s Rolling back created resources.\n", err)
resources, err := k8sClient.DeleteResources(ctx, resourcesCreated)
if err != nil {
return fmt.Errorf("could not delete resources: %w", err)
}
fmt.Printf("Rollback successful. Deleted %d resources.\n", len(resources))
}
err = addToRollBack(err, binding.Name, configOverrides.Context.Namespace, "clusterrolebinding", &resourcesCreated)
if err != nil {
return err
}
if coreDockerToCloudImage == "" {
coreDockerToCloudImageTag := utils.DefaultCoreOperatorToCloudDockerImageTag
if operatorVersion != "" {
coreDockerToCloudImageTag = operatorVersion
}
coreDockerToCloudImage = fmt.Sprintf("%s:%s", utils.DefaultCoreOperatorToCloudDockerImage, coreDockerToCloudImageTag)
}
if coreDockerFromCloudImage == "" {
coreDockerFromCloudImageTag := utils.DefaultCoreOperatorFromCloudDockerImageTag
if operatorVersion != "" {
coreDockerFromCloudImageTag = operatorVersion
}
coreDockerFromCloudImage = fmt.Sprintf("%s:%s", utils.DefaultCoreOperatorFromCloudDockerImage, coreDockerFromCloudImageTag)
}
syncDeployment, err := k8sClient.DeployCoreOperatorSync(ctx, coreCloudURL, coreDockerFromCloudImage, coreDockerToCloudImage, metricsPort, memoryLimit, annotations, tolerations, skipServiceCreation, !noTLSVerify, httpProxy, httpsProxy, created, serviceAccount.Name)
if err != nil {
if err := config.Cloud.DeleteCoreInstance(ctx, created.ID); err != nil {
return fmt.Errorf("failed to rollback created core instance %v", err)
}
fmt.Printf("An error occurred while creating the core operator instance. %s Rolling back created resources.\n", err)
resources, err := k8sClient.DeleteResources(ctx, resourcesCreated)
if err != nil {
return fmt.Errorf("could not delete resources: %w", err)
}
fmt.Printf("Rollback successful. Deleted %d resources.\n", len(resources))
}
if waitReady {
start := time.Now()
fmt.Printf("Waiting for core instance to be ready...\n")
err := k8sClient.WaitReady(ctx, syncDeployment.Namespace, syncDeployment.Name, false, waitTimeout)
if err != nil {
return err
}
fmt.Printf("Core instance is ready. Took %s\n", time.Since(start))
}
err = addToRollBack(err, syncDeployment.Name, configOverrides.Context.Namespace, "deployment", &resourcesCreated)
if err != nil {
return err
}
fmt.Printf("Core instance created successfully\n")
fmt.Printf("Deployed images=(sync-to-cloud: %s, sync-from-cloud: %s)\n", coreDockerToCloudImage, coreDockerFromCloudImage)
fmt.Printf("Resources created:\n")
fmt.Printf("Deployment=%s\n", syncDeployment.Name)
fmt.Printf("Secret=%s\n", secret.Name)
fmt.Printf("ClusterRole=%s\n", clusterRole.Name)
fmt.Printf("ClusterRoleBinding=%s\n", binding.Name)
fmt.Printf("ServiceAccount=%s\n", serviceAccount.Name)
return nil
},
}
fs := cmd.Flags()
fs.StringVar(&coreInstanceName, "name", "", "Core instance name (autogenerated if empty)")
fs.StringVar(&coreFluentBitDockerImage, "fluent-bit-image", "", "Calyptia core fluent-bit image to use.")
fs.StringVar(&coreCloudURL, "core-cloud-url", config.BaseURL, "Override the cloud URL for the core operator instance")
fs.StringVar(&coreDockerToCloudImage, "image-to-cloud", "", "Calyptia core operator (to-cloud) docker image to use (fully composed docker image).")
err := fs.MarkHidden("image-to-cloud")
if err != nil {
return nil
}
fs.StringVar(&coreDockerFromCloudImage, "image-from-cloud", "", "Calyptia core operator (from-cloud) docker image to use (fully composed docker image).")
err = fs.MarkHidden("image-from-cloud")
if err != nil {
return nil
}
fs.BoolVar(&waitReady, "wait", false, "Wait for the core instance to be ready before returning")
fs.DurationVar(&waitTimeout, "timeout", time.Second*30, "Wait timeout")
fs.BoolVar(&noHealthCheckPipeline, "no-health-check-pipeline", false, "Disable health check pipeline creation alongside the core instance")
fs.StringVar(&healthCheckPipelinePort, "health-check-pipeline-port-number", "", "Port number to expose the health-check pipeline")
fs.StringVar(&healthCheckPipelineServiceType, "health-check-pipeline-service-type", "", fmt.Sprintf("Service type to use for health-check pipeline, options: %s", AllValidPortKinds()))
fs.BoolVar(&enableClusterLogging, "enable-cluster-logging", false, "Enable cluster logging pipeline creation.")
fs.BoolVar(&skipServiceCreation, "skip-service-creation", false, "Skip the creation of kubernetes services for any pipeline under this core instance.")
fs.BoolVar(&dryRun, "dry-run", false, "Passing this value will skip creation of any Kubernetes resources and it will return resources as YAML manifest")
fs.BoolVar(&noTLSVerify, "no-tls-verify", false, "Disable TLS verification when connecting to Calyptia Cloud API.")
fs.StringVar(&metricsPort, "metrics-port", "15334", "Port for metrics endpoint.")
fs.StringVar(&memoryLimit, "memory-limit", "512Mi", "Minimum memory required")
fs.StringVar(&environment, "environment", "", "Calyptia environment name")
fs.StringVar(&httpProxy, "http-proxy", "", "http proxy to use on this core instance")
fs.StringVar(&httpsProxy, "https-proxy", "", "http proxy to use on this core instance")
fs.StringVar(&annotations, "annotations", "", "Custom annotations for pipelines. Format should be 'annotation1=value1,annotation2=value2'")
fs.StringVar(&tolerations, "tolerations", "", `Custom tolerations for pipelines. Format should be 'key1=Equal:value1:Execute:3600,key2=Exists:value2:NoExecute`)
fs.StringSliceVar(&tags, "tags", nil, "Tags to apply to the core instance")
clientcmd.BindOverrideFlags(configOverrides, fs, clientcmd.RecommendedConfigOverrideFlags("kube-"))
_ = cmd.RegisterFlagCompletionFunc("environment", completer.CompleteEnvironments)
_ = cmd.RegisterFlagCompletionFunc("version", completer.CompleteCoreContainerVersion)
return cmd
}
func addToRollBack(err error, name string, namespace string, resource string, resourcesCreated *[]k8s.ResourceRollBack) error {
if err != nil {
return err
}
*resourcesCreated = append(*resourcesCreated, k8s.ResourceRollBack{
Name: name,
Namespace: namespace,
GVR: schema.GroupVersionResource{
Resource: resource,
},
})
return nil
}
func getCoreInstanceMetadata(k8s *k8s.Client) (cloud.CoreInstanceMetadata, error) {
var metadata cloud.CoreInstanceMetadata
info, err := k8s.GetClusterInfo()
if err != nil {
return metadata, err
}
metadata.Namespace = info.Namespace
metadata.ClusterVersion = info.Version
metadata.ClusterPlatform = info.Platform
return metadata, nil
}
func getClusterName() (string, error) {
var err error
loadingRules := clientcmd.NewDefaultClientConfigLoadingRules()
rawKubeConfig, err := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &clientcmd.ConfigOverrides{}).RawConfig()
if err != nil {
return "", err
}
clusterName := rawKubeConfig.CurrentContext
if clusterName == "" {
clusterName = "default"
}
return clusterName, nil
}
func ValidPipelinePortKind(s string) bool {
for _, pk := range cloud.AllValidPipelinePortKinds {
if cloud.PipelinePortKind(s) == pk {
return true
}
}
return false
}
func AllValidPortKinds() string {
var r []string
for _, v := range cloud.AllValidPipelinePortKinds {
r = append(r, string(v))
}
return strings.Join(r, ",")
}