Skip to content

Commit

Permalink
Validate machineset before reconciling
Browse files Browse the repository at this point in the history
Even thought the MachineSet type implements Validate() function,
it's not called by default. The validation function is responsible
for making sure every machine set matchLabels selector is matched with
machine template labels. Given the validation is not performed by default,
it is possible to create an invalid machineset that causes the machineset
controller to start creating machine object one by one without any upper
bound. Causing the machine controller to launch as many instances as
there is machine objects.
  • Loading branch information
ingvagabund committed Jan 11, 2019
1 parent 633970e commit b886317
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package machinedeployment

import (
"fmt"
"testing"
"time"

Expand Down Expand Up @@ -174,6 +175,7 @@ func TestReconcile(t *testing.T) {
expectInt(t, i, func(ctx context.Context) int {
if err = c.Get(ctx, types.NamespacedName{
Namespace: newMachineSet.Namespace, Name: newMachineSet.Name}, newMachineSet); err != nil {
fmt.Printf("err: %v\n", err)
return -1
}
return int(*newMachineSet.Spec.Replicas)
Expand Down
7 changes: 7 additions & 0 deletions pkg/controller/machineset/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,13 @@ func (r *ReconcileMachineSet) Reconcile(request reconcile.Request) (reconcile.Re
}

klog.V(4).Infof("Reconcile machineset %v", machineSet.Name)

if errList := machineSet.Validate(); len(errList) > 0 {
err := fmt.Errorf("%q machineset validation failed: %v", machineSet.Name, errList.ToAggregate().Error())
klog.Error(err)
return reconcile.Result{}, err
}

allMachines := &clusterv1alpha1.MachineList{}

err = r.Client.List(context.Background(), client.InNamespace(machineSet.Namespace), allMachines)
Expand Down
7 changes: 7 additions & 0 deletions pkg/controller/machineset/machineset_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,18 @@ const timeout = time.Second * 5

func TestReconcile(t *testing.T) {
replicas := int32(2)
labels := map[string]string{"foo": "bar"}
instance := &clusterv1alpha1.MachineSet{
ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "default"},
Spec: clusterv1alpha1.MachineSetSpec{
Replicas: &replicas,
Selector: metav1.LabelSelector{
MatchLabels: labels,
},
Template: clusterv1alpha1.MachineTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: labels,
},
Spec: clusterv1alpha1.MachineSpec{
Versions: clusterv1alpha1.MachineVersionInfo{Kubelet: "1.10.3"},
},
Expand Down

0 comments on commit b886317

Please sign in to comment.