/
up.go
470 lines (416 loc) · 12.7 KB
/
up.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
package command
import (
"context"
"encoding/base64"
"fmt"
"path/filepath"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/fatih/color"
"github.com/joho/godotenv"
"github.com/pkg/errors"
"github.com/samber/lo"
"github.com/spf13/cobra"
"go.jetpack.io/envsec"
"go.jetpack.io/launchpad/goutil"
"go.jetpack.io/launchpad/goutil/errorutil"
"go.jetpack.io/launchpad/launchpad"
"go.jetpack.io/launchpad/padcli/command/jflags"
"go.jetpack.io/launchpad/padcli/jetconfig"
"go.jetpack.io/launchpad/padcli/k8s"
"go.jetpack.io/launchpad/padcli/provider"
"go.jetpack.io/launchpad/pkg/jetlog"
"go.jetpack.io/launchpad/pkg/kubevalidate"
)
const (
minReplicaFlag = "min-replicas"
mountSecretFileFlag = "mount-secret-file"
mountSecretFilesFlag = "mount-secret-files"
publicFlag = "public"
envOverrideFlag = "env-override"
)
// TODO: move all flag messages and flag names to the same place to re-use
const envOverrideFlagMsg = "Specifies Env file(s) to use. " +
"This makes Launchpad ignore environment variables set in `launchpad env`. "
// TODO: Move to a common file
var green = color.New(color.FgGreen)
type publishOptions struct {
// ImageRepo is <registry-uri>/<repository-path>
ImageRepo string
}
type upOptions struct {
deployOptions
embeddedBuildOptions
publishOptions
}
// set here for test
var upOpts = &upOptions{}
func upCmd() *cobra.Command {
opts := upOpts
upCmd := &cobra.Command{
Use: "up [path]",
Short: "Builds and deploys app",
Args: cobra.MaximumNArgs(1),
PreRunE: validateDeployUptions(&opts.deployOptions),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
absPath, err := projectDir(args)
if err != nil {
return errors.WithStack(err)
}
jetCfg, err := loadOrInitConfigFromFileSystem(ctx, cmd, args)
if err != nil {
return errors.WithStack(err)
}
ctx, err = cmdOpts.AuthProvider().Identify(cmd.Context())
if err != nil {
return errors.WithStack(err)
}
cluster, err := cmdOpts.ClusterProvider().Get(ctx)
if err != nil {
return errors.WithStack(err)
}
imageRepo := goutil.Coalesce(opts.ImageRepo, jetCfg.ImageRepository)
repoConfig, err := cmdOpts.RepositoryProvider().Get(ctx, cluster, imageRepo)
if err != nil {
return errors.WithStack(err)
}
store, err := newEnvStore(ctx, cmd, args, cmdOpts.EnvSecProvider(), jetCfg.Envsec.Provider)
if err != nil {
return errors.WithStack(err)
}
pad := launchpad.NewPad(cmdOpts.ErrorLogger())
do, bpdErr := buildPublishAndDeploy(
ctx,
pad,
cmd,
jetCfg,
&opts.embeddedBuildOptions,
&opts.deployOptions,
imageRepo,
absPath,
cluster,
repoConfig,
store,
)
if bpdErr != nil {
// NOTE: we should check that bpdError is a deploy error and not a build or publish error
if errors.Is(bpdErr, launchpad.ErrPodContainerError) {
return bpdErr
}
if err := pad.TailLogsOnErr(ctx, cluster.GetKubeContext(), do); err != nil {
return errors.Wrap(bpdErr, "failed to tail logs")
}
// TODO(Savil): Waiting 2 seconds is not reliable. We need to actually
// listen to the an event that tells us the logs have been displayed
secondsToWait := 2
fmt.Printf("Waiting %d seconds for all logs to be shown\n", secondsToWait)
time.Sleep(time.Duration(secondsToWait) * time.Second)
return errorutil.AddUserMessagef(
bpdErr,
"Deploy was not successful. See pod logs for more details.",
)
}
return printUpSuccess(ctx, do, cluster)
},
PostRun: func(cmd *cobra.Command, args []string) {
if err := cleanupPreviousBuildsPostRun(cmd, args); err != nil {
cmdOpts.ErrorLogger().CaptureException(err)
return
}
},
}
registerUpFlags(upCmd, opts)
return upCmd
}
func registerUpFlags(cmd *cobra.Command, opts *upOptions) {
jflags.RegisterCommonFlags(cmd, cmdOpts)
registerDeployFlags(cmd, &opts.deployOptions)
registerEmbeddedBuildFlags(cmd, &opts.embeddedBuildOptions)
registerPublishFlags(cmd, &opts.publishOptions)
}
func registerPublishFlags(cmd *cobra.Command, opts *publishOptions) {
cmd.Flags().StringVarP(
&opts.ImageRepo,
"image-repository",
"i",
"",
provider.ImageRepositoryFlagHelpMsg,
)
}
func registerDeployFlags(cmd *cobra.Command, opts *deployOptions) {
cmd.Flags().StringVar(
&opts.App.SetValues,
"helm.app.set",
"",
"args passed to helm --set option for app (can specify multiple or separate values with commas: key1=val1,key2=val2)",
)
cmd.Flags().StringSliceVar(
&opts.App.ValueFiles,
"helm.app.values",
[]string{},
"args passed to helm --values option for app. See helm docs for more info.",
)
cmd.Flags().StringVar(
&opts.App.ChartLocation,
"helm.app.chart-location",
"",
"custom location for app chart",
)
_ = cmd.Flags().MarkHidden("helm.app.chart-location")
cmd.Flags().StringVar(
&opts.App.Name,
"helm.app.name",
"",
"App install name",
)
_ = cmd.Flags().MarkHidden("helm.app.name")
cmd.Flags().StringVar(
&opts.Runtime.SetValues,
"helm.runtime.set",
"",
"args passed to helm --set option for runtime (can specify multiple or separate values with commas: key1=val1,key2=val2)",
)
_ = cmd.Flags().MarkHidden("helm.runtime.set")
cmd.Flags().StringVar(
&opts.Runtime.ChartLocation,
"helm.runtime.chart-location",
"",
"custom location for runtime chart",
)
_ = cmd.Flags().MarkHidden("helm.runtime.chart-location")
cmd.Flags().StringVarP(
&opts.Namespace,
"namespace",
"n",
"",
"K8s Namespace",
)
cmd.Flags().IntVar(
lo.ToPtr(lo.Empty[int]()),
minReplicaFlag,
0, // Should be in sync with helm/app/values.yaml
"Minimum number of replicas. Alias for "+
"--helm.app.set autoscaling.minReplicas=n. Setting "+
"min-replica count to zero will skip the creation of a deployment and "+
"service. Setting anything above zero will enable pod autoscalling",
)
_ = cmd.Flags().MarkHidden(minReplicaFlag)
_ = cmd.Flags().MarkDeprecated(
minReplicaFlag,
"This flag is deprecated and no longer does anything. min replicas is "+
"always 1",
)
cmd.Flags().StringSliceVar(
&opts.SecretFilePaths,
mountSecretFilesFlag,
[]string{},
"Mount a set of secret files : "+
"Each mounted entry has a name derived from the base file name",
)
cmd.Flags().BoolVar(
lo.ToPtr(lo.Empty[bool]()),
publicFlag,
false,
"Expose service publicly using API gateway.",
)
_ = cmd.Flags().MarkHidden(publicFlag)
_ = cmd.Flags().MarkDeprecated(
publicFlag,
"This flag is deprecated and no longer does anything. web apps are always public",
)
cmd.Flags().StringVar(
&opts.envOverrideFile,
envOverrideFlag,
"",
envOverrideFlagMsg,
)
cmd.Flags().StringVar(
&opts.execQualifiedSymbol,
"exec",
"",
"Execute a single cronjob or jetroutine for a given function.",
)
_ = cmd.Flags().MarkHidden("exec")
cmd.Flags().BoolVar(
&opts.ReinstallOnHelmUpgradeError,
"reinstall-on-error",
false,
"Attempt reinstall of helm charts if upgrade fails. This flag does not "+
"affect initial installs. Warning: reinstalling will cause loss of "+
"revision history and potential downtime.",
)
}
// TODO: lets figure out a way to reduce this long list of parameters
func buildPublishAndDeploy(
ctx context.Context,
pad launchpad.LaunchPad,
cmd *cobra.Command,
jetCfg *jetconfig.Config,
buildOpts *embeddedBuildOptions,
deployOpts *deployOptions,
imageRepoOverride string,
projPath string,
cluster provider.Cluster,
repoConfig provider.RepoConfig,
store envsec.Store,
) (*launchpad.DeployOutput, error) {
_, buildOutput, err := execLaunchpadBuild(
ctx, pad, buildOpts, jetCfg, cluster, projPath, repoConfig, "1/3" /*stepInfo*/)
if err != nil {
return nil, errors.Wrap(err, "Error building image")
}
_, pubOutput, err := execLaunchpadPublish(
ctx, pad, buildOutput, imageRepoOverride, repoConfig, cluster, jetCfg, "2/3" /*stepInfo*/)
if err != nil {
return nil, errors.Wrap(err, "Error publishing images")
}
jetlog.Logger(ctx).HeaderPrintf("Step 3/3: Deploying project to your cluster")
lpDeployOpts, err := makeDeployOptions(
ctx, cmd, jetCfg, pubOutput, buildOutput, deployOpts, projPath, cluster, store,
)
if err != nil {
return nil, errors.Wrap(err, "Error deploying")
}
do, err := pad.Deploy(ctx, lpDeployOpts)
if err != nil {
return nil, errors.Wrap(err, "failed to deploy image")
}
jetlog.Logger(ctx).HeaderPrintf(
"[Done] App deployed in %s",
do.Duration.Truncate(time.Millisecond*100),
)
return do, nil
}
func printUpSuccess(
ctx context.Context,
do *launchpad.DeployOutput,
c provider.Cluster,
) error {
jetlog.Logger(ctx).Println(green.Sprintf(
"App deployed to namespace \"%s\"",
do.Namespace,
))
// TODO(Landau) This gets more complicated when we add internal services.
// Consider adding ambassador.enabled value.
values := do.Releases[launchpad.AppChartName].Config
if fmt.Sprintf("%v", values["replicaCount"]) == "0" {
return nil
}
if c.IsLocal() {
name := values["jetpack"].(map[string]any)["instanceName"].(string) +
// Ugh, this makes me so sad
"-" + launchpad.AppChartName
port, err := k8s.ServiceNodePort(ctx, name, do.Namespace, c.GetKubeContext())
if err != nil {
return errors.Wrap(err, "failed to get service node port")
}
jetlog.Logger(ctx).Println(
green.Sprintf("App reachable at http://localhost:%d", port),
)
return nil
}
if amby, ok := values["ambassador"].(map[string]any); ok {
host := amby["hostname"].(string)
if host != "" {
jetlog.Logger(ctx).Println(green.Sprintf("App reachable at https://%s", host))
}
}
return nil
}
func readEnvVariables(
projectPath string,
envFile string,
encodeValues bool,
) (map[string]string, error) {
if !filepath.IsAbs(envFile) {
envFile = filepath.Join(projectPath, envFile)
}
vars, err := godotenv.Read(envFile)
if err != nil {
return nil, errorutil.ConvertToUserError(err)
}
var envVars = map[string]string{}
// encoded values are used for Kubernetes deployments
if encodeValues {
for key, value := range vars {
envVars[key] = base64.StdEncoding.EncodeToString([]byte(value))
}
} else { // non-encoded values are used for launchpad local
envVars = vars
}
return envVars, nil
}
func validateDeployUptions(opts *deployOptions) func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) error {
// TODO: this should load config, merge it into flags and then validate.
// TODO: prevent env-override and environment=prod flags together
err := validateNamespace(opts.Namespace)
if err != nil {
return errors.WithStack(err)
}
return nil
}
}
func validateNamespace(namespace string) error {
// namespace follows the same RFC1123 standard as subdomain
if namespace != "" && !kubevalidate.IsValidRFC1123Name(namespace) {
msg := "Invalid value for namespace %s.\n" +
"A lowercase RFC 1123 label must consist of:\n" +
" - alphanumeric characters\n" +
" - or '-' with starting and ending with an alphanumeric character.\n"
return errorutil.NewUserErrorf(msg, namespace)
}
return nil
}
func makePublishOptions(
imageRepoOverride string,
repoConfig provider.RepoConfig,
buildOutput *launchpad.BuildOutput,
config *jetconfig.Config,
) (*launchpad.PublishOptions, error) {
imageRegistryWithRepo := ""
if imageRepoOverride != "" {
imageRegistryWithRepo = imageRepoOverride
// This means anytime an override repo is set we ignore mc credentials.
// We could improve this by having mc return an ECR path that the user has
// access to and checking if the override repo is part of that path.
repoConfig = nil
} else if repoConfig != nil {
suffix, err := kubevalidate.ToValidName(config.GetProjectNameWithSlug())
if err != nil {
return nil, err
}
imageRegistryWithRepo = repoConfig.GetImageRepoPrefix() + "/" + suffix
} else {
return nil, errorutil.NewUserError(
"No image repo specified. Please use --image-repository flag or " +
"imageRepository field in launchpad.yaml or jetconfig.yaml to specify",
)
}
localImagesToPublish := []*launchpad.LocalImage{}
if buildOutput.DidBuildUsingDockerfile() {
localImagesToPublish = append(localImagesToPublish, buildOutput.Image)
}
for _, service := range config.Builders() {
if service.ShouldPublish() {
localImagesToPublish = append(
localImagesToPublish,
launchpad.NewLocalImage(service.GetImage()),
)
}
}
opts := &launchpad.PublishOptions{
ImageRegistryWithRepo: imageRegistryWithRepo,
LifecycleHook: cmdOpts.Hooks().Publish,
LocalImages: localImagesToPublish,
TagPrefix: cmdOpts.RootFlags().Env().ImageTagPrefix(),
}
if repoConfig != nil {
awsCreds, _ := repoConfig.GetCloudCredentials().(aws.CredentialsProvider)
opts.AWSCredentials = awsCreds
opts.ImageRepoCredentials = repoConfig.GetCredentials()
opts.Region = repoConfig.GetRegion()
}
return opts, nil
}