-
Notifications
You must be signed in to change notification settings - Fork 788
/
step_cluster_label.go
89 lines (77 loc) · 2.56 KB
/
step_cluster_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
package cluster
import (
"fmt"
"github.com/jenkins-x/jx-logging/pkg/log"
"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/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
var (
stepClusterLabelLong = templates.LongDesc(`Labels the given cluster.`)
stepClusterLabelExample = templates.Examples(`
`)
)
// StepClusterLabelOptions contains the command line flags and other helper objects
type StepClusterLabelOptions struct {
StepClusterOptions
Labels []string
ClusterName string
}
// NewCmdStepClusterLabel Creates a new Command object
func NewCmdStepClusterLabel(commonOpts *opts.CommonOptions) *cobra.Command {
options := &StepClusterLabelOptions{
StepClusterOptions: StepClusterOptions{
StepOptions: step.StepOptions{
CommonOptions: commonOpts,
},
},
}
cmd := &cobra.Command{
Use: "label",
Short: "Labels the given cluster",
Long: stepClusterLabelLong,
Example: stepClusterLabelExample,
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
helper.CheckErr(err)
},
}
options.ClusterOptions.AddClusterFlags(cmd)
cmd.Flags().StringArrayVarP(&options.Labels, "label", "l", nil, "The label key and value to set. Of the form 'label=value'")
cmd.Flags().StringVarP(&options.ClusterName, "name", "n", "", "The name of the cluster to unlock")
return cmd
}
// Run generates the report
func (o *StepClusterLabelOptions) Run() error {
if len(o.Labels) == 0 {
return util.MissingOption("label")
}
client, err := o.ClusterOptions.CreateClient(true)
if err != nil {
return err
}
clusterName, err := o.GetOrPickClusterName(client, o.ClusterName)
if err != nil {
return err
}
cluster, err := client.Get(clusterName)
if err != nil {
return errors.Wrapf(err, "failed to find cluster name %s using client %s", clusterName, client.String())
}
if cluster == nil {
return fmt.Errorf("there is no cluster called %s using client %s", clusterName, client.String())
}
labels := util.MergeMaps(map[string]string{}, cluster.Labels, util.KeyValuesToMap(o.Labels))
err = client.SetClusterLabels(cluster, labels)
if err != nil {
return errors.Wrapf(err, "failed to set cluster %s labels %s with client %s", clusterName, util.MapToString(labels), client.String())
}
log.Logger().Infof("the cluster %s now has labels %s", util.ColorInfo(cluster.Name), util.ColorInfo(util.MapToString(labels)))
return err
}