Skip to content

Commit cf43b19

Browse files
committed
Merge pull request #22757 from bdunne/node_affinity
Add Node Affinity arch selector based on the affinity of the orchestrator (cherry picked from commit 90989d7)
1 parent 4d3581b commit cf43b19

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

lib/container_orchestrator.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,14 @@ def my_pod
9595
get_pod_by_namespace_and_hostname(my_namespace, ENV["HOSTNAME"])
9696
end
9797

98+
def my_node_affinity_arch_values
99+
ContainerOrchestrator.new.my_pod.spec.affinity&.nodeAffinity&.requiredDuringSchedulingIgnoredDuringExecution&.nodeSelectorTerms&.each do |i|
100+
i.matchExpressions&.each { |a| return(a.values) if a.key == "kubernetes.io/arch" }
101+
end
102+
103+
return ["amd64"]
104+
end
105+
98106
private
99107

100108
def default_get_options

lib/container_orchestrator/object_definition.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@ def deployment_definition(name)
1515
:template => {
1616
:metadata => {:name => name, :labels => common_labels.merge(:name => name)},
1717
:spec => {
18+
:affinity => {
19+
:nodeAffinity => {
20+
:requiredDuringSchedulingIgnoredDuringExecution => {
21+
:nodeSelectorTerms => [{
22+
:matchExpressions => [
23+
{:key => "kubernetes.io/arch", :operator => "In", :values => ContainerOrchestrator.new.my_node_affinity_arch_values}
24+
]}
25+
]
26+
}
27+
}
28+
},
1829
:serviceAccountName => ENV["WORKER_SERVICE_ACCOUNT"],
1930
:containers => [{
2031
:name => name,

spec/lib/container_orchestrator_spec.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,12 @@
127127
end
128128

129129
context "#deployment_definition (private)" do
130+
let(:container_orchestrator) { ContainerOrchestrator.new }
131+
before do
132+
allow(ContainerOrchestrator).to receive(:new).and_return(container_orchestrator)
133+
expect(container_orchestrator).to receive(:my_node_affinity_arch_values).and_return(["amd64", "arm64"])
134+
end
135+
130136
it "skips the database root certificate if the orchestrator doesn't have it" do
131137
expect(File).to receive(:file?).with("/.postgresql/root.crt").and_return(false)
132138
allow(File).to receive(:file?).and_call_original # allow other calls to .file? to still work
@@ -181,6 +187,16 @@
181187
}
182188
})
183189
end
190+
191+
it "includes node affinities" do
192+
deployment_definition = subject.send(:deployment_definition, "test")
193+
194+
expect(deployment_definition.fetch_path(:spec, :template, :spec, :affinity, :nodeAffinity, :requiredDuringSchedulingIgnoredDuringExecution, :nodeSelectorTerms, 0, :matchExpressions, 0)).to include({
195+
:key => "kubernetes.io/arch",
196+
:operator => "In",
197+
:values => ["amd64", "arm64"],
198+
})
199+
end
184200
end
185201

186202
context "with stub connections" do
@@ -202,6 +218,12 @@
202218
end
203219

204220
describe "#create_deployment" do
221+
let(:container_orchestrator) { ContainerOrchestrator.new }
222+
before do
223+
allow(ContainerOrchestrator).to receive(:new).and_return(container_orchestrator)
224+
expect(container_orchestrator).to receive(:my_node_affinity_arch_values).and_return(["amd64", "arm64"])
225+
end
226+
205227
it "creates a deployment with the given name and edits" do
206228
expect(apps_connection_stub).to receive(:create_deployment) do |definition|
207229
expect(definition[:metadata][:name]).to eq("test")

spec/models/miq_worker/container_common_spec.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ def deployment_name_for(name)
4646

4747
it "MiqUiWorker adds the ui_httpd_configs volume mount" do
4848
container_orchestrator = ContainerOrchestrator.new
49+
expect(container_orchestrator).to receive(:my_node_affinity_arch_values).and_return(["amd64", "arm64"])
4950
kubeclient = double("Kubeclient::Client")
5051

51-
expect(ContainerOrchestrator).to receive(:new).and_return(container_orchestrator)
52+
allow(ContainerOrchestrator).to receive(:new).and_return(container_orchestrator)
5253
expect(container_orchestrator).to receive(:my_namespace).and_return("my-namespace")
5354
expect(container_orchestrator).to receive(:raw_connect).and_return(kubeclient)
5455

@@ -64,9 +65,10 @@ def deployment_name_for(name)
6465

6566
it "Service workers use httpGet liveness and readiness probes" do
6667
container_orchestrator = ContainerOrchestrator.new
68+
expect(container_orchestrator).to receive(:my_node_affinity_arch_values).and_return(["amd64", "arm64"])
6769
kubeclient = double("Kubeclient::Client")
6870

69-
expect(ContainerOrchestrator).to receive(:new).and_return(container_orchestrator)
71+
allow(ContainerOrchestrator).to receive(:new).and_return(container_orchestrator)
7072
expect(container_orchestrator).to receive(:my_namespace).and_return("my-namespace")
7173
expect(container_orchestrator).to receive(:raw_connect).and_return(kubeclient)
7274

0 commit comments

Comments
 (0)