@@ -27,9 +27,10 @@ type Engine struct {
27
27
Logger * zerolog.Logger
28
28
29
29
sync.Mutex
30
- installer []byte
31
- clusterToken string
32
- serverURL string
30
+ installer []byte
31
+ clusterToken string
32
+ serverURL string
33
+ cleanupPending bool
33
34
34
35
Spec * Config
35
36
}
@@ -77,21 +78,16 @@ func (e *Engine) SetSpec(config *Config) error {
77
78
78
79
e .Spec = config
79
80
80
- // If TLS SANs are configured, the first one will be used as the server URL.
81
- // If not, the host address of the first controlplane will be used.
82
- firstControlplane := e .FilterNodes (RoleServer )[0 ]
83
- e .serverURL = fmt .Sprintf ("https://%s:6443" , firstControlplane .SSH .Host )
84
- if len (e .Spec .Cluster .TLSSAN ) > 0 {
85
- e .serverURL = fmt .Sprintf ("https://%s:6443" , e .Spec .Cluster .TLSSAN [0 ])
86
- }
87
- e .Logger .Info ().Str ("server_url" , e .serverURL ).Msgf ("Detecting server URL" )
88
-
89
81
return nil
90
82
}
91
83
92
84
// ConfigureNode uploads the installer and the configuration
93
85
// to a node prior to running the installation script.
94
86
func (e * Engine ) ConfigureNode (node * Node ) error {
87
+ e .cleanupPending = true
88
+
89
+ node .Logger .Info ().Msg ("Configuring node" )
90
+
95
91
installer , err := e .fetchInstallationScript ()
96
92
if err != nil {
97
93
return err
@@ -152,6 +148,10 @@ func (e *Engine) ConfigureNode(node *Node) error {
152
148
153
149
// Install runs the installation script on the node.
154
150
func (e * Engine ) Install () error {
151
+ if err := e .configureServerURL (); err != nil {
152
+ return err
153
+ }
154
+
155
155
if err := e .installControlPlanes (); err != nil {
156
156
return err
157
157
}
@@ -171,6 +171,7 @@ func (e *Engine) Uninstall() error {
171
171
uninstallScript = "k3s-agent-uninstall.sh"
172
172
}
173
173
174
+ node .Logger .Info ().Msg ("Running uninstallation script" )
174
175
if err := node .Do (sshx.Cmd {
175
176
Cmd : uninstallScript ,
176
177
Shell : true ,
@@ -217,10 +218,13 @@ func (e *Engine) Disconnect() error {
217
218
218
219
for _ , node := range nodes {
219
220
// Clean up temporary files before disconnecting.
220
- if err := node .Do (sshx.Cmd {
221
- Cmd : "rm -rf /tmp/k3se" ,
222
- }); err != nil {
223
- return err
221
+ if e .cleanupPending {
222
+ node .Logger .Info ().Msg ("Cleaning up temporary files" )
223
+ if err := node .Do (sshx.Cmd {
224
+ Cmd : "rm -rf /tmp/k3se" ,
225
+ }); err != nil {
226
+ return err
227
+ }
224
228
}
225
229
226
230
if err := node .Disconnect (); err != nil {
@@ -233,11 +237,12 @@ func (e *Engine) Disconnect() error {
233
237
234
238
// KubeConfig writes the kubeconfig of the cluster to the specified location.
235
239
func (e * Engine ) KubeConfig (outputPath string ) error {
236
- firstControlPlane := e .FilterNodes (RoleServer )[0 ]
240
+ server := e .FilterNodes (RoleServer )[0 ]
237
241
238
242
// Download kubeconfig.
239
243
newConfigBuffer := new (bytes.Buffer )
240
- if err := firstControlPlane .Do (sshx.Cmd {
244
+ server .Logger .Info ().Msg ("Downloading kubeconfig" )
245
+ if err := server .Do (sshx.Cmd {
241
246
Cmd : "sudo cat /etc/rancher/k3s/k3s.yaml" ,
242
247
Stdout : newConfigBuffer ,
243
248
}); err != nil {
@@ -321,6 +326,20 @@ func (e *Engine) KubeConfig(outputPath string) error {
321
326
return clientcmd .WriteToFile (* oldConfig , outputPath )
322
327
}
323
328
329
+ // configureServerURL assembles the server URL based on the given spec.
330
+ func (e * Engine ) configureServerURL () error {
331
+ // If TLS SANs are configured, the first one will be used as the server URL.
332
+ // If not, the host address of the first controlplane will be used.
333
+ firstControlplane := e .FilterNodes (RoleServer )[0 ]
334
+ e .serverURL = fmt .Sprintf ("https://%s:6443" , firstControlplane .SSH .Host )
335
+ if len (e .Spec .Cluster .TLSSAN ) > 0 {
336
+ e .serverURL = fmt .Sprintf ("https://%s:6443" , e .Spec .Cluster .TLSSAN [0 ])
337
+ }
338
+ e .Logger .Info ().Str ("server_url" , e .serverURL ).Msg ("Configuring server URL" )
339
+
340
+ return nil
341
+ }
342
+
324
343
// fetchInstallationScript returns the downloaded the k3s installer.
325
344
func (e * Engine ) fetchInstallationScript () ([]byte , error ) {
326
345
// Lock engine to prevent concurrent access to installer cache.
@@ -387,6 +406,7 @@ func (e *Engine) installControlPlanes() error {
387
406
env ["K3S_TOKEN" ] = e .clusterToken
388
407
}
389
408
409
+ server .Logger .Info ().Msg ("Running installation script" )
390
410
if err := server .Do (sshx.Cmd {
391
411
Cmd : "/tmp/k3se/install.sh" ,
392
412
Env : env ,
@@ -422,6 +442,7 @@ func (e *Engine) installWorkers() error {
422
442
return
423
443
}
424
444
445
+ agent .Logger .Info ().Msg ("Running installation script" )
425
446
if err := agent .Do (sshx.Cmd {
426
447
Cmd : "/tmp/k3se/install.sh" ,
427
448
Env : map [string ]string {
0 commit comments