-
Notifications
You must be signed in to change notification settings - Fork 43
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
Add default podAntiAffinity to PodTemplateSpec #319
Add default podAntiAffinity to PodTemplateSpec #319
Conversation
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.
Thanks @fossedihelm! Great job! 👍
Overall looks great.
Small note about my perspective on comments:
IMO comments should be used to explain complicated code that cannot be simplified easily. In such cases comments need to expose as less implementation details as possible, i.e. explain what the codes does and not how it does it. Also, comments should never point out the obvious (explain something that is immediately obvious from looking at the code).
The reason I'm even mentioning this is because comments can easily do more harm than good. The danger is that the code changes but the comments stay outdated. This can lead to a very confusing code.
Example for what I'm referring to:
// Iterating over Pod's node selector
for i := Pod.NodeSelector { ... }
What do you think? :)
@iholder-redhat Thanks for your review. Agree with your perspective about the comments. In this case, they aren't necessary. They are leftovers because, in the first implementation, I wrote all the merge stuff inside the |
035442e
to
67f1518
Compare
@iholder-redhat I pushed the requested changes. I also push a change on |
7d82980
to
6dee28f
Compare
/test ci/prow/e2e-functests |
@fossedihelm: The specified target(s) for
Use In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
@fossedihelm: No presubmit jobs available for kubevirt/ssp-operator@master In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
/test e2e-functests |
@fossedihelm: No presubmit jobs available for kubevirt/ssp-operator@master In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
/retest |
/test e2e-functests |
@fossedihelm: No presubmit jobs available for kubevirt/ssp-operator@master In response to this:
Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
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.
Thanks for the PR.
My comments are mostly about code style. You can ignore them, if you want to. Except the last one about using client in reconcile_test.go
, that would be better.
return | ||
} | ||
if podSpec == nil { |
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.
This check can be removed, because the calling function never passes nil
as podSpec
. But if you want to keep this check, it can return on nil
, because the function will be noop.
nodePlacement := componentConfig.Placement | ||
if nodePlacement.NodeSelector == nil { | ||
nodePlacement.NodeSelector = make(map[string]string) | ||
} |
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.
This if
block can be safely removed. Iterating over nil
map is the same as iterating over an empty map.
if podSpec.Affinity == nil { | ||
podSpec.Affinity = nodePlacement.Affinity.DeepCopy() | ||
} else { | ||
if nodePlacement.Affinity.NodeAffinity != nil { |
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.
Can you move these != nil
checks inside the respective functions?
} | ||
} | ||
|
||
if len(nodePlacement.Tolerations) != 0 { |
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.
This whole if
block can be replaced with one line:
podSpec.Tolerations = append(podSpec.Tolerations, nodePlacement.Tolerations...)
Appending to a nil
slice allocates a new slice, so you don't need to check if it is nil
, and appending an empty slice is a noop.
func mergeNodeAffinity(currentAffinity *v1.Affinity, configNodeAffinity *v1.NodeAffinity) { | ||
if currentAffinity.NodeAffinity == nil { | ||
currentAffinity.NodeAffinity = configNodeAffinity.DeepCopy() | ||
} else { |
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.
Can you add early return
instead of else
block?
}, | ||
}, | ||
}, | ||
TopologyKey: "kubernetes.io/hostname", |
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.
This string is used in many places, can you extract it to a constant?
|
||
var setPodAntiAffinity = func(request *common.Request) { | ||
request.Instance.Spec.TemplateValidator.Placement.Affinity.PodAntiAffinity = antiAffinity | ||
return |
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.
This return
can be removed.
} | ||
var setPodAffinity = func(request *common.Request) { | ||
request.Instance.Spec.TemplateValidator.Placement.Affinity.PodAffinity = podAffinity | ||
return |
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.
This return
can be removed.
} | ||
var setNodeAffinity = func(request *common.Request) { | ||
request.Instance.Spec.TemplateValidator.Placement.Affinity.NodeAffinity = nodeAffinity | ||
return |
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.
This return
can be removed.
} | ||
res, err := operand.Reconcile(&request) | ||
Expect(err).ToNot(HaveOccurred()) | ||
deploymentResult := res[4] |
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.
Can you use the client to get the deployment instead of accessing the result of the call? The resource in result is only used for getting names and logging.
You can use:
deployment := &apps.Deployment{}
key := client.ObjectKeyFromObject(newDeployment(namespace, replicas, "test-img"))
Expect(request.Client.Get(request.Context, key, deployment)).To(Succeed())
@akrejcir Thanks for your review. It was constructive and instructive. I'm going to address your suggestions. Thanks |
Default PodTemplateSpec now has a podAntiAffinity that makes replicas to be scheduled on different node. This affinity will be merged with the requested one. The merge affect nodeAffinity, podAffinity, podAntiaffinity, tolerations and nodeSelector Signed-off-by: fossedihelm <ffossemo@redhat.com>
6dee28f
to
de4c3f1
Compare
Kudos, SonarCloud Quality Gate passed! 0 Bugs No Coverage information |
/retest |
/lgtm |
I've noticed that the commit message is not split into a title and body. When I run |
Thanks. I think I forgot a |
/approve |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: akrejcir The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
What this PR does / why we need it:
Default PodTemplateSpec now has a podAntiAffinity that makes pod replicas to be scheduled on different nodes.
This affinity will be merged with the requested one.
The merge affect nodeAffinity, podAffinity, podAntiaffinity, tolerations, and nodeSelector
Which issue(s) this PR fixes:
Fixes #
Special notes for your reviewer:
Release note: