[k8s] Adjust externaldns test functions
- Use ctl hostname instead of k8s.kube_host due kube_host
is an ip address of keepalived k8s api.
- Discover ctl with specific role 'k8s_controller'
- Fix nit with awk field
- Fix nit with format fields
User-Story:https://mirantis.jira.com/browse/PROD-12826
Change-Id: I52d89b4d3994338a800eed4f6b5555cad6bdedc8
diff --git a/tcp_tests/managers/k8smanager.py b/tcp_tests/managers/k8smanager.py
index d84fd73..87e3eaf 100644
--- a/tcp_tests/managers/k8smanager.py
+++ b/tcp_tests/managers/k8smanager.py
@@ -17,8 +17,11 @@
import yaml
from devops.helpers import helpers
+from devops.error import DevopsCalledProcessError
from tcp_tests import logger
+from tcp_tests.helpers import ext
+from tcp_tests.helpers.utils import retry
from tcp_tests.managers.execute_commands import ExecuteCommandsMixin
from tcp_tests.managers.k8s import cluster
from k8sclient.client.rest import ApiException
@@ -80,6 +83,12 @@
default_namespace='default')
return self._api_client
+ @property
+ def ctl_host(self):
+ nodes = [node for node in self.__config.underlay.ssh if
+ ext.UNDERLAY_NODE_ROLES.k8s_controller in node['roles']]
+ return nodes[0]['node_name']
+
def get_pod_phase(self, pod_name, namespace=None):
return self.api.pods.get(
name=pod_name, namespace=namespace).phase
@@ -296,7 +305,7 @@
params = ' '.join(["-f {}".format(p) for p in path])
cmd = 'kubectl create {params}'.format(params=params)
with self.__underlay.remote(
- host=self.__config.k8s.kube_host) as remote:
+ node_name=self.ctl_host) as remote:
LOG.info("Running command '{cmd}' on node {node}".format(
cmd=cmd,
node=remote.hostname)
@@ -315,7 +324,7 @@
def get_running_pods_by_ssh(self, pod_name, namespace=None):
with self.__underlay.remote(
- host=self.__config.k8s.kube_host) as remote:
+ node_name=self.ctl_host) as remote:
result = remote.check_call("kubectl get pods --namespace {} |"
" grep {} | awk '{{print $1 \" \""
" $3}}'".format(namespace,
@@ -331,7 +340,7 @@
def run_conformance(self, timeout=60 * 60):
with self.__underlay.remote(
- host=self.__config.k8s.kube_host) as remote:
+ node_name=self.ctl_host) as remote:
result = remote.check_call(
"docker run --rm --net=host -e API_SERVER="
"'http://127.0.0.1:8080' {}".format(
@@ -347,7 +356,7 @@
def kubectl_run(self, name, image, port):
with self.__underlay.remote(
- host=self.__config.k8s.kube_host) as remote:
+ node_name=self.ctl_host) as remote:
result = remote.check_call(
"kubectl run {0} --image={1} --port={2}".format(
name, image, port
@@ -357,7 +366,7 @@
def kubectl_expose(self, resource, name, port, type):
with self.__underlay.remote(
- host=self.__config.k8s.kube_host) as remote:
+ node_name=self.ctl_host) as remote:
result = remote.check_call(
"kubectl expose {0} {1} --port={2} --type={3}".format(
resource, name, port, type
@@ -365,27 +374,28 @@
)
return result
- def kubectl_annotate(self, resource, name, annotaion):
+ def kubectl_annotate(self, resource, name, annotation):
with self.__underlay.remote(
- host=self.__config.k8s.kube_host) as remote:
+ node_name=self.ctl_host) as remote:
result = remote.check_call(
- "kubectl annotate {0} {1} {3}".format(
- resource, name, annotaion
+ "kubectl annotate {0} {1} {2}".format(
+ resource, name, annotation
)
)
return result
- def get_svc_ip(self, name):
+ def get_svc_ip(self, name, namespace='kube-system'):
with self.__underlay.remote(
- host=self.__config.k8s.kube_host) as remote:
+ node_name=self.ctl_host) as remote:
result = remote.check_call(
- "kubectl get svc --all-namespaces | grep {0} | "
- "awk '{{print $2}}'".format(name)
+ "kubectl get svc {0} -n {1} | "
+ "awk '{{print $2}}' | tail -1".format(name, namespace)
)
return result['stdout'][0].strip()
+ @retry(300, exception=DevopsCalledProcessError)
def nslookup(self, host, src):
with self.__underlay.remote(
- host=self.__config.k8s.kube_host) as remote:
+ node_name=self.ctl_host) as remote:
remote.check_call("nslookup {0} {1}".format(host, src))