-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Validate machineset before reconciling #669
Closed
ingvagabund
wants to merge
1
commit into
kubernetes-sigs:master
from
ingvagabund:validate-machineset-in-reconsiliation
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,19 +36,6 @@ var expectedRequest = reconcile.Request{NamespacedName: types.NamespacedName{Nam | |
const timeout = time.Second * 5 | ||
|
||
func TestReconcile(t *testing.T) { | ||
replicas := int32(2) | ||
instance := &clusterv1alpha1.MachineSet{ | ||
ObjectMeta: metav1.ObjectMeta{Name: "foo", Namespace: "default"}, | ||
Spec: clusterv1alpha1.MachineSetSpec{ | ||
Replicas: &replicas, | ||
Template: clusterv1alpha1.MachineTemplateSpec{ | ||
Spec: clusterv1alpha1.MachineSpec{ | ||
Versions: clusterv1alpha1.MachineVersionInfo{Kubelet: "1.10.3"}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
// Setup the Manager and Controller. Wrap the Controller Reconcile function so it writes each request to a | ||
// channel when it is finished. | ||
mgr, err := manager.New(cfg, manager.Options{}) | ||
|
@@ -64,61 +51,142 @@ func TestReconcile(t *testing.T) { | |
} | ||
defer close(StartTestManager(mgr, t)) | ||
|
||
// Create the MachineSet object and expect Reconcile to be called and the Machines to be created. | ||
if err := c.Create(context.TODO(), instance); err != nil { | ||
t.Errorf("error creating instance: %v", err) | ||
} | ||
defer c.Delete(context.TODO(), instance) | ||
select { | ||
case recv := <-requests: | ||
if recv != expectedRequest { | ||
t.Error("received request does not match expected request") | ||
} | ||
case <-time.After(timeout): | ||
t.Error("timed out waiting for request") | ||
replicas := int32(2) | ||
labels := map[string]string{"foo": "bar"} | ||
|
||
testCases := []struct { | ||
name string | ||
instance *clusterv1alpha1.MachineSet | ||
expectedRequest reconcile.Request | ||
verifyFnc func() | ||
}{ | ||
{ | ||
name: "Refuse invalid machineset (with invalid matching labels)", | ||
instance: &clusterv1alpha1.MachineSet{ | ||
ObjectMeta: metav1.ObjectMeta{Name: "invalidfoo", Namespace: "default"}, | ||
Spec: clusterv1alpha1.MachineSetSpec{ | ||
Replicas: &replicas, | ||
Selector: metav1.LabelSelector{ | ||
MatchLabels: map[string]string{"foo": "bar"}, | ||
}, | ||
Template: clusterv1alpha1.MachineTemplateSpec{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Labels: map[string]string{"foo": "bar2"}, | ||
}, | ||
Spec: clusterv1alpha1.MachineSpec{ | ||
Versions: clusterv1alpha1.MachineVersionInfo{Kubelet: "1.10.3"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedRequest: reconcile.Request{NamespacedName: types.NamespacedName{Name: "invalidfoo", Namespace: "default"}}, | ||
verifyFnc: func() { | ||
// expecting machineset validation error | ||
if _, err := r.Reconcile(reconcile.Request{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could also validate |
||
NamespacedName: types.NamespacedName{Name: "invalidfoo", Namespace: "default"}, | ||
}); err == nil { | ||
t.Errorf("expected validation error did not occur") | ||
} | ||
}, | ||
}, | ||
{ | ||
name: "Create the MachineSet object and expect Reconcile to be called and the Machines to be created", | ||
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"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
expectedRequest: reconcile.Request{NamespacedName: types.NamespacedName{Name: "foo", Namespace: "default"}}, | ||
// Verify machines are created and recreated after deletion | ||
verifyFnc: func() { | ||
machines := &clusterv1alpha1.MachineList{} | ||
|
||
// TODO(joshuarubin) there seems to be a race here. If expectInt sleeps | ||
// briefly, even 10ms, the number of replicas is 4 and not 2 as expected | ||
expectInt(t, int(replicas), func(ctx context.Context) int { | ||
if err := c.List(ctx, &client.ListOptions{}, machines); err != nil { | ||
return -1 | ||
} | ||
return len(machines.Items) | ||
}) | ||
|
||
// Verify that each machine has the desired kubelet version. | ||
for _, m := range machines.Items { | ||
if k := m.Spec.Versions.Kubelet; k != "1.10.3" { | ||
t.Errorf("kubelet was %q not '1.10.3'", k) | ||
} | ||
} | ||
|
||
// Delete a Machine and expect Reconcile to be called to replace it. | ||
m := machines.Items[0] | ||
if err := c.Delete(context.TODO(), &m); err != nil { | ||
t.Errorf("error deleting machine: %v", err) | ||
} | ||
select { | ||
case recv := <-requests: | ||
if recv != expectedRequest { | ||
t.Error("received request does not match expected request") | ||
} | ||
case <-time.After(timeout): | ||
t.Error("timed out waiting for request") | ||
} | ||
|
||
// TODO (robertbailey): Figure out why the control loop isn't working as expected. | ||
/* | ||
g.Eventually(func() int { | ||
if err := c.List(context.TODO(), &client.ListOptions{}, machines); err != nil { | ||
return -1 | ||
} | ||
return len(machines.Items) | ||
}, timeout).Should(gomega.BeEquivalentTo(replicas)) | ||
*/ | ||
}, | ||
}, | ||
} | ||
|
||
machines := &clusterv1alpha1.MachineList{} | ||
|
||
// TODO(joshuarubin) there seems to be a race here. If expectInt sleeps | ||
// briefly, even 10ms, the number of replicas is 4 and not 2 as expected | ||
expectInt(t, int(replicas), func(ctx context.Context) int { | ||
if err := c.List(ctx, &client.ListOptions{}, machines); err != nil { | ||
return -1 | ||
} | ||
return len(machines.Items) | ||
}) | ||
for _, tc := range testCases { | ||
t.Logf("Running %q testcase", tc.name) | ||
func() { | ||
if err := c.Create(context.TODO(), tc.instance); err != nil { | ||
t.Errorf("error creating instance: %v", err) | ||
} | ||
|
||
// Verify that each machine has the desired kubelet version. | ||
for _, m := range machines.Items { | ||
if k := m.Spec.Versions.Kubelet; k != "1.10.3" { | ||
t.Errorf("kubelet was %q not '1.10.3'", k) | ||
} | ||
} | ||
defer func() { | ||
c.Delete(context.TODO(), tc.instance) | ||
select { | ||
case recv := <-requests: | ||
if recv != tc.expectedRequest { | ||
t.Error("received request does not match expected request") | ||
} | ||
case <-time.After(timeout): | ||
t.Error("timed out waiting for request") | ||
} | ||
}() | ||
|
||
select { | ||
case recv := <-requests: | ||
if recv != tc.expectedRequest { | ||
t.Error("received request does not match expected request") | ||
} | ||
case <-time.After(timeout): | ||
t.Error("timed out waiting for request") | ||
} | ||
|
||
// Delete a Machine and expect Reconcile to be called to replace it. | ||
m := machines.Items[0] | ||
if err := c.Delete(context.TODO(), &m); err != nil { | ||
t.Errorf("error deleting machine: %v", err) | ||
tc.verifyFnc() | ||
}() | ||
} | ||
select { | ||
case recv := <-requests: | ||
if recv != expectedRequest { | ||
t.Error("received request does not match expected request") | ||
} | ||
case <-time.After(timeout): | ||
t.Error("timed out waiting for request") | ||
} | ||
|
||
// TODO (robertbailey): Figure out why the control loop isn't working as expected. | ||
/* | ||
g.Eventually(func() int { | ||
if err := c.List(context.TODO(), &client.ListOptions{}, machines); err != nil { | ||
return -1 | ||
} | ||
return len(machines.Items) | ||
}, timeout).Should(gomega.BeEquivalentTo(replicas)) | ||
*/ | ||
} | ||
|
||
func expectInt(t *testing.T, expect int, fn func(context.Context) int) { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could consider to run and Admission Webhook for this and similar features. This is better than we have today though