-
Notifications
You must be signed in to change notification settings - Fork 23
test: kubectl-dns secret-generation command #841
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| """Wrapper around kubectl-dns binary""" | ||
|
|
||
| import os | ||
| import subprocess | ||
|
|
||
|
|
||
| class KubectlDNS: | ||
| """Wrapper on top of kubectl-dns binary""" | ||
|
|
||
| def __init__(self, binary) -> None: | ||
| super().__init__() | ||
| self.binary = binary | ||
|
|
||
| def run(self, *args, **kwargs): | ||
| """Passes arguments to subprocess.run()""" | ||
| args = (self.binary, *args) | ||
| kwargs.setdefault("capture_output", True) | ||
| kwargs.setdefault("text", True) | ||
|
|
||
| if "env" in kwargs: | ||
| env = os.environ.copy() | ||
| env.update(kwargs["env"]) | ||
| kwargs["env"] = env | ||
|
|
||
| return subprocess.run(args, **kwargs) # pylint: disable= subprocess-run-check | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
|
|
||
| from functools import cached_property | ||
| from urllib.parse import urlparse | ||
| import tempfile | ||
| import yaml | ||
|
|
||
| import openshift_client as oc | ||
| from openshift_client import Context, OpenShiftPythonException | ||
|
|
@@ -45,6 +47,13 @@ def context(self): | |
|
|
||
| return context | ||
|
|
||
| @property | ||
| def current_context_name(self) -> str: | ||
| """Returns the current context name from the kubeconfig""" | ||
| if self._kubeconfig_path is None: | ||
| raise ValueError("Kubeconfig path is not set") | ||
| return self.do_action("config", "current-context").out().strip() | ||
|
|
||
| @property | ||
| def api_url(self): | ||
| """Returns real API url""" | ||
|
|
@@ -142,3 +151,25 @@ def apply_from_string(self, string, cls, cmd_args=None): | |
| obj = selector.object(cls=cls) | ||
| obj.context = self.context | ||
| return obj | ||
|
|
||
| def create_merged_kubeconfig(self, cluster2: "KubernetesClient") -> str: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Due to coredns tests we will probably deal with more then two clusters, maybe refactor this method to take list of additional clusters? wdyt?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure if we should have other tests running this command. I would leave it for when a new test is being developed |
||
| """ | ||
| Creates a merged kubeconfig from this instance and another KubernetesClient instance. | ||
| Returns the path to the temporary kubeconfig file. | ||
| """ | ||
| with self.context: | ||
| config1_yaml = oc.invoke("config", ["view", "--minify=true", "--flatten=true"]).out() | ||
| with cluster2.context: | ||
| config2_yaml = oc.invoke("config", ["view", "--minify=true", "--flatten=true"]).out() | ||
|
|
||
| config1 = yaml.safe_load(config1_yaml) | ||
| config2 = yaml.safe_load(config2_yaml) | ||
|
|
||
| merged_config = config1.copy() | ||
| merged_config["clusters"].extend(config2.get("clusters", [])) | ||
| merged_config["contexts"].extend(config2.get("contexts", [])) | ||
| merged_config["users"].extend(config2.get("users", [])) | ||
|
|
||
| with tempfile.NamedTemporaryFile(mode="w", suffix=".kubeconfig", delete=False) as temp_file: | ||
| yaml.safe_dump(merged_config, temp_file) | ||
| return temp_file.name | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| """Test kubectl-dns secret-generate command with basic coredns setup with 1 primary and 1 secondary clusters""" | ||
|
|
||
| import shutil | ||
|
|
||
| import dns.resolver | ||
| import pytest | ||
|
|
||
| from testsuite.cli.kubectl_dns import KubectlDNS | ||
| from testsuite.tests.multicluster.coredns.conftest import IP1, IP2 | ||
|
|
||
| pytestmark = [pytest.mark.cli] | ||
|
|
||
|
|
||
| @pytest.fixture(scope="session") | ||
| def kubectl_dns(testconfig, skip_or_fail): | ||
| """Return Kuadrantctl wrapper with merged kubeconfig""" | ||
| binary_path = testconfig["kubectl-dns"] | ||
| if not shutil.which(binary_path): | ||
averevki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| skip_or_fail("kubectl-dns binary not found") | ||
| return KubectlDNS(binary_path) | ||
|
|
||
|
|
||
| @pytest.fixture(scope="module") | ||
| def kubeconfig_secrets(request, testconfig, cluster, cluster2, kubectl_dns, blame): | ||
| """Run generate-secret command on merged kubeconfig to generate kubeconfig secret for the secondary cluster""" | ||
| system_project = testconfig["service_protection"]["system_project"] | ||
| secret_name = blame("kubecfg") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would use
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Binary creates this secret directly on cluster. I have finalizer set right under this comment |
||
| request.addfinalizer( | ||
| lambda: cluster.do_action("delete", "secret", secret_name, "-n", system_project, "--ignore-not-found") | ||
| ) | ||
|
|
||
| merged_kubeconfig = cluster.create_merged_kubeconfig(cluster2) | ||
| result = kubectl_dns.run( | ||
| "secret-generation", | ||
| "--name", | ||
| secret_name, | ||
| "--context", | ||
| cluster2.current_context_name, | ||
| "--namespace", | ||
| system_project, | ||
| "--service-account", | ||
| "coredns", | ||
| env={"KUBECONFIG": merged_kubeconfig}, | ||
| ) | ||
| assert result.returncode == 0, f"kubectl-dns couldn't generate kubeconfig secret: {result.stderr}" | ||
| return [] | ||
|
|
||
|
|
||
| def test_kubectl_dns_secret_generation(hostname): | ||
| """IPs from both, primary and secondary, clusters should return in DNS A record set""" | ||
| dns_ips = {ip.address for ip in dns.resolver.resolve(hostname.hostname)} | ||
| assert {IP1, IP2} == dns_ips, "CoreDNS should have returned both IP addresses in A record set" | ||
Uh oh!
There was an error while loading. Please reload this page.