def run_expvar(): """expvar container fixture""" with run_service("expvar") as container: host = container_ip(container) assert wait_for(lambda: tcp_socket_open(host, 8080), 60), "service didn't start" yield host
def deploy(self, k8s_version, timeout=300, options=None): self.k8s_version = check_k8s_version(k8s_version) assert self.get_version(k8s_version), "failed to get minikube version" if options is None: options = {} options.setdefault("name", MINIKUBE_CONTAINER_NAME) try: self.host_client.containers.get(options["name"]).remove(force=True, v=True) except docker.errors.NotFound: pass options.setdefault("privileged", True) options.setdefault( "environment", {"K8S_VERSION": self.k8s_version, "TIMEOUT": str(timeout), "KUBECONFIG_PATH": MINIKUBE_KUBECONFIG_PATH}, ) if tcp_socket_open("127.0.0.1", self.registry_port): self.registry_port = get_free_port() options.setdefault("ports", {"%d/tcp" % self.registry_port: self.registry_port}) options.setdefault("detach", True) print("\nBuilding %s image ..." % self.image_tag) build_opts = dict( buildargs={"MINIKUBE_VERSION": self.version}, tag=self.image_tag, dockerfile=MINIKUBE_DOCKERFILE_PATH ) image_id = self.build_image(PROJECT_DIR, build_opts, "unix://var/run/docker.sock") print("\nDeploying minikube %s cluster ..." % self.k8s_version) self.container = self.host_client.containers.run(image_id, **options) self.container_name = self.container.name assert wait_for(self.is_running, timeout_seconds=30, interval_seconds=2), ( "timed out waiting for %s container" % self.container_name ) self.container.exec_run("start-minikube.sh", detach=True) self.connect_to_cluster(timeout) self.start_registry()
def fake_k8s_api_server(print_logs=False): """ Runs a fake k8s API server instance that supports generic get/list/watch/create/update operations. It does **not** do any of the controller functionality, such as creating pods based on a deployment, etc. """ with run_service( "fakek8s", print_logs=print_logs, path=REPO_ROOT_DIR, dockerfile="./test-services/fakek8s/Dockerfile") as fakek8s_cont: ipaddr = container_ip(fakek8s_cont) conf = client.Configuration() conf.host = f"https://{ipaddr}:8443" conf.verify_ssl = False assert wait_for(lambda: tcp_socket_open(ipaddr, 8443) ), "fake k8s never opened port" warnings.filterwarnings( "ignore", category=urllib3.exceptions.InsecureRequestWarning) yield [ client.ApiClient(conf), { "KUBERNETES_SERVICE_HOST": ipaddr, "KUBERNETES_SERVICE_PORT": "8443" } ]
def is_ready(self): def kubeconfig_exists(): try: return container_cmd_exit_0(self.container, "test -f %s" % MINIKUBE_KUBECONFIG_PATH) except requests.exceptions.RequestException as e: print("requests.exceptions.RequestException:\n%s" % str(e)) return False return self.is_running() and tcp_socket_open(self.container_ip, K8S_API_PORT) and kubeconfig_exists()
def fake_k8s_api_server(print_logs=False): with run_service( "fakek8s", print_logs=print_logs, path=REPO_ROOT_DIR, dockerfile="./test-services/fakek8s/Dockerfile" ) as fakek8s_cont: ipaddr = container_ip(fakek8s_cont) conf = client.Configuration() conf.host = f"https://{ipaddr}:8443" conf.verify_ssl = False assert wait_for(lambda: tcp_socket_open(ipaddr, 8443)), "fake k8s never opened port" warnings.filterwarnings("ignore", category=urllib3.exceptions.InsecureRequestWarning) yield [client.ApiClient(conf), {"KUBERNETES_SERVICE_HOST": ipaddr, "KUBERNETES_SERVICE_PORT": "8443"}]