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

     1  package gke
     2  
     3  import (
     4  	"sort"
     5  	"strings"
     6  
     7  	"github.com/pkg/errors"
     8  
     9  	"github.com/jenkins-x/jx/v2/pkg/util"
    10  )
    11  
    12  var PROJECT_LIST_HEADER = "PROJECT_ID"
    13  
    14  func GetGoogleZones(project string) ([]string, error) {
    15  	var zones []string
    16  	args := []string{"compute", "zones", "list"}
    17  
    18  	if "" != project {
    19  		args = append(args, "--project")
    20  		args = append(args, project)
    21  	}
    22  
    23  	cmd := util.Command{
    24  		Name: "gcloud",
    25  		Args: args,
    26  	}
    27  
    28  	out, err := cmd.RunWithoutRetry()
    29  	if err != nil {
    30  		return nil, err
    31  	}
    32  
    33  	for _, item := range strings.Split(out, "\n") {
    34  		zone := strings.Split(item, " ")[0]
    35  		if strings.Contains(zone, "-") {
    36  			zones = append(zones, zone)
    37  		}
    38  		sort.Strings(zones)
    39  	}
    40  	return zones, nil
    41  }
    42  
    43  func GetGoogleRegions(project string) ([]string, error) {
    44  	var regions []string
    45  	args := []string{"compute", "regions", "list"}
    46  
    47  	if "" != project {
    48  		args = append(args, "--project")
    49  		args = append(args, project)
    50  	}
    51  
    52  	cmd := util.Command{
    53  		Name: "gcloud",
    54  		Args: args,
    55  	}
    56  
    57  	out, err := cmd.RunWithoutRetry()
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  
    62  	regions = append(regions, "none")
    63  	for _, item := range strings.Split(out, "\n") {
    64  		region := strings.Split(item, " ")[0]
    65  		if strings.Contains(region, "-") {
    66  			regions = append(regions, region)
    67  		}
    68  		sort.Strings(regions)
    69  	}
    70  	return regions, nil
    71  }
    72  
    73  func GetGoogleProjects() ([]string, error) {
    74  	cmd := util.Command{
    75  		Name: "gcloud",
    76  		Args: []string{"projects", "list"},
    77  	}
    78  	out, err := cmd.RunWithoutRetry()
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	if out == "Listed 0 items." {
    84  		return []string{}, nil
    85  	}
    86  
    87  	lines := strings.Split(out, "\n")
    88  	var existingProjects []string
    89  	for _, l := range lines {
    90  		if strings.Contains(l, PROJECT_LIST_HEADER) {
    91  			continue
    92  		}
    93  		fields := strings.Fields(l)
    94  		existingProjects = append(existingProjects, fields[0])
    95  	}
    96  	return existingProjects, nil
    97  }
    98  
    99  func GetCurrentProject() (string, error) {
   100  	cmd := util.Command{
   101  		Name: "gcloud",
   102  		Args: []string{"config", "get-value", "project"},
   103  	}
   104  	out, err := cmd.RunWithoutRetry()
   105  	if err != nil {
   106  		return "", err
   107  	}
   108  
   109  	index := strings.LastIndex(out, "\n")
   110  	if index >= 0 {
   111  		return out[index+1:], nil
   112  	}
   113  
   114  	return out, nil
   115  }
   116  
   117  func GetGoogleMachineTypes() []string {
   118  
   119  	return []string{
   120  		"g1-small",
   121  		"n1-standard-1",
   122  		"n1-standard-2",
   123  		"n1-standard-4",
   124  		"n1-standard-8",
   125  		"n1-standard-16",
   126  		"n1-standard-32",
   127  		"n1-standard-64",
   128  		"n1-standard-96",
   129  		"n1-highmem-2",
   130  		"n1-highmem-4",
   131  		"n1-highmem-8",
   132  		"n1-highmem-16",
   133  		"n1-highmem-32",
   134  		"n1-highmem-64",
   135  		"n1-highmem-96",
   136  		"n1-highcpu-2",
   137  		"n1-highcpu-4",
   138  		"n1-highcpu-8",
   139  		"n1-highcpu-16",
   140  		"n1-highcpu-32",
   141  		"n1-highcpu-64",
   142  		"n1-highcpu-96",
   143  	}
   144  }
   145  
   146  // ParseContext parses the context string for GKE and gets the GKE project, GKE zone and cluster name
   147  func ParseContext(context string) (string, string, string, error) {
   148  	parts := strings.Split(context, "_")
   149  	if len(parts) != 4 {
   150  		return "", "", "", errors.Errorf("unable to parse %s as <project id>_<zone>_<cluster name>", context)
   151  	}
   152  	return parts[1], parts[2], parts[3], nil
   153  }