github.com/jenkins-x/jx/v2@v2.1.155/pkg/cluster/gke/gke.go (about)

     1  package gke
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  
     7  	gcp "github.com/jenkins-x/jx/v2/pkg/cloud/gke"
     8  	"github.com/jenkins-x/jx/v2/pkg/cluster"
     9  	"github.com/jenkins-x/jx/v2/pkg/util"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  type gcloud struct {
    14  	region  string
    15  	project string
    16  	gcloud  gcp.GCloud
    17  }
    18  
    19  // NewGKE create a new client for working with GKE clusters using the given region and project
    20  func NewGKE(project string, region string) (cluster.Client, error) {
    21  	return &gcloud{
    22  		region:  region,
    23  		project: project,
    24  	}, nil
    25  }
    26  
    27  // NewGKEFromEnv create a new client for working with GKE clusters using environment variables to define the region/project
    28  func NewGKEFromEnv() (cluster.Client, error) {
    29  	project := os.Getenv(cluster.EnvGKEProject)
    30  	if project == "" {
    31  		return nil, util.MissingEnv(cluster.EnvGKEProject)
    32  	}
    33  	region := os.Getenv(cluster.EnvGKERegion)
    34  	if region == "" {
    35  		return nil, util.MissingEnv(cluster.EnvGKERegion)
    36  	}
    37  	return NewGKE(project, region)
    38  }
    39  
    40  // List lists the clusters
    41  func (c *gcloud) List() ([]*cluster.Cluster, error) {
    42  	items, err := c.gcloud.ListClusters(c.region, c.project)
    43  	if err != nil {
    44  		return nil, errors.Wrapf(err, "failed to list clusters in region %s project %s", c.region, c.project)
    45  	}
    46  	var answer []*cluster.Cluster
    47  
    48  	for _, item := range items {
    49  		answer = append(answer, &cluster.Cluster{
    50  			Name:     item.Name,
    51  			Labels:   item.ResourceLabels,
    52  			Status:   item.Status,
    53  			Location: item.Location,
    54  		})
    55  	}
    56  	return answer, nil
    57  }
    58  
    59  // ListFilter lists the clusters with a filter
    60  func (c *gcloud) ListFilter(labels map[string]string) ([]*cluster.Cluster, error) {
    61  	return cluster.ListFilter(c, labels)
    62  }
    63  
    64  // Connect connects to a cluster
    65  func (c *gcloud) Connect(cluster *cluster.Cluster) error {
    66  	return c.gcloud.ConnectToRegionCluster(c.project, cluster.Location, cluster.Name)
    67  }
    68  
    69  // String return the string representation
    70  func (c *gcloud) String() string {
    71  	return fmt.Sprintf("GKE project: %s region: %s", c.project, c.region)
    72  }
    73  
    74  // Get looks up a cluster by name
    75  func (c *gcloud) Get(name string) (*cluster.Cluster, error) {
    76  	return cluster.GetCluster(c, name)
    77  }
    78  
    79  // Delete should delete the cluster from GKE
    80  func (c *gcloud) Delete(cluster *cluster.Cluster) error {
    81  	return fmt.Errorf("not implemented")
    82  }
    83  
    84  // SetClusterLabels labels the given cluster
    85  func (c *gcloud) SetClusterLabels(cluster *cluster.Cluster, labelMap map[string]string) error {
    86  	labels := util.MapToKeyValues(labelMap)
    87  	return c.gcloud.UpdateGkeClusterLabels(cluster.Location, c.project, cluster.Name, labels)
    88  }