github.com/jenkins-x/jx/v2@v2.1.155/pkg/cmd/step/cluster/step_cluster_unlock.go (about) 1 package cluster 2 3 import ( 4 "fmt" 5 6 "github.com/jenkins-x/jx-logging/pkg/log" 7 clusters "github.com/jenkins-x/jx/v2/pkg/cluster" 8 "github.com/jenkins-x/jx/v2/pkg/cmd/helper" 9 "github.com/jenkins-x/jx/v2/pkg/cmd/opts" 10 "github.com/jenkins-x/jx/v2/pkg/cmd/opts/step" 11 "github.com/jenkins-x/jx/v2/pkg/cmd/templates" 12 "github.com/jenkins-x/jx/v2/pkg/util" 13 "github.com/pkg/errors" 14 "github.com/spf13/cobra" 15 ) 16 17 var ( 18 stepClusterUnlockLong = templates.LongDesc(`Unlocks the given cluster name so it joins the pool of test clusters again.`) 19 stepClusterUnlockExample = templates.Examples(` 20 `) 21 ) 22 23 // StepClusterUnlockOptions contains the command line flags and other helper objects 24 type StepClusterUnlockOptions struct { 25 StepClusterOptions 26 LockLabel string 27 TestLabel string 28 ClusterName string 29 } 30 31 // NewCmdStepClusterUnlock Creates a new Command object 32 func NewCmdStepClusterUnlock(commonOpts *opts.CommonOptions) *cobra.Command { 33 options := &StepClusterUnlockOptions{ 34 StepClusterOptions: StepClusterOptions{ 35 StepOptions: step.StepOptions{ 36 CommonOptions: commonOpts, 37 }, 38 }, 39 } 40 41 cmd := &cobra.Command{ 42 Use: "unlock", 43 Short: "Unlocks the given cluster name so it joins the pool of test clusters again", 44 Long: stepClusterUnlockLong, 45 Example: stepClusterUnlockExample, 46 Run: func(cmd *cobra.Command, args []string) { 47 options.Cmd = cmd 48 options.Args = args 49 err := options.Run() 50 helper.CheckErr(err) 51 }, 52 } 53 54 options.ClusterOptions.AddClusterFlags(cmd) 55 56 cmd.Flags().StringVarP(&options.LockLabel, "label", "l", "locked", "The label name for the lock") 57 cmd.Flags().StringVarP(&options.TestLabel, "test-label", "", "test", "The label name for the test") 58 cmd.Flags().StringVarP(&options.ClusterName, "name", "n", "", "The name of the cluster to unlock") 59 return cmd 60 } 61 62 // Run generates the report 63 func (o *StepClusterUnlockOptions) Run() error { 64 client, err := o.ClusterOptions.CreateClient(true) 65 if err != nil { 66 return err 67 } 68 clusterName, err := o.GetOrPickClusterName(client, o.ClusterName) 69 if err != nil { 70 return err 71 } 72 73 cluster, err := client.Get(clusterName) 74 if err != nil { 75 return errors.Wrapf(err, "failed to find cluster name %s using client %s", clusterName, client.String()) 76 } 77 if cluster == nil { 78 return fmt.Errorf("there is no cluster called %s using client %s", clusterName, client.String()) 79 } 80 resultLabels, err := clusters.RemoveLabels(client, cluster, []string{o.LockLabel, o.TestLabel}) 81 if err != nil { 82 return err 83 } 84 log.Logger().Infof("unlocked cluster %s so now has labels %s", util.ColorInfo(cluster.Name), util.ColorInfo(util.MapToString(resultLabels))) 85 return err 86 }