2022-07-13 21:09:45 +00:00
|
|
|
package createcluster
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-04-06 15:17:13 +00:00
|
|
|
"strconv"
|
2022-07-13 21:09:45 +00:00
|
|
|
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gruntwork-io/terratest/modules/terraform"
|
|
|
|
tf "github.com/k3s-io/k3s/tests/terraform"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-04-06 15:17:13 +00:00
|
|
|
KubeConfigFile string
|
|
|
|
MasterIPs string
|
|
|
|
WorkerIPs string
|
|
|
|
NumServers int
|
|
|
|
NumWorkers int
|
|
|
|
AwsUser string
|
|
|
|
AccessKey string
|
|
|
|
RenderedTemplate string
|
|
|
|
ExternalDb string
|
|
|
|
ClusterType string
|
|
|
|
TfVarsPath = "/tests/terraform/modules/k3scluster/config/local.tfvars"
|
|
|
|
modulesPath = "/tests/terraform/modules/k3scluster"
|
2022-07-13 21:09:45 +00:00
|
|
|
)
|
|
|
|
|
2023-04-06 15:17:13 +00:00
|
|
|
func BuildCluster(t *testing.T, destroy bool) (string, error) {
|
2022-07-13 21:09:45 +00:00
|
|
|
basepath := tf.GetBasepath()
|
2023-04-06 15:17:13 +00:00
|
|
|
tfDir, err := filepath.Abs(basepath + modulesPath)
|
2022-07-13 21:09:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2023-04-06 15:17:13 +00:00
|
|
|
varDir, err := filepath.Abs(basepath + TfVarsPath)
|
2022-07-13 21:09:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
TerraformOptions := &terraform.Options{
|
|
|
|
TerraformDir: tfDir,
|
|
|
|
VarFiles: []string{varDir},
|
|
|
|
}
|
|
|
|
|
2023-04-06 15:17:13 +00:00
|
|
|
NumServers, err = strconv.Atoi(terraform.GetVariableAsStringFromVarFile(t, varDir,
|
|
|
|
"no_of_server_nodes"))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
NumWorkers, err = strconv.Atoi(terraform.GetVariableAsStringFromVarFile(t, varDir,
|
|
|
|
"no_of_worker_nodes"))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
ClusterType = terraform.GetVariableAsStringFromVarFile(t, varDir, "cluster_type")
|
|
|
|
ExternalDb = terraform.GetVariableAsStringFromVarFile(t, varDir, "external_db")
|
|
|
|
AwsUser = terraform.GetVariableAsStringFromVarFile(t, varDir, "aws_user")
|
|
|
|
AccessKey = terraform.GetVariableAsStringFromVarFile(t, varDir, "access_key")
|
|
|
|
|
2022-07-13 21:09:45 +00:00
|
|
|
if destroy {
|
|
|
|
fmt.Printf("Cluster is being deleted")
|
|
|
|
terraform.Destroy(t, TerraformOptions)
|
|
|
|
return "cluster destroyed", err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("Creating Cluster")
|
2023-04-06 15:17:13 +00:00
|
|
|
|
2022-07-13 21:09:45 +00:00
|
|
|
terraform.InitAndApply(t, TerraformOptions)
|
|
|
|
KubeConfigFile = "/tmp/" + terraform.Output(t, TerraformOptions, "kubeconfig") + "_kubeconfig"
|
|
|
|
MasterIPs = terraform.Output(t, TerraformOptions, "master_ips")
|
|
|
|
WorkerIPs = terraform.Output(t, TerraformOptions, "worker_ips")
|
2023-04-06 15:17:13 +00:00
|
|
|
RenderedTemplate = terraform.Output(t, TerraformOptions, "rendered_template")
|
2022-07-13 21:09:45 +00:00
|
|
|
|
2023-04-06 15:17:13 +00:00
|
|
|
return "cluster created", err
|
2022-07-13 21:09:45 +00:00
|
|
|
}
|