Skip to content

Commit

Permalink
Support MachineDeployment adoption of MachineSets
Browse files Browse the repository at this point in the history
Signed-off-by: Vince Prignano <[email protected]>
  • Loading branch information
vincepri committed Mar 1, 2019
1 parent b6e166e commit 5274bb8
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 26 deletions.
1 change: 1 addition & 0 deletions pkg/controller/machinedeployment/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ go_library(
"//pkg/apis/cluster/common:go_default_library",
"//pkg/apis/cluster/v1alpha1:go_default_library",
"//pkg/controller/machinedeployment/util:go_default_library",
"//pkg/util:go_default_library",
"//vendor/github.com/pkg/errors:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/equality:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
Expand Down
24 changes: 18 additions & 6 deletions pkg/controller/machinedeployment/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,9 @@ func (r *ReconcileMachineDeployment) getMachineSetsForDeployment(d *v1alpha1.Mac
return nil, err
}

// TODO: flush out machine set adoption.

filteredMS := make([]*v1alpha1.MachineSet, 0, len(machineSets.Items))
for idx := range machineSets.Items {
ms := &machineSets.Items[idx]
if metav1.GetControllerOf(ms) == nil || (metav1.GetControllerOf(ms) != nil && !metav1.IsControlledBy(ms, d)) {
klog.V(4).Infof("%s not controlled by %v", ms.Name, d.Name)
continue
}

selector, err := metav1.LabelSelectorAsSelector(&d.Spec.Selector)
if err != nil {
Expand All @@ -134,12 +128,30 @@ func (r *ReconcileMachineDeployment) getMachineSetsForDeployment(d *v1alpha1.Mac
continue
}

// Attempt to adopt machine if it meets previous conditions and it has no controller references.
if metav1.GetControllerOf(ms) == nil {
if err := r.adoptOrphan(d, ms); err != nil {
klog.Warningf("Failed to adopt MachineSet %q into MachineDeployment %q: %v", ms.Name, d.Name, err)
continue
}
}

if !metav1.IsControlledBy(ms, d) {
continue
}

filteredMS = append(filteredMS, ms)
}

return filteredMS, nil
}

func (r *ReconcileMachineDeployment) adoptOrphan(deployment *v1alpha1.MachineDeployment, machineSet *v1alpha1.MachineSet) error {
newRef := *metav1.NewControllerRef(deployment, controllerKind)
machineSet.OwnerReferences = append(machineSet.OwnerReferences, newRef)
return r.Client.Update(context.Background(), machineSet)
}

// Reconcile reads that state of the cluster for a MachineDeployment object and makes changes based on the state read
// and what is in the MachineDeployment.Spec
// +kubebuilder:rbac:groups=cluster.k8s.io,resources=machinedeployments,verbs=get;list;watch;create;update;patch;delete
Expand Down
27 changes: 7 additions & 20 deletions pkg/controller/machineset/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,10 @@ func (r *ReconcileMachineSet) Reconcile(request reconcile.Request) (reconcile.Re
continue
}

// Attempt to adopt machine if it meets previous conditions and it has no controller ref.
// Attempt to adopt machine if it meets previous conditions and it has no controller references.
if metav1.GetControllerOf(machine) == nil {
if err := r.adoptOrphan(machineSet, machine); err != nil {
klog.Warningf("failed to adopt machine %v into machineset %v. %v", machine.Name, machineSet.Name, err)
klog.Warningf("Failed to adopt MachineSet %q into MachineSet %q: %v", machine.Name, machineSet.Name, err)
continue
}
}
Expand Down Expand Up @@ -291,12 +291,12 @@ func (r *ReconcileMachineSet) syncReplicas(ms *clusterv1alpha1.MachineSet, machi
var machineList []*clusterv1alpha1.Machine
var errstrings []string
for i := 0; i < diff; i++ {
klog.Infof("creating machine %d of %d, ( spec.replicas(%d) > currentMachineCount(%d) )",
klog.Infof("Creating machine %d of %d, ( spec.replicas(%d) > currentMachineCount(%d) )",
i+1, diff, *(ms.Spec.Replicas), len(machines))

machine := r.createMachine(ms)
if err := r.Client.Create(context.Background(), machine); err != nil {
klog.Errorf("unable to create a machine = %s, due to %v", machine.Name, err)
klog.Errorf("Unable to create Machine %q: %v", machine.Name, err)
errstrings = append(errstrings, err.Error())
continue
}
Expand Down Expand Up @@ -325,7 +325,7 @@ func (r *ReconcileMachineSet) syncReplicas(ms *clusterv1alpha1.MachineSet, machi
defer wg.Done()
err := r.Client.Delete(context.Background(), targetMachine)
if err != nil {
klog.Errorf("unable to delete a machine = %s, due to %v", targetMachine.Name, err)
klog.Errorf("Unable to delete Machine %s: %v", targetMachine.Name, err)
errCh <- err
}
}(machine)
Expand Down Expand Up @@ -386,22 +386,9 @@ func shouldExcludeMachine(machineSet *clusterv1alpha1.MachineSet, machine *clust
}

func (r *ReconcileMachineSet) adoptOrphan(machineSet *clusterv1alpha1.MachineSet, machine *clusterv1alpha1.Machine) error {
// Add controller reference.
ownerRefs := machine.ObjectMeta.GetOwnerReferences()
if ownerRefs == nil {
ownerRefs = []metav1.OwnerReference{}
}

newRef := *metav1.NewControllerRef(machineSet, controllerKind)
ownerRefs = append(ownerRefs, newRef)
machine.ObjectMeta.SetOwnerReferences(ownerRefs)

if err := r.Client.Update(context.Background(), machine); err != nil {
klog.Warningf("Failed to update machine owner reference. %v", err)
return err
}

return nil
machine.OwnerReferences = append(machine.OwnerReferences, newRef)
return r.Client.Update(context.Background(), machine)
}

func (r *ReconcileMachineSet) waitForMachineCreation(machineList []*clusterv1alpha1.Machine) error {
Expand Down
3 changes: 3 additions & 0 deletions pkg/controller/machineset/machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,17 @@ func hasMatchingLabels(machineSet *v1alpha1.MachineSet, machine *v1alpha1.Machin
klog.Warningf("unable to convert selector: %v", err)
return false
}

// If a deployment with a nil or empty selector creeps in, it should match nothing, not everything.
if selector.Empty() {
klog.V(2).Infof("%v machineset has empty selector", machineSet.Name)
return false
}

if !selector.Matches(labels.Set(machine.Labels)) {
klog.V(4).Infof("%v machine has mismatch labels", machine.Name)
return false
}

return true
}

0 comments on commit 5274bb8

Please sign in to comment.