-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathjoin.go
240 lines (195 loc) · 9.98 KB
/
join.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package stages
import (
"bytes"
"fmt"
"path/filepath"
"k8s.io/apimachinery/pkg/runtime"
kubeadmapiv4 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta4"
"github.com/kairos-io/kairos/provider-kubeadm/domain"
"github.com/kairos-io/kairos-sdk/clusterplugin"
"github.com/kairos-io/kairos/provider-kubeadm/utils"
yip "github.com/mudler/yip/pkg/schema"
"k8s.io/cli-runtime/pkg/printers"
kubeadmapiv3 "k8s.io/kubernetes/cmd/kubeadm/app/apis/kubeadm/v1beta3"
)
func GetJoinYipStagesV1Beta3(clusterCtx *domain.ClusterContext, kubeadmConfig domain.KubeadmConfigBeta3) []yip.Stage {
utils.MutateClusterConfigBeta3Defaults(clusterCtx, &kubeadmConfig.ClusterConfiguration)
utils.MutateKubeletDefaults(clusterCtx, &kubeadmConfig.KubeletConfiguration)
clusterCtx.KubeletArgs = utils.RegenerateKubeletKubeadmArgsUsingBeta3Config(&kubeadmConfig.JoinConfiguration.NodeRegistration, clusterCtx.NodeRole)
clusterCtx.CertSansRevision = utils.GetCertSansRevision(kubeadmConfig.ClusterConfiguration.APIServer.CertSANs)
joinStg := []yip.Stage{
getKubeadmJoinConfigStage(getJoinNodeConfigurationBeta3(clusterCtx, kubeadmConfig.JoinConfiguration), clusterCtx.RootPath),
getKubeadmJoinStage(clusterCtx),
getKubeadmJoinUpgradeStage(clusterCtx),
}
if clusterCtx.NodeRole != clusterplugin.RoleWorker {
joinStg = append(joinStg,
getKubeadmJoinCreateClusterConfigStage(&kubeadmConfig.ClusterConfiguration, &kubeadmConfig.InitConfiguration, &kubeadmConfig.JoinConfiguration, clusterCtx.RootPath),
getKubeadmJoinCreateKubeletConfigStage(&kubeadmConfig.ClusterConfiguration, &kubeadmConfig.InitConfiguration, &kubeadmConfig.KubeletConfiguration, clusterCtx.RootPath))
}
return append(joinStg, getKubeadmJoinReconfigureStage(clusterCtx))
}
func GetJoinYipStagesV1Beta4(clusterCtx *domain.ClusterContext, kubeadmConfig domain.KubeadmConfigBeta4) []yip.Stage {
utils.MutateClusterConfigBeta4Defaults(clusterCtx, &kubeadmConfig.ClusterConfiguration)
utils.MutateKubeletDefaults(clusterCtx, &kubeadmConfig.KubeletConfiguration)
clusterCtx.KubeletArgs = utils.RegenerateKubeletKubeadmArgsUsingBeta4Config(&kubeadmConfig.JoinConfiguration.NodeRegistration, clusterCtx.NodeRole)
clusterCtx.CertSansRevision = utils.GetCertSansRevision(kubeadmConfig.ClusterConfiguration.APIServer.CertSANs)
joinStg := []yip.Stage{
getKubeadmJoinConfigStage(getJoinNodeConfigurationBeta4(clusterCtx, kubeadmConfig.JoinConfiguration), clusterCtx.RootPath),
getKubeadmJoinStage(clusterCtx),
getKubeadmJoinUpgradeStage(clusterCtx),
}
if clusterCtx.NodeRole != clusterplugin.RoleWorker {
joinStg = append(joinStg,
getKubeadmJoinCreateClusterConfigStage(&kubeadmConfig.ClusterConfiguration, &kubeadmConfig.InitConfiguration, &kubeadmConfig.JoinConfiguration, clusterCtx.RootPath),
getKubeadmJoinCreateKubeletConfigStage(&kubeadmConfig.ClusterConfiguration, &kubeadmConfig.InitConfiguration, &kubeadmConfig.KubeletConfiguration, clusterCtx.RootPath))
}
return append(joinStg, getKubeadmJoinReconfigureStage(clusterCtx))
}
func getJoinNodeConfigurationBeta3(clusterCtx *domain.ClusterContext, joinCfg kubeadmapiv3.JoinConfiguration) string {
if joinCfg.Discovery.BootstrapToken == nil {
joinCfg.Discovery.BootstrapToken = &kubeadmapiv3.BootstrapTokenDiscovery{
Token: clusterCtx.ClusterToken,
APIServerEndpoint: fmt.Sprintf("%s:6443", clusterCtx.ControlPlaneHost),
UnsafeSkipCAVerification: true,
}
}
if clusterCtx.NodeRole == clusterplugin.RoleControlPlane {
if joinCfg.ControlPlane == nil {
joinCfg.ControlPlane = &kubeadmapiv3.JoinControlPlane{}
}
joinCfg.ControlPlane.CertificateKey = utils.GetCertificateKey(clusterCtx.ClusterToken)
var apiEndpoint kubeadmapiv3.APIEndpoint
if joinCfg.ControlPlane.LocalAPIEndpoint.AdvertiseAddress == "" {
apiEndpoint.AdvertiseAddress = domain.DefaultAPIAdvertiseAddress
} else {
apiEndpoint.AdvertiseAddress = joinCfg.ControlPlane.LocalAPIEndpoint.AdvertiseAddress
}
if joinCfg.ControlPlane.LocalAPIEndpoint.BindPort != 0 {
apiEndpoint.BindPort = joinCfg.ControlPlane.LocalAPIEndpoint.BindPort
}
joinCfg.ControlPlane.LocalAPIEndpoint = apiEndpoint
}
joinPrinter := printers.NewTypeSetter(scheme).ToPrinter(&printers.YAMLPrinter{})
out := bytes.NewBuffer([]byte{})
_ = joinPrinter.PrintObj(&joinCfg, out)
return out.String()
}
func getJoinNodeConfigurationBeta4(clusterCtx *domain.ClusterContext, joinCfg kubeadmapiv4.JoinConfiguration) string {
if joinCfg.Discovery.BootstrapToken == nil {
joinCfg.Discovery.BootstrapToken = &kubeadmapiv4.BootstrapTokenDiscovery{
Token: clusterCtx.ClusterToken,
APIServerEndpoint: fmt.Sprintf("%s:6443", clusterCtx.ControlPlaneHost),
UnsafeSkipCAVerification: true,
}
}
if clusterCtx.NodeRole == clusterplugin.RoleControlPlane {
if joinCfg.ControlPlane == nil {
joinCfg.ControlPlane = &kubeadmapiv4.JoinControlPlane{}
}
joinCfg.ControlPlane.CertificateKey = utils.GetCertificateKey(clusterCtx.ClusterToken)
var apiEndpoint kubeadmapiv4.APIEndpoint
if joinCfg.ControlPlane.LocalAPIEndpoint.AdvertiseAddress == "" {
apiEndpoint.AdvertiseAddress = domain.DefaultAPIAdvertiseAddress
} else {
apiEndpoint.AdvertiseAddress = joinCfg.ControlPlane.LocalAPIEndpoint.AdvertiseAddress
}
if joinCfg.ControlPlane.LocalAPIEndpoint.BindPort != 0 {
apiEndpoint.BindPort = joinCfg.ControlPlane.LocalAPIEndpoint.BindPort
}
joinCfg.ControlPlane.LocalAPIEndpoint = apiEndpoint
}
joinPrinter := printers.NewTypeSetter(scheme).ToPrinter(&printers.YAMLPrinter{})
out := bytes.NewBuffer([]byte{})
_ = joinPrinter.PrintObj(&joinCfg, out)
return out.String()
}
func getKubeadmJoinConfigStage(kubeadmCfg, rootPath string) yip.Stage {
return yip.Stage{
Name: "Generate Kubeadm Join Config File",
Files: []yip.File{
{
Path: filepath.Join(rootPath, configurationPath, "kubeadm.yaml"),
Permissions: 0640,
Content: kubeadmCfg,
},
},
}
}
func getKubeadmJoinStage(clusterCtx *domain.ClusterContext) yip.Stage {
clusterRootPath := clusterCtx.RootPath
joinStage := yip.Stage{
Name: "Run Kubeadm Join",
If: fmt.Sprintf("[ ! -f %s ]", filepath.Join(clusterRootPath, "opt/kubeadm.join")),
}
if utils.IsProxyConfigured(clusterCtx.EnvConfig) {
proxy := clusterCtx.EnvConfig
joinStage.Commands = []string{
fmt.Sprintf("bash %s %s %s %t %s %s %s", filepath.Join(clusterRootPath, helperScriptPath, "kube-join.sh"), clusterCtx.NodeRole, clusterRootPath, true, proxy["HTTP_PROXY"], proxy["HTTPS_PROXY"], utils.GetNoProxyConfig(clusterCtx)),
fmt.Sprintf("touch %s", filepath.Join(clusterRootPath, "opt/kubeadm.join")),
}
} else {
joinStage.Commands = []string{
fmt.Sprintf("bash %s %s %s", filepath.Join(clusterRootPath, helperScriptPath, "kube-join.sh"), clusterCtx.NodeRole, clusterRootPath),
fmt.Sprintf("touch %s", filepath.Join(clusterRootPath, "opt/kubeadm.join")),
}
}
return joinStage
}
func getKubeadmJoinUpgradeStage(clusterCtx *domain.ClusterContext) yip.Stage {
upgradeStage := yip.Stage{
Name: "Run Kubeadm Join Upgrade",
}
if utils.IsProxyConfigured(clusterCtx.EnvConfig) {
proxy := clusterCtx.EnvConfig
upgradeStage.Commands = []string{
fmt.Sprintf("bash %s %s %s %t %s %s %s", filepath.Join(clusterCtx.RootPath, helperScriptPath, "kube-upgrade.sh"), clusterCtx.NodeRole, clusterCtx.RootPath, true, proxy["HTTP_PROXY"], proxy["HTTPS_PROXY"], utils.GetNoProxyConfig(clusterCtx)),
}
} else {
upgradeStage.Commands = []string{
fmt.Sprintf("bash %s %s %s", filepath.Join(clusterCtx.RootPath, helperScriptPath, "kube-upgrade.sh"), clusterCtx.NodeRole, clusterCtx.RootPath),
}
}
return upgradeStage
}
func getKubeadmJoinCreateClusterConfigStage(clusterCfgObj, initCfgObj, joinCfgObj runtime.Object, rootPath string) yip.Stage {
return utils.GetFileStage("Generate Cluster Config File", filepath.Join(rootPath, configurationPath, "cluster-config.yaml"), getUpdatedJoinClusterConfig(clusterCfgObj, initCfgObj, joinCfgObj))
}
func getKubeadmJoinCreateKubeletConfigStage(clusterCfgObj, initCfg, kubeletCfg runtime.Object, rootPath string) yip.Stage {
return utils.GetFileStage("Generate Kubelet Config File", filepath.Join(rootPath, configurationPath, "kubelet-config.yaml"), getUpdatedKubeletConfig(clusterCfgObj, initCfg, kubeletCfg))
}
func getKubeadmJoinReconfigureStage(clusterCtx *domain.ClusterContext) yip.Stage {
reconfigureStage := yip.Stage{
Name: "Run Kubeadm Join Reconfiguration",
}
if utils.IsProxyConfigured(clusterCtx.EnvConfig) {
proxy := clusterCtx.EnvConfig
reconfigureStage.Commands = []string{
fmt.Sprintf("bash %s %s %s %s %s %s %s %s", filepath.Join(clusterCtx.RootPath, helperScriptPath, "kube-reconfigure.sh"), clusterCtx.NodeRole, clusterCtx.CertSansRevision, clusterCtx.KubeletArgs, clusterCtx.RootPath, proxy["HTTP_PROXY"], proxy["HTTPS_PROXY"], utils.GetNoProxyConfig(clusterCtx)),
}
} else {
reconfigureStage.Commands = []string{
fmt.Sprintf("bash %s %s %s %s %s", filepath.Join(clusterCtx.RootPath, helperScriptPath, "kube-reconfigure.sh"), clusterCtx.NodeRole, clusterCtx.CertSansRevision, clusterCtx.KubeletArgs, clusterCtx.RootPath),
}
}
return reconfigureStage
}
func getUpdatedJoinClusterConfig(clusterCfgObj, initCfgObj, joinCfgObj runtime.Object) string {
switch initCfgObj.(type) {
case *kubeadmapiv3.InitConfiguration:
initCfg := initCfgObj.(*kubeadmapiv3.InitConfiguration)
joinCfg := joinCfgObj.(*kubeadmapiv3.JoinConfiguration)
if joinCfg.ControlPlane != nil {
initCfg.LocalAPIEndpoint.AdvertiseAddress = joinCfg.ControlPlane.LocalAPIEndpoint.AdvertiseAddress
initCfg.LocalAPIEndpoint.BindPort = joinCfg.ControlPlane.LocalAPIEndpoint.BindPort
}
case *kubeadmapiv4.InitConfiguration:
initCfg := initCfgObj.(*kubeadmapiv4.InitConfiguration)
joinCfg := joinCfgObj.(*kubeadmapiv4.JoinConfiguration)
if joinCfg.ControlPlane != nil {
initCfg.LocalAPIEndpoint.AdvertiseAddress = joinCfg.ControlPlane.LocalAPIEndpoint.AdvertiseAddress
initCfg.LocalAPIEndpoint.BindPort = joinCfg.ControlPlane.LocalAPIEndpoint.BindPort
}
}
return printObj([]runtime.Object{clusterCfgObj, initCfgObj, joinCfgObj})
}