Fix get_pod_logs logic to pick up accurate pods

Shipyard/Airflow Worker selection of pods for log retrieval inaccurate

Change-Id: I8aade93db6e657da220254242bc51a166853d965
This commit is contained in:
skovaleff 2018-10-05 09:58:32 -07:00
parent 124de57262
commit d6b7506869
2 changed files with 72 additions and 1 deletions

View File

@ -46,6 +46,7 @@ def get_pod_logs(pod_name_pattern, namespace, container, since_seconds):
precedes the time a pod was started, only logs precedes the time a pod was started, only logs
since the pod start will be returned. If this since the pod start will be returned. If this
value is in the future, no logs will be returned. value is in the future, no logs will be returned.
:return: None
Example:: Example::
@ -74,7 +75,7 @@ def get_pod_logs(pod_name_pattern, namespace, container, since_seconds):
if ret: if ret:
for i in ret.items: for i in ret.items:
if pod_name_pattern in i.metadata.name: if i.metadata.name.startswith(pod_name_pattern):
pods_list.append(i.metadata.name) pods_list.append(i.metadata.name)
else: else:
raise K8sLoggingException(_NOT_FOUND_MSG_FMT.format(namespace, raise K8sLoggingException(_NOT_FOUND_MSG_FMT.format(namespace,

View File

@ -0,0 +1,70 @@
# Copyright 2018 AT&T Intellectual Property. All other rights reserved.
#
# 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.
"""Tests for get_k8s_logs functions"""
from unittest import mock
from shipyard_airflow.plugins.get_k8s_logs import get_pod_logs
class Metadata:
def __init__(self, _name):
self.name = _name
class Pods:
def __init__(self, _name):
self.metadata = Metadata(_name)
class TestGetK8slogs:
@mock.patch("shipyard_airflow.plugins"
".get_k8s_logs.client.CoreV1Api", autospec=True)
@mock.patch("shipyard_airflow.plugins"
".get_k8s_logs.config.load_incluster_config")
def test_get_pod_logs(self, mock_config, mock_client):
"""Assert that get_pod_logs picks up accurate pods
get_pod_logs('armada-api', 'ucp', 'armada-api', 3600)
First case - old logic to find pods with "in"
Second case - new logic with "starts"
"""
test_pods = [
Pods('armada-api-66d5f59856-h9c27'),
Pods('armada-api-66d5f59856-42zvp'),
# this is the offender if we use "in" instead of "startwith"
Pods('clcp-ucp-armada-armada-api-test'),
Pods('armada-ks-endpoints-6ztcg')
]
mock_client.return_value \
.list_namespaced_pod.return_value \
.items = test_pods
get_pod_logs('armada-api', 'ucp', 'armada-api', 3600)
mock_client.return_value \
.read_namespaced_pod_log \
.assert_any_call(container='armada-api',
name='armada-api-66d5f59856-h9c27',
namespace='ucp',
pretty='true',
since_seconds=3600)
mock_client.return_value \
.read_namespaced_pod_log \
.assert_any_call(container='armada-api',
name='armada-api-66d5f59856-42zvp',
namespace='ucp',
pretty='true',
since_seconds=3600)