kubernator 1.0.4__tar.gz → 1.0.6__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of kubernator might be problematic. Click here for more details.
- {kubernator-1.0.4 → kubernator-1.0.6}/PKG-INFO +1 -1
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator/__init__.py +2 -2
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator/api.py +19 -1
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator/app.py +64 -28
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator/plugins/awscli.py +3 -3
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator/plugins/eks.py +1 -1
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator/plugins/helm.py +7 -7
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator/plugins/istio.py +10 -9
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator/plugins/k8s.py +61 -14
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator/plugins/k8s_api.py +3 -1
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator/plugins/kubectl.py +5 -5
- kubernator-1.0.6/kubernator/plugins/minikube.py +201 -0
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator/plugins/terraform.py +5 -5
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator/plugins/terragrunt.py +5 -5
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator/proc.py +4 -1
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator.egg-info/PKG-INFO +1 -1
- {kubernator-1.0.4 → kubernator-1.0.6}/setup.py +1 -1
- kubernator-1.0.4/kubernator/plugins/minikube.py +0 -101
- {kubernator-1.0.4 → kubernator-1.0.6}/MANIFEST.in +0 -0
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator/LICENSE +0 -0
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator/__main__.py +0 -0
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator/plugins/__init__.py +0 -0
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator/plugins/kops.py +0 -0
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator/plugins/kubeconfig.py +0 -0
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator/plugins/template.py +0 -0
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator.egg-info/SOURCES.txt +0 -0
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator.egg-info/dependency_links.txt +0 -0
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator.egg-info/entry_points.txt +0 -0
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator.egg-info/namespace_packages.txt +0 -0
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator.egg-info/requires.txt +0 -0
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator.egg-info/top_level.txt +0 -0
- {kubernator-1.0.4 → kubernator-1.0.6}/kubernator.egg-info/zip-safe +0 -0
- {kubernator-1.0.4 → kubernator-1.0.6}/setup.cfg +0 -0
|
@@ -152,8 +152,12 @@ def _download_remote_file(url, file_name, cache: dict):
|
|
|
152
152
|
return dict(r.headers)
|
|
153
153
|
|
|
154
154
|
|
|
155
|
+
def get_app_cache_dir():
|
|
156
|
+
return Path(user_config_dir("kubernator"))
|
|
157
|
+
|
|
158
|
+
|
|
155
159
|
def get_cache_dir(category: str, sub_category: str = None):
|
|
156
|
-
config_dir =
|
|
160
|
+
config_dir = get_app_cache_dir() / category
|
|
157
161
|
if sub_category:
|
|
158
162
|
config_dir = config_dir / sub_category
|
|
159
163
|
if not config_dir.exists():
|
|
@@ -224,6 +228,19 @@ def validator_with_defaults(validator_class):
|
|
|
224
228
|
return validators.extend(validator_class, {"properties": set_defaults})
|
|
225
229
|
|
|
226
230
|
|
|
231
|
+
def install_python_k8s_client(run, package_major, logger_stdout, logger_stderr):
|
|
232
|
+
cache_dir = get_cache_dir("python")
|
|
233
|
+
package_major_dir = cache_dir / str(package_major)
|
|
234
|
+
|
|
235
|
+
if not package_major_dir.exists():
|
|
236
|
+
package_major_dir.mkdir(parents=True, exist_ok=True)
|
|
237
|
+
|
|
238
|
+
run(["pip", "install", "--no-deps", "--no-cache-dir", "--no-input", "--target", str(package_major_dir),
|
|
239
|
+
f"kubernetes~={package_major}.0"], logger_stdout, logger_stderr).wait()
|
|
240
|
+
|
|
241
|
+
return package_major_dir
|
|
242
|
+
|
|
243
|
+
|
|
227
244
|
class _PropertyList(MutableSequence):
|
|
228
245
|
|
|
229
246
|
def __init__(self, seq, read_parent, name):
|
|
@@ -620,6 +637,7 @@ def clone_url_str(url):
|
|
|
620
637
|
|
|
621
638
|
|
|
622
639
|
def prepend_os_path(path):
|
|
640
|
+
path = str(path)
|
|
623
641
|
paths = os.environ["PATH"].split(os.pathsep)
|
|
624
642
|
if path not in paths:
|
|
625
643
|
paths.insert(0, path)
|
|
@@ -26,11 +26,13 @@ import urllib.parse
|
|
|
26
26
|
from collections import deque
|
|
27
27
|
from collections.abc import MutableMapping, Callable
|
|
28
28
|
from pathlib import Path
|
|
29
|
+
from shutil import rmtree
|
|
29
30
|
from typing import Optional, Union
|
|
30
31
|
|
|
31
32
|
import kubernator
|
|
32
33
|
from kubernator.api import (KubernatorPlugin, Globs, scan_dir, PropertyDict, config_as_dict, config_parent,
|
|
33
|
-
download_remote_file, load_remote_file, Repository, StripNL, jp
|
|
34
|
+
download_remote_file, load_remote_file, Repository, StripNL, jp, get_app_cache_dir,
|
|
35
|
+
install_python_k8s_client)
|
|
34
36
|
from kubernator.proc import run, run_capturing_out
|
|
35
37
|
|
|
36
38
|
TRACE = 5
|
|
@@ -55,10 +57,16 @@ logger = logging.getLogger("kubernator")
|
|
|
55
57
|
|
|
56
58
|
|
|
57
59
|
def define_arg_parse():
|
|
58
|
-
parser = argparse.ArgumentParser(
|
|
60
|
+
parser = argparse.ArgumentParser(prog="kubernator",
|
|
61
|
+
description="Kubernetes Provisioning Tool",
|
|
59
62
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
|
|
60
|
-
|
|
61
|
-
|
|
63
|
+
g = parser.add_mutually_exclusive_group()
|
|
64
|
+
g.add_argument("--version", action="version", version=kubernator.__version__,
|
|
65
|
+
help="print version and exit")
|
|
66
|
+
g.add_argument("--clear-cache", action="store_true",
|
|
67
|
+
help="clear cache and exit")
|
|
68
|
+
g.add_argument("--pre-cache-k8s-client", action="extend", nargs="+", type=int,
|
|
69
|
+
help="download specified K8S client library minor(!) version(s) and exit")
|
|
62
70
|
parser.add_argument("--log-format", choices=["human", "json"], default="human",
|
|
63
71
|
help="whether to log for human or machine consumption")
|
|
64
72
|
parser.add_argument("--log-file", type=argparse.FileType("w"), default=None,
|
|
@@ -158,47 +166,50 @@ class App(KubernatorPlugin):
|
|
|
158
166
|
|
|
159
167
|
self.register_plugin(self)
|
|
160
168
|
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
169
|
+
try:
|
|
170
|
+
while True:
|
|
171
|
+
cwd = self.next()
|
|
172
|
+
if not cwd:
|
|
173
|
+
logger.debug("No paths left to traverse")
|
|
174
|
+
break
|
|
166
175
|
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
logger.debug("Inspecting directory %s", self._display_path(cwd))
|
|
170
|
-
self._run_handlers(KubernatorPlugin.handle_before_dir, False, context, None, cwd)
|
|
176
|
+
context = self.context
|
|
171
177
|
|
|
172
|
-
|
|
173
|
-
self._run_handlers(KubernatorPlugin.
|
|
178
|
+
logger.debug("Inspecting directory %s", self._display_path(cwd))
|
|
179
|
+
self._run_handlers(KubernatorPlugin.handle_before_dir, False, context, None, cwd)
|
|
174
180
|
|
|
175
|
-
|
|
176
|
-
|
|
181
|
+
if (ktor_py := (cwd / ".kubernator.py")).exists():
|
|
182
|
+
self._run_handlers(KubernatorPlugin.handle_before_script, False, context, None, cwd)
|
|
177
183
|
|
|
178
|
-
|
|
184
|
+
for h in self.context._plugins:
|
|
185
|
+
h.set_context(context)
|
|
179
186
|
|
|
180
|
-
|
|
181
|
-
h.set_context(None)
|
|
187
|
+
self._exec_ktor(ktor_py)
|
|
182
188
|
|
|
183
|
-
|
|
189
|
+
for h in self.context._plugins:
|
|
190
|
+
h.set_context(None)
|
|
184
191
|
|
|
185
|
-
|
|
192
|
+
self._run_handlers(KubernatorPlugin.handle_after_script, True, context, None, cwd)
|
|
186
193
|
|
|
187
|
-
|
|
188
|
-
context = self.context
|
|
194
|
+
self._run_handlers(KubernatorPlugin.handle_after_dir, True, context, None, cwd)
|
|
189
195
|
|
|
190
|
-
|
|
196
|
+
self.context = self._top_dir_context
|
|
197
|
+
context = self.context
|
|
191
198
|
|
|
192
|
-
|
|
199
|
+
self._run_handlers(KubernatorPlugin.handle_apply, True, context, None)
|
|
193
200
|
|
|
194
|
-
|
|
201
|
+
self._run_handlers(KubernatorPlugin.handle_verify, True, context, None)
|
|
202
|
+
finally:
|
|
203
|
+
self.context = self._top_dir_context
|
|
204
|
+
context = self.context
|
|
205
|
+
self._run_handlers(KubernatorPlugin.handle_shutdown, True, context, None)
|
|
195
206
|
|
|
196
207
|
def discover_plugins(self):
|
|
197
208
|
importlib.invalidate_caches()
|
|
198
209
|
search_path = Path(kubernator.__path__[0], "plugins")
|
|
199
210
|
[importlib.import_module(name)
|
|
200
211
|
for finder, name, is_pkg in
|
|
201
|
-
pkgutil.iter_modules([search_path], "kubernator.plugins.")]
|
|
212
|
+
pkgutil.iter_modules([str(search_path)], "kubernator.plugins.")]
|
|
202
213
|
|
|
203
214
|
for plugin in KubernatorPlugin.__subclasses__():
|
|
204
215
|
if plugin._name in self._plugin_types:
|
|
@@ -440,11 +451,36 @@ class App(KubernatorPlugin):
|
|
|
440
451
|
return "Kubernator"
|
|
441
452
|
|
|
442
453
|
|
|
454
|
+
def clear_cache():
|
|
455
|
+
cache_dir = get_app_cache_dir()
|
|
456
|
+
logger.info("Clearing application cache at %s", cache_dir)
|
|
457
|
+
if cache_dir.exists():
|
|
458
|
+
rmtree(cache_dir)
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
def pre_cache_k8s_clients(*versions):
|
|
462
|
+
proc_logger = logger.getChild("proc")
|
|
463
|
+
stdout_logger = StripNL(proc_logger.info)
|
|
464
|
+
stderr_logger = StripNL(proc_logger.warning)
|
|
465
|
+
|
|
466
|
+
for v in versions:
|
|
467
|
+
logger.info("Caching K8S client library ~=v%s.0...", v)
|
|
468
|
+
install_python_k8s_client(run, v, stdout_logger, stderr_logger)
|
|
469
|
+
|
|
470
|
+
|
|
443
471
|
def main():
|
|
444
472
|
args = define_arg_parse().parse_args()
|
|
445
473
|
init_logging(args.verbose, args.log_format, args.log_file)
|
|
446
474
|
|
|
447
475
|
try:
|
|
476
|
+
if args.clear_cache:
|
|
477
|
+
clear_cache()
|
|
478
|
+
return
|
|
479
|
+
|
|
480
|
+
if args.pre_cache_k8s_client:
|
|
481
|
+
pre_cache_k8s_clients(*args.pre_cache_k8s_client)
|
|
482
|
+
return
|
|
483
|
+
|
|
448
484
|
with App(args) as app:
|
|
449
485
|
app.run()
|
|
450
486
|
except SystemExit as e:
|
|
@@ -54,7 +54,7 @@ class AwsCliPlugin(KubernatorPlugin):
|
|
|
54
54
|
def set_context(self, context):
|
|
55
55
|
self.context = context
|
|
56
56
|
|
|
57
|
-
def
|
|
57
|
+
def stanza(self, *args, output="json", region=None):
|
|
58
58
|
context = self.context
|
|
59
59
|
stanza = [context.awscli.aws_file, "--output", output]
|
|
60
60
|
if region:
|
|
@@ -101,7 +101,7 @@ class AwsCliPlugin(KubernatorPlugin):
|
|
|
101
101
|
aws_file.chmod(0o500)
|
|
102
102
|
|
|
103
103
|
self.aws_dir = awscli_cache_dir
|
|
104
|
-
prepend_os_path(
|
|
104
|
+
prepend_os_path(self.aws_dir)
|
|
105
105
|
|
|
106
106
|
self.aws_file = str(aws_file)
|
|
107
107
|
|
|
@@ -112,7 +112,7 @@ class AwsCliPlugin(KubernatorPlugin):
|
|
|
112
112
|
|
|
113
113
|
context.globals.awscli = dict(version=version,
|
|
114
114
|
aws_file=self.aws_file,
|
|
115
|
-
|
|
115
|
+
stanza=self.stanza
|
|
116
116
|
)
|
|
117
117
|
|
|
118
118
|
def __repr__(self):
|
|
@@ -64,7 +64,7 @@ class EksPlugin(KubernatorPlugin):
|
|
|
64
64
|
def handle_init(self):
|
|
65
65
|
context = self.context
|
|
66
66
|
|
|
67
|
-
self.context.app.run(context.awscli.
|
|
67
|
+
self.context.app.run(context.awscli.stanza(
|
|
68
68
|
"eks", "update-kubeconfig", "--name", self.name, "--kubeconfig", context.eks.kubeconfig,
|
|
69
69
|
region=self.region),
|
|
70
70
|
stdout_logger,
|
|
@@ -106,7 +106,7 @@ class HelmPlugin(KubernatorPlugin):
|
|
|
106
106
|
def set_context(self, context):
|
|
107
107
|
self.context = context
|
|
108
108
|
|
|
109
|
-
def
|
|
109
|
+
def stanza(self):
|
|
110
110
|
context = self.context
|
|
111
111
|
stanza = [context.helm.helm_file, f"--kubeconfig={context.kubeconfig.kubeconfig}"]
|
|
112
112
|
if logger.getEffectiveLevel() < logging.INFO:
|
|
@@ -132,7 +132,7 @@ class HelmPlugin(KubernatorPlugin):
|
|
|
132
132
|
copy(Path(self.helm_dir.name)/f"{get_golang_os()}-{get_golang_machine()}"/"helm", helm_file)
|
|
133
133
|
|
|
134
134
|
os.chmod(helm_file, 0o500)
|
|
135
|
-
prepend_os_path(
|
|
135
|
+
prepend_os_path(self.helm_dir.name)
|
|
136
136
|
else:
|
|
137
137
|
# Use current version
|
|
138
138
|
helm_file = which("helm")
|
|
@@ -145,13 +145,13 @@ class HelmPlugin(KubernatorPlugin):
|
|
|
145
145
|
default_excludes=Globs([".*"], True),
|
|
146
146
|
namespace_transformer=True,
|
|
147
147
|
helm_file=helm_file,
|
|
148
|
-
|
|
148
|
+
stanza=self.stanza,
|
|
149
149
|
add_helm_template=self.add_helm_template,
|
|
150
150
|
add_helm=self.add_helm,
|
|
151
151
|
)
|
|
152
152
|
|
|
153
153
|
def handle_init(self):
|
|
154
|
-
version = self.context.app.run_capturing_out(self.
|
|
154
|
+
version = self.context.app.run_capturing_out(self.stanza() + ["version", "--template", "{{.Version}}"],
|
|
155
155
|
logger.error)
|
|
156
156
|
logger.info("Found Helm version %s", version)
|
|
157
157
|
|
|
@@ -204,10 +204,10 @@ class HelmPlugin(KubernatorPlugin):
|
|
|
204
204
|
logger.debug("Repository %s mapping to %s", repository, repository_hash)
|
|
205
205
|
if repository_hash not in self.repositories:
|
|
206
206
|
logger.info("Adding and updating repository %s mapping to %s", repository, repository_hash)
|
|
207
|
-
self.context.app.run(self.
|
|
207
|
+
self.context.app.run(self.stanza() + ["repo", "add", repository_hash, repository],
|
|
208
208
|
stdout_logger,
|
|
209
209
|
stderr_logger).wait()
|
|
210
|
-
self.context.app.run(self.
|
|
210
|
+
self.context.app.run(self.stanza() + ["repo", "update"],
|
|
211
211
|
stdout_logger,
|
|
212
212
|
stderr_logger).wait()
|
|
213
213
|
self.repositories.add(repository_hash)
|
|
@@ -233,7 +233,7 @@ class HelmPlugin(KubernatorPlugin):
|
|
|
233
233
|
|
|
234
234
|
stdin = write_stdin
|
|
235
235
|
|
|
236
|
-
resources = self.context.app.run_capturing_out(self.
|
|
236
|
+
resources = self.context.app.run_capturing_out(self.stanza() +
|
|
237
237
|
["template",
|
|
238
238
|
name,
|
|
239
239
|
f"{repository_hash}/{chart}",
|
|
@@ -77,7 +77,7 @@ class IstioPlugin(KubernatorPlugin, K8SResourcePluginMixin):
|
|
|
77
77
|
istio_tar.extractall(self.istioctl_dir.name)
|
|
78
78
|
|
|
79
79
|
os.chmod(istioctl_file, 0o500)
|
|
80
|
-
prepend_os_path(
|
|
80
|
+
prepend_os_path(self.istioctl_dir.name)
|
|
81
81
|
else:
|
|
82
82
|
# Use current version
|
|
83
83
|
istioctl_file = which("istioctl")
|
|
@@ -90,19 +90,19 @@ class IstioPlugin(KubernatorPlugin, K8SResourcePluginMixin):
|
|
|
90
90
|
default_includes=Globs(["*.istio.yaml", "*.istio.yml"], True),
|
|
91
91
|
default_excludes=Globs([".*"], True),
|
|
92
92
|
istioctl_file=istioctl_file,
|
|
93
|
-
|
|
93
|
+
stanza=self.stanza,
|
|
94
94
|
test=self.test_istioctl
|
|
95
95
|
)
|
|
96
96
|
|
|
97
97
|
def test_istioctl(self):
|
|
98
98
|
context = self.context
|
|
99
|
-
version_out: str = context.app.run_capturing_out(context.istio.
|
|
99
|
+
version_out: str = context.app.run_capturing_out(context.istio.stanza() + ["version", "-o", "json"],
|
|
100
100
|
stderr_logger)
|
|
101
101
|
|
|
102
102
|
version_out_js = json.loads(version_out)
|
|
103
103
|
version = version_out_js["clientVersion"]["version"]
|
|
104
104
|
logger.info("Using istioctl %r version %r with stanza %r",
|
|
105
|
-
self.context.istio.istioctl_file, version, context.istio.
|
|
105
|
+
self.context.istio.istioctl_file, version, context.istio.stanza())
|
|
106
106
|
|
|
107
107
|
logger.info("Found Istio client version %s", version)
|
|
108
108
|
|
|
@@ -111,7 +111,7 @@ class IstioPlugin(KubernatorPlugin, K8SResourcePluginMixin):
|
|
|
111
111
|
def set_context(self, context):
|
|
112
112
|
self.context = context
|
|
113
113
|
|
|
114
|
-
def
|
|
114
|
+
def stanza(self):
|
|
115
115
|
context = self.context.istio
|
|
116
116
|
return [context.istioctl_file, f"--kubeconfig={self.context.kubeconfig.kubeconfig}"]
|
|
117
117
|
|
|
@@ -145,7 +145,8 @@ class IstioPlugin(KubernatorPlugin, K8SResourcePluginMixin):
|
|
|
145
145
|
# This plugin only deals with Istio Operator, so only load that stuff
|
|
146
146
|
self.resource_definitions_schema = load_remote_file(logger,
|
|
147
147
|
f"https://raw.githubusercontent.com/kubernetes/kubernetes/"
|
|
148
|
-
f"{self.context.k8s.
|
|
148
|
+
f"{self.context.k8s.server_git_version}"
|
|
149
|
+
f"/api/openapi-spec/swagger.json",
|
|
149
150
|
FileType.JSON)
|
|
150
151
|
self._populate_resource_definitions()
|
|
151
152
|
self.add_remote_crds(f"{url_prefix}/crd-operator.yaml", FileType.YAML)
|
|
@@ -188,9 +189,9 @@ class IstioPlugin(KubernatorPlugin, K8SResourcePluginMixin):
|
|
|
188
189
|
|
|
189
190
|
if context.app.args.command == "apply":
|
|
190
191
|
logger.info("Running Istio precheck")
|
|
191
|
-
context.app.run(context.istio.
|
|
192
|
+
context.app.run(context.istio.stanza() + ["x", "precheck"],
|
|
192
193
|
stdout_logger, stderr_logger).wait()
|
|
193
|
-
context.app.run(context.istio.
|
|
194
|
+
context.app.run(context.istio.stanza() + ["validate", "-f", operators_file.name],
|
|
194
195
|
stdout_logger, stderr_logger).wait()
|
|
195
196
|
|
|
196
197
|
self._operator_init(operators_file, True)
|
|
@@ -229,7 +230,7 @@ class IstioPlugin(KubernatorPlugin, K8SResourcePluginMixin):
|
|
|
229
230
|
raise
|
|
230
231
|
|
|
231
232
|
logger.info("Running Istio operator init%s", status_details)
|
|
232
|
-
istio_operator_init = context.istio.
|
|
233
|
+
istio_operator_init = context.istio.stanza() + ["operator", "init", "-f", operators_file.name]
|
|
233
234
|
context.app.run(istio_operator_init + (["--dry-run"] if dry_run else []),
|
|
234
235
|
stdout_logger,
|
|
235
236
|
stderr_logger).wait()
|
|
@@ -24,19 +24,30 @@ import sys
|
|
|
24
24
|
import types
|
|
25
25
|
from collections.abc import Mapping
|
|
26
26
|
from functools import partial
|
|
27
|
+
from importlib.metadata import version as pkg_version
|
|
27
28
|
from pathlib import Path
|
|
28
29
|
from typing import Iterable, Callable, Sequence
|
|
29
30
|
|
|
30
31
|
import jsonpatch
|
|
31
32
|
import yaml
|
|
32
33
|
|
|
33
|
-
from kubernator.api import (KubernatorPlugin,
|
|
34
|
+
from kubernator.api import (KubernatorPlugin,
|
|
35
|
+
Globs,
|
|
36
|
+
scan_dir,
|
|
37
|
+
load_file,
|
|
38
|
+
FileType,
|
|
39
|
+
load_remote_file,
|
|
40
|
+
StripNL,
|
|
41
|
+
install_python_k8s_client)
|
|
34
42
|
from kubernator.plugins.k8s_api import (K8SResourcePluginMixin,
|
|
35
43
|
K8SResource,
|
|
36
44
|
K8SResourcePatchType,
|
|
37
45
|
K8SPropagationPolicy)
|
|
38
46
|
|
|
39
47
|
logger = logging.getLogger("kubernator.k8s")
|
|
48
|
+
proc_logger = logger.getChild("proc")
|
|
49
|
+
stdout_logger = StripNL(proc_logger.info)
|
|
50
|
+
stderr_logger = StripNL(proc_logger.warning)
|
|
40
51
|
|
|
41
52
|
|
|
42
53
|
def final_resource_validator(resources: Sequence[K8SResource],
|
|
@@ -62,6 +73,8 @@ class KubernetesPlugin(KubernatorPlugin, K8SResourcePluginMixin):
|
|
|
62
73
|
super().__init__()
|
|
63
74
|
self.context = None
|
|
64
75
|
|
|
76
|
+
self.embedded_pkg_version = self._get_kubernetes_client_version()
|
|
77
|
+
|
|
65
78
|
self._transformers = []
|
|
66
79
|
self._validators = []
|
|
67
80
|
|
|
@@ -109,30 +122,64 @@ class KubernetesPlugin(KubernatorPlugin, K8SResourcePluginMixin):
|
|
|
109
122
|
def _kubeconfig_changed(self):
|
|
110
123
|
self.setup_client()
|
|
111
124
|
|
|
125
|
+
def _get_kubernetes_client_version(self):
|
|
126
|
+
return pkg_version("kubernetes").split(".")
|
|
127
|
+
|
|
112
128
|
def setup_client(self):
|
|
129
|
+
k8s = self.context.k8s
|
|
130
|
+
if "server_version" not in k8s:
|
|
131
|
+
self._setup_client()
|
|
132
|
+
|
|
133
|
+
server_minor = k8s.server_version[1]
|
|
134
|
+
pkg_major = self.embedded_pkg_version[0]
|
|
135
|
+
if server_minor != pkg_major:
|
|
136
|
+
logger.info("Bundled Kubernetes client version %s doesn't match server version %s",
|
|
137
|
+
".".join(self.embedded_pkg_version), ".".join(k8s.server_version))
|
|
138
|
+
pkg_dir = install_python_k8s_client(self.context.app.run, server_minor, stdout_logger, stderr_logger)
|
|
139
|
+
|
|
140
|
+
modules_to_delete = []
|
|
141
|
+
for k, v in sys.modules.items():
|
|
142
|
+
if k == "kubernetes" or k.startswith("kubernetes."):
|
|
143
|
+
modules_to_delete.append(k)
|
|
144
|
+
for k in modules_to_delete:
|
|
145
|
+
del sys.modules[k]
|
|
146
|
+
|
|
147
|
+
logger.info("Adding sys.path reference to %s", pkg_dir)
|
|
148
|
+
sys.path.insert(0, str(pkg_dir))
|
|
149
|
+
self.embedded_pkg_version = self._get_kubernetes_client_version()
|
|
150
|
+
logger.info("Switching to Kubernetes client version %s", ".".join(self.embedded_pkg_version))
|
|
151
|
+
self._setup_client()
|
|
152
|
+
else:
|
|
153
|
+
logger.info("Bundled Kubernetes client version %s matches server version %s",
|
|
154
|
+
".".join(self.embedded_pkg_version), ".".join(k8s.server_version))
|
|
155
|
+
|
|
156
|
+
logger.debug("Reading Kubernetes OpenAPI spec for %s", k8s.server_git_version)
|
|
157
|
+
|
|
158
|
+
k8s_def = load_remote_file(logger, f"https://raw.githubusercontent.com/kubernetes/kubernetes/"
|
|
159
|
+
f"{k8s.server_git_version}/api/openapi-spec/swagger.json",
|
|
160
|
+
FileType.JSON)
|
|
161
|
+
self.resource_definitions_schema = k8s_def
|
|
162
|
+
|
|
163
|
+
self._populate_resource_definitions()
|
|
164
|
+
|
|
165
|
+
def _setup_client(self):
|
|
113
166
|
from kubernetes import client
|
|
114
167
|
|
|
115
168
|
context = self.context
|
|
169
|
+
k8s = context.k8s
|
|
116
170
|
|
|
117
|
-
|
|
118
|
-
version = client.VersionApi(
|
|
171
|
+
k8s.client = self._setup_k8s_client()
|
|
172
|
+
version = client.VersionApi(k8s.client).get_code()
|
|
119
173
|
if "-eks-" in version.git_version:
|
|
120
174
|
git_version = version.git_version.split("-")[0]
|
|
121
175
|
else:
|
|
122
176
|
git_version = version.git_version
|
|
123
177
|
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
logger.info("Found Kubernetes %s on %s", version.git_version, context.k8s.client.configuration.host)
|
|
178
|
+
k8s.server_version = git_version[1:].split(".")
|
|
179
|
+
k8s.server_git_version = git_version
|
|
127
180
|
|
|
128
|
-
logger.
|
|
129
|
-
|
|
130
|
-
k8s_def = load_remote_file(logger, f"https://raw.githubusercontent.com/kubernetes/kubernetes/"
|
|
131
|
-
f"{git_version}/api/openapi-spec/swagger.json",
|
|
132
|
-
FileType.JSON)
|
|
133
|
-
self.resource_definitions_schema = k8s_def
|
|
134
|
-
|
|
135
|
-
self._populate_resource_definitions()
|
|
181
|
+
logger.info("Found Kubernetes %s on %s", k8s.server_git_version, k8s.client.configuration.host)
|
|
182
|
+
K8SResource._k8s_client_version = tuple(map(int, pkg_version("kubernetes").split(".")))
|
|
136
183
|
|
|
137
184
|
def handle_before_dir(self, cwd: Path):
|
|
138
185
|
context = self.context
|
|
@@ -337,6 +337,8 @@ class K8SResourceKey(namedtuple("K8SResourceKey", ["group", "kind", "name", "nam
|
|
|
337
337
|
|
|
338
338
|
|
|
339
339
|
class K8SResource:
|
|
340
|
+
_k8s_client_version = None
|
|
341
|
+
|
|
340
342
|
def __init__(self, manifest: dict, rdef: K8SResourceDef, source: Union[str, Path] = None):
|
|
341
343
|
self.key = self.get_manifest_key(manifest)
|
|
342
344
|
|
|
@@ -413,7 +415,7 @@ class K8SResource:
|
|
|
413
415
|
rdef = self.rdef
|
|
414
416
|
kwargs = {"name": self.name,
|
|
415
417
|
"body": json_patch
|
|
416
|
-
if patch_type != K8SResourcePatchType.SERVER_SIDE_PATCH
|
|
418
|
+
if patch_type != K8SResourcePatchType.SERVER_SIDE_PATCH or self._k8s_client_version[0] > 24
|
|
417
419
|
else json.dumps(json_patch),
|
|
418
420
|
"_preload_content": False,
|
|
419
421
|
"field_manager": "kubernator",
|
|
@@ -49,7 +49,7 @@ class KubectlPlugin(KubernatorPlugin):
|
|
|
49
49
|
def set_context(self, context):
|
|
50
50
|
self.context = context
|
|
51
51
|
|
|
52
|
-
def
|
|
52
|
+
def stanza(self):
|
|
53
53
|
context = self.context.kubectl
|
|
54
54
|
return [context.kubectl_file, f"--kubeconfig={context.kubeconfig}"]
|
|
55
55
|
|
|
@@ -73,7 +73,7 @@ class KubectlPlugin(KubernatorPlugin):
|
|
|
73
73
|
kubectl_file = str(Path(self.kubectl_dir.name) / "kubectl")
|
|
74
74
|
copy(kubectl_file_dl, kubectl_file)
|
|
75
75
|
os.chmod(kubectl_file, 0o500)
|
|
76
|
-
prepend_os_path(
|
|
76
|
+
prepend_os_path(self.kubectl_dir.name)
|
|
77
77
|
else:
|
|
78
78
|
# Use current version
|
|
79
79
|
kubectl_file = which("kubectl")
|
|
@@ -85,14 +85,14 @@ class KubectlPlugin(KubernatorPlugin):
|
|
|
85
85
|
context.globals.kubectl = dict(version=version,
|
|
86
86
|
kubeconfig=kubeconfig,
|
|
87
87
|
kubectl_file=kubectl_file,
|
|
88
|
-
|
|
88
|
+
stanza=self.stanza,
|
|
89
89
|
test=self.test_kubectl
|
|
90
90
|
)
|
|
91
91
|
|
|
92
92
|
context.globals.kubectl.version = context.kubectl.test()
|
|
93
93
|
|
|
94
94
|
def test_kubectl(self):
|
|
95
|
-
version_out: str = self.context.app.run_capturing_out(self.
|
|
95
|
+
version_out: str = self.context.app.run_capturing_out(self.stanza() +
|
|
96
96
|
["version", "--client=true", "-o", "json"],
|
|
97
97
|
stderr_logger)
|
|
98
98
|
|
|
@@ -100,7 +100,7 @@ class KubectlPlugin(KubernatorPlugin):
|
|
|
100
100
|
kubectl_version = version_out_js["clientVersion"]["gitVersion"][1:]
|
|
101
101
|
|
|
102
102
|
logger.info("Using kubectl %r version %r with stanza %r",
|
|
103
|
-
self.context.kubectl.kubectl_file, kubectl_version, self.
|
|
103
|
+
self.context.kubectl.kubectl_file, kubectl_version, self.stanza())
|
|
104
104
|
|
|
105
105
|
return kubectl_version
|
|
106
106
|
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
# -*- coding: utf-8 -*-
|
|
2
|
+
#
|
|
3
|
+
# Copyright 2020 Express Systems USA, Inc
|
|
4
|
+
# Copyright 2023 Karellen, Inc.
|
|
5
|
+
#
|
|
6
|
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
+
# you may not use this file except in compliance with the License.
|
|
8
|
+
# You may obtain a copy of the License at
|
|
9
|
+
#
|
|
10
|
+
# http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
+
#
|
|
12
|
+
# Unless required by applicable law or agreed to in writing, software
|
|
13
|
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
+
# See the License for the specific language governing permissions and
|
|
16
|
+
# limitations under the License.
|
|
17
|
+
#
|
|
18
|
+
|
|
19
|
+
import logging
|
|
20
|
+
import os
|
|
21
|
+
import tempfile
|
|
22
|
+
from pathlib import Path
|
|
23
|
+
|
|
24
|
+
from kubernator.api import (KubernatorPlugin,
|
|
25
|
+
StripNL,
|
|
26
|
+
get_golang_os,
|
|
27
|
+
get_golang_machine,
|
|
28
|
+
prepend_os_path,
|
|
29
|
+
get_cache_dir,
|
|
30
|
+
CalledProcessError
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
logger = logging.getLogger("kubernator.minikube")
|
|
34
|
+
proc_logger = logger.getChild("proc")
|
|
35
|
+
stdout_logger = StripNL(proc_logger.info)
|
|
36
|
+
stderr_logger = StripNL(proc_logger.warning)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class MinikubePlugin(KubernatorPlugin):
|
|
40
|
+
logger = logger
|
|
41
|
+
|
|
42
|
+
_name = "minikube"
|
|
43
|
+
|
|
44
|
+
def __init__(self):
|
|
45
|
+
self.context = None
|
|
46
|
+
self.minikube_dir = None
|
|
47
|
+
self.minikube_home_dir = None
|
|
48
|
+
self.kubeconfig_dir = None
|
|
49
|
+
|
|
50
|
+
super().__init__()
|
|
51
|
+
|
|
52
|
+
def set_context(self, context):
|
|
53
|
+
self.context = context
|
|
54
|
+
|
|
55
|
+
def get_latest_minikube_version(self):
|
|
56
|
+
context = self.context
|
|
57
|
+
versions = context.app.run_capturing_out(["git", "ls-remote", "-t", "--refs",
|
|
58
|
+
"https://github.com/kubernetes/minikube", "v*"],
|
|
59
|
+
stderr_logger)
|
|
60
|
+
|
|
61
|
+
# 06e3b0cf7999f74fc52af362b42fb21076ade64a refs/tags/v1.9.1
|
|
62
|
+
# "refs/tags/v1.9.1"
|
|
63
|
+
# "1.9.1"
|
|
64
|
+
# ("1","9","1")
|
|
65
|
+
# (1, 9, 1)
|
|
66
|
+
# sort and get latest, which is the last/highest
|
|
67
|
+
# "v1.9.1"
|
|
68
|
+
return (".".join(map(str, sorted(list(map(lambda v: tuple(map(int, v)),
|
|
69
|
+
filter(lambda v: len(v) == 3,
|
|
70
|
+
map(lambda line: line.split()[1][11:].split("."),
|
|
71
|
+
versions.splitlines(False))))))[-1])))
|
|
72
|
+
|
|
73
|
+
def cmd(self, *extra_args):
|
|
74
|
+
stanza, env = self._stanza(list(extra_args))
|
|
75
|
+
return self.context.app.run(stanza, stdout_logger, stderr_logger, env=env).wait()
|
|
76
|
+
|
|
77
|
+
def cmd_out(self, *extra_args):
|
|
78
|
+
stanza, env = self._stanza(list(extra_args))
|
|
79
|
+
return self.context.app.run_capturing_out(stanza, stderr_logger, env=env)
|
|
80
|
+
|
|
81
|
+
def _stanza(self, extra_args):
|
|
82
|
+
context = self.context
|
|
83
|
+
minikube = context.minikube
|
|
84
|
+
stanza = [context.minikube.minikube_file, "-p", f"minikube-{minikube.profile}"] + extra_args
|
|
85
|
+
env = dict(os.environ)
|
|
86
|
+
env["MINIKUBE_HOME"] = str(self.minikube_home_dir)
|
|
87
|
+
env["KUBECONFIG"] = str(minikube.kubeconfig)
|
|
88
|
+
return stanza, env
|
|
89
|
+
|
|
90
|
+
def register(self, minikube_version=None, profile="default", k8s_version=None,
|
|
91
|
+
keep_running=False, start_fresh=False,
|
|
92
|
+
nodes=1, driver="docker", cpus="no-limit", extra_args=None):
|
|
93
|
+
context = self.context
|
|
94
|
+
|
|
95
|
+
context.app.register_plugin("kubeconfig")
|
|
96
|
+
|
|
97
|
+
if not k8s_version:
|
|
98
|
+
msg = "No Kubernetes version is specified for Minikube"
|
|
99
|
+
logger.critical(msg)
|
|
100
|
+
raise RuntimeError(msg)
|
|
101
|
+
|
|
102
|
+
if not minikube_version:
|
|
103
|
+
minikube_version = self.get_latest_minikube_version()
|
|
104
|
+
logger.info("No minikube version is specified, latest is %s", minikube_version)
|
|
105
|
+
|
|
106
|
+
minikube_dl_file, _ = context.app.download_remote_file(logger,
|
|
107
|
+
f"https://github.com/kubernetes/minikube/releases"
|
|
108
|
+
f"/download/v{minikube_version}/"
|
|
109
|
+
f"minikube-{get_golang_os()}-{get_golang_machine()}",
|
|
110
|
+
"bin")
|
|
111
|
+
|
|
112
|
+
os.chmod(minikube_dl_file, 0o500)
|
|
113
|
+
self.minikube_dir = tempfile.TemporaryDirectory()
|
|
114
|
+
minikube_file = Path(self.minikube_dir.name) / "minikube"
|
|
115
|
+
minikube_file.symlink_to(minikube_dl_file)
|
|
116
|
+
prepend_os_path(self.minikube_dir.name)
|
|
117
|
+
version_out: str = self.context.app.run_capturing_out([minikube_file, "version", "--short"],
|
|
118
|
+
stderr_logger).strip()
|
|
119
|
+
version = version_out[1:]
|
|
120
|
+
logger.info("Found minikube %s in %s", version, minikube_file)
|
|
121
|
+
|
|
122
|
+
profile_dir = get_cache_dir("minikube", profile)
|
|
123
|
+
self.minikube_home_dir = profile_dir / "home"
|
|
124
|
+
self.minikube_home_dir.mkdir(parents=True, exist_ok=True)
|
|
125
|
+
self.kubeconfig_dir = profile_dir / ".kube"
|
|
126
|
+
self.kubeconfig_dir.mkdir(parents=True, exist_ok=True)
|
|
127
|
+
|
|
128
|
+
context.globals.minikube = dict(version=version,
|
|
129
|
+
minikube_file=str(minikube_file),
|
|
130
|
+
profile=profile,
|
|
131
|
+
k8s_version=k8s_version,
|
|
132
|
+
start_fresh=start_fresh,
|
|
133
|
+
keep_running=keep_running,
|
|
134
|
+
nodes=nodes,
|
|
135
|
+
driver=driver,
|
|
136
|
+
cpus=cpus,
|
|
137
|
+
extra_args=extra_args or [],
|
|
138
|
+
kubeconfig=str(self.kubeconfig_dir / "config"),
|
|
139
|
+
cmd=self.cmd,
|
|
140
|
+
cmd_out=self.cmd_out
|
|
141
|
+
)
|
|
142
|
+
context.kubeconfig.kubeconfig = context.minikube.kubeconfig
|
|
143
|
+
|
|
144
|
+
logger.info("Minikube Home is %s", self.minikube_home_dir)
|
|
145
|
+
logger.info("Minikube Kubeconfig is %s", context.minikube.kubeconfig)
|
|
146
|
+
|
|
147
|
+
def minikube_is_running(self):
|
|
148
|
+
try:
|
|
149
|
+
out = self.cmd_out("status", "-o", "json")
|
|
150
|
+
logger.info("Minikube profile %r is running: %s", self.context.minikube.profile,
|
|
151
|
+
out.strip())
|
|
152
|
+
return True
|
|
153
|
+
except CalledProcessError as e:
|
|
154
|
+
logger.info("Minikube profile %r is not running: %s", self.context.minikube.profile,
|
|
155
|
+
e.output.strip())
|
|
156
|
+
return False
|
|
157
|
+
|
|
158
|
+
def minikube_start(self):
|
|
159
|
+
minikube = self.context.minikube
|
|
160
|
+
if not self.minikube_is_running():
|
|
161
|
+
logger.info("Starting minikube profile %r...", minikube.profile)
|
|
162
|
+
self.cmd("start",
|
|
163
|
+
"--driver", str(minikube.driver),
|
|
164
|
+
"--nodes", str(minikube.nodes),
|
|
165
|
+
"--cpus", str(minikube.cpus),
|
|
166
|
+
"--kubernetes-version", str(minikube.k8s_version),
|
|
167
|
+
"--wait", "apiserver")
|
|
168
|
+
else:
|
|
169
|
+
logger.warning("Minikube profile %r is already running!", minikube.profile)
|
|
170
|
+
|
|
171
|
+
logger.info("Updating minikube profile %r context", minikube.profile)
|
|
172
|
+
self.cmd("update-context")
|
|
173
|
+
|
|
174
|
+
def minikube_stop(self):
|
|
175
|
+
minikube = self.context.minikube
|
|
176
|
+
if self.minikube_is_running():
|
|
177
|
+
logger.info("Shutting down minikube profile %r...", minikube.profile)
|
|
178
|
+
self.cmd("stop", "-o", "json")
|
|
179
|
+
|
|
180
|
+
def minikube_delete(self):
|
|
181
|
+
minikube = self.context.minikube
|
|
182
|
+
self.minikube_stop()
|
|
183
|
+
logger.warning("Deleting minikube profile %r!", minikube.profile)
|
|
184
|
+
self.cmd("delete")
|
|
185
|
+
|
|
186
|
+
def handle_start(self):
|
|
187
|
+
minikube = self.context.minikube
|
|
188
|
+
if minikube.start_fresh:
|
|
189
|
+
self.minikube_delete()
|
|
190
|
+
|
|
191
|
+
self.minikube_start()
|
|
192
|
+
|
|
193
|
+
def handle_shutdown(self):
|
|
194
|
+
minikube = self.context.minikube
|
|
195
|
+
if not minikube.keep_running:
|
|
196
|
+
self.minikube_stop()
|
|
197
|
+
else:
|
|
198
|
+
logger.warning("Will keep minikube profile %s running!", minikube.profile)
|
|
199
|
+
|
|
200
|
+
def __repr__(self):
|
|
201
|
+
return "Minikube Plugin"
|
|
@@ -51,7 +51,7 @@ class TerraformPlugin(KubernatorPlugin):
|
|
|
51
51
|
def set_context(self, context):
|
|
52
52
|
self.context = context
|
|
53
53
|
|
|
54
|
-
def
|
|
54
|
+
def stanza(self):
|
|
55
55
|
return [self.tf_file]
|
|
56
56
|
|
|
57
57
|
def register(self, version=None):
|
|
@@ -71,7 +71,7 @@ class TerraformPlugin(KubernatorPlugin):
|
|
|
71
71
|
tf_zip.extractall(self.tf_dir.name)
|
|
72
72
|
|
|
73
73
|
os.chmod(tf_file, 0o500)
|
|
74
|
-
prepend_os_path(
|
|
74
|
+
prepend_os_path(self.tf_dir.name)
|
|
75
75
|
else:
|
|
76
76
|
# Use current version
|
|
77
77
|
tf_file = which("terraform")
|
|
@@ -95,7 +95,7 @@ class TerraformPlugin(KubernatorPlugin):
|
|
|
95
95
|
|
|
96
96
|
context.globals.terraform = dict(version=version,
|
|
97
97
|
tf_file=self.tf_file,
|
|
98
|
-
|
|
98
|
+
stanza=self.stanza,
|
|
99
99
|
)
|
|
100
100
|
|
|
101
101
|
logger.info("Found Terraform version %s at %s", version, self.tf_file)
|
|
@@ -127,10 +127,10 @@ class TerraformPlugin(KubernatorPlugin):
|
|
|
127
127
|
if not tf_detected:
|
|
128
128
|
return
|
|
129
129
|
|
|
130
|
-
context.app.run(self.
|
|
130
|
+
context.app.run(self.stanza() + ["init", "-reconfigure", "-input=false", "-upgrade=false"],
|
|
131
131
|
stdout_logger, stderr_logger, cwd=cwd).wait()
|
|
132
132
|
|
|
133
|
-
output = json.loads(context.app.run_capturing_out(self.
|
|
133
|
+
output = json.loads(context.app.run_capturing_out(self.stanza() + ["output", "-json"],
|
|
134
134
|
stderr_logger, cwd=cwd))
|
|
135
135
|
if not output:
|
|
136
136
|
raise RuntimeError("Terraform output produced no values. Please check if Terraform is functioning.")
|
|
@@ -49,7 +49,7 @@ class TerragruntPlugin(KubernatorPlugin):
|
|
|
49
49
|
def set_context(self, context):
|
|
50
50
|
self.context = context
|
|
51
51
|
|
|
52
|
-
def
|
|
52
|
+
def stanza(self):
|
|
53
53
|
return [self.tg_file]
|
|
54
54
|
|
|
55
55
|
def register(self, version=None):
|
|
@@ -66,7 +66,7 @@ class TerragruntPlugin(KubernatorPlugin):
|
|
|
66
66
|
tg_file = Path(self.tg_dir.name) / "terragrunt"
|
|
67
67
|
copy(tg_file_cache, tg_file)
|
|
68
68
|
os.chmod(tg_file, 0o500)
|
|
69
|
-
prepend_os_path(
|
|
69
|
+
prepend_os_path(self.tg_dir.name)
|
|
70
70
|
else:
|
|
71
71
|
# Use current version
|
|
72
72
|
tg_file = which("terragrunt")
|
|
@@ -81,7 +81,7 @@ class TerragruntPlugin(KubernatorPlugin):
|
|
|
81
81
|
version = version_out.split(" ")[-1][1:].strip()
|
|
82
82
|
context.globals.terragrunt = dict(version=version,
|
|
83
83
|
tg_file=self.tg_file,
|
|
84
|
-
|
|
84
|
+
stanza=self.stanza,
|
|
85
85
|
)
|
|
86
86
|
|
|
87
87
|
logger.info("Found Terragrunt version %s at %s", version, self.tg_file)
|
|
@@ -113,10 +113,10 @@ class TerragruntPlugin(KubernatorPlugin):
|
|
|
113
113
|
if not tg_detected:
|
|
114
114
|
return
|
|
115
115
|
|
|
116
|
-
context.app.run(self.
|
|
116
|
+
context.app.run(self.stanza() + ["run-all", "init", "-reconfigure", "-input=false", "-upgrade=false"],
|
|
117
117
|
stdout_logger, stderr_logger, cwd=cwd).wait()
|
|
118
118
|
|
|
119
|
-
output_json = context.app.run_capturing_out(self.
|
|
119
|
+
output_json = context.app.run_capturing_out(self.stanza() + ["run-all", "output", "-json"],
|
|
120
120
|
stderr_logger, cwd=cwd)
|
|
121
121
|
|
|
122
122
|
json_decoder = json.JSONDecoder()
|
|
@@ -70,13 +70,16 @@ class ProcessRunner:
|
|
|
70
70
|
safe_args=None, universal_newlines=True, **kwargs):
|
|
71
71
|
self._safe_args = safe_args or args
|
|
72
72
|
logger.trace("Starting %r", self._safe_args)
|
|
73
|
+
|
|
74
|
+
if "env" not in kwargs:
|
|
75
|
+
kwargs["env"] = os.environ
|
|
76
|
+
|
|
73
77
|
self._proc = Popen(args,
|
|
74
78
|
stdout=PIPE if isinstance(stdout, Callable) else (stdout if stdout is not None else DEVNULL),
|
|
75
79
|
stderr=PIPE if isinstance(stderr, Callable) else (stderr if stderr is not None else DEVNULL),
|
|
76
80
|
stdin=PIPE if isinstance(stdin, (Callable, bytes, str)) else
|
|
77
81
|
(stdin if stdin is not None else DEVNULL),
|
|
78
82
|
universal_newlines=universal_newlines,
|
|
79
|
-
env=os.environ if "env" not in kwargs else kwargs["env"],
|
|
80
83
|
**kwargs)
|
|
81
84
|
|
|
82
85
|
self._stdin_writer = (spawn(partial(stream_writer_text if universal_newlines else stream_writer_buf,
|
|
@@ -21,7 +21,7 @@ class install(_install):
|
|
|
21
21
|
if __name__ == '__main__':
|
|
22
22
|
setup(
|
|
23
23
|
name = 'kubernator',
|
|
24
|
-
version = '1.0.
|
|
24
|
+
version = '1.0.6',
|
|
25
25
|
description = 'Kubernator is the a pluggable framework for K8S provisioning',
|
|
26
26
|
long_description = '# Kubernator\n\nKubernator™ (Ktor™) is an integrated solution for the Kubernetes state management. It operates on directories,\nprocessing their content via a collection of plugins, generating Kubernetes resources in the process, validating them,\ntransforming them and then applying against the Kubernetes cluster.\n\n[](https://gitter.im/karellen/Lobby)\n[](https://github.com/karellen/kubernator/actions/workflows/kubernator.yml)\n[](https://coveralls.io/r/karellen/kubernator?branch=master)\n\n[](https://pypi.org/project/kubernator/)\n[](https://pypi.org/project/kubernator/)\n[](https://pypi.org/project/kubernator/)\n[](https://pypi.org/project/kubernator/)\n[](https://pypi.org/project/kubernator/)\n\n## Notices\n\n### Beta Software\n\nWhile fully functional in the current state and used in production, this software is in **BETA**. A lot of things\nare expected to change rapidly, including main APIs, initialization procedures and some core features. Documentation at\nthis stage is basically non-existent.\n\n### License\n\nThe product is licensed under the Apache License, Version 2.0. Please see LICENSE for further details.\n\n### Warranties and Liability\n\nKubernator and its plugins are provided on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either\nexpress or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT,\nMERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of\nusing or redistributing Kubernator and assume any risks associated with doing so.\n\n### Trademarks\n\n"Kubernator" and "Ktor" are trademarks or registered trademarks of Express Systems USA, Inc and Karellen, Inc. All other\ntrademarks are property of their respective owners.\n\n## Problem Statement\n\n## Solution\n\n## Using Kubernator with Docker\n\nA simple example is as follows:\n```\n$ docker run --mount type=bind,source="$(pwd)",target=/root,readonly -t ghcr.io/karellen/kubernator:latest\n```\n\nPlease note, that some plugins (e.g. `awscli`, `eks`) may require additional volume mounts or environmental\nvariables to be passed for credentials and other external configuration.\n\n## Mode of Operation\n\nKubernator is a command line utility. Upon startup and processing of the command line arguments and initializing\nlogging, Kubernator initializes plugins. Current plugins include:\n\n0. Kubernator App\n1. Terraform\n2. kOps\n3. Kubernetes\n4. Helm\n5. Template\n\nThe order of initialization matters as it\'s the order the plugin handlers are executed!\n\nThe entire application operates in the following stages by invoking each plugin\'s stage handler in sequence:\n\n1. Plugin Init Stage\n2. Pre-start script (if specified)\n3. Plugin Start Stage\n4. For each directory in the pipeline:\n 1. Plugin Before Directory Stage\n 2. If `.kubernator.py` is present in the directory:\n 1. Plugin Before Script Stage\n 2. `.kubernator.py` script\n 3. Plugin After Script Stage\n 3. Plugin After Directory Stage\n5. Plugin End Stage\n\nEach plugin individually plays a specific role and performs a specific function which will be described in a later\nsection.\n\n## State/Context\n\nThere is a global state that is carried through as the application is running. It is a hierarchy of objects (`context`)\nthat follows the parent-child relationship as the application traverses the directory structure. For example, given the\ndirectory structure `/a/b`, `/a/c`, and `/a/c/d` any value of the context set or modified in context scoped to\ndirectory `/a` is visible in directories `/a/b`, `/a/c` and `/a/c/d`, while the same modified or set in `/a/b` is only\nvisible there, while one in `/a/c` is visible in `/a/c` and in `/a/c/d` but not `/a` or `/a/b`.\n\nAdditionally, there is a `context.globals` which is the top-most context that is available in all stages that are not\nassociated with the directory structure.\n\nNote, that in cases where the directory structure traversal moves to remote directories (that are actualized by local\ntemporary directories), such remote directory structure enters the context hierarchy as a child of the directory in\nwhich remote was registered.\n\nAlso note, that context carries not just data by references to essential functions.\n\nIn pre-start and `.kubernator.py` scripts the context is fully available as a global variable `ktor`.\n\n### Plugins\n\n#### Kubernator App Plugin\n\nThe role of the Kubernator App Plugin is to traverse the directory structure, expose essential functions through context\nand to run Kubernator scripts.\n\nIn the *After Directory Stage* Kubernator app scans the directories immediately available in the current, sorts them in\nthe alphabetic order, excludes those matching any of the patterns in `context.app.excludes` and then queues up the\nremaining directories in the order the match the patterns in `context.app.includes`.\n\nThus, for a directory content `/a/foo`, `/a/bal`, `/a/bar`, `/a/baz`, excludes `f*`, and includes `baz` and `*`, the\nresulting queue of directories to traverse will be `/a/baz`, `/a/bal`, `/a/bar`.\n\nNotice, that user can further interfere with processing order of the directory queue by asking Kubernator to walk\narbitrary paths, both local and remote.\n\n##### Context\n\n* `ktor.app.args`\n > Namespace containing command line argument values\n* `ktor.app.walk_local(*paths: Union[Path, str, bytes])`\n > Immediately schedules the paths to be traversed after the current directory by adding them to the queue\n > Relative path is relative to the current directory\n* `ktor.app.walk_remote(repo, *path_prefixes: Union[Path, str, bytes])`\n > Immediately schedules the path prefixes under the remote repo URL to be traversed after the current directory by\n > adding them to the queue. Only Git URLs are currently supported.\n > All absolute path prefixes are relativized based on the repository.\n* `ktor.app.repository_credentials_provider(func: Callable)`\n > Sets a repository credentials provider function `func` that sets/overwrites credentials for URLs being specified by\n > `walk_remote`. The callable `func` accepts a single argument containing a parsed URL in a form of tuple. The `func`\n > is expected to return a tuple of three elements representing URL schema, username and password. If the value should\n > not be changed it should be None. To convert from `git://repo.com/hello` to HTTPS authentication one should write\n > a function returning `("https", "username", "password")`. The best utility is achieved by logic that allows running\n > the plan both in CI and local environments using different authentication mechanics in different environments.\n\n#### Terraform\n\nThis is exclusively designed to pull the configuration options out of Terraform and to allow scripts and plugins to\nutilize that data.\n\n##### Context\n\n* `ktor.tf`\n > A dictionary containing the values from Terraform output\n\n#### Kops\n\n##### Context\n\n#### Kubernetes\n\n##### Context\n\n#### Helm\n\n##### Context\n\n#### Templates\n\n##### Context\n\n## Examples\n\n### Adding Remote Directory\n\n```python\nktor.app.repository_credentials_provider(lambda r: ("ssh", "git", None))\nktor.app.walk_remote("git://repo.example.com/org/project?ref=dev", "/project")\n```\n\n### Adding Local Directory\n\n```python\nktor.app.walk_local("/home/username/local-dir")\n```\n\n### Using Transformers\n\n```python\ndef remove_replicas(resources, r: "K8SResource"):\n if (r.group == "apps" and r.kind in ("StatefulSet", "Deployment")\n and "replicas" in r.manifest["spec"]):\n logger.warning("Resource %s in %s contains `replica` specification that will be removed. Use HPA!!!",\n r, r.source)\n del r.manifest["spec"]["replicas"]\n\n\nktor.k8s.add_transformer(remove_replicas)\n```\n',
|
|
27
27
|
long_description_content_type = 'text/markdown',
|
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
# -*- coding: utf-8 -*-
|
|
2
|
-
#
|
|
3
|
-
# Copyright 2020 Express Systems USA, Inc
|
|
4
|
-
# Copyright 2023 Karellen, Inc.
|
|
5
|
-
#
|
|
6
|
-
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
7
|
-
# you may not use this file except in compliance with the License.
|
|
8
|
-
# You may obtain a copy of the License at
|
|
9
|
-
#
|
|
10
|
-
# http://www.apache.org/licenses/LICENSE-2.0
|
|
11
|
-
#
|
|
12
|
-
# Unless required by applicable law or agreed to in writing, software
|
|
13
|
-
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
14
|
-
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
15
|
-
# See the License for the specific language governing permissions and
|
|
16
|
-
# limitations under the License.
|
|
17
|
-
#
|
|
18
|
-
|
|
19
|
-
import logging
|
|
20
|
-
import os
|
|
21
|
-
|
|
22
|
-
from kubernator.api import (KubernatorPlugin,
|
|
23
|
-
StripNL,
|
|
24
|
-
get_golang_os,
|
|
25
|
-
get_golang_machine
|
|
26
|
-
)
|
|
27
|
-
|
|
28
|
-
logger = logging.getLogger("kubernator.minikube")
|
|
29
|
-
proc_logger = logger.getChild("proc")
|
|
30
|
-
stdout_logger = StripNL(proc_logger.info)
|
|
31
|
-
stderr_logger = StripNL(proc_logger.warning)
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
class MinikubePlugin(KubernatorPlugin):
|
|
35
|
-
logger = logger
|
|
36
|
-
|
|
37
|
-
_name = "minikube"
|
|
38
|
-
|
|
39
|
-
def __init__(self):
|
|
40
|
-
self.context = None
|
|
41
|
-
self.minikube_home_dir = None
|
|
42
|
-
self.kubeconfig_dir = None
|
|
43
|
-
|
|
44
|
-
super().__init__()
|
|
45
|
-
|
|
46
|
-
def set_context(self, context):
|
|
47
|
-
self.context = context
|
|
48
|
-
|
|
49
|
-
def get_latest_minikube_version(self):
|
|
50
|
-
context = self.context
|
|
51
|
-
versions = context.app.run_capturing_out(["git", "ls-remote", "-t", "--refs",
|
|
52
|
-
"https://github.com/kubernetes/minikube", "v*"],
|
|
53
|
-
stderr_logger)
|
|
54
|
-
|
|
55
|
-
# 06e3b0cf7999f74fc52af362b42fb21076ade64a refs/tags/v1.9.1
|
|
56
|
-
# "refs/tags/v1.9.1"
|
|
57
|
-
# "1.9.1"
|
|
58
|
-
# ("1","9","1")
|
|
59
|
-
# (1, 9, 1)
|
|
60
|
-
# sort and get latest, which is the last/highest
|
|
61
|
-
# "v1.9.1"
|
|
62
|
-
return (".".join(map(str, sorted(list(map(lambda v: tuple(map(int, v)),
|
|
63
|
-
filter(lambda v: len(v) == 3,
|
|
64
|
-
map(lambda line: line.split()[1][11:].split("."),
|
|
65
|
-
versions.splitlines(False))))))[-1])))
|
|
66
|
-
|
|
67
|
-
def cmd(self, ):
|
|
68
|
-
pass
|
|
69
|
-
|
|
70
|
-
def register(self, minikube_version=None, k8s_version=None):
|
|
71
|
-
context = self.context
|
|
72
|
-
|
|
73
|
-
context.app.register_plugin("kubectl")
|
|
74
|
-
|
|
75
|
-
if not minikube_version:
|
|
76
|
-
minikube_version = self.get_latest_minikube_version()
|
|
77
|
-
logger.info("No minikube version is specified, latest is %s", minikube_version)
|
|
78
|
-
|
|
79
|
-
minikube_file = context.app.download_remote_file(logger,
|
|
80
|
-
f"https://github.com/kubernetes/minikube/releases/download/"
|
|
81
|
-
f"v{minikube_version}/"
|
|
82
|
-
f"minikube-{get_golang_os()}-{get_golang_machine()}", "bin")
|
|
83
|
-
os.chmod(minikube_file, 0o500)
|
|
84
|
-
version_out: str = self.context.app.run_capturing_out([minikube_file, "version", "--short"],
|
|
85
|
-
stderr_logger)
|
|
86
|
-
version = version_out[1:]
|
|
87
|
-
logger.info("Found minikube %s in %r", version, minikube_file)
|
|
88
|
-
|
|
89
|
-
context.globals.minikube = dict(version=version,
|
|
90
|
-
minikube_file=minikube_file,
|
|
91
|
-
cmd=self.cmd
|
|
92
|
-
)
|
|
93
|
-
|
|
94
|
-
def handle_init(self):
|
|
95
|
-
pass
|
|
96
|
-
|
|
97
|
-
def handle_shutdown(self):
|
|
98
|
-
pass
|
|
99
|
-
|
|
100
|
-
def __repr__(self):
|
|
101
|
-
return "AWS CLI Plugin"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|