/
clusters.go
103 lines (92 loc) · 2.81 KB
/
clusters.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
package opts
import (
"sort"
"github.com/jenkins-x/jx-logging/pkg/log"
gcp "github.com/jenkins-x/jx/v2/pkg/cloud/gke"
"github.com/jenkins-x/jx/v2/pkg/cluster/fake"
"github.com/jenkins-x/jx/v2/pkg/cluster"
"github.com/jenkins-x/jx/v2/pkg/cluster/gke"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
// ClusterOptions used to determine which kind of cluster to query
type ClusterOptions struct {
GKE GKEClusterOptions
Fake bool
}
// GKEClusterOptions GKE specific configurations
type GKEClusterOptions struct {
Project string
Region string
}
// CreateClient creates a new cluster client from the CLI options
func (o *ClusterOptions) CreateClient(requireProject bool) (cluster.Client, error) {
if o.Fake {
clusters := []*cluster.Cluster{
{
Name: "cluster1",
},
{
Name: "cluster2",
},
{
Name: "cluster1",
},
}
return fake.NewClient(clusters), nil
}
if o.GKE.Project != "" || o.GKE.Region != "" {
if o.GKE.Project == "" {
return nil, util.MissingOption("gke-project")
}
if o.GKE.Region == "" {
return nil, util.MissingOption("gke-region")
}
}
if o.GKE.Project == "" {
// lets try detect the GKE project...
g := &gcp.GCloud{}
project, err := g.CurrentProject()
if err != nil {
return nil, errors.Wrapf(err, "failed to detect if we are using GKE")
}
if requireProject {
if project == "" {
log.Logger().Warn("could not detect the current GKE project")
return nil, util.MissingOption("gke-project")
}
}
o.GKE.Project = project
}
return gke.NewGKE(o.GKE.Project, o.GKE.Region)
//return nil, fmt.Errorf("could not detect the kind of cluster via the command line options")
}
// AddClusterFlags adds the cluster CLI arguments
func (o *ClusterOptions) AddClusterFlags(cmd *cobra.Command) {
cmd.Flags().StringVarP(&o.GKE.Project, "gke-project", "", "", "The GKE project name")
cmd.Flags().StringVarP(&o.GKE.Region, "gke-region", "", "", "The GKE project name")
cmd.Flags().BoolVarP(&o.Fake, "fake", "", false, "Use the fake clusters client")
}
// GetOrPickClusterName returns the selected cluster name or returns an error
func (o *CommonOptions) GetOrPickClusterName(client cluster.Client, clusterName string) (string, error) {
if clusterName == "" && !o.BatchMode {
clusters, err := client.List()
if err != nil {
return "", errors.Wrapf(err, "failed to list clusters for %s", client.String())
}
names := []string{}
for _, c := range clusters {
names = append(names, c.Name)
}
sort.Strings(names)
clusterName, err = util.PickName(names, "pick the cluster name", "you need to provide a cluster name to unlock", o.GetIOFileHandles())
if err != nil {
return "", errors.Wrap(err, "failed to pick cluster name")
}
}
if clusterName == "" {
return "", util.MissingOption("name")
}
return clusterName, nil
}