Commit 49de2bda authored by Adrien Kohlbecker's avatar Adrien Kohlbecker Committed by Dylan Griffith

Fix access to logs when multiple pods exist

parent 479fa52d
...@@ -43,7 +43,7 @@ module PodLogs ...@@ -43,7 +43,7 @@ module PodLogs
end end
def check_container_name(result) def check_container_name(result)
pod_details = result[:raw_pods].first { |p| p.metadata.name == result[:pod_name] } pod_details = result[:raw_pods].find { |p| p.metadata.name == result[:pod_name] }
containers = pod_details.spec.containers.map(&:name) containers = pod_details.spec.containers.map(&:name)
# select first container if not specified # select first container if not specified
......
---
title: Fix access to logs when multiple pods exist
merge_request: 27008
author:
type: fixed
...@@ -9,11 +9,13 @@ describe ::PodLogs::BaseService do ...@@ -9,11 +9,13 @@ describe ::PodLogs::BaseService do
let(:namespace) { 'autodevops-deploy-9-production' } let(:namespace) { 'autodevops-deploy-9-production' }
let(:pod_name) { 'pod-1' } let(:pod_name) { 'pod-1' }
let(:pod_name_2) { 'pod-2' }
let(:container_name) { 'container-0' } let(:container_name) { 'container-0' }
let(:params) { {} } let(:params) { {} }
let(:raw_pods) do let(:raw_pods) do
JSON.parse([ JSON.parse([
kube_pod(name: pod_name) kube_pod(name: pod_name),
kube_pod(name: pod_name_2)
].to_json, object_class: OpenStruct) ].to_json, object_class: OpenStruct)
end end
...@@ -115,7 +117,7 @@ describe ::PodLogs::BaseService do ...@@ -115,7 +117,7 @@ describe ::PodLogs::BaseService do
result = subject.send(:get_pod_names, raw_pods: raw_pods) result = subject.send(:get_pod_names, raw_pods: raw_pods)
expect(result[:status]).to eq(:success) expect(result[:status]).to eq(:success)
expect(result[:pods]).to eq([pod_name]) expect(result[:pods]).to eq([pod_name, pod_name_2])
end end
end end
end end
...@@ -9,16 +9,20 @@ describe ::PodLogs::KubernetesService do ...@@ -9,16 +9,20 @@ describe ::PodLogs::KubernetesService do
let(:namespace) { 'autodevops-deploy-9-production' } let(:namespace) { 'autodevops-deploy-9-production' }
let(:pod_name) { 'pod-1' } let(:pod_name) { 'pod-1' }
let(:pod_name_2) { 'pod-2' }
let(:container_name) { 'container-0' } let(:container_name) { 'container-0' }
let(:container_name_2) { 'foo-0' }
let(:params) { {} } let(:params) { {} }
let(:raw_logs) do let(:raw_logs) do
"2019-12-13T14:04:22.123456Z Log 1\n2019-12-13T14:04:23.123456Z Log 2\n" \ "2019-12-13T14:04:22.123456Z Log 1\n2019-12-13T14:04:23.123456Z Log 2\n" \
"2019-12-13T14:04:24.123456Z Log 3" "2019-12-13T14:04:24.123456Z Log 3"
end end
let(:raw_pods) do let(:raw_pods) do
JSON.parse([ JSON.parse([
kube_pod(name: pod_name) kube_pod(name: pod_name),
kube_pod(name: pod_name_2, container_name: container_name_2)
].to_json, object_class: OpenStruct) ].to_json, object_class: OpenStruct)
end end
...@@ -220,12 +224,12 @@ describe ::PodLogs::KubernetesService do ...@@ -220,12 +224,12 @@ describe ::PodLogs::KubernetesService do
it 'returns success if container_name was not specified and there are containers' do it 'returns success if container_name was not specified and there are containers' do
result = subject.send(:check_container_name, result = subject.send(:check_container_name,
pod_name: pod_name, pod_name: pod_name_2,
raw_pods: raw_pods raw_pods: raw_pods
) )
expect(result[:status]).to eq(:success) expect(result[:status]).to eq(:success)
expect(result[:container_name]).to eq(container_name) expect(result[:container_name]).to eq(container_name_2)
end end
it 'returns error if container_name was not specified and there are no containers on the pod' do it 'returns error if container_name was not specified and there are no containers on the pod' do
......
...@@ -489,7 +489,7 @@ module KubernetesHelpers ...@@ -489,7 +489,7 @@ module KubernetesHelpers
# This is a partial response, it will have many more elements in reality but # This is a partial response, it will have many more elements in reality but
# these are the ones we care about at the moment # these are the ones we care about at the moment
def kube_pod(name: "kube-pod", environment_slug: "production", namespace: "project-namespace", project_slug: "project-path-slug", status: "Running", track: nil) def kube_pod(name: "kube-pod", container_name: "container-0", environment_slug: "production", namespace: "project-namespace", project_slug: "project-path-slug", status: "Running", track: nil)
{ {
"metadata" => { "metadata" => {
"name" => name, "name" => name,
...@@ -506,8 +506,8 @@ module KubernetesHelpers ...@@ -506,8 +506,8 @@ module KubernetesHelpers
}, },
"spec" => { "spec" => {
"containers" => [ "containers" => [
{ "name" => "container-0" }, { "name" => "#{container_name}" },
{ "name" => "container-1" } { "name" => "#{container_name}-1" }
] ]
}, },
"status" => { "phase" => status } "status" => { "phase" => status }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment