Merge ~exsdev/charm-kubernetes-service-checks:add_nodes_readiness_check_w_lint into charm-kubernetes-service-checks:master

Proposed by Edin S
Status: Merged
Approved by: James Troup
Approved revision: 98a7d24feb992840654535436f5ee9efc25fb60c
Merged at revision: 98a7d24feb992840654535436f5ee9efc25fb60c
Proposed branch: ~exsdev/charm-kubernetes-service-checks:add_nodes_readiness_check_w_lint
Merge into: charm-kubernetes-service-checks:master
Diff against target: 124 lines (+55/-9)
3 files modified
files/plugins/check_kubernetes_api.py (+52/-7)
lib/lib_kubernetes_service_checks.py (+2/-2)
src/charm.py (+1/-0)
Reviewer Review Type Date Requested Status
James Troup (community) Needs Information
🤖 prod-jenkaas-bootstack (community) continuous-integration Approve
Xav Paice Pending
Review via email: mp+404868@code.launchpad.net

This proposal supersedes a proposal from 2021-05-19.

Commit message

Added Kubernetes API Nagios plugin for node status

Description of the change

Diko's work with linting fixes.

To post a comment you must log in.
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote : Posted in a previous version of this proposal

A CI job is currently in progress. A follow up comment will be added when it completes.

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote : Posted in a previous version of this proposal

FAILED: Continuous integration, rev:5ed702a19431693f2a0d3e19a3379efdae513e55

No commit message was specified in the merge proposal. Click on the following link and set the commit message (if you want jenkins to rebuild you need to trigger it yourself):
https://code.launchpad.net/~dparv/charm-kubernetes-service-checks/+git/charm-kubernetes-service-checks/+merge/402981/+edit-commit-message

https://jenkins.canonical.com/bootstack/job/lp-charm-kubernetes-service-checks-ci/3/
Executed test runs:
    FAILURE: https://jenkins.canonical.com/bootstack/job/lp-charm-test-unit/74/
    None: https://jenkins.canonical.com/bootstack/job/lp-update-mp/216/

Click here to trigger a rebuild:
https://jenkins.canonical.com/bootstack/job/lp-charm-kubernetes-service-checks-ci/3//rebuild

