Skip to content

Commit

Permalink
Merge pull request #3034 from elmiko/improve-delete
Browse files Browse the repository at this point in the history
Improve delete node mechanisms for cluster-api autoscaler provider
  • Loading branch information
k8s-ci-robot authored Jun 4, 2020
2 parents e057aa2 + 1a62952 commit 9b1f6e2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ func (r machineDeploymentScalableResource) SetSize(nreplicas int32) error {
return updateErr
}

func (r machineDeploymentScalableResource) UnmarkMachineForDeletion(machine *Machine) error {
return unmarkMachineForDeletion(r.controller, machine)
}

func (r machineDeploymentScalableResource) MarkMachineForDeletion(machine *Machine) error {
u, err := r.controller.dynamicclient.Resource(*r.controller.machineResource).Namespace(machine.Namespace).Get(context.TODO(), machine.Name, metav1.GetOptions{})

if err != nil {
return err
}
if u == nil {
return fmt.Errorf("unknown machine %s", machine.Name)
}

u = u.DeepCopy()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,9 @@ func (r machineSetScalableResource) SetSize(nreplicas int32) error {

func (r machineSetScalableResource) MarkMachineForDeletion(machine *Machine) error {
u, err := r.controller.dynamicclient.Resource(*r.controller.machineResource).Namespace(machine.Namespace).Get(context.TODO(), machine.Name, metav1.GetOptions{})

if err != nil {
return err
}
if u == nil {
return fmt.Errorf("unknown machine %s", machine.Name)
}

u = u.DeepCopy()

Expand All @@ -123,6 +119,10 @@ func (r machineSetScalableResource) MarkMachineForDeletion(machine *Machine) err
return updateErr
}

func (r machineSetScalableResource) UnmarkMachineForDeletion(machine *Machine) error {
return unmarkMachineForDeletion(r.controller, machine)
}

func newMachineSetScalableResource(controller *machineController, machineSet *MachineSet) (*machineSetScalableResource, error) {
minSize, maxSize, err := parseScalingBounds(machineSet.Annotations)
if err != nil {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ func (ng *nodegroup) DeleteNodes(nodes []*corev1.Node) error {
}

if err := ng.scalableResource.SetSize(replicas - 1); err != nil {
nodeGroup.scalableResource.UnmarkMachineForDeletion(machine)
return err
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ limitations under the License.

package clusterapi

import (
"context"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// scalableResource is a resource that can be scaled up and down by
// adjusting its replica count field.
type scalableResource interface {
Expand Down Expand Up @@ -46,4 +52,23 @@ type scalableResource interface {

// MarkMachineForDeletion marks machine for deletion
MarkMachineForDeletion(machine *Machine) error

// UnmarkMachineForDeletion unmarks machine for deletion
UnmarkMachineForDeletion(machine *Machine) error
}

func unmarkMachineForDeletion(controller *machineController, machine *Machine) error {
u, err := controller.dynamicclient.Resource(*controller.machineResource).Namespace(machine.Namespace).Get(context.TODO(), machine.Name, metav1.GetOptions{})
if err != nil {
return err
}

annotations := u.GetAnnotations()
if _, ok := annotations[machineDeleteAnnotationKey]; ok {
delete(annotations, machineDeleteAnnotationKey)
u.SetAnnotations(annotations)
_, updateErr := controller.dynamicclient.Resource(*controller.machineResource).Namespace(u.GetNamespace()).Update(context.TODO(), u, metav1.UpdateOptions{})
return updateErr
}
return nil
}

0 comments on commit 9b1f6e2

Please sign in to comment.