github.com/1aal/kubeblocks@v0.0.0-20231107070852-e1c03e598921/pkg/cli/cloudprovider/provider.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 "io" 25 "os" 26 "path/filepath" 27 28 "github.com/1aal/kubeblocks/version" 29 ) 30 31 type cloudProvider struct { 32 name string 33 tfPath string 34 35 stdout io.Writer 36 stderr io.Writer 37 } 38 39 var _ Interface = &cloudProvider{} 40 41 func newCloudProvider(provider, rootPath string, stdout, stderr io.Writer) (Interface, error) { 42 k8sSvc := K8sService(provider) 43 if k8sSvc == "" { 44 return nil, fmt.Errorf("unknown cloud provider %s", provider) 45 } 46 47 tfPath := filepath.Join(rootPath, provider, k8sSvc) 48 if _, err := os.Stat(tfPath); err != nil { 49 return nil, err 50 } 51 52 return &cloudProvider{ 53 name: provider, 54 tfPath: tfPath, 55 stdout: stdout, 56 stderr: stderr, 57 }, nil 58 } 59 60 func (p *cloudProvider) Name() string { 61 return p.name 62 } 63 64 // CreateK8sCluster creates a kubernetes cluster 65 func (p *cloudProvider) CreateK8sCluster(clusterInfo *K8sClusterInfo) error { 66 // init terraform 67 fmt.Fprintf(p.stdout, "Check and install terraform... \n") 68 if err := initTerraform(); err != nil { 69 return err 70 } 71 72 // create cluster 73 fmt.Fprintf(p.stdout, "\nInit and apply %s in %s\n", K8sService(p.name), p.tfPath) 74 return tfInitAndApply(p.tfPath, p.stdout, p.stderr, clusterInfo.buildApplyOpts()...) 75 } 76 77 func (p *cloudProvider) DeleteK8sCluster(clusterInfo *K8sClusterInfo) error { 78 var err error 79 if clusterInfo == nil { 80 clusterInfo, err = p.GetClusterInfo() 81 if err != nil { 82 return err 83 } 84 } 85 // init terraform 86 fmt.Fprintf(p.stdout, "Check and install terraform... \n") 87 if err = initTerraform(); err != nil { 88 return err 89 } 90 91 // destroy cluster 92 fmt.Fprintf(p.stdout, "\nDestroy %s cluster in %s\n", K8sService(p.name), p.tfPath) 93 return tfInitAndDestroy(p.tfPath, p.stdout, p.stderr, clusterInfo.buildDestroyOpts()...) 94 } 95 96 func (p *cloudProvider) GetClusterInfo() (*K8sClusterInfo, error) { 97 vals, err := getOutputValues(p.tfPath, clusterNameKey, regionKey, kubeConfigKey) 98 if err != nil { 99 return nil, err 100 } 101 return &K8sClusterInfo{ 102 CloudProvider: p.Name(), 103 ClusterName: vals[0], 104 Region: vals[1], 105 KubeConfig: vals[2], 106 KbcliVersion: version.GetVersion(), 107 }, nil 108 }