From 5274bb89bb1ed4630f44b0fdfedb56e184f2ede8 Mon Sep 17 00:00:00 2001 From: Vince Prignano Date: Fri, 1 Mar 2019 12:59:24 -0800 Subject: [PATCH] Support MachineDeployment adoption of MachineSets Signed-off-by: Vince Prignano --- pkg/controller/machinedeployment/BUILD.bazel | 1 + .../machinedeployment/controller.go | 24 ++++++++++++----- pkg/controller/machineset/controller.go | 27 +++++-------------- pkg/controller/machineset/machine.go | 3 +++ 4 files changed, 29 insertions(+), 26 deletions(-) diff --git a/pkg/controller/machinedeployment/BUILD.bazel b/pkg/controller/machinedeployment/BUILD.bazel index 6901a9849cd5..66465a688805 100644 --- a/pkg/controller/machinedeployment/BUILD.bazel +++ b/pkg/controller/machinedeployment/BUILD.bazel @@ -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", diff --git a/pkg/controller/machinedeployment/controller.go b/pkg/controller/machinedeployment/controller.go index e49ee4c7cfe5..30686e8c0148 100644 --- a/pkg/controller/machinedeployment/controller.go +++ b/pkg/controller/machinedeployment/controller.go @@ -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 { @@ -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 diff --git a/pkg/controller/machineset/controller.go b/pkg/controller/machineset/controller.go index 3dfbede7f930..96247fce5896 100644 --- a/pkg/controller/machineset/controller.go +++ b/pkg/controller/machineset/controller.go @@ -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 } } @@ -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 } @@ -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) @@ -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 { diff --git a/pkg/controller/machineset/machine.go b/pkg/controller/machineset/machine.go index 7776a7a86132..c421da741f3d 100644 --- a/pkg/controller/machineset/machine.go +++ b/pkg/controller/machineset/machine.go @@ -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 }