Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2c87b1c

Browse files
committed
Added podAffinity and podAntiAffinity
Initialize the affinity for the pod spec once
1 parent 4cfef62 commit 2c87b1c

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

Documentation/cluster-crd.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ Placement configuration for the cluster services. It includes the following keys
7171

7272
A Placement configuration is specified (according to the kubernetes [PodSpec](https://kubernetes.io/docs/api-reference/v1.6/#podspec-v1-core)) as:
7373
- `nodeAffinity`: kubernetes [NodeAffinity](https://kubernetes.io/docs/api-reference/v1.6/#nodeaffinity-v1-core)
74+
- `podAffinity`: kubernetes [PodAffinity](https://kubernetes.io/docs/api-reference/v1.6/#podaffinity-v1-core)
75+
- `podAntiAffinity`: kubernetes [PodAntiAffinity](https://kubernetes.io/docs/api-reference/v1.6/#podantiaffinity-v1-core)
7476
- `tolerations`: list of kubernetes [Toleration](https://kubernetes.io/docs/api-reference/v1.6/#toleration-v1-core)
7577

7678
## Samples
@@ -106,7 +108,7 @@ spec:
106108

107109
### Storage Configuration: Specific devices
108110

109-
Individual nodes and their config can be specified so that only the named nodes below will be used as storage resources.
111+
Individual nodes and their config can be specified so that only the named nodes below will be used as storage resources.
110112
Each node's 'name' field should match their 'kubernetes.io/hostname' label.
111113

112114
```

cluster/examples/kubernetes/rook-cluster.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,30 @@ spec:
2424
# operator: In
2525
# values:
2626
# - storage-node
27+
# podAffinity:
28+
# podAntiAffinity:
2729
# tolerations:
2830
# - key: storage-node
2931
# operator: Exists
3032
# api:
3133
# nodeAffinity:
34+
# podAffinity:
35+
# podAntiAffinity:
3236
# tolerations:
3337
# mgr:
3438
# nodeAffinity:
39+
# podAffinity:
40+
# podAntiAffinity:
3541
# tolerations:
3642
# mon:
3743
# nodeAffinity:
44+
# podAffinity:
45+
# podAntiAffinity:
3846
# tolerations:
3947
# osd:
4048
# nodeAffinity:
49+
# podAffinity:
50+
# podAntiAffinity:
4151
# tolerations:
4252
storage: # cluster level storage configuration and selection
4353
useAllNodes: true

pkg/operator/k8sutil/placement.go

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,27 @@ import (
2424
// Placement encapsulates the various kubernetes options that control where
2525
// pods are scheduled and executed.
2626
type Placement struct {
27-
NodeAffinity *v1.NodeAffinity `json:"nodeAffinity,omitempty"`
28-
Tolerations []v1.Toleration `json:"tolerations,omitemtpy"`
27+
NodeAffinity *v1.NodeAffinity `json:"nodeAffinity,omitempty"`
28+
PodAffinity *v1.PodAffinity `json:"podAffinity,omitempty"`
29+
PodAntiAffinity *v1.PodAntiAffinity `json:"podAntiAffinity,omitempty"`
30+
Tolerations []v1.Toleration `json:"tolerations,omitemtpy"`
2931
}
3032

3133
// ApplyToPodSpec adds placement to a pod spec
3234
func (p Placement) ApplyToPodSpec(t *v1.PodSpec) {
35+
if t.Affinity == nil {
36+
t.Affinity = &v1.Affinity{}
37+
}
3338
if p.NodeAffinity != nil {
34-
if t.Affinity == nil {
35-
t.Affinity = &v1.Affinity{}
36-
}
3739
t.Affinity.NodeAffinity = p.NodeAffinity
3840
}
41+
if p.PodAffinity != nil {
42+
t.Affinity.PodAffinity = p.PodAffinity
43+
}
44+
if p.PodAntiAffinity != nil {
45+
t.Affinity.PodAntiAffinity = p.PodAntiAffinity
46+
}
47+
3948
if p.Tolerations != nil {
4049
t.Tolerations = p.Tolerations
4150
}
@@ -49,6 +58,12 @@ func (p Placement) Merge(with Placement) Placement {
4958
if with.NodeAffinity != nil {
5059
ret.NodeAffinity = with.NodeAffinity
5160
}
61+
if with.PodAffinity != nil {
62+
ret.PodAffinity = with.PodAffinity
63+
}
64+
if with.PodAntiAffinity != nil {
65+
ret.PodAntiAffinity = with.PodAntiAffinity
66+
}
5267
if with.Tolerations != nil {
5368
ret.Tolerations = with.Tolerations
5469
}

0 commit comments

Comments
 (0)