-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
503 additions
and
53 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# flake8: noqa | ||
import os | ||
|
||
ktor.app.register_plugin("minikube", k8s_version=os.environ["K8S_VERSION"], | ||
start_fresh=True, keep_running=True, profile="issue-48") | ||
ktor.app.register_plugin("k8s") | ||
|
||
if False: | ||
_old_req = ktor.k8s.client.rest_client.pool_manager.request | ||
|
||
|
||
def request(method, url, fields=None, headers=None, **urlopen_kw): | ||
resp = _old_req(method, url, fields=fields, headers=headers, **urlopen_kw) | ||
logger.info("Send:\n%s %s\n\n%s\n\n%s", | ||
method, url, "\n".join(map(lambda t: "%s: %s" % (t[0], t[1]), headers.items())), | ||
urlopen_kw.get("body", "")) | ||
logger.info("Recv:\n%s %s\n\n%s\n\n%s", | ||
resp.status, resp.reason, "\n".join(map(lambda t: "%s: %s" % (t[0], t[1]), resp.headers.items())), | ||
resp.data.decode("utf-8")) | ||
return resp | ||
|
||
|
||
ktor.k8s.client.rest_client.pool_manager.request = request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: ns1 | ||
annotations: | ||
a: x | ||
--- | ||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: ns2 | ||
annotations: | ||
a: x |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# flake8: noqa | ||
import os | ||
|
||
ktor.app.register_plugin("minikube", k8s_version=os.environ["K8S_VERSION"], | ||
start_fresh=False, keep_running=False, profile="issue-48") | ||
ktor.app.register_plugin("k8s") | ||
|
||
if False: | ||
_old_req = ktor.k8s.client.rest_client.pool_manager.request | ||
|
||
|
||
def request(method, url, fields=None, headers=None, **urlopen_kw): | ||
resp = _old_req(method, url, fields=fields, headers=headers, **urlopen_kw) | ||
logger.info("Send:\n%s %s\n\n%s\n\n%s", | ||
method, url, "\n".join(map(lambda t: "%s: %s" % (t[0], t[1]), headers.items())), | ||
urlopen_kw.get("body", "")) | ||
logger.info("Recv:\n%s %s\n\n%s\n\n%s", | ||
resp.status, resp.reason, "\n".join(map(lambda t: "%s: %s" % (t[0], t[1]), resp.headers.items())), | ||
resp.data.decode("utf-8")) | ||
return resp | ||
|
||
|
||
ktor.k8s.client.rest_client.pool_manager.request = request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: ns1 | ||
annotations: | ||
$patch: replace | ||
b: y | ||
--- | ||
apiVersion: v1 | ||
kind: Namespace | ||
metadata: | ||
name: ns2 | ||
annotations: | ||
$patch: delete | ||
b: y | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright 2020 Express Systems USA, Inc | ||
# Copyright 2023 Karellen, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
from test_support import IntegrationTestSupport, unittest | ||
|
||
unittest # noqa | ||
# Above import must be first | ||
|
||
from pathlib import Path # noqa: E402 | ||
import os # noqa: E402 | ||
import tempfile # noqa: E402 | ||
import yaml # noqa: E402 | ||
from pprint import pprint # noqa: E402 | ||
|
||
|
||
class Issue48Test(IntegrationTestSupport): | ||
def test_issue_48(self): | ||
test_dir = Path(__file__).parent / "issue_48" | ||
test_dir_1 = test_dir / "phase1" | ||
test_dir_2 = test_dir / "phase2" | ||
with tempfile.TemporaryDirectory() as results_dir: | ||
results_file = Path(results_dir) / "results" | ||
for k8s_version in (self.K8S_TEST_VERSIONS[-1],): | ||
with self.subTest(k8s_version=k8s_version): | ||
os.environ["K8S_VERSION"] = k8s_version | ||
|
||
self.run_module_test("kubernator", "-p", str(test_dir_1), "-v", "TRACE", "apply", "--yes") | ||
self.run_module_test("kubernator", "-p", str(test_dir_2), | ||
"-v", "TRACE", "-f", str(results_file), "dump") | ||
|
||
with open(results_file, "rb") as f: | ||
results = list(yaml.safe_load_all(f)) | ||
pprint(results) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright 2020 Express Systems USA, Inc | ||
# Copyright 2024 Karellen, Inc. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
|
||
import re | ||
from functools import cache | ||
|
||
from jsonpath_ng import JSONPath, DatumInContext | ||
from jsonpath_ng.ext import parse as jp_parse, parser | ||
from jsonpath_ng.ext.string import DefintionInvalid | ||
|
||
__all__ = ["jp", "JPath"] | ||
|
||
|
||
class JPath: | ||
def __init__(self, pattern): | ||
self.pattern = jp_parse(pattern) | ||
|
||
def find(self, val): | ||
return self.pattern.find(val) | ||
|
||
def all(self, val): | ||
return list(map(lambda x: x.value, self.find(val))) | ||
|
||
def first(self, val): | ||
"""Returns the first element or None if it doesn't exist""" | ||
try: | ||
return next(map(lambda x: x.value, self.find(val))) | ||
except StopIteration: | ||
return None | ||
|
||
def only(self, val): | ||
"""Returns the first and only element. | ||
Raises ValueError if more than one value found | ||
Raises KeyError if no value found | ||
""" | ||
m = map(lambda x: x.value, self.find(val)) | ||
try: | ||
v = next(m) | ||
except StopIteration: | ||
raise KeyError("no value found") | ||
try: | ||
next(m) | ||
raise ValueError("more than one value returned") | ||
except StopIteration: | ||
return v | ||
|
||
|
||
@cache | ||
def jp(pattern) -> JPath: | ||
return JPath(pattern) | ||
|
||
|
||
MATCH = re.compile(r"match\(/(.*)(?<!\\)/\)") | ||
|
||
|
||
class Match(JSONPath): | ||
"""Direct node regex matcher | ||
Concrete syntax is '`match(/regex/)`' | ||
""" | ||
|
||
def __init__(self, method=None): | ||
m = MATCH.match(method) | ||
if m is None: | ||
raise DefintionInvalid("%s is not valid" % method) | ||
self.expr = m.group(1).strip() | ||
self.regex = re.compile(self.expr) | ||
self.method = method | ||
|
||
def find(self, datum): | ||
datum = DatumInContext.wrap(datum) | ||
|
||
if hasattr(datum.path, "fields") and self.regex.match(datum.path.fields[0]): | ||
return [datum] | ||
return [] | ||
|
||
def __eq__(self, other): | ||
return isinstance(other, Match) and self.method == other.method | ||
|
||
def __repr__(self): | ||
return '%s(%r)' % (self.__class__.__name__, self.method) | ||
|
||
def __str__(self): | ||
return '`match(/%s/)`' % (self.expr,) | ||
|
||
|
||
old_p_jsonpath_named_operator = parser.ExtentedJsonPathParser.p_jsonpath_named_operator | ||
|
||
|
||
def p_jsonpath_named_operator(self, p): | ||
"jsonpath : NAMED_OPERATOR" | ||
if p[1].startswith("match("): | ||
p[0] = Match(p[1]) | ||
else: | ||
old_p_jsonpath_named_operator(self, p) | ||
|
||
|
||
parser.ExtentedJsonPathParser.p_jsonpath_named_operator = p_jsonpath_named_operator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.