Skip to content
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

⚠️ MachineHealthCheck Spec.Selector field cannot be empty #3032

Merged
merged 1 commit into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion api/v1alpha3/machinehealthcheck_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,22 @@ func (m *MachineHealthCheck) validate(old *MachineHealthCheck) error {
var allErrs field.ErrorList

// Validate selector parses as Selector
_, err := metav1.LabelSelectorAsSelector(&m.Spec.Selector)
selector, err := metav1.LabelSelectorAsSelector(&m.Spec.Selector)
if err != nil {
allErrs = append(
allErrs,
field.Invalid(field.NewPath("spec", "selector"), m.Spec.Selector, err.Error()),
)
}

// Validate that the selector isn't empty.
Copy link
Member

@enxebre enxebre May 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Since empty label selector is actually a valid semantic https://github.com/kubernetes/kubernetes/blob/665e76d97623447d13bb3b33b68591a985b49c9d/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/types.go#L1041
It'd be helpful to add the reasoning behind this in the commit description (git commit -m"" -m"This is to prevent users from accidentally...").

if selector != nil && selector.Empty() {
allErrs = append(
allErrs,
field.Invalid(field.NewPath("spec", "selector"), m.Spec.Selector, "selector must not be empty"),
)
}

if old != nil && old.Spec.ClusterName != m.Spec.ClusterName {
allErrs = append(
allErrs,
Expand Down
28 changes: 28 additions & 0 deletions api/v1alpha3/machinehealthcheck_webhook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,21 @@ func TestMachineHealthCheckClusterNameImmutable(t *testing.T) {
newMHC := &MachineHealthCheck{
Spec: MachineHealthCheckSpec{
ClusterName: tt.newClusterName,
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{
"test": "test",
},
},
},
}
oldMHC := &MachineHealthCheck{
Spec: MachineHealthCheckSpec{
ClusterName: tt.oldClusterName,
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{
"test": "test",
},
},
},
}

Expand Down Expand Up @@ -172,6 +182,11 @@ func TestMachineHealthCheckNodeStartupTimeout(t *testing.T) {
mhc := &MachineHealthCheck{
Spec: MachineHealthCheckSpec{
NodeStartupTimeout: tt.timeout,
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{
"test": "test",
},
},
},
}

Expand Down Expand Up @@ -220,6 +235,11 @@ func TestMachineHealthCheckMaxUnhealthy(t *testing.T) {
mhc := &MachineHealthCheck{
Spec: MachineHealthCheckSpec{
MaxUnhealthy: &maxUnhealthy,
Selector: metav1.LabelSelector{
MatchLabels: map[string]string{
"test": "test",
},
},
},
}

Expand All @@ -232,3 +252,11 @@ func TestMachineHealthCheckMaxUnhealthy(t *testing.T) {
}
}
}

func TestMachineHealthCheckSelectorValidation(t *testing.T) {
g := NewWithT(t)
mhc := &MachineHealthCheck{}
err := mhc.validate(nil)
g.Expect(err).ToNot(BeNil())
g.Expect(err.Error()).To(ContainSubstring("selector must not be empty"))
}