-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
instructions.go
153 lines (141 loc) · 5.84 KB
/
instructions.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
package planner
import (
"fmt"
"strings"
rkev1 "github.com/rancher/rancher/pkg/apis/rke.cattle.io/v1"
"github.com/rancher/rancher/pkg/apis/rke.cattle.io/v1/plan"
"github.com/rancher/rancher/pkg/capr"
)
const (
captureAddressInstructionName = "capture-address"
etcdNameInstructionName = "etcd-name"
)
// generateInstallInstruction generates the instruction necessary to install the desired tool.
func (p *Planner) generateInstallInstruction(controlPlane *rkev1.RKEControlPlane, entry *planEntry, env []string) plan.OneTimeInstruction {
var instruction plan.OneTimeInstruction
image := p.getInstallerImage(controlPlane)
cattleOS := entry.Metadata.Labels[capr.CattleOSLabel]
for _, arg := range controlPlane.Spec.AgentEnvVars {
if arg.Value == "" {
continue
}
switch cattleOS {
case capr.WindowsMachineOS:
env = append(env, fmt.Sprintf("$env:%s=\"%s\"", arg.Name, arg.Value))
default:
env = append(env, fmt.Sprintf("%s=%s", arg.Name, arg.Value))
}
}
switch cattleOS {
case capr.WindowsMachineOS:
instruction = plan.OneTimeInstruction{
Name: "install",
Image: image,
Command: "powershell.exe",
Args: []string{"-File", "run.ps1"},
Env: env,
}
default:
instruction = plan.OneTimeInstruction{
Name: "install",
Image: image,
Command: "sh",
Args: []string{"-c", "run.sh"},
Env: env,
}
}
if isOnlyWorker(entry) {
switch cattleOS {
case capr.WindowsMachineOS:
instruction.Env = append(instruction.Env, fmt.Sprintf("$env:INSTALL_%s_EXEC=\"agent\"", capr.GetRuntimeEnv(controlPlane.Spec.KubernetesVersion)))
default:
instruction.Env = append(instruction.Env, fmt.Sprintf("INSTALL_%s_EXEC=agent", capr.GetRuntimeEnv(controlPlane.Spec.KubernetesVersion)))
}
}
return instruction
}
// addInstallInstructionWithRestartStamp will generate an instruction and append it to the node plan that executes the `run.sh` or `run.ps1`
// from the installer image based on the control plane configuration. It will generate a restart stamp based on the
// passed in configuration to determine whether it needs to start/restart the service being managed.
func (p *Planner) addInstallInstructionWithRestartStamp(nodePlan plan.NodePlan, controlPlane *rkev1.RKEControlPlane, entry *planEntry) (plan.NodePlan, error) {
var restartStampEnv string
stamp := restartStamp(nodePlan, controlPlane, p.getInstallerImage(controlPlane))
switch entry.Metadata.Labels[capr.CattleOSLabel] {
case capr.WindowsMachineOS:
restartStampEnv = "$env:RESTART_STAMP=\"" + stamp + "\""
default:
restartStampEnv = "RESTART_STAMP=" + stamp
}
instEnv := []string{restartStampEnv}
nodePlan.Instructions = append(nodePlan.Instructions, p.generateInstallInstruction(controlPlane, entry, instEnv))
return nodePlan, nil
}
// generateInstallInstructionWithSkipStart will generate an instruction that executes the `run.sh` or `run.ps1`
// from the installer image based on the control plane configuration. It will add a `SKIP_START` environment variable to prevent
// the service from being started/restarted.
func (p *Planner) generateInstallInstructionWithSkipStart(controlPlane *rkev1.RKEControlPlane, entry *planEntry) plan.OneTimeInstruction {
var skipStartEnv string
switch entry.Metadata.Labels[capr.CattleOSLabel] {
case capr.WindowsMachineOS:
skipStartEnv = fmt.Sprintf("$env:INSTALL_%s_SKIP_START=\"true\"", strings.ToUpper(capr.GetRuntime(controlPlane.Spec.KubernetesVersion)))
default:
skipStartEnv = fmt.Sprintf("INSTALL_%s_SKIP_START=true", strings.ToUpper(capr.GetRuntime(controlPlane.Spec.KubernetesVersion)))
}
instEnv := []string{skipStartEnv}
return p.generateInstallInstruction(controlPlane, entry, instEnv)
}
func (p *Planner) addInitNodePeriodicInstruction(nodePlan plan.NodePlan, controlPlane *rkev1.RKEControlPlane) (plan.NodePlan, error) {
nodePlan.PeriodicInstructions = append(nodePlan.PeriodicInstructions, []plan.PeriodicInstruction{
{
Name: captureAddressInstructionName,
Command: "sh",
Args: []string{
"-c",
// the grep here is to make the command fail if we don't get the output we expect, like empty string.
fmt.Sprintf("curl -f --retry 100 --retry-delay 5 --cacert "+
"/var/lib/rancher/%s/server/tls/server-ca.crt https://localhost:%d/db/info | grep 'clientURLs'",
capr.GetRuntime(controlPlane.Spec.KubernetesVersion),
capr.GetRuntimeSupervisorPort(controlPlane.Spec.KubernetesVersion)),
},
PeriodSeconds: 600,
},
{
Name: etcdNameInstructionName,
Command: "sh",
Args: []string{
"-c",
fmt.Sprintf("cat /var/lib/rancher/%s/server/db/etcd/name", capr.GetRuntime(controlPlane.Spec.KubernetesVersion)),
},
PeriodSeconds: 600,
},
}...)
return nodePlan, nil
}
func (p *Planner) addEtcdSnapshotListLocalPeriodicInstruction(nodePlan plan.NodePlan, controlPlane *rkev1.RKEControlPlane) (plan.NodePlan, error) {
nodePlan.PeriodicInstructions = append(nodePlan.PeriodicInstructions, plan.PeriodicInstruction{
Name: "etcd-snapshot-list-local",
Command: "sh",
Args: []string{
"-c",
// the grep here is to make the command fail if we don't get the output we expect, like empty string.
fmt.Sprintf("%s etcd-snapshot list --etcd-s3=false 2>/dev/null",
capr.GetRuntime(controlPlane.Spec.KubernetesVersion)),
},
PeriodSeconds: 600,
})
return nodePlan, nil
}
func (p *Planner) addEtcdSnapshotListS3PeriodicInstruction(nodePlan plan.NodePlan, controlPlane *rkev1.RKEControlPlane) (plan.NodePlan, error) {
nodePlan.PeriodicInstructions = append(nodePlan.PeriodicInstructions, plan.PeriodicInstruction{
Name: "etcd-snapshot-list-s3",
Command: "sh",
Args: []string{
"-c",
// the grep here is to make the command fail if we don't get the output we expect, like empty string.
fmt.Sprintf("%s etcd-snapshot list --etcd-s3 2>/dev/null",
capr.GetRuntime(controlPlane.Spec.KubernetesVersion)),
},
PeriodSeconds: 600,
})
return nodePlan, nil
}