/
managed_main.go
108 lines (85 loc) · 2.82 KB
/
managed_main.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
package local
import (
"os"
"time"
"github.com/ksctl/ksctl/pkg/helpers"
"github.com/ksctl/ksctl/pkg/helpers/consts"
"github.com/ksctl/ksctl/pkg/resources"
)
// DelManagedCluster implements resources.CloudFactory.
func (cloud *LocalProvider) DelManagedCluster(storage resources.StorageFactory) error {
cloud.client.NewProvider(log, storage, nil)
if len(cloud.Metadata.tempDirKubeconfig) == 0 {
var err error
cloud.Metadata.tempDirKubeconfig, err = os.MkdirTemp("", cloud.ClusterName+"*")
if err != nil {
return err
}
if err := os.WriteFile(cloud.Metadata.tempDirKubeconfig+helpers.PathSeparator+"kubeconfig",
[]byte(mainStateDocument.ClusterKubeConfig), 0755); err != nil {
return err
}
defer func() {
_ = os.RemoveAll(cloud.Metadata.tempDirKubeconfig)
}()
}
if err := cloud.client.Delete(cloud.ClusterName,
cloud.Metadata.tempDirKubeconfig+helpers.PathSeparator+"kubeconfig"); err != nil {
return log.NewError("failed to delete cluster %v", err)
}
if err := storage.DeleteCluster(); err != nil {
return log.NewError(err.Error())
}
return nil
}
// NewManagedCluster implements resources.CloudFactory.
func (cloud *LocalProvider) NewManagedCluster(storage resources.StorageFactory, noOfNodes int) error {
cloud.client.NewProvider(log, storage, nil)
cni := false
if consts.KsctlValidCNIPlugin(cloud.Metadata.Cni) == consts.CNINone {
cni = true
}
withConfig, err := configOption(noOfNodes, cni)
if err != nil {
return log.NewError(err.Error())
}
mainStateDocument.CloudInfra.Local.B.KubernetesVer = cloud.Metadata.Version
mainStateDocument.CloudInfra.Local.Nodes = noOfNodes
Wait := 50 * time.Second
cloud.tempDirKubeconfig, err = os.MkdirTemp("", cloud.ClusterName+"*")
if err != nil {
return err
}
ConfigHandler := func() string {
path, err := createNecessaryConfigs(cloud.tempDirKubeconfig)
if err != nil {
log.Error("rollback Cannot continue 😢")
err = cloud.DelManagedCluster(storage) // TODO: check if it works or not??
if err != nil {
log.Error(err.Error())
return "" // asumming it never comes here
}
}
return path
}
Image := "kindest/node:v" + mainStateDocument.CloudInfra.Local.B.KubernetesVer
if err := cloud.client.Create(cloud.ClusterName, withConfig, Image, Wait, ConfigHandler); err != nil {
return log.NewError("failed to create cluster", "err", err)
}
path := cloud.tempDirKubeconfig + helpers.PathSeparator + "kubeconfig"
if err != nil {
return err
}
data, err := os.ReadFile(path)
if err != nil {
return err
}
log.Debug("kubeconfig", "kubeconfigTempPath", path)
mainStateDocument.ClusterKubeConfig = string(data)
mainStateDocument.CloudInfra.Local.B.IsCompleted = true
if err := storage.Write(mainStateDocument); err != nil {
return err
}
_ = os.RemoveAll(cloud.tempDirKubeconfig) // remove the temp directory
return nil
}