-
Notifications
You must be signed in to change notification settings - Fork 787
/
gke.go
88 lines (75 loc) · 2.45 KB
/
gke.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
package gke
import (
"fmt"
"os"
gcp "github.com/jenkins-x/jx/v2/pkg/cloud/gke"
"github.com/jenkins-x/jx/v2/pkg/cluster"
"github.com/jenkins-x/jx/v2/pkg/util"
"github.com/pkg/errors"
)
type gcloud struct {
region string
project string
gcloud gcp.GCloud
}
// NewGKE create a new client for working with GKE clusters using the given region and project
func NewGKE(project string, region string) (cluster.Client, error) {
return &gcloud{
region: region,
project: project,
}, nil
}
// NewGKEFromEnv create a new client for working with GKE clusters using environment variables to define the region/project
func NewGKEFromEnv() (cluster.Client, error) {
project := os.Getenv(cluster.EnvGKEProject)
if project == "" {
return nil, util.MissingEnv(cluster.EnvGKEProject)
}
region := os.Getenv(cluster.EnvGKERegion)
if region == "" {
return nil, util.MissingEnv(cluster.EnvGKERegion)
}
return NewGKE(project, region)
}
// List lists the clusters
func (c *gcloud) List() ([]*cluster.Cluster, error) {
items, err := c.gcloud.ListClusters(c.region, c.project)
if err != nil {
return nil, errors.Wrapf(err, "failed to list clusters in region %s project %s", c.region, c.project)
}
var answer []*cluster.Cluster
for _, item := range items {
answer = append(answer, &cluster.Cluster{
Name: item.Name,
Labels: item.ResourceLabels,
Status: item.Status,
Location: item.Location,
})
}
return answer, nil
}
// ListFilter lists the clusters with a filter
func (c *gcloud) ListFilter(labels map[string]string) ([]*cluster.Cluster, error) {
return cluster.ListFilter(c, labels)
}
// Connect connects to a cluster
func (c *gcloud) Connect(cluster *cluster.Cluster) error {
return c.gcloud.ConnectToRegionCluster(c.project, cluster.Location, cluster.Name)
}
// String return the string representation
func (c *gcloud) String() string {
return fmt.Sprintf("GKE project: %s region: %s", c.project, c.region)
}
// Get looks up a cluster by name
func (c *gcloud) Get(name string) (*cluster.Cluster, error) {
return cluster.GetCluster(c, name)
}
// Delete should delete the cluster from GKE
func (c *gcloud) Delete(cluster *cluster.Cluster) error {
return fmt.Errorf("not implemented")
}
// SetClusterLabels labels the given cluster
func (c *gcloud) SetClusterLabels(cluster *cluster.Cluster, labelMap map[string]string) error {
labels := util.MapToKeyValues(labelMap)
return c.gcloud.UpdateGkeClusterLabels(cluster.Location, c.project, cluster.Name, labels)
}