Skip to content

Commit 0979666

Browse files
authored
Revert "fix(platform): delete machine failed & move machine operation to provider (#1435)"
This reverts commit 99117f4.
1 parent bf971d9 commit 0979666

File tree

8 files changed

+77
-126
lines changed

8 files changed

+77
-126
lines changed

pkg/platform/controller/cluster/cluster_controller.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ func (c *Controller) needsUpdate(old *platformv1.Cluster, new *platformv1.Cluste
152152
return true
153153
}
154154

155-
if old.Status.Phase != platformv1.ClusterTerminating && new.Status.Phase == platformv1.ClusterTerminating {
155+
if old.Status.Phase == platformv1.ClusterRunning && new.Status.Phase == platformv1.ClusterTerminating {
156156
return true
157157
}
158158

@@ -281,7 +281,7 @@ func (c *Controller) reconcile(ctx context.Context, key string, cluster *platfor
281281
log.FromContext(ctx).Info("Cluster has been terminated. Attempting to cleanup resources")
282282
err = c.deleter.Delete(ctx, key)
283283
if err == nil {
284-
log.FromContext(ctx).Info("Cluster has been successfully deleted")
284+
log.FromContext(ctx).Info("Machine has been successfully deleted")
285285
}
286286
default:
287287
log.FromContext(ctx).Info("unknown cluster phase", "status.phase", cluster.Status.Phase)

pkg/platform/controller/cluster/deletion/cluster_deleter.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ import (
2424

2525
"k8s.io/apimachinery/pkg/api/errors"
2626
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27+
"k8s.io/apimachinery/pkg/fields"
2728
utilerrors "k8s.io/apimachinery/pkg/util/errors"
2829
"k8s.io/apimachinery/pkg/util/sets"
29-
3030
v1clientset "tkestack.io/tke/api/client/clientset/versioned/typed/platform/v1"
3131
platformv1 "tkestack.io/tke/api/platform/v1"
3232
clusterprovider "tkestack.io/tke/pkg/platform/provider/cluster"
@@ -244,6 +244,7 @@ var deleteResourceFuncs = []deleteResourceFunc{
244244
deleteIPAM,
245245
deleteTappControllers,
246246
deleteClusterProvider,
247+
deleteMachine,
247248
}
248249

249250
// deleteAllContent will use the client to delete each resource identified in cluster.
@@ -432,3 +433,29 @@ func deleteClusterCredential(ctx context.Context, deleter *clusterDeleter, clust
432433
return nil
433434
}
434435
*/
436+
437+
func deleteMachine(ctx context.Context, deleter *clusterDeleter, cluster *platformv1.Cluster) error {
438+
log.FromContext(ctx).Info("deleteMachine doing")
439+
440+
fieldSelector := fields.OneTermEqualSelector("spec.clusterName", cluster.Name).String()
441+
machineList, err := deleter.platformClient.Machines().List(ctx, metav1.ListOptions{FieldSelector: fieldSelector})
442+
if err != nil {
443+
return err
444+
}
445+
if len(machineList.Items) == 0 {
446+
return nil
447+
}
448+
background := metav1.DeletePropagationForeground
449+
deleteOpt := metav1.DeleteOptions{PropagationPolicy: &background}
450+
for _, machine := range machineList.Items {
451+
if err := deleter.platformClient.Machines().Delete(ctx, machine.Name, deleteOpt); err != nil {
452+
if !errors.IsNotFound(err) {
453+
return err
454+
}
455+
}
456+
}
457+
458+
log.FromContext(ctx).Info("deleteMachine done")
459+
460+
return nil
461+
}

pkg/platform/controller/machine/deletion/machine_deleter.go

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -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

241242
var 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+
}

pkg/platform/controller/machine/machine_controller.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"k8s.io/client-go/tools/cache"
3535
"k8s.io/client-go/util/retry"
3636
"k8s.io/client-go/util/workqueue"
37-
3837
platformversionedclient "tkestack.io/tke/api/client/clientset/versioned/typed/platform/v1"
3938
platformv1informer "tkestack.io/tke/api/client/informers/externalversions/platform/v1"
4039
platformv1lister "tkestack.io/tke/api/client/listers/platform/v1"
@@ -124,10 +123,6 @@ func (c *Controller) needsUpdate(oldMachine *platformv1.Machine, newMachine *pla
124123
return true
125124
}
126125

127-
if oldMachine.Status.Phase != platformv1.MachineTerminating && newMachine.Status.Phase == platformv1.MachineTerminating {
128-
return true
129-
}
130-
131126
// Control the synchronization interval through the health detection interval
132127
// to avoid version conflicts caused by concurrent modification
133128
healthCondition := newMachine.GetCondition(conditionTypeHealthCheck)

pkg/platform/provider/baremetal/cluster/delete.go

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,9 @@ package cluster
2020

2121
import (
2222
"context"
23-
"time"
2423

2524
"k8s.io/apimachinery/pkg/api/errors"
2625
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27-
"k8s.io/apimachinery/pkg/fields"
28-
"k8s.io/apimachinery/pkg/util/wait"
29-
30-
platformv1client "tkestack.io/tke/api/client/clientset/versioned/typed/platform/v1"
3126
"tkestack.io/tke/pkg/platform/provider/baremetal/phases/kubeadm"
3227
"tkestack.io/tke/pkg/platform/provider/util/mark"
3328
typesv1 "tkestack.io/tke/pkg/platform/types/v1"
@@ -80,44 +75,3 @@ func (p *Provider) EnsureRemoveNode(ctx context.Context, c *v1.Cluster) error {
8075
}
8176
return nil
8277
}
83-
84-
func (p *Provider) EnsureRemoveMachine(ctx context.Context, c *v1.Cluster) error {
85-
log.FromContext(ctx).Info("delete machine start")
86-
fieldSelector := fields.OneTermEqualSelector("spec.clusterName", c.Name).String()
87-
machineList, err := p.platformClient.Machines().List(ctx, metav1.ListOptions{FieldSelector: fieldSelector})
88-
if err != nil {
89-
return err
90-
}
91-
if len(machineList.Items) == 0 {
92-
return nil
93-
}
94-
for _, machine := range machineList.Items {
95-
if err := p.platformClient.Machines().Delete(ctx, machine.Name, metav1.DeleteOptions{}); err != nil {
96-
if errors.IsNotFound(err) {
97-
return nil
98-
}
99-
return err
100-
}
101-
102-
if err = wait.PollImmediate(5*time.Second, 5*time.Minute, waitForMachineDelete(ctx, p.platformClient, machine.Name)); err != nil {
103-
return err
104-
}
105-
}
106-
107-
log.FromContext(ctx).Info("delete machine done")
108-
109-
return nil
110-
}
111-
112-
func waitForMachineDelete(ctx context.Context, c platformv1client.PlatformV1Interface, machineName string) wait.ConditionFunc {
113-
return func() (done bool, err error) {
114-
115-
if _, err := c.Machines().Get(ctx, machineName, metav1.GetOptions{}); err != nil {
116-
if errors.IsNotFound(err) {
117-
return true, nil
118-
}
119-
}
120-
121-
return false, nil
122-
}
123-
}

pkg/platform/provider/baremetal/cluster/provider.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ func NewProvider() (*Provider, error) {
153153
p.EnsureRemoveNode,
154154
},
155155
DeleteHandlers: []clusterprovider.Handler{
156-
p.EnsureRemoveMachine,
157156
p.EnsureCleanClusterMark,
158157
},
159158
}

pkg/platform/provider/baremetal/machine/delete.go

Lines changed: 0 additions & 59 deletions
This file was deleted.

pkg/platform/provider/baremetal/machine/provider.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,6 @@ func NewProvider() (*Provider, error) {
9494
p.EnsureUpgrade,
9595
p.EnsurePostUpgradeHook,
9696
},
97-
DeleteHandlers: []machineprovider.Handler{
98-
p.EnsureRemoveNode,
99-
},
10097
}
10198

10299
cfg, err := config.New(constants.ConfigFile)

0 commit comments

Comments
 (0)