-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathprovider.go
More file actions
108 lines (89 loc) · 2.75 KB
/
provider.go
File metadata and controls
108 lines (89 loc) · 2.75 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*
Copyright (C) 2022-2026 ApeCloud Co., Ltd
This file is part of KubeBlocks project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package cloudprovider
import (
"fmt"
"io"
"os"
"path/filepath"
"github.com/apecloud/kbcli/version"
)
type cloudProvider struct {
name string
tfPath string
stdout io.Writer
stderr io.Writer
}
var _ Interface = &cloudProvider{}
func newCloudProvider(provider, rootPath string, stdout, stderr io.Writer) (Interface, error) {
k8sSvc := K8sService(provider)
if k8sSvc == "" {
return nil, fmt.Errorf("unknown cloud provider %s", provider)
}
tfPath := filepath.Join(rootPath, provider, k8sSvc)
if _, err := os.Stat(tfPath); err != nil {
return nil, err
}
return &cloudProvider{
name: provider,
tfPath: tfPath,
stdout: stdout,
stderr: stderr,
}, nil
}
func (p *cloudProvider) Name() string {
return p.name
}
// CreateK8sCluster creates a kubernetes cluster
func (p *cloudProvider) CreateK8sCluster(clusterInfo *K8sClusterInfo) error {
// init terraform
fmt.Fprintf(p.stdout, "Check and install terraform... \n")
if err := initTerraform(); err != nil {
return err
}
// create cluster
fmt.Fprintf(p.stdout, "\nInit and apply %s in %s\n", K8sService(p.name), p.tfPath)
return tfInitAndApply(p.tfPath, p.stdout, p.stderr, clusterInfo.buildApplyOpts()...)
}
func (p *cloudProvider) DeleteK8sCluster(clusterInfo *K8sClusterInfo) error {
var err error
if clusterInfo == nil {
clusterInfo, err = p.GetClusterInfo()
if err != nil {
return err
}
}
// init terraform
fmt.Fprintf(p.stdout, "Check and install terraform... \n")
if err = initTerraform(); err != nil {
return err
}
// destroy cluster
fmt.Fprintf(p.stdout, "\nDestroy %s cluster in %s\n", K8sService(p.name), p.tfPath)
return tfInitAndDestroy(p.tfPath, p.stdout, p.stderr, clusterInfo.buildDestroyOpts()...)
}
func (p *cloudProvider) GetClusterInfo() (*K8sClusterInfo, error) {
vals, err := getOutputValues(p.tfPath, clusterNameKey, regionKey, kubeConfigKey)
if err != nil {
return nil, err
}
return &K8sClusterInfo{
CloudProvider: p.Name(),
ClusterName: vals[0],
Region: vals[1],
KubeConfig: vals[2],
KbcliVersion: version.GetVersion(),
}, nil
}