Skip to content

Commit

Permalink
Add since_seconds filter when reading pod logs (#136)
Browse files Browse the repository at this point in the history
* Add since_seconds filter when reading pod logs

* Generate docs

* Update testcase and changelog file

* Add prerelease tag to testtecases

---------

Co-authored-by: Ryfczynska Sara <[email protected]>
Co-authored-by: DevOps Spiral <[email protected]>
  • Loading branch information
3 people authored Nov 19, 2023
1 parent 9306257 commit 63c7f80
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 44 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## In progress
- update read_namespaced_pod_log keyword with new optional parameter 'since_seconds'

## [0.8.6] - 2022-05-27
### Added
- add new keyword to list all namespaced custom objects [#133] (https://github.com/devopsspiral/KubeLibrary/pull/133) by [@kutayy]
Expand Down
57 changes: 32 additions & 25 deletions docs/index.html

Large diffs are not rendered by default.

23 changes: 16 additions & 7 deletions src/KubeLibrary/KubeLibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ class KubeLibrary:
| Library KubeLibrary None True
"""

def __init__(self, kube_config=None, context=None, api_url=None, bearer_token=None,
ca_cert=None, incluster=False, cert_validation=True):
"""KubeLibrary can be configured with several optional arguments.
Expand Down Expand Up @@ -98,7 +99,8 @@ def __init__(self, kube_config=None, context=None, api_url=None, bearer_token=No

@staticmethod
def get_proxy():
return environ.get('https_proxy') or environ.get('HTTPS_PROXY') or environ.get('http_proxy') or environ.get('HTTP_PROXY')
return environ.get('https_proxy') or environ.get('HTTPS_PROXY') or environ.get('http_proxy') or environ.get(
'HTTP_PROXY')

@staticmethod
def get_no_proxy():
Expand Down Expand Up @@ -221,7 +223,8 @@ def replace(self, api_version, kind, **kwargs):
resource = self.get_dynamic_resource(api_version, kind)
resource.replace(**kwargs)

def reload_config(self, kube_config=None, context=None, api_url=None, bearer_token=None, ca_cert=None, incluster=False, cert_validation=True):
def reload_config(self, kube_config=None, context=None, api_url=None, bearer_token=None, ca_cert=None,
incluster=False, cert_validation=True):
"""Reload the KubeLibrary to be configured with different optional arguments.
This can be used to connect to a different cluster during the same test.
- ``kube_config``:
Expand Down Expand Up @@ -394,9 +397,11 @@ def get_pods_in_namespace(self, name_pattern, namespace, label_selector=""):
pods = [item for item in ret.items if r.match(item.metadata.name)]
return pods

def read_namespaced_pod_log(self, name, namespace, container):
def read_namespaced_pod_log(self, name, namespace, container, since_seconds=None):
"""Gets container logs of given pod in given namespace.
Can be optionally filtered by time in seconds. e.g. since_seconds=1000.
Returns logs.
- ``name``:
Expand All @@ -406,7 +411,8 @@ def read_namespaced_pod_log(self, name, namespace, container):
- ``container``:
Container to check
"""
pod_logs = self.v1.read_namespaced_pod_log(name=name, namespace=namespace, container=container, follow=False)
pod_logs = self.v1.read_namespaced_pod_log(name=name, namespace=namespace, container=container, follow=False,
since_seconds=since_seconds)
return pod_logs

def get_pod_logs(self, name, namespace, container):
Expand Down Expand Up @@ -873,7 +879,8 @@ def assert_pod_has_annotations(pod, annotations_json):
for k, v in annotations.items():
if pod.metadata.annotations and k in pod.metadata.annotations:
if pod.metadata.annotations[k] != v:
logger.error(f'Annotation "{k}" value "{v}" not matching actual "{pod.metadata.annotations[k]}"')
logger.error(
f'Annotation "{k}" value "{v}" not matching actual "{pod.metadata.annotations[k]}"')
return False
else:
logger.error(f'Annotation "{k}" not found in actual')
Expand Down Expand Up @@ -983,7 +990,8 @@ def list_namespaced_horizontal_pod_autoscaler(self, namespace, label_selector=""
- ``namespace``:
Namespace to check
"""
ret = self.autoscalingv1.list_namespaced_horizontal_pod_autoscaler(namespace, watch=False, label_selector=label_selector)
ret = self.autoscalingv1.list_namespaced_horizontal_pod_autoscaler(namespace, watch=False,
label_selector=label_selector)
return [item for item in ret.items]

def get_hpas_in_namespace(self, namespace, label_selector=""):
Expand All @@ -998,7 +1006,8 @@ def get_hpas_in_namespace(self, namespace, label_selector=""):
- ``namespace``:
Namespace to check
"""
ret = self.autoscalingv1.list_namespaced_horizontal_pod_autoscaler(namespace, watch=False, label_selector=label_selector)
ret = self.autoscalingv1.list_namespaced_horizontal_pod_autoscaler(namespace, watch=False,
label_selector=label_selector)
return [item.metadata.name for item in ret.items]

def read_namespaced_horizontal_pod_autoscaler(self, name, namespace):
Expand Down
1 change: 1 addition & 0 deletions test/test_KubeLibrary.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class AttributeDict(object):
allowing you to recurse down nested dicts (like: AttributeDict.attr.attr)
"""
def __init__(self, entries):
self._root = None
self.add_entries(entries)

def add_entries(self, entries):
Expand Down
32 changes: 20 additions & 12 deletions testcases/pod/pod.robot
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@
Resource ./pod_kw.robot

*** Variables ***
${KLIB_POD_PATTERN} %{KLIB_POD_PATTERN}
${KLIB_POD_NAMESPACE} %{KLIB_POD_NAMESPACE}
${KLIB_POD_REPLICAS} %{KLIB_POD_REPLICAS=1}
${KLIB_POD_TIMEOUT} %{KLIB_POD_TIMEOUT=2min}
${KLIB_POD_RETRY_INTERVAL} %{KLIB_POD_RETRY_INTERVAL=5sec}
${KLIB_POD_LABELS} %{KLIB_POD_LABELS='Labels missing!'}
${KLIB_POD_ANNOTATIONS} %{KLIB_POD_ANNOTATIONS='Annotations missing!'}
${KLIB_RESOURCE_REQUESTS_CPU} %{KLIB_RESOURCE_REQUESTS_CPU='Resource requests missing!'}
${KLIB_RESOURCE_REQUESTS_MEMORY} %{KLIB_RESOURCE_REQUESTS_MEMORY='Resource requests missing!'}
${KLIB_RESOURCE_LIMITS_CPU} %{KLIB_RESOURCE_LIMITS_CPU='Resource limits missing!'}
${KLIB_RESOURCE_LIMITs_MEMORY} %{KLIB_RESOURCE_LIMITS_MEMORY='Resource limits missing!'}
${KLIB_ENV_VARS} %{KLIB_ENV_VARS='Env vars missing!'}
${KLIB_POD_PATTERN} %{KLIB_POD_PATTERN}
${KLIB_POD_NAMESPACE} %{KLIB_POD_NAMESPACE}
${KLIB_POD_REPLICAS} %{KLIB_POD_REPLICAS=1}
${KLIB_POD_TIMEOUT} %{KLIB_POD_TIMEOUT=2min}
${KLIB_POD_RETRY_INTERVAL} %{KLIB_POD_RETRY_INTERVAL=5sec}
${KLIB_POD_LABELS} %{KLIB_POD_LABELS='Labels missing!'}
${KLIB_POD_ANNOTATIONS} %{KLIB_POD_ANNOTATIONS='Annotations missing!'}
${KLIB_RESOURCE_REQUESTS_CPU} %{KLIB_RESOURCE_REQUESTS_CPU='Resource requests missing!'}
${KLIB_RESOURCE_REQUESTS_MEMORY} %{KLIB_RESOURCE_REQUESTS_MEMORY='Resource requests missing!'}
${KLIB_RESOURCE_LIMITS_CPU} %{KLIB_RESOURCE_LIMITS_CPU='Resource limits missing!'}
${KLIB_RESOURCE_LIMITs_MEMORY} %{KLIB_RESOURCE_LIMITS_MEMORY='Resource limits missing!'}
${KLIB_ENV_VARS} %{KLIB_ENV_VARS='Env vars missing!'}
${LOGS_SINCE} 1000

*** Test Cases ***

Expand Down Expand Up @@ -68,6 +69,13 @@ Logs of pod are available
Then logs of pod can be retrived
And logs contain expected string

Logs of pod since 1000s are available
[Tags] prerelease
Given waited for pods matching "${KLIB_POD_PATTERN}" in namespace "${KLIB_POD_NAMESPACE}" to be READY
When getting pods matching "${KLIB_POD_PATTERN}" in namespace "${KLIB_POD_NAMESPACE}"
Then logs of pod can be retrived since "${LOGS_SINCE}"
And logs contain expected string

List pods by label
[Tags] other
Given waited for pods matching "${KLIB_POD_PATTERN}" in namespace "${KLIB_POD_NAMESPACE}" to be READY
Expand Down
6 changes: 6 additions & 0 deletions testcases/pod/pod_kw.robot
Original file line number Diff line number Diff line change
Expand Up @@ -106,5 +106,11 @@ logs of pod can be retrived
Log ${pod_logs} console=True
Set Test Variable ${POD_LOGS} ${pod_logs}

logs of pod can be retrived since "${seconds}"
Set Test Variable ${POD_NAME} ${namespace_pods[0].metadata.name}
${pod_logs}= Read namespaced pod log ${POD_NAME} ${KLIB_POD_NAMESPACE} busybox since_seconds=${seconds}
Log ${pod_logs} console=True
Set Test Variable ${POD_LOGS} ${pod_logs}

logs contain expected string
Should Contain ${POD_LOGS} I am

0 comments on commit 63c7f80

Please sign in to comment.