-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #199 from ffromani/sched-run-control-plane
sched: manifests: run on the control plane
- Loading branch information
Showing
15 changed files
with
456 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* Copyright 2023 Red Hat, Inc. | ||
*/ | ||
|
||
package objectupdate | ||
|
||
import corev1 "k8s.io/api/core/v1" | ||
|
||
const ( | ||
NodeRoleControlPlane = "node-role.kubernetes.io/control-plane" | ||
NodeRoleControlPlaneDeprecated = "node-role.kubernetes.io/master" | ||
) | ||
|
||
func SetPodSchedulerAffinityOnControlPlane(podSpec *corev1.PodSpec) { | ||
if podSpec == nil { | ||
return | ||
} | ||
|
||
for _, label := range []string{ | ||
NodeRoleControlPlane, | ||
NodeRoleControlPlaneDeprecated, | ||
} { | ||
if toleration := findTolerationByKey(podSpec.Tolerations, label); toleration == nil { | ||
podSpec.Tolerations = append(podSpec.Tolerations, corev1.Toleration{ | ||
Key: label, | ||
Effect: corev1.TaintEffectNoSchedule, | ||
}) | ||
} | ||
} | ||
if podSpec.Affinity == nil { | ||
podSpec.Affinity = &corev1.Affinity{} | ||
} | ||
if podSpec.Affinity.NodeAffinity == nil { | ||
podSpec.Affinity.NodeAffinity = &corev1.NodeAffinity{} | ||
} | ||
if podSpec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution == nil { | ||
podSpec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution = &corev1.NodeSelector{ | ||
NodeSelectorTerms: []corev1.NodeSelectorTerm{ | ||
{ | ||
MatchExpressions: []corev1.NodeSelectorRequirement{ | ||
{ | ||
Key: NodeRoleControlPlane, | ||
Operator: corev1.NodeSelectorOpExists, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
} | ||
|
||
func findTolerationByKey(tolerations []corev1.Toleration, key string) *corev1.Toleration { | ||
for idx := range tolerations { | ||
toleration := &tolerations[idx] | ||
if toleration.Key == key { | ||
return toleration | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* Copyright 2023 Red Hat, Inc. | ||
*/ | ||
|
||
package objectupdate | ||
|
||
import ( | ||
"testing" | ||
|
||
"sigs.k8s.io/yaml" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func TestSetPodSchedulerAffinityOnControlPlane(t *testing.T) { | ||
|
||
type testCase struct { | ||
name string | ||
podSpec *corev1.PodSpec | ||
expectedYAML string | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
name: "nil", | ||
expectedYAML: "null\n", | ||
}, | ||
{ | ||
name: "empty", | ||
podSpec: &corev1.PodSpec{}, | ||
expectedYAML: podSpecOnlyAffinity, | ||
}, | ||
{ | ||
name: "partial affinity", | ||
podSpec: &corev1.PodSpec{ | ||
Affinity: &corev1.Affinity{ | ||
NodeAffinity: &corev1.NodeAffinity{}, | ||
}, | ||
}, | ||
expectedYAML: podSpecOnlyAffinity, | ||
}, | ||
{ | ||
name: "partial tolerations", | ||
podSpec: &corev1.PodSpec{ | ||
Tolerations: []corev1.Toleration{ | ||
{ | ||
Key: NodeRoleControlPlane, | ||
Effect: corev1.TaintEffectNoSchedule, | ||
}, | ||
}, | ||
}, | ||
expectedYAML: podSpecOnlyAffinity, | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
got := tc.podSpec.DeepCopy() | ||
SetPodSchedulerAffinityOnControlPlane(got) | ||
data, err := yaml.Marshal(got) | ||
if err != nil { | ||
t.Errorf("error marshalling yaml: %v", err) | ||
} | ||
gotYAML := string(data) | ||
if gotYAML != tc.expectedYAML { | ||
t.Errorf("output mismatch:\ngot=%v\nexpected=%v\n", gotYAML, tc.expectedYAML) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
const podSpecOnlyAffinity = `affinity: | ||
nodeAffinity: | ||
requiredDuringSchedulingIgnoredDuringExecution: | ||
nodeSelectorTerms: | ||
- matchExpressions: | ||
- key: node-role.kubernetes.io/control-plane | ||
operator: Exists | ||
containers: null | ||
tolerations: | ||
- effect: NoSchedule | ||
key: node-role.kubernetes.io/control-plane | ||
- effect: NoSchedule | ||
key: node-role.kubernetes.io/master | ||
` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.