Module: check_mk
Branch: master
Commit: ff0c84dd2e56c3937a930c0cfdc068238b3660d6
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=ff0c84dd2e56c3…
Author: Tom Baerwinkel <tb(a)mathias-kettner.de>
Date: Wed Jan 16 15:39:58 2019 +0100
agent_kubernetes: add the option to specify a URL path prefix
The path prefix is prepended to all Kubernetes API calls.
This e.g. useful for Kubernetes setups that are managed by
Rancher.
CMK-1478
Change-Id: I15fd9ddb89670271aab1431cc7c8ea9635a1164d
---
checks/agent_kubernetes | 3 +++
cmk/gui/plugins/wato/datasource_programs.py | 24 +++++++++++++++++++++---
cmk/special_agents/agent_kubernetes.py | 18 ++++++++++++++++--
3 files changed, 40 insertions(+), 5 deletions(-)
diff --git a/checks/agent_kubernetes b/checks/agent_kubernetes
index ff039c0..9497ce1 100644
--- a/checks/agent_kubernetes
+++ b/checks/agent_kubernetes
@@ -37,6 +37,9 @@ def agent_kubernetes_arguments(params, _hostname, ipaddress):
if "url-prefix" in params:
args += ["--url-prefix", params["url-prefix"]]
+ if "path-prefix" in params:
+ args += ["--path-prefix", params["path-prefix"]]
+
args += [ipaddress]
return args
diff --git a/cmk/gui/plugins/wato/datasource_programs.py
b/cmk/gui/plugins/wato/datasource_programs.py
index b2dd7b9..c67ee48 100644
--- a/cmk/gui/plugins/wato/datasource_programs.py
+++ b/cmk/gui/plugins/wato/datasource_programs.py
@@ -113,8 +113,26 @@ register_rule(
title=_("Token"),
allow_empty=False,
)),
- ("port", Integer(title=_(u"Port"), default_value=443)),
- ("url-prefix", HTTPUrl(title=_("Custom URL prefix"),
allow_empty=False)),
+ ("port",
+ Integer(
+ title=_(u"Port"),
+ help=_("If no port is given a default value of 443 will be
used."),
+ default_value=443)),
+ ("url-prefix",
+ HTTPUrl(
+ title=_("Custom URL prefix"),
+ help=_("Defines the scheme (either HTTP or HTTPS) and host part
"
+ "of Kubernetes API calls like e.g.
\"https://example.com\". "
+ "If no prefix is specified HTTPS together with the IP of
"
+ "the host will be used."),
+ allow_empty=False)),
+ ("path-prefix",
+ TextAscii(
+ title=_("Custom path prefix"),
+ help=_("Specifies a URL path prefix which is prepended to the path
in calls "
+ "to the Kubernetes API. This is e.g. useful if Rancher is
used to "
+ "manage Kubernetes clusters. If no prefix is given
\"/\" will be used."),
+ allow_empty=False)),
("no-cert-check",
Alternative(
title=_("Disable certificate verification"),
@@ -124,7 +142,7 @@ register_rule(
],
default_value=False)),
],
- optional_keys=["port", "url-prefix",
"no-cert-check"],
+ optional_keys=["port", "url-prefix", "path-prefix",
"no-cert-check"],
),
match="first",
title=_(u"Kubernetes"),
diff --git a/cmk/special_agents/agent_kubernetes.py
b/cmk/special_agents/agent_kubernetes.py
index 03951d3..e689e92 100644
--- a/cmk/special_agents/agent_kubernetes.py
+++ b/cmk/special_agents/agent_kubernetes.py
@@ -54,6 +54,14 @@ import cmk.utils.profile
import cmk.utils.password_store
+class PathPrefixAction(argparse.Action):
+ def __call__(self, parser, namespace, values, option_string=None):
+ if not values:
+ return ''
+ path_prefix = '/' + values.strip('/')
+ setattr(namespace, self.dest, path_prefix)
+
+
def parse(args):
# type: (List[str]) -> argparse.Namespace
p = argparse.ArgumentParser(description=__doc__)
@@ -68,6 +76,11 @@ def parse(args):
p.add_argument('--port', type=int, default=443, help='Port to connect
to')
p.add_argument('--token', required=True, help='Token for that user')
p.add_argument('--url-prefix', help='Custom URL prefix for Kubernetes API
calls')
+ p.add_argument(
+ '--path-prefix',
+ default='',
+ action=PathPrefixAction,
+ help='Optional URL path prefix to prepend to Kubernetes API calls')
p.add_argument('--no-cert-check', action='store_true',
help='Disable certificate verification')
p.add_argument(
'--profile',
@@ -736,9 +749,10 @@ def get_api_client(arguments):
config = client.Configuration()
if arguments.url_prefix:
- config.host = '%s:%s' % (arguments.url_prefix, arguments.port)
+ config.host = '%s:%s%s' % (arguments.url_prefix, arguments.port,
arguments.path_prefix)
else:
- config.host = 'https://%s:%s' % (arguments.host, arguments.port)
+ config.host = 'https://%s:%s%s' % (arguments.host, arguments.port,
arguments.path_prefix)
+
config.api_key_prefix['authorization'] = 'Bearer'
config.api_key['authorization'] = arguments.token