Skip to content

Commit 026fffc

Browse files
authored
feat: allow custom clean up commands for ssh provisioner (#1100)
Signed-off-by: apedriza <[email protected]>
1 parent 736f8c5 commit 026fffc

File tree

9 files changed

+78
-6
lines changed

9 files changed

+78
-6
lines changed

api/infrastructure/v1beta1/remote_machine_types.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ type RemoteMachineSpec struct {
7373
// +kubebuilder:validation:Optional
7474
SSHKeyRef SecretRef `json:"sshKeyRef,omitempty"`
7575

76+
// CustomCleanUpCommands allow the user to run custom command for the clean up process of the machine.
77+
// +kubebuilder:validation:Optional
78+
CustomCleanUpCommands []string `json:"customCleanUpCommands,omitempty"`
79+
7680
// ProvisionJob describes the kubernetes Job to use to provision the machine.
7781
ProvisionJob *ProvisionJob `json:"provisionJob,omitempty"`
7882
}
@@ -148,6 +152,10 @@ type PooledMachineSpec struct {
148152
// +kubebuilder:validation:Optional
149153
UseSudo bool `json:"useSudo,omitempty"`
150154

155+
// CustomCleanUpCommands allow the user to run custom command for the clean up process of the machine.
156+
// +kubebuilder:validation:Optional
157+
CustomCleanUpCommands []string `json:"customCleanUpCommands,omitempty"`
158+
151159
// SSHKeyRef is a reference to a secret that contains the SSH private key.
152160
// The key must be placed on the secret using the key "value".
153161
// +kubebuilder:validation:Required

api/infrastructure/v1beta1/zz_generated.deepcopy.go

Lines changed: 12 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/clusterapi/infrastructure/bases/infrastructure.cluster.x-k8s.io_pooledremotemachines.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ spec:
4646
description: Address is the IP address or DNS name of the remote
4747
machine.
4848
type: string
49+
customCleanUpCommands:
50+
description: CustomCleanUpCommands allow the user to run custom
51+
command for the clean up process of the machine.
52+
items:
53+
type: string
54+
type: array
4955
port:
5056
default: 22
5157
description: Port is the SSH port of the remote machine.

config/clusterapi/infrastructure/bases/infrastructure.cluster.x-k8s.io_remotemachines.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ spec:
4444
address:
4545
description: Address is the IP address or DNS name of the remote machine.
4646
type: string
47+
customCleanUpCommands:
48+
description: CustomCleanUpCommands allow the user to run custom command
49+
for the clean up process of the machine.
50+
items:
51+
type: string
52+
type: array
4753
pool:
4854
description: Pool is the name of the pool where the machine belongs
4955
to.

config/crd/bases/infrastructure/infrastructure.cluster.x-k8s.io_pooledremotemachines.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ spec:
4646
description: Address is the IP address or DNS name of the remote
4747
machine.
4848
type: string
49+
customCleanUpCommands:
50+
description: CustomCleanUpCommands allow the user to run custom
51+
command for the clean up process of the machine.
52+
items:
53+
type: string
54+
type: array
4955
port:
5056
default: 22
5157
description: Port is the SSH port of the remote machine.

config/crd/bases/infrastructure/infrastructure.cluster.x-k8s.io_remotemachines.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ spec:
4444
address:
4545
description: Address is the IP address or DNS name of the remote machine.
4646
type: string
47+
customCleanUpCommands:
48+
description: CustomCleanUpCommands allow the user to run custom command
49+
for the clean up process of the machine.
50+
items:
51+
type: string
52+
type: array
4753
pool:
4854
description: Pool is the name of the pool where the machine belongs
4955
to.

docs/resource-reference/infrastructure.cluster.x-k8s.io-v1beta1.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ Resource Types:
140140
The key must be placed on the secret using the key "value".<br/>
141141
</td>
142142
<td>true</td>
143+
</tr><tr>
144+
<td><b>customCleanUpCommands</b></td>
145+
<td>[]string</td>
146+
<td>
147+
CustomCleanUpCommands allow the user to run custom command for the clean up process of the machine.<br/>
148+
</td>
149+
<td>false</td>
143150
</tr><tr>
144151
<td><b>port</b></td>
145152
<td>integer</td>
@@ -744,6 +751,13 @@ RemoteMachineSpec defines the desired state of RemoteMachine
744751
Address is the IP address or DNS name of the remote machine.<br/>
745752
</td>
746753
<td>false</td>
754+
</tr><tr>
755+
<td><b>customCleanUpCommands</b></td>
756+
<td>[]string</td>
757+
<td>
758+
CustomCleanUpCommands allow the user to run custom command for the clean up process of the machine.<br/>
759+
</td>
760+
<td>false</td>
747761
</tr><tr>
748762
<td><b>pool</b></td>
749763
<td>string</td>

internal/controller/infrastructure/remote_machine_controller.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ func (r *RemoteMachineController) reservePooledMachine(ctx context.Context, rm *
340340
rm.Spec.User = foundPooledMachine.Spec.Machine.User
341341
rm.Spec.SSHKeyRef = foundPooledMachine.Spec.Machine.SSHKeyRef
342342
rm.Spec.UseSudo = foundPooledMachine.Spec.Machine.UseSudo
343+
rm.Spec.CustomCleanUpCommands = foundPooledMachine.Spec.Machine.CustomCleanUpCommands
343344

344345
return nil
345346
}

internal/controller/infrastructure/ssh_provisioner.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,6 @@ func (p *SSHProvisioner) Provision(ctx context.Context) error {
123123
// 3. Removes node from etcd
124124
// 4. Runs k0s reset
125125
func (p *SSHProvisioner) Cleanup(ctx context.Context, mode RemoteMachineMode) error {
126-
if mode == ModeNonK0s {
127-
return nil
128-
}
129-
130126
authM, err := rigssh.ParseSSHPrivateKey(p.sshKey, nil)
131127
if err != nil {
132128
return fmt.Errorf("failed to parse ssh key: %w", err)
@@ -155,6 +151,24 @@ func (p *SSHProvisioner) Cleanup(ctx context.Context, mode RemoteMachineMode) er
155151
rigClient = rigClient.Sudo()
156152
}
157153

154+
// When k0s is not the bootstrap provider, the user can set custom commands for the clean up process.
155+
if mode == ModeNonK0s {
156+
if p.machine.Spec.CustomCleanUpCommands != nil {
157+
p.log.Info("Cleaning up remote machine...")
158+
for _, cmd := range p.machine.Spec.CustomCleanUpCommands {
159+
output, err := rigClient.ExecOutput(cmd)
160+
if err != nil {
161+
p.log.Error(err, "failed to run command", "command", cmd, "output", output)
162+
} else {
163+
p.log.Info("executed command", "command", cmd, "output", output)
164+
}
165+
}
166+
}
167+
168+
return nil
169+
}
170+
171+
// k0s bootstrap provider used.
158172
var cmds []string
159173
if mode == ModeController {
160174
cmds = append(cmds, "k0s etcd leave")
@@ -179,6 +193,7 @@ func (p *SSHProvisioner) Cleanup(ctx context.Context, mode RemoteMachineMode) er
179193
cmds = append(cmds, "k0s reset --kubelet-root-dir "+kubeletRootDir)
180194
}
181195

196+
p.log.Info("Cleaning up remote machine...")
182197
for _, cmd := range cmds {
183198
output, err := rigClient.ExecOutput(cmd)
184199
if err != nil {

0 commit comments

Comments
 (0)