Beispiel #1
0
def check_logs(k8s_client):
    pod_status = 'Running'
    all_pods = k8s_client.list_pod_for_all_namespaces()

    for pod in all_pods.items:
        pod_name = pod.metadata.name
        namespace = pod.metadata.namespace

        for container in pod.spec.containers:
            if container.name in SKIP_CONTAINERS:
                continue

            if pod.status.phase != pod_status:
                # Wait for the Pod to be ready
                utils.retry(
                    kube_utils.check_pod_status(
                        k8s_client,
                        name=pod_name,
                        namespace=namespace,
                        state=pod_status
                    ),
                    times=12,
                    wait=5,
                    name="wait for Pod '{}'".format(pod_name),
                )
            logs = k8s_client.read_namespaced_pod_log(
                pod_name,
                namespace,
                container=container.name
            )

            assert logs.strip(), (
                "Couldn't find logs for container '{}' in Pod '{}' (status {})"
            ).format(container.name, pod_name, pod.status.phase)
Beispiel #2
0
def utils_pod(k8s_client, utils_image):
    # Create the Pod
    manifest_file = os.path.join(
        os.path.realpath(os.path.dirname(__file__)),
        "files",
        "utils.yaml"
    )
    with open(manifest_file, encoding='utf-8') as fd:
        manifest = yaml.safe_load(fd)

    manifest["spec"]["containers"][0]["image"] = utils_image
    pod_name = manifest["metadata"]["name"]

    k8s_client.create_namespaced_pod(body=manifest, namespace="default")

    # Wait for the Pod to be ready
    utils.retry(
        kube_utils.check_pod_status(
            k8s_client, name=pod_name, namespace="default", state="Running"
        ),
        times=10,
        wait=5,
        name="wait for Pod '{}'".format(pod_name),
    )

    yield pod_name

    # Clean-up resources
    k8s_client.delete_namespaced_pod(
        name=pod_name,
        namespace="default",
        body=client.V1DeleteOptions(
            grace_period_seconds=0,  # Force deletion instantly
        ),
    )
Beispiel #3
0
 def create_with_volume(self, volume_name, command):
     """Create a pod using the specified volume."""
     binary, *args = ast.literal_eval(command)
     body = POD_TEMPLATE.format(volume_name=volume_name,
                                image_name=self._image,
                                command=json.dumps(binary),
                                args=json.dumps(args))
     self.create_from_yaml(body)
     # Wait for the Pod to be up and running.
     pod_name = '{}-pod'.format(volume_name)
     utils.retry(kube_utils.check_pod_status(self._client, pod_name),
                 times=self._count,
                 wait=self._delay,
                 name="wait for pod {}".format(pod_name))
Beispiel #4
0
def check_static_pod_changed(host, hostname, k8s_client, static_pod_id):
    fullname = "{}-{}".format(DEFAULT_POD_NAME, hostname)

    wait_for_pod = kube_utils.check_pod_status(
        k8s_client,
        name=fullname,
        namespace="default",
    )

    def wait_for_pod_reloaded():
        pod = wait_for_pod()
        assert pod.metadata.uid != static_pod_id

    utils.retry(
        wait_for_pod_reloaded,
        times=12,
        wait=5,
        name="wait for Pod '{}' to be reloaded".format(fullname),
    )

    pod = k8s_client.read_namespaced_pod(name=fullname, namespace="default")

    assert pod.metadata.uid != static_pod_id
Beispiel #5
0
def set_up_static_pod(host, hostname, k8s_client, utils_image,
                      transient_files):
    manifest_path = str(MANIFESTS_PATH / "{}.yaml".format(DEFAULT_POD_NAME))

    with host.sudo():
        if host.file(manifest_path).exists:
            pytest.fail("Cannot set up static Pod with manifest at path '{}': "
                        "already exists".format(manifest_path))

    # A sample config file for the static Pod
    config_path = "/tmp/{}.conf".format(DEFAULT_POD_NAME)
    write_config = utils.write_string(host, config_path, '{"hello": "world"}')
    assert write_config.rc == 0, (
        "Failed to write static Pod config file at '{}': {}").format(
            config_path, write_config.stderr)

    transient_files.append(config_path)

    # The manifest template to use with Salt
    manifest_template = SOURCE_TEMPLATE.read_text(encoding="utf-8")
    manifest = string.Template(manifest_template).substitute(
        name=DEFAULT_POD_NAME,
        image=utils_image,
        config_path=config_path,
    )

    with host.sudo():
        host.run_test("mkdir -p %s", str(TEMPLATES_PATH))

        template_path = str(TEMPLATES_PATH /
                            "{}.yaml.j2".format(DEFAULT_POD_NAME))
        write_template = utils.write_string(host, template_path, manifest)
        assert write_template.rc == 0, (
            "Failed to create static Pod manifest template '{}': {}").format(
                template_path, write_template.stderr)

    transient_files.append(template_path)

    # Use Salt to generate the effective static Pod manifest
    _manage_static_pod(host, manifest_path, template_path, config_path)

    assert host.file(manifest_path).exists, (
        "Something went wrong: "
        "static Pod manifest could not be found after set-up")

    # We want to remove the manifest before the config file, since kubelet
    # may try to mount it and, when not found, create a directory (bug).
    # See: https://github.com/kubernetes/kubernetes/issues/65825
    transient_files.insert(0, manifest_path)

    fullname = "{}-{}".format(DEFAULT_POD_NAME, hostname)

    utils.retry(
        kube_utils.check_pod_status(k8s_client, fullname),
        times=10,
        wait=5,
        name="wait for Pod '{}'".format(fullname),
    )

    pod = k8s_client.read_namespaced_pod(name=fullname, namespace="default")
    return pod.metadata.uid