review: Needs Fixing (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote : Posted in a previous version of this proposal
review: Needs Fixing (continuous-integration)
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote : Posted in a previous version of this proposal
review: Needs Fixing (continuous-integration)
Revision history for this message
Xav Paice (xavpaice) wrote : Posted in a previous version of this proposal

Please fix the lint error.

lint run-test: commands[1] | black --check --exclude '/(\.eggs|\.git|\.tox|\.venv|\.build|dist|charmhelpers|mod)/' .
would reformat files/plugins/check_kubernetes_api.py
Oh no! 💥 💔 💥
1 file would be reformatted, 9 files would be left unchanged.

review: Needs Fixing
Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :

A CI job is currently in progress. A follow up comment will be added when it completes.

Revision history for this message
🤖 prod-jenkaas-bootstack (prod-jenkaas-bootstack) wrote :
review: Approve (continuous-integration)
Revision history for this message
James Troup (elmo) wrote :

See comment inline

review: Needs Information
Revision history for this message
Edin S (exsdev) wrote :

See reply to comment inline.

Revision history for this message
James Troup (elmo) wrote :

Duh, thanks.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/files/plugins/check_kubernetes_api.py b/files/plugins/check_kubernetes_api.py
2index 6181699..6c31168 100755
3--- a/files/plugins/check_kubernetes_api.py
4+++ b/files/plugins/check_kubernetes_api.py
5@@ -2,6 +2,7 @@
6 """NRPE Plugin for checking Kubernetes API."""
7
8 import argparse
9+import json
10 import sys
11
12 import urllib3
13@@ -67,6 +68,52 @@ def check_kubernetes_health(k8s_address, client_token, disable_ssl):
14 return NAGIOS_STATUS_OK, "Kubernetes health 'ok'"
15
16
17+def check_kubernetes_nodes(k8s_address, client_token, disable_ssl):
18+ """Call <kubernetes-api>/api/v1/nodes endpoint and check each node status.
19+
20+ :param k8s_address: Address to kube-api-server formatted 'https://<IP>:<PORT>'
21+ :param client_token: Token for authenticating with the kube-api
22+ :param disable_ssl: Disables SSL Host Key verification
23+ """
24+ url = k8s_address + "/api/v1/nodes"
25+ if disable_ssl:
26+ # perform check without SSL verification
27+ http = urllib3.PoolManager(cert_reqs="CERT_NONE", assert_hostname=False)
28+ else:
29+ http = urllib3.PoolManager()
30+
31+ try:
32+ resp = http.request(
33+ "GET", url, headers={"Authorization": "Bearer {}".format(client_token)}
34+ )
35+ except urllib3.exceptions.MaxRetryError as e:
36+ return NAGIOS_STATUS_CRITICAL, e
37+
38+ if resp.status != 200:
39+ return (
40+ NAGIOS_STATUS_CRITICAL,
41+ "Unexpected HTTP Response code ({})".format(resp.status),
42+ )
43+
44+ response_body = json.loads(resp.data)
45+ nodes_not_ready = []
46+ for item in response_body["items"]:
47+ for condition in item["status"]["conditions"]:
48+ if condition["type"] == "Ready":
49+ node_name = item["metadata"]["name"]
50+ if condition["status"] != "True":
51+ nodes_not_ready.append(node_name)
52+
53+ if nodes_not_ready:
54+ nodes = ", ".join(nodes_not_ready)
55+ return (
56+ NAGIOS_STATUS_CRITICAL,
57+ f"Nodes NotReady: {nodes}",
58+ )
59+
60+ return NAGIOS_STATUS_OK, "All Nodes Ready"
61+
62+
63 if __name__ == "__main__":
64 parser = argparse.ArgumentParser(
65 description="Check Kubernetes API status",
66@@ -98,7 +145,7 @@ if __name__ == "__main__":
67 help="Client access token for authenticate with the Kubernetes API",
68 )
69
70- check_choices = ["health"]
71+ check_choices = ["health", "nodes"]
72 parser.add_argument(
73 "--check",
74 dest="check",
75@@ -119,7 +166,10 @@ if __name__ == "__main__":
76 )
77 args = parser.parse_args()
78
79- checks = {"health": check_kubernetes_health}
80+ checks = {
81+ "health": check_kubernetes_health,
82+ "nodes": check_kubernetes_nodes,
83+ }
84
85 k8s_url = "https://{}:{}".format(args.host, args.port)
86 nagios_exit(
87@@ -134,9 +184,4 @@ Authorization: Bearer $TOKEN
88 Accept: application/json
89 Connection: close
90
91-GET /api/va/nodes HTTP/1.1
92-Authorization: Bearer $TOKEN
93-Accept: application/json
94-Connection: close
95-
96 """
97diff --git a/lib/lib_kubernetes_service_checks.py b/lib/lib_kubernetes_service_checks.py
98index 14db4e6..688f77c 100644
99--- a/lib/lib_kubernetes_service_checks.py
100+++ b/lib/lib_kubernetes_service_checks.py
101@@ -108,9 +108,9 @@ class KSCHelper:
102 if not os.path.exists(self.plugins_dir):
103 os.makedirs(self.plugins_dir)
104
105- # register basic api health check
106+ # register basic api health check and nodes readiness status
107 check_k8s_plugin = os.path.join(self.plugins_dir, "check_kubernetes_api.py")
108- for check in ["health"]:
109+ for check in ["health", "nodes"]:
110 check_command = "{} -H {} -P {} -T {} --check {}".format(
111 check_k8s_plugin,
112 self.kubernetes_api_address,
113diff --git a/src/charm.py b/src/charm.py
114index 00923fb..066ebff 100755
115--- a/src/charm.py
116+++ b/src/charm.py
117@@ -27,6 +27,7 @@ class KubernetesServiceChecksCharm(CharmBase):
118 super().__init__(*args)
119 # -- standard hook observation
120 self.framework.observe(self.on.install, self.on_install)
121+ self.framework.observe(self.on.upgrade_charm, self.on_upgrade_charm)
122 self.framework.observe(self.on.start, self.on_start)
123 self.framework.observe(self.on.config_changed, self.on_config_changed)
124 self.framework.observe(

Subscribers

People subscribed via source and target branches

to all changes: