Skip to content

Commit

Permalink
Merge branch 'improvement/CI_tests_for_metalk8s_monitoring_and_metalk…
Browse files Browse the repository at this point in the history
…8s_kubernetes_cronjob_modules' into q/126.0
  • Loading branch information
bert-e committed Mar 20, 2024
2 parents 311dfe7 + 89a7cc4 commit d178477
Show file tree
Hide file tree
Showing 15 changed files with 722 additions and 136 deletions.
22 changes: 11 additions & 11 deletions salt/_modules/metalk8s_kubernetes_cronjob.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def __virtual__():
return __virtualname__


def list_cronjobs(
def get_cronjobs(
suspended=None,
mark=None,
all_namespaces=False,
Expand Down Expand Up @@ -166,17 +166,17 @@ def activate_cronjob(name, namespace, mark=None, **kwargs):
return _set_cronjob_suspend(name, namespace, suspend=False, mark=mark, **kwargs)


def get_jobs(cronjob_name, namespace, **kwargs):
def get_jobs(name, namespace, **kwargs):
"""Get the Jobs created by a CronJob.
Args:
cronjob_name (str): name of the CronJob.
name (str): name of the CronJob.
namespace (str): namespace of the CronJob.
Returns:
list: list of Jobs created by the CronJob.
"""
cronjob_uid = get_cronjob(cronjob_name, namespace, **kwargs)["metadata"]["uid"]
cronjob_uid = get_cronjob(name, namespace, **kwargs)["metadata"]["uid"]

# Get all Jobs in the namespace
jobs = __salt__["metalk8s_kubernetes.list_objects"](
Expand All @@ -198,8 +198,8 @@ def get_jobs(cronjob_name, namespace, **kwargs):
return filtered_jobs


def stop_jobs(
cronjob_name,
def suspend_cronjob_and_delete_jobs(
name,
namespace,
mark=None,
wait=False,
Expand All @@ -209,7 +209,7 @@ def stop_jobs(
"""Suspend the CronJob and delete all its Jobs.
Args:
cronjob_name (str): name of the CronJob.
name (str): name of the CronJob.
namespace (str): namespace of the CronJob.
wait (bool, optional): wait for the Jobs to be deleted. Defaults to False.
timeout_seconds (int, optional): timeout in seconds to wait for the Jobs to be deleted. Defaults to 60.
Expand All @@ -220,9 +220,9 @@ def stop_jobs(
Returns:
list: list of Jobs that have been deleted.
"""
suspend_cronjob(cronjob_name, namespace, mark, **kwargs)
suspend_cronjob(name, namespace, mark, **kwargs)

jobs = get_jobs(cronjob_name, namespace, **kwargs)
jobs = get_jobs(name, namespace, **kwargs)

for job in jobs:
__salt__["metalk8s_kubernetes.delete_object"](
Expand All @@ -238,7 +238,7 @@ def stop_jobs(
if wait:
start_ts = time.time()
while time.time() - start_ts < timeout_seconds:
waiting_jobs = get_jobs(cronjob_name, namespace, **kwargs)
waiting_jobs = get_jobs(name, namespace, **kwargs)
if not waiting_jobs:
break

Expand All @@ -248,7 +248,7 @@ def stop_jobs(

raise CommandExecutionError(
f"Wait timeout exceeded while deleting the following Jobs {waiting_jobs_names} "
f"for CronJob {cronjob_name} in namespace {namespace}"
f"for CronJob {name} in namespace {namespace}"
)

# Return the list of deleted jobs
Expand Down
52 changes: 16 additions & 36 deletions salt/_modules/metalk8s_monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,12 @@
from salt.exceptions import CommandExecutionError

from datetime import datetime, timedelta

MISSING_DEPS = []

try:
import requests
except ImportError:
MISSING_DEPS.append("requests")
from json.decoder import JSONDecodeError

__virtualname__ = "metalk8s_monitoring"


def __virtual__():
if MISSING_DEPS:
error_msg = f"Missing dependencies: {', '.join(MISSING_DEPS)}"
return False, error_msg

return __virtualname__


Expand Down Expand Up @@ -91,7 +81,7 @@ def add_silence(
"api/v2/silences", "POST", json=body, **kwargs
)

return response["silenceId"]
return response["silenceID"]


def delete_silence(silence_id, **kwargs):
Expand Down Expand Up @@ -191,28 +181,18 @@ def _requests_alertmanager_api(route, method="GET", **kwargs):
raise CommandExecutionError(
f"Unable to query Alertmanager API on {url}"
) from exc

try:
json = response.json()
except ValueError as exc:
if response.status_code != requests.codes.ok:
error = (
f"Received HTTP code {response.status_code} when "
f"querying Alertmanager API on {url}"
)
else:
error = (
"Malformed response returned from Alertmanager API: "
f"{exc}: {response.text}"
)
raise CommandExecutionError(error) from exc

if json["status"] == "error":
raise CommandExecutionError(f"{json['errorType']}: {json['error']}")

# When we successfully delete a silence, the API returns a 200 with a
# `status` key set to `success` and no `data` key.
if json["status"] == "success" and json.get("data") is None:
if not response.ok:
raise CommandExecutionError(
f"Received HTTP code {response.status_code} when "
f"querying Alertmanager API on {url} "
f"Error message: '{response.text}'"
)
if not response.content:
return True

return json.get("data")
try:
return response.json()
except JSONDecodeError as exc:
raise CommandExecutionError(
"Unable to decode JSON response from Alertmanager API "
f"Content: '{response.text}'"
) from exc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
list_cronjobs:
get_cronjobs:
# Single namespace : default
- list_objects: &_list_objects_default
- name: my_cronjob
Expand Down Expand Up @@ -201,7 +201,7 @@ get_jobs:
ownerReferences:
- uid: 5678
get_cronjob: *get_cronjob_default
cronjob_name: my_cronjob
name: my_cronjob
namespace: default
result:
- name: my_job
Expand Down Expand Up @@ -264,7 +264,7 @@ get_jobs:
ownerReferences:
- uid: 1234
get_cronjob: *get_cronjob_default
cronjob_name: my_cronjob
name: my_cronjob
namespace: default
result:
- name: my_job
Expand Down Expand Up @@ -294,11 +294,11 @@ get_jobs:

# No jobs
- get_cronjob: *get_cronjob_default
cronjob_name: my_cronjob
name: my_cronjob
namespace: default
result: []

stop_jobs:
suspend_cronjob_and_delete_jobs:
# Nominal case
- get_jobs: &_get_jobs_my_cronjob
- name: my_job
Expand All @@ -309,27 +309,27 @@ stop_jobs:
name: my_cronjob
ownerReferences:
- uid: 1234
cronjob_name: my_cronjob
name: my_cronjob
namespace: default
result: *_get_jobs_my_cronjob

# No jobs
- get_jobs: []
cronjob_name: my_cronjob
name: my_cronjob
namespace: default
result: []

# Wait for jobs to be deleted
- get_jobs: *_get_jobs_my_cronjob
cronjob_name: my_cronjob
name: my_cronjob
namespace: default
wait: true
delete_jobs_delay: 1
result: *_get_jobs_my_cronjob

# Timeout exceeded
- get_jobs: *_get_jobs
cronjob_name: my_cronjob
name: my_cronjob
namespace: default
wait: true
timeout_seconds: 1
Expand Down
60 changes: 31 additions & 29 deletions salt/tests/unit/modules/files/test_metalk8s_monitoring.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ alertmanager_api_helper:
ports:
http-web: 1234
reloader-web: 1235
route: api/v1/alerts
resp_body:
status: success
route: api/v2/alerts
resp_content:
data: some result
result:
data: some result
result: some result
called_with:
args: [GET, 'http://10.0.0.1:1234/api/v1/alerts']
args: [GET, 'http://10.0.0.1:1234/api/v2/alerts']

- _id: bad-endpoint
ips_and_ports:
Expand All @@ -23,15 +23,15 @@ alertmanager_api_helper:
ports:
web: 1234
reloader-web: 1235
route: api/v1/alerts
route: api/v2/alerts
raises: True
request_called_once: False
result: >-
Unable to get proper Alertmanager API endpoint: Available endpoints:
- _id: empty-endpoint
ips_and_ports: {}
route: api/v1/alerts
route: api/v2/alerts
raises: True
request_called_once: False
result: >-
Expand All @@ -43,60 +43,62 @@ alertmanager_api_helper:
method: POST
json:
my-data: my-value
resp_body:
status: success
resp_content:
data: some result
result:
data: some result
result: some result
called_with:
args: [POST, 'http://10.0.0.1:1234/my-route']
kwargs:
json: {my-data: my-value}

- _id: request-error
ips_and_ports: *alertmanager_ips_and_ports
route: api/v1/alerts
route: api/v2/alerts
request_raises: 'some request error'
raises: True
result: >-
Unable to query Alertmanager API on http://10.0.0.1:1234/api/v1/alerts
Unable to query Alertmanager API on http://10.0.0.1:1234/api/v2/alerts
- _id: response-error
ips_and_ports: *alertmanager_ips_and_ports
route: api/v1/alerts
resp_body:
status: error
error: 'some error happened'
errorType: 'IMPOSSIBRU'
route: api/v2/alerts
resp_status: 404
resp_content:
code: 601
message: 'alert not found'
raises: True
result: 'IMPOSSIBRU: some error happened'
result: >-
Received HTTP code 404 when querying Alertmanager API on
http://10.0.0.1:1234/api/v2/alerts
Error message: '{"code": 601, "message": "alert not found"}'
- _id: response-bad-format
ips_and_ports: *alertmanager_ips_and_ports
route: api/v1/alerts
resp_body: 'wrong format'
resp_status: 200
route: api/v2/alerts
resp_content: 'wrong format'
raises: True
result: >-
Malformed response returned from Alertmanager API: .*: wrong format
Unable to decode JSON response from Alertmanager API
Content: 'wrong format'
- _id: response-bad-status
ips_and_ports: *alertmanager_ips_and_ports
route: api/v1/alerts
resp_body: null
route: api/v2/alerts
resp_status: 401
resp_content: Unauthorized
raises: True
result: >-
Received HTTP code 401 when querying Alertmanager API on
http://10.0.0.1:1234/api/v1/alerts
http://10.0.0.1:1234/api/v2/alerts
Error message: 'Unauthorized'
- _id: nominal-delete
ips_and_ports: *alertmanager_ips_and_ports
route: api/v1/alerts
resp_body:
status: success
route: api/v2/alerts
result: True
called_with:
args: [GET, 'http://10.0.0.1:1234/api/v1/alerts']
args: [GET, 'http://10.0.0.1:1234/api/v2/alerts']

add_silence:
- _id: nominal
Expand Down

0 comments on commit d178477

Please sign in to comment.