@@ -26,11 +26,12 @@ import (
2626 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2727 utilerrors "k8s.io/apimachinery/pkg/util/errors"
2828 "k8s.io/apimachinery/pkg/util/sets"
29-
3029 v1clientset "tkestack.io/tke/api/client/clientset/versioned/typed/platform/v1"
30+ platformv1 "tkestack.io/tke/api/platform/v1"
3131 v1 "tkestack.io/tke/api/platform/v1"
3232 clusterprovider "tkestack.io/tke/pkg/platform/provider/cluster"
3333 machineprovider "tkestack.io/tke/pkg/platform/provider/machine"
34+ "tkestack.io/tke/pkg/util/apiclient"
3435 "tkestack.io/tke/pkg/util/log"
3536)
3637
@@ -100,7 +101,7 @@ func (d *machineDeleter) Delete(ctx context.Context, name string) error {
100101
101102 // ensure that the status is up to date on the machine
102103 // if we get a not found error, we assume the machine is truly gone
103- machine , err = d .retryOnConflictError (ctx , machine , d .updateMachineStatusFunc )
104+ machine , err = d .retryOnConflictError (machine , d .updateMachineStatusFunc )
104105 if err != nil {
105106 if errors .IsNotFound (err ) {
106107 return nil
@@ -125,7 +126,7 @@ func (d *machineDeleter) Delete(ctx context.Context, name string) error {
125126 }
126127
127128 // we have removed content, so mark it finalized by us
128- machine , err = d .retryOnConflictError (ctx , machine , d .finalizeMachine )
129+ machine , err = d .retryOnConflictError (machine , d .finalizeMachine )
129130 if err != nil {
130131 // in normal practice, this should not be possible, but if a deployment is running
131132 // two controllers to do machine deletion that share a common finalizer token it's
@@ -158,15 +159,15 @@ func (d *machineDeleter) deleteMachine(machine *v1.Machine) error {
158159}
159160
160161// updateMachineFunc is a function that makes an update to a namespace
161- type updateMachineFunc func (ctx context. Context , machine * v1.Machine ) (* v1.Machine , error )
162+ type updateMachineFunc func (machine * v1.Machine ) (* v1.Machine , error )
162163
163164// retryOnConflictError retries the specified fn if there was a conflict error
164165// it will return an error if the UID for an object changes across retry operations.
165166// TODO RetryOnConflict should be a generic concept in client code
166- func (d * machineDeleter ) retryOnConflictError (ctx context. Context , machine * v1.Machine , fn updateMachineFunc ) (result * v1.Machine , err error ) {
167+ func (d * machineDeleter ) retryOnConflictError (machine * v1.Machine , fn updateMachineFunc ) (result * v1.Machine , err error ) {
167168 latestMachine := machine
168169 for {
169- result , err = fn (ctx , latestMachine )
170+ result , err = fn (latestMachine )
170171 if err == nil {
171172 return result , nil
172173 }
@@ -185,7 +186,7 @@ func (d *machineDeleter) retryOnConflictError(ctx context.Context, machine *v1.M
185186}
186187
187188// updateMachineStatusFunc will verify that the status of the machine is correct
188- func (d * machineDeleter ) updateMachineStatusFunc (ctx context. Context , machine * v1.Machine ) (* v1.Machine , error ) {
189+ func (d * machineDeleter ) updateMachineStatusFunc (machine * v1.Machine ) (* v1.Machine , error ) {
189190 if machine .DeletionTimestamp .IsZero () || machine .Status .Phase == v1 .MachineTerminating {
190191 return machine , nil
191192 }
@@ -202,7 +203,7 @@ func finalized(machine *v1.Machine) bool {
202203}
203204
204205// finalizeMachine removes the specified finalizerToken and finalizes the machine
205- func (d * machineDeleter ) finalizeMachine (ctx context. Context , machine * v1.Machine ) (* v1.Machine , error ) {
206+ func (d * machineDeleter ) finalizeMachine (machine * v1.Machine ) (* v1.Machine , error ) {
206207 machineFinalize := v1.Machine {}
207208 machineFinalize .ObjectMeta = machine .ObjectMeta
208209 machineFinalize .Spec = machine .Spec
@@ -224,7 +225,7 @@ func (d *machineDeleter) finalizeMachine(ctx context.Context, machine *v1.Machin
224225 Name (machineFinalize .Name ).
225226 SubResource ("finalize" ).
226227 Body (& machineFinalize ).
227- Do (ctx ).
228+ Do (context . Background () ).
228229 Into (machine )
229230
230231 if err != nil {
@@ -240,6 +241,7 @@ type deleteResourceFunc func(ctx context.Context, deleter *machineDeleter, machi
240241
241242var deleteResourceFuncs = []deleteResourceFunc {
242243 deleteMachineProvider ,
244+ deleteNode ,
243245}
244246
245247// deleteAllContent will use the client to delete each resource identified in machine.
@@ -285,3 +287,39 @@ func deleteMachineProvider(ctx context.Context, deleter *machineDeleter, machine
285287
286288 return nil
287289}
290+
291+ func deleteNode (ctx context.Context , deleter * machineDeleter , machine * v1.Machine ) error {
292+ log .FromContext (ctx ).Info ("deleteNode doing" )
293+
294+ cluster , err := clusterprovider .GetV1ClusterByName (context .Background (), deleter .platformClient , machine .Spec .ClusterName , clusterprovider .AdminUsername )
295+ if err != nil {
296+ return err
297+ }
298+ if cluster .Status .Phase == platformv1 .ClusterTerminating {
299+ return nil
300+ }
301+ clientset , err := cluster .Clientset ()
302+ if err != nil {
303+ return err
304+ }
305+
306+ node , err := apiclient .GetNodeByMachineIP (ctx , clientset , machine .Spec .IP )
307+ if err != nil {
308+ if ! errors .IsNotFound (err ) {
309+ return err
310+ }
311+ log .FromContext (ctx ).Info ("deleteNode done" )
312+ return nil
313+ }
314+
315+ err = clientset .CoreV1 ().Nodes ().Delete (context .Background (), node .Name , metav1.DeleteOptions {})
316+ if err != nil {
317+ if ! errors .IsNotFound (err ) {
318+ return err
319+ }
320+ }
321+
322+ log .FromContext (ctx ).Info ("deleteNode done" )
323+
324+ return nil
325+ }
0 commit comments