github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cloudprovider/types.go (about)

     1  /*
     2  Copyright (C) 2022-2023 ApeCloud Co., Ltd
     3  
     4  This file is part of KubeBlocks project
     5  
     6  This program is free software: you can redistribute it and/or modify
     7  it under the terms of the GNU Affero General Public License as published by
     8  the Free Software Foundation, either version 3 of the License, or
     9  (at your option) any later version.
    10  
    11  This program is distributed in the hope that it will be useful
    12  but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  GNU Affero General Public License for more details.
    15  
    16  You should have received a copy of the GNU Affero General Public License
    17  along with this program.  If not, see <http://www.gnu.org/licenses/>.
    18  */
    19  
    20  package cloudprovider
    21  
    22  import (
    23  	"fmt"
    24  	"strings"
    25  
    26  	"github.com/hashicorp/terraform-exec/tfexec"
    27  )
    28  
    29  type outputKey string
    30  
    31  // terraform output keys
    32  const (
    33  	clusterNameKey outputKey = "cluster_name"
    34  	regionKey      outputKey = "region"
    35  	kubeConfigKey  outputKey = "kube_config"
    36  )
    37  
    38  const (
    39  	GitRepoName = "cloud-provider"
    40  	GitRepoURL  = "https://github.com/apecloud/cloud-provider"
    41  )
    42  
    43  const (
    44  	Local        = "local"
    45  	AWS          = "aws"
    46  	AliCloud     = "alicloud"
    47  	Azure        = "azure"
    48  	GCP          = "gcp"
    49  	TencentCloud = "tencentcloud"
    50  )
    51  
    52  var (
    53  	cloudProviderK8sServiceMap = map[string]string{
    54  		Local:        "k3s",
    55  		AWS:          "eks",
    56  		AliCloud:     "ack",
    57  		Azure:        "aks",
    58  		GCP:          "gke",
    59  		TencentCloud: "tke",
    60  	}
    61  )
    62  
    63  func CloudProviders() []string {
    64  	return []string{Local, AWS, Azure, GCP, AliCloud, TencentCloud}
    65  }
    66  
    67  func K8sService(provider string) string {
    68  	return cloudProviderK8sServiceMap[provider]
    69  }
    70  
    71  // K8sClusterInfo is the kubernetes cluster information for playground that will
    72  // be serialized to a state file
    73  type K8sClusterInfo struct {
    74  	ClusterName   string `json:"cluster_name"`
    75  	CloudProvider string `json:"cloud_provider"`
    76  	Region        string `json:"region,omitempty"`
    77  	KubeConfig    string `json:"kube_config,omitempty"`
    78  	KbcliVersion  string `json:"kbcli_version,omitempty"`
    79  }
    80  
    81  // IsValid checks if kubernetes cluster info is valid
    82  func (c *K8sClusterInfo) IsValid() bool {
    83  	if c.ClusterName == "" || c.CloudProvider == "" || (c.CloudProvider != Local && c.Region == "") {
    84  		return false
    85  	}
    86  	return true
    87  }
    88  
    89  func (c *K8sClusterInfo) String() string {
    90  	fields := []string{"  cloud provider: " + c.CloudProvider,
    91  		"cluster name: " + c.ClusterName,
    92  	}
    93  	if c.CloudProvider != Local {
    94  		fields = append(fields, "region: "+c.Region)
    95  	}
    96  	return strings.Join(fields, "\n  ")
    97  }
    98  
    99  func (c *K8sClusterInfo) buildApplyOpts() []tfexec.ApplyOption {
   100  	return []tfexec.ApplyOption{tfexec.Var(fmt.Sprintf("%s=%s", clusterNameKey, c.ClusterName)),
   101  		tfexec.Var(fmt.Sprintf("%s=%s", regionKey, c.Region))}
   102  }
   103  
   104  func (c *K8sClusterInfo) buildDestroyOpts() []tfexec.DestroyOption {
   105  	return []tfexec.DestroyOption{tfexec.Var(fmt.Sprintf("%s=%s", clusterNameKey, c.ClusterName)),
   106  		tfexec.Var(fmt.Sprintf("%s=%s", regionKey, c.Region))}
   107  }