Skip to content

remove apps/v1beta2 defaulting codes for obj.Spec.Selector and obj.Labels #50164

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

Merged
merged 1 commit into from
Aug 10, 2017

Conversation

crimsonfaith91
Copy link
Contributor

@crimsonfaith91 crimsonfaith91 commented Aug 4, 2017

What this PR does / why we need it:
This PR removes defaulting codes for obj.Spec.Selector. Currently, obj.Spec.Selector.MatchLabels is set to obj.Spec.Template.Labels if obj.Spec.Template.Labels != nil && obj.Spec.Selector == nil. We should not perform this defaulting operation as controllers selectors are immutable.

Which issue this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close that issue when PR gets merged): fixes #50339

Special notes for your reviewer:
This PR removes defaulting codes for apps/v1beta2 only. The defaulting codes for validation will be removed in another PR.

Release note:

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Aug 4, 2017
@k8s-github-robot k8s-github-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. release-note-none Denotes a PR that doesn't merit a release note. labels Aug 4, 2017
@k8s-ci-robot k8s-ci-robot assigned kow3ns and unassigned jianhuiz and tallclair Aug 4, 2017
@@ -416,13 +406,6 @@ func TestSetDefaultReplicaSet(t *testing.T) {
t.Errorf("unexpected object: %v", rs2)
t.FailNow()
}
if test.expectSelector != reflect.DeepEqual(rs2.Spec.Selector.MatchLabels, rs2.Spec.Template.Labels) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kow3ns I spent some time thinking whether to set obj.Spec.Selector to nil or &metav1.LabelSelector{MatchLabels: map[string]string{}}. In the end, i chose nil for simplicity. As a result, the test to compare rs2.Spec.Selector.MatchLabels and rs2.Spec.Template.Labels became unnecessary and was removed.

Copy link
Member

Choose a reason for hiding this comment

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

Do not default the selector to anything. We want to eliminate setting the Selector, or any field for that matter, based on another field of the same object. Doing so has caused us pain wrt apply and patch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do not default the selector to anything.
Did you mean obj.Spec.Selector should be nil? obj.Spec.Selector will be nil without explicitly setting it.

Copy link
Member

@kow3ns kow3ns left a comment

Choose a reason for hiding this comment

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

Don't set the labels on the specification objects based on the PodTemplateSpecs.

if len(obj.Labels) == 0 {
obj.Labels = labels
}
if labels != nil && len(obj.Labels) == 0 {
Copy link
Member

Choose a reason for hiding this comment

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

Delete this. You are setting the labels of the DaemonSet to the labels of the PodTemplateSpec. You do not want to do this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kow3ns Noted! I will remove this. Same applies for other comments. :)

