-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.go
190 lines (154 loc) · 6.23 KB
/
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package main
import (
"encoding/json"
"fmt"
"os/exec"
"path/filepath"
"github.com/kairos-io/kairos/provider-kubeadm/log"
"github.com/kairos-io/kairos/provider-kubeadm/domain"
"github.com/kairos-io/kairos/provider-kubeadm/stages"
"github.com/kairos-io/kairos/provider-kubeadm/utils"
"gopkg.in/yaml.v3"
kyaml "sigs.k8s.io/yaml"
"github.com/kairos-io/kairos-sdk/bus"
"github.com/kairos-io/kairos-sdk/clusterplugin"
"github.com/mudler/go-pluggable"
yip "github.com/mudler/yip/pkg/schema"
"github.com/sirupsen/logrus"
)
func main() {
log.InitLogger("/var/log/provider-kubeadm.log")
logrus.Info("starting provider-kubeadm")
plugin := clusterplugin.ClusterPlugin{
Provider: clusterProvider,
}
if err := plugin.Run(
pluggable.FactoryPlugin{
EventType: clusterplugin.EventClusterReset,
PluginHandler: handleClusterReset,
},
); err != nil {
logrus.Fatal(err)
}
logrus.Infof("completed provider-kubeadm")
}
func handleClusterReset(event *pluggable.Event) pluggable.EventResponse {
logrus.Info("handling cluster reset event")
var payload bus.EventPayload
var config clusterplugin.Config
var response pluggable.EventResponse
// parse the boot payload
if err := json.Unmarshal([]byte(event.Data), &payload); err != nil {
logrus.Error(fmt.Sprintf("failed to parse boot event: %s", err.Error()))
response.Error = fmt.Sprintf("failed to parse boot event: %s", err.Error())
return response
}
// parse config from boot payload
if err := yaml.Unmarshal([]byte(payload.Config), &config); err != nil {
logrus.Error(fmt.Sprintf("failed to parse config from boot event: %s", err.Error()))
response.Error = fmt.Sprintf("failed to parse config from boot event: %s", err.Error())
return response
}
if config.Cluster == nil {
return response
}
clusterRootPath := utils.GetClusterRootPath(*config.Cluster)
cmd := exec.Command("/bin/sh", "-c", filepath.Join(clusterRootPath, "/opt/kubeadm/scripts", "kube-reset.sh"))
output, err := cmd.CombinedOutput()
if err != nil {
logrus.Error(fmt.Sprintf("failed to reset cluster: %s", string(output)))
response.Error = fmt.Sprintf("failed to reset cluster: %s", string(output))
}
return response
}
func clusterProvider(cluster clusterplugin.Cluster) yip.YipConfig {
var finalStages []yip.Stage
clusterCtx := CreateClusterContext(cluster)
cmpResult, err := utils.IsKubeadmVersionGreaterThan131()
if err != nil {
logrus.Fatalf("failed to check if kubeadm version is greater than 131: %v", err)
} else if cmpResult < 0 {
logrus.Info("kubeadm version is less than 1.31")
finalStages = append(finalStages, getV1Beta3FinalStage(clusterCtx)...)
} else {
logrus.Info("kubeadm version is greater than or equal to 1.31")
finalStages = append(finalStages, getV1Beta4FinalStage(clusterCtx)...)
}
cfg := yip.YipConfig{
Name: "Kubeadm Kairos Cluster Provider",
Stages: map[string][]yip.Stage{
"boot.before": finalStages,
},
}
return cfg
}
func CreateClusterContext(cluster clusterplugin.Cluster) *domain.ClusterContext {
clusterContext := &domain.ClusterContext{
RootPath: utils.GetClusterRootPath(cluster),
NodeRole: string(cluster.Role),
EnvConfig: cluster.Env,
ControlPlaneHost: cluster.ControlPlaneHost,
ClusterToken: utils.TransformToken(cluster.ClusterToken),
UserOptions: cluster.Options,
ContainerdServiceFolderName: getContainerdServiceFolderName(cluster.ProviderOptions),
}
if cluster.LocalImagesPath == "" {
clusterContext.LocalImagesPath = filepath.Join(clusterContext.RootPath, "opt/content/images")
} else {
clusterContext.LocalImagesPath = cluster.LocalImagesPath
}
return clusterContext
}
func getV1Beta3FinalStage(clusterCtx *domain.ClusterContext) []yip.Stage {
var finalStages []yip.Stage
var kubeadmConfig domain.KubeadmConfigBeta3
if clusterCtx.UserOptions != "" {
userOptions, _ := kyaml.YAMLToJSON([]byte(clusterCtx.UserOptions))
_ = json.Unmarshal(userOptions, &kubeadmConfig)
}
setClusterSubnetCtx(clusterCtx, kubeadmConfig.ClusterConfiguration.Networking.ServiceSubnet, kubeadmConfig.ClusterConfiguration.Networking.PodSubnet)
// pre stages
finalStages = append(finalStages, getKubeadmPreStages(clusterCtx)...)
if clusterCtx.NodeRole == clusterplugin.RoleInit {
finalStages = append(finalStages, stages.GetInitYipStagesV1Beta3(clusterCtx, kubeadmConfig)...)
} else if (clusterCtx.NodeRole == clusterplugin.RoleControlPlane) || (clusterCtx.NodeRole == clusterplugin.RoleWorker) {
finalStages = append(finalStages, stages.GetJoinYipStagesV1Beta3(clusterCtx, kubeadmConfig)...)
}
return finalStages
}
func getV1Beta4FinalStage(clusterCtx *domain.ClusterContext) []yip.Stage {
var finalStages []yip.Stage
var kubeadmConfig domain.KubeadmConfigBeta4
if clusterCtx.UserOptions != "" {
userOptions, _ := kyaml.YAMLToJSON([]byte(clusterCtx.UserOptions))
_ = json.Unmarshal(userOptions, &kubeadmConfig)
}
setClusterSubnetCtx(clusterCtx, kubeadmConfig.ClusterConfiguration.Networking.ServiceSubnet, kubeadmConfig.ClusterConfiguration.Networking.PodSubnet)
// pre stages
finalStages = append(finalStages, getKubeadmPreStages(clusterCtx)...)
if clusterCtx.NodeRole == clusterplugin.RoleInit {
finalStages = append(finalStages, stages.GetInitYipStagesV1Beta4(clusterCtx, kubeadmConfig)...)
} else if (clusterCtx.NodeRole == clusterplugin.RoleControlPlane) || (clusterCtx.NodeRole == clusterplugin.RoleWorker) {
finalStages = append(finalStages, stages.GetJoinYipStagesV1Beta4(clusterCtx, kubeadmConfig)...)
}
return finalStages
}
func getKubeadmPreStages(clusterCtx *domain.ClusterContext) []yip.Stage {
return []yip.Stage{
stages.GetPreKubeadmProxyStage(clusterCtx),
stages.GetPreKubeadmCommandStages(clusterCtx.RootPath),
stages.GetPreKubeadmSwapOffDisableStage(),
stages.GetPreKubeadmImportCoreK8sImageStage(clusterCtx.RootPath),
stages.GetPreKubeadmImportLocalImageStage(clusterCtx),
}
}
func getContainerdServiceFolderName(options map[string]string) string {
if _, ok := options["spectro-containerd-service-name"]; ok {
return "spectro-containerd"
}
return "containerd"
}
func setClusterSubnetCtx(clusterCtx *domain.ClusterContext, serviceSubnet, podSubnet string) {
clusterCtx.ServiceCidr = serviceSubnet
clusterCtx.ClusterCidr = podSubnet
}