-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathentrypoint.go
188 lines (165 loc) · 5.66 KB
/
entrypoint.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
// Copyright 2023 Authors of kubean-io
// SPDX-License-Identifier: Apache-2.0
package entrypoint
import (
_ "embed"
"fmt"
"strings"
"text/template"
klog "k8s.io/klog/v2"
)
// Generate kubespray job entrypoint script
const (
PBAction = "playbook"
SHAction = "shell"
FactsPB = "facts.yml"
ResetPB = "reset.yml"
ScalePB = "scale.yml"
ClusterPB = "cluster.yml"
RemoveNodePB = "remove-node.yml"
UpgradeClusterPB = "upgrade-cluster.yml"
PingPB = "ping.yml"
RepoPB = "enable-repo.yml"
FirewallPB = "disable-firewalld.yml"
KubeconfigPB = "kubeconfig.yml"
ClusterInfoPB = "cluster-info.yml"
UpdateHostsPB = "update-hosts.yml"
RemovePkgsPB = "remove-pkgs.yml"
PreCheckPB = "precheck.yml"
// #nosec
RenewCertsPB = "renew-certs.yml"
KubeVipConfigPB = "config-for-kube-vip.yml"
ConfigInsecureRegistryPB = "config-insecure-registry.yml"
NfConntrackPB = "enable-nf-conntrack.yml"
MountXFSPquotaPB = "mount-xfs-pquota.yml"
SetContainerdRegistryMirror = "set-containerd-registry-mirror.yml"
DisableKernelUnattendedUpgrade = "disable-kernel-unattended-upgrade.yml"
ConfigDockerCgroupDriverForKylinSP2 = "config-docker-cgroup-driver-for-kylinSP2.yml"
)
//go:embed entrypoint.sh.template
var entrypointTemplate string
type void struct{}
var member void
type Playbooks struct {
List []string
Dict map[string]void
}
type Actions struct {
Types []string
Playbooks *Playbooks
}
func NewActions() *Actions {
actions := &Actions{}
actions.Types = []string{PBAction, SHAction}
actions.Playbooks = &Playbooks{}
actions.Playbooks.List = []string{
FactsPB, ResetPB, ScalePB, ClusterPB, RemoveNodePB, UpgradeClusterPB,
PingPB, RepoPB, FirewallPB, KubeconfigPB, ClusterInfoPB, UpdateHostsPB,
RemovePkgsPB, PreCheckPB, RenewCertsPB,
KubeVipConfigPB, ConfigInsecureRegistryPB, NfConntrackPB, MountXFSPquotaPB,
SetContainerdRegistryMirror, DisableKernelUnattendedUpgrade, ConfigDockerCgroupDriverForKylinSP2,
}
actions.Playbooks.Dict = map[string]void{}
for _, pbItem := range actions.Playbooks.List {
actions.Playbooks.Dict[pbItem] = member
}
return actions
}
type ArgsError struct {
msg string
}
func (argsError ArgsError) Error() string {
return argsError.msg
}
type EntryPoint struct {
PreHookCMDs []string
SprayCMD string
PostHookCMDs []string
Actions *Actions
}
func NewEntryPoint() *EntryPoint {
ep := &EntryPoint{}
ep.Actions = NewActions()
return ep
}
func (ep *EntryPoint) buildPlaybookCmd(action, extraArgs string, isPrivateKey, builtinAction bool) (string, error) {
if builtinAction {
if _, ok := ep.Actions.Playbooks.Dict[action]; !ok {
return "", ArgsError{fmt.Sprintf("unknown playbook type, the currently supported ranges include: %s", ep.Actions.Playbooks.List)}
}
}
playbookCmd := "ansible-playbook -i /conf/hosts.yml -b --become-user root -e \"@/conf/group_vars.yml\""
if isPrivateKey {
playbookCmd = fmt.Sprintf("%s --private-key /auth/ssh-privatekey", playbookCmd)
}
if action == ResetPB {
playbookCmd = fmt.Sprintf("%s -e \"reset_confirmation=yes\"", playbookCmd)
}
if action == RemoveNodePB {
playbookCmd = fmt.Sprintf("%s -e \"skip_confirmation=true\"", playbookCmd)
}
playbookCmd = fmt.Sprintf("%s /kubespray/%s", playbookCmd, action)
if len(extraArgs) > 0 {
playbookCmd = fmt.Sprintf("%s %s", playbookCmd, extraArgs)
}
return playbookCmd, nil
}
func (ep *EntryPoint) hookRunPart(actionType, action, extraArgs string, isPrivateKey, builtinAction bool) (string, error) {
if !builtinAction {
klog.Infof("use external action %s, type %s", action, actionType)
}
hookRunCmd := ""
if actionType == PBAction {
playbookCmd, err := ep.buildPlaybookCmd(action, extraArgs, isPrivateKey, builtinAction)
if err != nil {
return "", ArgsError{fmt.Sprintf("buildPlaybookCmd: %s", err)}
}
hookRunCmd = playbookCmd
} else if actionType == SHAction {
hookRunCmd = action
} else {
return "", ArgsError{fmt.Sprintf("unknown action type, the currently supported ranges include: %s", ep.Actions.Types)}
}
return hookRunCmd, nil
}
func (ep *EntryPoint) PreHookRunPart(actionType, action, extraArgs string, isPrivateKey, builtinAction bool) error {
prehook, err := ep.hookRunPart(actionType, action, extraArgs, isPrivateKey, builtinAction)
if err != nil {
return ArgsError{fmt.Sprintf("prehook: %s", err)}
}
ep.PreHookCMDs = append(ep.PreHookCMDs, prehook)
return nil
}
func (ep *EntryPoint) PostHookRunPart(actionType, action, extraArgs string, isPrivateKey, builtinAction bool) error {
posthook, err := ep.hookRunPart(actionType, action, extraArgs, isPrivateKey, builtinAction)
if err != nil {
return ArgsError{fmt.Sprintf("posthook: %s", err)}
}
ep.PostHookCMDs = append(ep.PostHookCMDs, posthook)
return nil
}
func (ep *EntryPoint) SprayRunPart(actionType, action, extraArgs string, isPrivateKey, builtinAction bool) error {
if !builtinAction {
klog.Infof("use external action %s, type %s", action, actionType)
}
if actionType == PBAction {
playbookCmd, err := ep.buildPlaybookCmd(action, extraArgs, isPrivateKey, builtinAction)
if err != nil {
return ArgsError{fmt.Sprintf("buildPlaybookCmd: %s", err)}
}
ep.SprayCMD = playbookCmd
} else if actionType == SHAction {
ep.SprayCMD = action
} else {
return ArgsError{fmt.Sprintf("unknown action type, the currently supported ranges include: %s", ep.Actions.Types)}
}
return nil
}
func (ep *EntryPoint) Render() (string, error) {
b := &strings.Builder{}
tmpl := template.Must(template.New("entrypoint").Parse(entrypointTemplate))
if err := tmpl.Execute(b, ep); err != nil {
return "", err
}
return b.String(), nil
}