Skip to content

Commit

Permalink
Add ability to get the container orchestrator's pod
Browse files Browse the repository at this point in the history
  • Loading branch information
Fryguy committed Jan 25, 2022
1 parent f2f6bc6 commit 4d938c8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/container_orchestrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,22 @@ def watch_pods(resource_version = nil)
kube_connection.watch_pods(default_get_options.merge(:resource_version => resource_version))
end

# Returns the pod with the given hostname in the given namespace.
def get_pod_by_namespace_and_hostname(namespace, hostname)
kube_connection.get_pods(:namespace => namespace).detect { |i| i.metadata.name == hostname }
end

# Returns the pod for this container orchestrator.
#
# NOTE: It is presumed that this method is only called from within the
# container orchestrator process itself, as it uses environment info
# that only the running orchestrator pod will have.
def my_pod
get_pod_by_namespace_and_hostname(my_namespace, ENV["HOSTNAME"])
end

private

def default_get_options
{:namespace => my_namespace, :label_selector => [app_name_selector, orchestrated_by_selector].join(",")}
end
Expand Down
24 changes: 24 additions & 0 deletions spec/lib/container_orchestrator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,30 @@
subject.watch_pods(100)
end
end

it "#get_pod_by_namespace_and_hostname" do
pods = [
double("pod", :metadata => double("pod-metadata", :name => "3r1-ui-abcdef123-xyz12")),
double("pod", :metadata => double("pod-metadata", :name => "3r1-api-bcdefa123-yza12")),
double("pod", :metadata => double("pod-metadata", :name => "orchestrator-cdefab1234-zab12"))
]
expect(kube_connection_stub).to receive(:get_pods).with(:namespace => namespace).and_return(pods)

expect(subject.get_pod_by_namespace_and_hostname(namespace, "3r1-ui-abcdef123-xyz12")).to eq pods.first
end

it "#my_pod" do
stub_const("ENV", ENV.to_h.merge("HOSTNAME" => "orchestrator-cdefab1234-zab12"))
pods = [
double("pod", :metadata => double("pod-metadata", :name => "3r1-ui-abcdef123-xyz12")),
double("pod", :metadata => double("pod-metadata", :name => "3r1-api-bcdefa123-yza12")),
double("pod", :metadata => double("pod-metadata", :name => "orchestrator-cdefab1234-zab12"))
]
expect(kube_connection_stub).to receive(:get_pods).with(:namespace => namespace).and_return(pods)
orchestrator_pod = pods.last

expect(subject.my_pod).to eq orchestrator_pod
end
end
end
end

0 comments on commit 4d938c8

Please sign in to comment.