-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathk3s.go
124 lines (101 loc) · 3.57 KB
/
k3s.go
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package k3s
import (
"os"
"text/template"
"capten/pkg/clog"
"capten/pkg/config"
"capten/pkg/terraform"
"capten/pkg/types"
"github.com/pkg/errors"
)
func getClusterInfo(captenConfig config.CaptenConfig) (interface{}, error) {
var clusterInfo interface{}
if captenConfig.CloudService == "aws" {
awsclusterInfo, err := config.GetClusterInfo(captenConfig.PrepareFilePath(captenConfig.ConfigDirPath, captenConfig.CloudService+"_config.yaml"))
if err != nil {
return nil, err
}
clusterInfo = awsclusterInfo
} else if captenConfig.CloudService == "azure" {
azureClusterInfo, err := config.GetClusterInfoAzure(captenConfig.PrepareFilePath(captenConfig.ConfigDirPath, captenConfig.CloudService+"_config.yaml"))
if err != nil {
return nil, err
}
clusterInfo = azureClusterInfo
} else {
return nil, errors.Errorf("Unsupported Cloud Service")
}
return clusterInfo, nil
}
func createOrDestroyCluster(captenConfig config.CaptenConfig, action string) error {
clog.Logger.Debugf("%s cluster on %s cloud with %s cluster type", action, captenConfig.CloudService, captenConfig.ClusterType)
clusterInfo, err := getClusterInfo(captenConfig)
if err != nil {
return err
}
switch info := clusterInfo.(type) {
case types.AWSClusterInfo:
info.ConfigFolderPath = captenConfig.PrepareDirPath(captenConfig.ConfigDirPath)
info.TerraformModulesDirPath = captenConfig.PrepareDirPath(captenConfig.TerraformModulesDirPath)
err = generateTemplateVarFile(captenConfig, info, captenConfig.AWSTerraformTemplateFileName)
if err != nil {
return err
}
tf, err := terraform.NewAws(captenConfig, info)
if err != nil {
return errors.WithMessage(err, "failed to initialize the terraform")
}
if action == "create" {
return tf.Apply()
} else if action == "destroy" {
return tf.Destroy()
}
case types.AzureClusterInfo:
info.ConfigFolderPath = captenConfig.PrepareDirPath(captenConfig.ConfigDirPath)
info.TerraformModulesDirPath = captenConfig.PrepareDirPath(captenConfig.TerraformModulesDirPath)
err = generateTemplateVarFile(captenConfig, info, captenConfig.AzureTerraformTemplateFileName)
if err != nil {
return err
}
tf, err := terraform.NewAzure(captenConfig, info)
if err != nil {
return errors.WithMessage(err, "failed to initialize the terraform")
}
if action == "create" {
return tf.Apply()
} else if action == "destroy" {
return tf.Destroy()
}
default:
return errors.New("unsupported cloud service")
}
return nil
}
func Create(captenConfig config.CaptenConfig) error {
return createOrDestroyCluster(captenConfig, "create")
}
func Destroy(captenConfig config.CaptenConfig) error {
return createOrDestroyCluster(captenConfig, "destroy")
}
func generateTemplateVarFile(captenConfig config.CaptenConfig, clusterInfo interface{}, templateFileName string) error {
content, err := os.ReadFile(captenConfig.PrepareFilePath(captenConfig.TerraformTemplateDirPath, templateFileName))
if err != nil {
return errors.WithMessage(err, "failed to read template file")
}
contentStr := string(content)
templateObj, err := template.New("terraformTemplate").Parse(contentStr)
if err != nil {
clog.Logger.Error("Error while creating templateObj", err)
return err
}
templateFile, err := os.Create(captenConfig.PrepareFilePath(captenConfig.TerraformTemplateDirPath, captenConfig.TerraformVarFileName))
if err != nil {
clog.Logger.Error("Error while creating templateFile", err)
return err
}
if err := templateObj.Execute(templateFile, clusterInfo); err != nil {
clog.Logger.Error("Error while executing templateObj", err)
return err
}
return nil
}