Skip to content

Commit

Permalink
validate that daemonsets don't have empty selectors on creation
Browse files Browse the repository at this point in the history
  • Loading branch information
mikedanese committed Mar 28, 2016
1 parent ab20b88 commit 2eb4896
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/apis/extensions/validation/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ func ValidateDaemonSetSpec(spec *extensions.DaemonSetSpec, fldPath *field.Path)
if err == nil && !selector.Matches(labels.Set(spec.Template.Labels)) {
allErrs = append(allErrs, field.Invalid(fldPath.Child("template", "metadata", "labels"), spec.Template.Labels, "`selector` does not match template `labels`"))
}
if spec.Selector != nil && len(spec.Selector.MatchLabels)+len(spec.Selector.MatchExpressions) == 0 {
allErrs = append(allErrs, field.Invalid(fldPath.Child("selector"), spec.Selector, "empty selector is not valid for daemonset."))
}

allErrs = append(allErrs, apivalidation.ValidatePodTemplateSpec(&spec.Template, fldPath.Child("template"))...)
// Daemons typically run on more than one node, so mark Read-Write persistent disks as invalid.
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/extensions/validation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,9 +700,16 @@ func TestValidateDaemonSet(t *testing.T) {
Template: validPodTemplate.Template,
},
},
"nil selector": {
ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: api.NamespaceDefault},
Spec: extensions.DaemonSetSpec{
Template: validPodTemplate.Template,
},
},
"empty selector": {
ObjectMeta: api.ObjectMeta{Name: "abc", Namespace: api.NamespaceDefault},
Spec: extensions.DaemonSetSpec{
Selector: &unversioned.LabelSelector{},
Template: validPodTemplate.Template,
},
},
Expand Down

4 comments on commit 2eb4896

@k8s-teamcity-mesosphere

Choose a reason for hiding this comment

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

TeamCity OSS :: Kubernetes Mesos :: 4 - Smoke Tests Build 20063 outcome was SUCCESS
Summary: Tests passed: 1, ignored: 267 Build time: 00:05:31

@r0mant
Copy link

@r0mant r0mant commented on 2eb4896 Apr 11, 2016

Choose a reason for hiding this comment

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

@mikedanese

Hey Mike,

I was investigating why my DaemonSet creation started failing with empty selector is not valid for daemonset after upgrade to 1.2.2 and found this commit. I think that both the docs and the code mention that selector defaults to labels on the pod template so it sounds like it should be okay if it's empty (e.g. https://github.com/kubernetes/kubernetes/blob/v1.2.2/pkg/apis/extensions/types.go#L410). Am I missing something here?

Thanks!

@mikedanese
Copy link
Member Author

Choose a reason for hiding this comment

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

@r0mant defaulting happens before validation, so providing only pod labels should be okay. Were your daemonsets created in v1.1? We made a backwards incompatible change (the daemonsets were an alpha feature) that requires the daemonsets to be deleted and recreated when upgrading to 1.2. This is documented in the release notes.

https://github.com/kubernetes/kubernetes/blob/master/CHANGELOG.md/#action-required-1

@r0mant
Copy link

@r0mant r0mant commented on 2eb4896 Apr 11, 2016

Choose a reason for hiding this comment

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

Okay, I see. Thanks @mikedanese!

Please sign in to comment.