-
Notifications
You must be signed in to change notification settings - Fork 785
/
step_e2e_label.go
112 lines (102 loc) · 2.92 KB
/
step_e2e_label.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
package e2e
import (
"errors"
"github.com/jenkins-x/jx-logging/pkg/log"
"github.com/jenkins-x/jx/v2/pkg/cloud"
"github.com/jenkins-x/jx/v2/pkg/cmd/helper"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts"
"github.com/jenkins-x/jx/v2/pkg/cmd/opts/step"
"github.com/jenkins-x/jx/v2/pkg/cmd/templates"
"github.com/spf13/cobra"
)
// StepE2ELabelOptions contains the command line flags
type StepE2ELabelOptions struct {
step.StepOptions
ProjectID string
Region string
Keep bool
Delete bool
}
var (
stepE2EApplyLabelLong = templates.LongDesc(`
Add a label to a cluster used for e2e testing
`)
stepE2EApplyLabelExample = templates.Examples(`
# Mark a cluster to not be deleted by the gc
jx step e2e label --keep clusterName
# Mark a cluster to be deleted by the gc
jx step e2e label --delete clusterName
`)
)
// NewCmdStepE2ELabel creates the CLI command
func NewCmdStepE2ELabel(commonOpts *opts.CommonOptions) *cobra.Command {
options := StepE2ELabelOptions{
StepOptions: step.StepOptions{
CommonOptions: commonOpts,
},
}
cmd := &cobra.Command{
Use: "label",
Short: "Removes unused e2e clusters",
Aliases: []string{},
Long: stepE2EApplyLabelLong,
Example: stepE2EApplyLabelExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Region, "region", "", "europe-west1-c", "GKE region to use. Default: europe-west1-c")
cmd.Flags().StringVarP(&options.ProjectID, "project-id", "p", "", "Google Project ID to delete cluster from")
cmd.Flags().BoolVarP(&options.Keep, "keep", "k", false, "Add a label top mark cluster for non deletion")
cmd.Flags().BoolVarP(&options.Delete, "delete", "d", false, "Add a label top mark cluster for deletion")
return cmd
}
// Run runs the command
func (o *StepE2ELabelOptions) Run() error {
if len(o.Args) == 0 {
return errors.New("Please specify a cluster name")
}
if o.Keep == o.Delete {
return errors.New("Please specify either --keep or --delete")
}
err := o.InstallRequirements(cloud.GKE)
if err != nil {
return err
}
clusterName := o.Args[0]
cluster, err := o.GCloud().LoadGkeCluster(o.Region, o.ProjectID, clusterName)
if err != nil {
return err
}
if cluster != nil {
labelMap := cluster.ResourceLabels
labels := make([]string, 0)
if o.Keep {
labelMap["keep-me"] = "true"
} else {
delete(labelMap, "keep-me")
}
if o.Delete {
labelMap["delete-me"] = "true"
} else {
delete(labelMap, "delete-me")
}
for key, value := range labelMap {
labels = append(labels, key+"="+value)
}
err := o.GCloud().UpdateGkeClusterLabels(o.Region, o.ProjectID, clusterName, labels)
if err == nil {
if o.Keep {
log.Logger().Infof("%s was marked to be kept", cluster.Name)
} else {
log.Logger().Infof("%s was marked to be deleted", cluster.Name)
}
} else {
log.Logger().Error(err)
}
}
return nil
}