obj.Labels = labels
}
if labels != nil && len(obj.Labels) == 0 {
obj.Labels = labels
Copy link
Member

Choose a reason for hiding this comment

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

Delete this. You are setting the labels of the StatefulSet to the labels of the PodTemplateSpec. You do not want to do this.

if len(obj.Labels) == 0 {
obj.Labels = labels
}
if labels != nil && len(obj.Labels) == 0 {
Copy link
Member

Choose a reason for hiding this comment

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

Delete this. You are setting the labels of the Deployment to the labels of the PodTemplateSpec. You do not want to do this.

if len(obj.Labels) == 0 {
obj.Labels = labels
}
if labels != nil && len(obj.Labels) == 0 {
Copy link
Member

Choose a reason for hiding this comment

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

Delete this. You are setting the labels of the ReplicaSet to the labels of the PodTemplateSpec. You do not want to do this.

@@ -416,13 +406,6 @@ func TestSetDefaultReplicaSet(t *testing.T) {
t.Errorf("unexpected object: %v", rs2)
t.FailNow()
}
if test.expectSelector != reflect.DeepEqual(rs2.Spec.Selector.MatchLabels, rs2.Spec.Template.Labels) {
Copy link
Member

Choose a reason for hiding this comment

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

Do not default the selector to anything. We want to eliminate setting the Selector, or any field for that matter, based on another field of the same object. Doing so has caused us pain wrt apply and patch.

@crimsonfaith91 crimsonfaith91 changed the title remove defaulting for obj.Spec.Selector remove defaulting codes for obj.Spec.Selector and obj.Labels Aug 5, 2017
@k8s-github-robot k8s-github-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Aug 5, 2017
@@ -133,7 +133,7 @@ var etcdStorageData = map[schema.GroupVersionResource]struct {

// k8s.io/kubernetes/pkg/apis/apps/v1beta1
gvr("apps", "v1beta1", "statefulsets"): {
stub: `{"metadata": {"name": "ss1"}, "spec": {"template": {"metadata": {"labels": {"a": "b"}}}}}`,
stub: `{"metadata": {"name": "ss1"}, "spec": {"selector": {"matchLabels": {"a": "b"}}, "template": {"metadata": {"labels": {"a": "b"}}}}}`,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kow3ns The test case for statefulset did not have spec.selector, but similar test cases for deployments, daemonsets, and replicasets all have spec.selector. Hence, i modified the test case.

@@ -149,7 +149,7 @@ var etcdStorageData = map[schema.GroupVersionResource]struct {

// k8s.io/kubernetes/pkg/apis/apps/v1beta2
gvr("apps", "v1beta2", "statefulsets"): {
stub: `{"metadata": {"name": "ss2"}, "spec": {"template": {"metadata": {"labels": {"a": "b"}}}}}`,
stub: `{"metadata": {"name": "ss2"}, "spec": {"selector": {"matchLabels": {"a": "b"}}, "template": {"metadata": {"labels": {"a": "b"}}}}}`,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@kow3ns Same here.

@kow3ns
Copy link
Member

kow3ns commented Aug 7, 2017

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 7, 2017
@crimsonfaith91 crimsonfaith91 changed the title remove defaulting codes for obj.Spec.Selector and obj.Labels remove apps/v1beta2 defaulting codes for obj.Spec.Selector and obj.Labels Aug 7, 2017
@k8s-github-robot k8s-github-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 7, 2017
@kow3ns
Copy link
Member

kow3ns commented Aug 8, 2017

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 8, 2017
@erictune
Copy link
Member

erictune commented Aug 8, 2017

/approve

@k8s-github-robot k8s-github-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Aug 8, 2017
@liggitt
Copy link
Member

liggitt commented Aug 9, 2017

We still ensure the pod spec has labels and the selector matches the labels in validation, right?

@kow3ns
Copy link
Member

kow3ns commented Aug 9, 2017

Current validation for apps and extensions is still in place. We will issue a subsequent PR for validation changes (where necessary) in the apps and extensions groups. As none of the controllers handle selector mutation in a sane way, we intend to make selectors immutable for v1. We may lift this restriction at a future date when controllers can handle coordinated label updates and selector mutation. This PR for defaults has been done separately because it is isolated to the defaults in the v1beta2 group.

@k8s-github-robot k8s-github-robot removed lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Aug 9, 2017
@kow3ns
Copy link
Member

kow3ns commented Aug 9, 2017

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 9, 2017
@k8s-github-robot k8s-github-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 9, 2017
@kow3ns
Copy link
Member

kow3ns commented Aug 9, 2017

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Aug 9, 2017
@k8s-github-robot
Copy link

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: crimsonfaith91, erictune, kow3ns

Associated issue: 50339

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these OWNERS Files:

You can indicate your approval by writing /approve in a comment
You can cancel your approval by writing /approve cancel in a comment

@k8s-github-robot
Copy link

/test all [submit-queue is verifying that this PR is safe to merge]

@k8s-github-robot
Copy link

Automatic merge from submit-queue

@0xmichalis
Copy link
Contributor

@mfojtik @tnozicka

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. release-note-none Denotes a PR that doesn't merit a release note. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Controller selector should not default to template labels
9 participants