Skip to content

Commit ca93da2

Browse files
committed
fix(engine): reduce duplicate steps and improve logging
1 parent 1bdd051 commit ca93da2

File tree

2 files changed

+40
-19
lines changed

2 files changed

+40
-19
lines changed

pkg/engine/engine.go

+39-18
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,10 @@ type Engine struct {
2727
Logger *zerolog.Logger
2828

2929
sync.Mutex
30-
installer []byte
31-
clusterToken string
32-
serverURL string
30+
installer []byte
31+
clusterToken string
32+
serverURL string
33+
cleanupPending bool
3334

3435
Spec *Config
3536
}
@@ -77,21 +78,16 @@ func (e *Engine) SetSpec(config *Config) error {
7778

7879
e.Spec = config
7980

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-
8981
return nil
9082
}
9183

9284
// ConfigureNode uploads the installer and the configuration
9385
// to a node prior to running the installation script.
9486
func (e *Engine) ConfigureNode(node *Node) error {
87+
e.cleanupPending = true
88+
89+
node.Logger.Info().Msg("Configuring node")
90+
9591
installer, err := e.fetchInstallationScript()
9692
if err != nil {
9793
return err
@@ -152,6 +148,10 @@ func (e *Engine) ConfigureNode(node *Node) error {
152148

153149
// Install runs the installation script on the node.
154150
func (e *Engine) Install() error {
151+
if err := e.configureServerURL(); err != nil {
152+
return err
153+
}
154+
155155
if err := e.installControlPlanes(); err != nil {
156156
return err
157157
}
@@ -171,6 +171,7 @@ func (e *Engine) Uninstall() error {
171171
uninstallScript = "k3s-agent-uninstall.sh"
172172
}
173173

174+
node.Logger.Info().Msg("Running uninstallation script")
174175
if err := node.Do(sshx.Cmd{
175176
Cmd: uninstallScript,
176177
Shell: true,
@@ -217,10 +218,13 @@ func (e *Engine) Disconnect() error {
217218

218219
for _, node := range nodes {
219220
// 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+
}
224228
}
225229

226230
if err := node.Disconnect(); err != nil {
@@ -233,11 +237,12 @@ func (e *Engine) Disconnect() error {
233237

234238
// KubeConfig writes the kubeconfig of the cluster to the specified location.
235239
func (e *Engine) KubeConfig(outputPath string) error {
236-
firstControlPlane := e.FilterNodes(RoleServer)[0]
240+
server := e.FilterNodes(RoleServer)[0]
237241

238242
// Download kubeconfig.
239243
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{
241246
Cmd: "sudo cat /etc/rancher/k3s/k3s.yaml",
242247
Stdout: newConfigBuffer,
243248
}); err != nil {
@@ -321,6 +326,20 @@ func (e *Engine) KubeConfig(outputPath string) error {
321326
return clientcmd.WriteToFile(*oldConfig, outputPath)
322327
}
323328

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+
324343
// fetchInstallationScript returns the downloaded the k3s installer.
325344
func (e *Engine) fetchInstallationScript() ([]byte, error) {
326345
// Lock engine to prevent concurrent access to installer cache.
@@ -387,6 +406,7 @@ func (e *Engine) installControlPlanes() error {
387406
env["K3S_TOKEN"] = e.clusterToken
388407
}
389408

409+
server.Logger.Info().Msg("Running installation script")
390410
if err := server.Do(sshx.Cmd{
391411
Cmd: "/tmp/k3se/install.sh",
392412
Env: env,
@@ -422,6 +442,7 @@ func (e *Engine) installWorkers() error {
422442
return
423443
}
424444

445+
agent.Logger.Info().Msg("Running installation script")
425446
if err := agent.Do(sshx.Cmd{
426447
Cmd: "/tmp/k3se/install.sh",
427448
Env: map[string]string{

pkg/engine/node.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (node *Node) Write(raw []byte) (int, error) {
102102
for i := 0; i < len(lines)-1; i++ {
103103
line := strings.TrimSpace(lines[i])
104104
if line != "" {
105-
node.Logger.Info().Msg(line)
105+
node.Logger.Debug().Msg(line)
106106
}
107107
}
108108

0 commit comments

Comments
 (0)