示例#1
0
 def test_create_apiservice_from_yaml_with_conflict(self):
     """
     Should be able to create an API service.
     Should verify that creating the same API service should
     fail due to conflict.
     """
     k8s_client = client.api_client.ApiClient(configuration=self.config)
     utils.create_from_yaml(k8s_client,
                            self.path_prefix + "api-service.yaml")
     reg_api = client.ApiregistrationV1Api(k8s_client)
     svc = reg_api.read_api_service(name="v1alpha1.wardle.k8s.io")
     self.assertIsNotNone(svc)
     with self.assertRaises(utils.FailToCreateError) as cm:
         utils.create_from_yaml(
             k8s_client, "kubernetes/e2e_test/test_yaml/api-service.yaml")
     exp_error = ('Error from server (Conflict): '
                  '{"kind":"Status","apiVersion":"v1","metadata":{},'
                  '"status":"Failure",'
                  '"message":"apiservices.apiregistration.k8s.io '
                  '\\"v1alpha1.wardle.k8s.io\\" already exists",'
                  '"reason":"AlreadyExists",'
                  '"details":{"name":"v1alpha1.wardle.k8s.io",'
                  '"group":"apiregistration.k8s.io","kind":"apiservices"},'
                  '"code":409}\n')
     self.assertEqual(exp_error, str(cm.exception))
     reg_api.delete_api_service(name="v1alpha1.wardle.k8s.io", body={})
    def test_a_create_user_cluster_role(self):
        # create service account and cluster role bindings
        try:
            utils.create_from_yaml(
                aApiClient,
                filepath + "cluster/files/" + "dashboard-adminuser.yaml")
            core_api = client.CoreV1Api(aApiClient)
            user_resp = core_api.read_namespaced_service_account(
                name="admin-user", namespace="kube-system")
            #print("user response %s " %user_resp)

            self.assertIsNotNone(user_resp)

            rbac_body = utils.create_from_yaml(
                aApiClient,
                filepath + "cluster/files/" + "dashboard-adminrolebind.yaml")
            rbac_api = client.RbacAuthorizationV1Api(aApiClient)
            cluster_resp = rbac_api.read_cluster_role_binding(
                name="admin-user")
            #print("cluster response %s " %cluster_resp)
            self.assertIsNotNone(user_resp)
        except ApiException as e:
            print("Exception creating user role: %s\n" % e)

        # tidy up created settings
        """ try:
示例#3
0
    def test_create_apps_deployment_from_yaml(self):
        """
        Should be able to create an apps/v1 deployment.
        """
        result = []
        # Create a configuration object
        aConfiguration = config.load_kube_config()

        # Create a ApiClient with our config
        aApiClient = client.ApiClient(aConfiguration)
        core_v1 = client.CoreV1Api(aApiClient)

        utils.create_from_yaml(
            aApiClient, filepath + "cluster/files/" + "app-deployment.yaml")
        app_api = client.AppsV1Api(aApiClient)
        dep = app_api.read_namespaced_deployment(name="kubernetes-dashboard",
                                                 namespace="kube-system")
        self.assertIsNotNone(dep)
        while True:
            try:
                app_api.delete_namespaced_deployment(
                    name="kubernetes-dashboard",
                    namespace="kube-system",
                    body={})
                break
            except ApiException:
                continue
示例#4
0
    def test_create_apiservice_from_yaml_conflict(self):
        """
        Should be able to create an API service.
        Should verify that creating the same API service should
        fail due to conflict.
        """
        result = []
        # Create a configuration object
        aConfiguration = config.load_kube_config()

        # Create a ApiClient with our config
        aApiClient = client.ApiClient(aConfiguration)
        core_v1 = client.CoreV1Api(aApiClient)

        utils.create_from_yaml(
            aApiClient, filepath + "cluster/files/" + "api-service.yaml")
        reg_api = client.ApiregistrationV1beta1Api(aApiClient)
        svc = reg_api.read_api_service(name="v1alpha1.wardle.k8s.io")
        self.assertIsNotNone(svc)

        with self.assertRaises(utils.FailToCreateError) as cm:
            utils.create_from_yaml(aApiClient,
                                   filepath + "cluster/files/api-service.yaml")
        exp_error = ('Error from server (Conflict): '
                     '{"kind":"Status","apiVersion":"v1","metadata":{},'
                     '"status":"Failure",'
                     '"message":"apiservices.apiregistration.k8s.io '
                     '\\"v1alpha1.wardle.k8s.io\\" already exists",'
                     '"reason":"AlreadyExists",'
                     '"details":{"name":"v1alpha1.wardle.k8s.io",'
                     '"group":"apiregistration.k8s.io","kind":"apiservices"},'
                     '"code":409}\n')
        self.assertEqual(exp_error, str(cm.exception))
        reg_api.delete_api_service(name="v1alpha1.wardle.k8s.io", body={})
示例#5
0
def create_job(file_path):
    api = client.ApiClient()
    namespace = 'default'

    try:     
        utils.create_from_yaml(api, file_path, namespace=namespace)
    except utils.FailToCreateError as e:
        print("already exists")


    batch = client.BatchV1Api()
    job = batch.read_namespaced_job(name="job-wq-2", namespace=namespace)
    controller_uid = job.metadata.labels["controller-uid"]

    core = client.CoreV1Api()

    pod_label_selector = "controller-uid=" + controller_uid
    pods_list = core.list_namespaced_pod(namespace=namespace, label_selector=pod_label_selector)
    pods = pods_list.items
    for p in pods:
        pod = p.metadata.name
        print(pod)
        try:
            pod_log_response = core.read_namespaced_pod_log(name=pod, namespace=namespace, _return_http_data_only=True, _preload_content=False)
            pod_log = pod_log_response.data
            data = json.loads(pod_log)
            print(data)
        except client.rest.ApiException as e:
            print("Exception when calling CoreV1Api->read_namespaced_pod_log: %s\n" % e)
        except Exception as e:
            print(e)
示例#6
0
def deploy_tekton(tekton_config: model.tekton.TektonConfig, ):
    cfg_factory = ctx().cfg_factory()

    kubernetes_config_name = tekton_config.kubernetes_config_name()
    kubernetes_cfg = cfg_factory.kubernetes(kubernetes_config_name)

    kube_ctx = kube.ctx.Ctx(kubeconfig_dict=kubernetes_cfg.kubeconfig())
    api_client = kubernetes_client.ApiClient(configuration=kube_ctx.kubeconfig)
    namespace_helper = kube_ctx.namespace_helper()

    if pipelines_config := tekton_config.pipelines_config():
        install_manifests = yaml.safe_load_all(
            pipelines_config.install_manifests())
        namespace_helper.create_if_absent(pipelines_config.namespace())
        with tempfile.NamedTemporaryFile(mode='w') as temp_file:
            yaml.dump_all(install_manifests, temp_file)
            # This wont work until kubernetes.python v1.12 is released, see
            # https://github.com/kubernetes-client/python/issues/1022
            #
            # For now, a manual install using 'kubectl apply -f' is required.
            kubernetes_utils.create_from_yaml(
                k8s_client=api_client,
                yaml_file=temp_file.name,
                namespace=pipelines_config.namespace(),
            )
示例#7
0
 def test_create_from_multi_resource_yaml_with_multi_conflicts(self):
     """
     Should create an apps/v1 deployment
     and fail to create the same deployment twice.
     Should raise an exception that contains two error messages.
     """
     k8s_client = client.api_client.ApiClient(configuration=self.config)
     with self.assertRaises(utils.FailToCreateError) as cm:
         utils.create_from_yaml(k8s_client,
                                self.path_prefix + "triple-nginx.yaml")
     exp_error = ('Error from server (Conflict): {"kind":"Status",'
                  '"apiVersion":"v1","metadata":{},"status":"Failure",'
                  '"message":"deployments.apps \\"triple-nginx\\" '
                  'already exists","reason":"AlreadyExists",'
                  '"details":{"name":"triple-nginx","group":"apps",'
                  '"kind":"deployments"},"code":409}\n')
     exp_error += exp_error
     self.assertEqual(exp_error, str(cm.exception))
     ext_api = client.AppsV1Api(k8s_client)
     dep = ext_api.read_namespaced_deployment(name="triple-nginx",
                                              namespace="default")
     self.assertIsNotNone(dep)
     ext_api.delete_namespaced_deployment(name="triple-nginx",
                                          namespace="default",
                                          body={})
示例#8
0
 def test_create_from_multi_resource_yaml_with_conflict(self):
     """
     Should be able to create a service from the first yaml file.
     Should fail to create the same service from the second yaml file
     and create a replication controller.
     Should raise an exception for failure to create the same service twice.
     """
     k8s_client = client.api_client.ApiClient(configuration=self.config)
     utils.create_from_yaml(k8s_client,
                            self.path_prefix + "yaml-conflict-first.yaml")
     core_api = client.CoreV1Api(k8s_client)
     svc = core_api.read_namespaced_service(name="mock-2",
                                            namespace="default")
     self.assertIsNotNone(svc)
     with self.assertRaises(utils.FailToCreateError) as cm:
         utils.create_from_yaml(
             k8s_client, self.path_prefix + "yaml-conflict-multi.yaml")
     exp_error = ('Error from server (Conflict): {"kind":"Status",'
                  '"apiVersion":"v1","metadata":{},"status":"Failure",'
                  '"message":"services \\"mock-2\\" already exists",'
                  '"reason":"AlreadyExists","details":{"name":"mock-2",'
                  '"kind":"services"},"code":409}\n')
     self.assertEqual(exp_error, str(cm.exception))
     ctr = core_api.read_namespaced_replication_controller(
         name="mock-2", namespace="default")
     self.assertIsNotNone(ctr)
     core_api.delete_namespaced_replication_controller(
         name="mock-2",
         namespace="default",
         propagation_policy="Background")
     core_api.delete_namespaced_service(name="mock-2",
                                        namespace="default",
                                        body={})
示例#9
0
def createDs(name):
    v1 = client.CoreV1Api()
    k8s_client = client.ApiClient()
    try:
        utils.create_from_yaml(k8s_client, "yamls/{}.yaml".format(name))
    except ApiException as e:
        logging.debug("{} - ignored".format(e.reason))

    lSel = "name={}".format(name)

    def readyChecker():
        pod_list = v1.list_namespaced_pod("default", label_selector=lSel)
        if len(pod_list.items) < 2:
            return "Not enough pods"

        for pod in pod_list.items:
            if pod.status.phase != "Running":
                return "pod {} status is {}".format(pod.metadata.name,
                                                    pod.status.phase)

        return ""

    assertEventually(readyChecker, 2, 30)
    pod_list = v1.list_namespaced_pod("default", label_selector=lSel)
    pod_names = []
    for pod in pod_list.items:
        pod_names.append(pod.metadata.name)
    return pod_names
示例#10
0
 def test_create_from_list_in_multi_resource_yaml_namespaced(self):
     """
     Should be able to create the items in the PodList and a deployment
     specified in the multi-resource file in a test namespace
     """
     k8s_client = client.api_client.ApiClient(configuration=self.config)
     utils.create_from_yaml(k8s_client,
                            self.path_prefix +
                            "multi-resource-with-list.yaml",
                            namespace=self.test_namespace)
     core_api = client.CoreV1Api(k8s_client)
     app_api = client.AppsV1Api(k8s_client)
     pod_0 = core_api.read_namespaced_pod(name="mock-pod-0",
                                          namespace=self.test_namespace)
     self.assertIsNotNone(pod_0)
     pod_1 = core_api.read_namespaced_pod(name="mock-pod-1",
                                          namespace=self.test_namespace)
     self.assertIsNotNone(pod_1)
     dep = app_api.read_namespaced_deployment(name="mock",
                                              namespace=self.test_namespace)
     self.assertIsNotNone(dep)
     core_api.delete_namespaced_pod(name="mock-pod-0",
                                    namespace=self.test_namespace,
                                    body={})
     core_api.delete_namespaced_pod(name="mock-pod-1",
                                    namespace=self.test_namespace,
                                    body={})
     app_api.delete_namespaced_deployment(name="mock",
                                          namespace=self.test_namespace,
                                          body={})
示例#11
0
def test_deny_all_traffic_to_an_application():
    namespace = "01-deny-all"
    config.load_kube_config()
    k8s_client = client.ApiClient()
    corev1 = client.CoreV1Api()

    corev1.create_namespace(
        client.V1Namespace(metadata=client.V1ObjectMeta(
            name=namespace, labels={"illuminatio-e2e": namespace})))
    utils.create_from_yaml(
        k8s_client,
        "e2e-manifests/01-deny-all-traffic-to-an-application.yml",
        namespace=namespace)

    # ToDo add sleep or wait until all resources are up otherwise we have a race condition
    # ToDo handle execptions
    res = subprocess.run([
        "illuminatio", "run",
        "--runner-image=localhost:5000/illuminatio-runner:dev"
    ],
                         capture_output=True,
                         timeout=60)

    assert res.returncode == 0
    # ToDo evaluate result
    print(res.stdout)

    # Clean up
    res = subprocess.run(["illuminatio", "clean"], capture_output=True)
    assert res.returncode == 0
    print(res.stdout)

    # Clean up
    corev1.delete_namespace(name=namespace)
示例#12
0
    def test_create_from_multi_resource_yaml(self):
        """
        Should be able to create a service and a replication controller
        from a multi-resource yaml file
        """
        result = []
        # Create a configuration object
        aConfiguration = config.load_kube_config()

        # Create a ApiClient with our config
        aApiClient = client.ApiClient(aConfiguration)
        core_v1 = client.CoreV1Api(aApiClient)

        utils.create_from_yaml(
            aApiClient, filepath + "cluster/files/" + "multi-resource.yaml")
        core_api = client.CoreV1Api(aApiClient)
        svc = core_api.read_namespaced_service(name="mock",
                                               namespace="default")
        self.assertIsNotNone(svc)
        ctr = core_api.read_namespaced_replication_controller(
            name="mock", namespace="default")
        self.assertIsNotNone(ctr)
        core_api.delete_namespaced_replication_controller(name="mock",
                                                          namespace="default",
                                                          body={})
        core_api.delete_namespaced_service(name="mock",
                                           namespace="default",
                                           body={})
示例#13
0
def metallb(request):
    m = "metallb"

    config.load_kube_config()
    k8s_client = client.ApiClient()
    try:
        utils.create_from_yaml(k8s_client, "./manifests/{}.yaml".format(m))
        utils.create_from_yaml(k8s_client,
                               "./kubernetes-networks/metallb-config.yaml")
    except TypeError as e:
        LOG.warning(
            "Error while loading manifest file {}. But we continue, anyway\n{}"
            .format(m, e))
        pass
    finally:

        def fin():
            # Someday i'll do it better ))
            LOG.info(
                'Calling Kubectl to delete objects from "{}" manifest'.format(
                    m))
            subprocess.check_call(
                ["kubectl", "delete", "-f", "./manifests/{}.yaml".format(m)])

        request.addfinalizer(fin)
示例#14
0
def run_master_deployment(deploy_name='pymada-master-deployment',
                          template_label={'app': 'pymada-master'},
                          container_port=8000,
                          container_name='pymada-master-container',
                          config_path=None,
                          auth_token=None,
                          max_task_duration=None,
                          max_task_retries=None):

    env_vars = []

    if auth_token is not None:
        env_vars.append(client.V1EnvVar("PYMADA_TOKEN_AUTH", auth_token))

    if max_task_duration is not None:
        env_vars.append(
            client.V1EnvVar("PYMADA_MAX_TASK_DURATION_SECONDS",
                            str(max_task_duration)))

    if max_task_retries is not None:
        env_vars.append(
            client.V1EnvVar("PYMADA_MAX_TASK_RETRIES", str(max_task_retries)))

    container_ports = [client.V1ContainerPort(container_port=container_port)]

    container = client.V1Container(name=container_name,
                                   image='pymada/master',
                                   ports=container_ports,
                                   env=env_vars)

    pod_node_selector = {'pymada-role': 'master'}
    pod_spec = client.V1PodSpec(containers=[container],
                                node_selector=pod_node_selector)

    run_deployment(pod_spec,
                   1,
                   deploy_name,
                   template_label,
                   config_path=config_path)

    base_dir = os.path.dirname(os.path.realpath(__file__))

    if config_path is None:
        cwd = os.getcwd()
        config_path = os.path.join(cwd, 'k3s_config.yaml')

    kube_config.load_kube_config(config_file=config_path)
    kube_client = client.ApiClient()

    if len(get_service_status('service=pymada-master-service').items) == 0:
        utils.create_from_yaml(
            kube_client,
            os.path.join(base_dir, 'kube_yaml', 'pymada_master_service.yaml'))

    if len(get_service_status('service=pymada-master-nodeport').items) == 0:
        utils.create_from_yaml(
            kube_client,
            os.path.join(base_dir, 'kube_yaml', 'pymada_master_nodeport.yaml'))
示例#15
0
def main():
    config.load_kube_config()
    v1 = client.CoreV1Api()
    k8s_client = client.ApiClient()
    k8s_api = client.ExtensionsV1beta1Api(k8s_client)
    pp = pprint.PrettyPrinter(indent=4)
    try:
        # checks if deployment, service, configmap already created
        check = k8s_api.read_namespaced_deployment_status(name="login-node-n",
                                                          namespace="default")
        print("deployment already exists")
    except Exception:
        pass
        # rendering template and creating configmap
        config_data = yaml.load(open('vals.yaml'), Loader=yaml.FullLoader)
        #itemp_up = render_template('condor_config.local.j2', request_name = "request",inventory_hostname = "hostname")
        env = Environment(loader=FileSystemLoader('./templates'),
                          trim_blocks=True,
                          lstrip_blocks=True)
        template = env.get_template('condor_config.local.j2')
        temp_up = template.render(config_data)
        name = 'temcon'
        namespace = 'default'
        body = kubernetes.client.V1ConfigMap()
        body.data = dict([("condor_config.local", temp_up)])
        body.metadata = kubernetes.client.V1ObjectMeta()
        body.metadata.name = name
        configuration = kubernetes.client.Configuration()
        api_instance = kubernetes.client.CoreV1Api(
            kubernetes.client.ApiClient(configuration))
        try:
            api_response = api_instance.create_namespaced_config_map(
                namespace, body)
        except ApiException as e:
            print(
                "Exception when calling CoreV1Api->create_namespaced_config_map: %s\n"
                % e)
        #creating deployment, service, and configmap
        utils.create_from_yaml(k8s_client, "deployNservice.yaml")
        utils.create_from_yaml(k8s_client, "tconfig.yaml")
        # waits till deployment created
        deps = k8s_api.read_namespaced_deployment_status(name="login-node-n",
                                                         namespace="default")
        while (deps.status.available_replicas != 1):
            k8s_api = client.ExtensionsV1beta1Api(k8s_client)
            deps = k8s_api.read_namespaced_deployment_status(
                name="login-node-n", namespace="default")
        print("DEPLOYMENT CREATED")
    dep = k8s_api.read_namespaced_deployment(name="login-node-n",
                                             namespace="default")
    serv = v1.read_namespaced_service(name="login-node-service",
                                      namespace="default")
    pp.pprint(serv.spec.ports[0].node_port)
    list_pods = v1.list_namespaced_pod("default")
    pod = list_pods.items[0]
    node = v1.read_node(pod.spec.node_name)
    pp.pprint(node.status.addresses[0].address)
示例#16
0
def step_impl(context):
    try:
        k8s_client = client.ApiClient()
        utils.create_from_yaml(k8s_client,
                               load_generator_yaml,
                               namespace="product-app")
        common.watch_events(context.v1.list_pod_for_all_namespaces, 75,
                            "sample-app")
    except Exception as err:
        raise AssertionError(f"Failed to create deployment {str(err)}")
示例#17
0
 def _kube_submit(self, manifests):
     utils.create_from_yaml(self.kube_client, 'cm-runjob.yaml')
     for i in range(0, len(manifests), self.limit):
         yaml = ''
         for m in manifests[i:i + self.limit]:
             yaml += "\n---\n%s" % m
         f = open('/tmp/yaml', 'w')
         f.write(yaml)
         f.close()
         utils.create_from_yaml(self.kube_client, '/tmp/yaml')
def create_deployment_from_yaml():
    # Configs can be set in Configuration class directly or using helper
    # utility. If no argument provided, the config will be loaded from
    # default location.
    config.load_kube_config()
    k8s_client = client.ApiClient()
    utils.create_from_yaml(k8s_client, "nginx-deployment.yaml")
    k8s_api = client.ExtensionsV1beta1Api(k8s_client)
    deps = k8s_api.read_namespaced_deployment("nginx-deployment", "default")
    print("Deployment {0} created".format(deps.metadata.name))
示例#19
0
 def _ensure_plugin_fp(self, fp, namespace):
     with tempfile.NamedTemporaryFile("wb") as temp_fp:
         shutil.copyfileobj(fp, temp_fp)
         temp_fp.flush()
         try:
             utils.create_from_yaml(self._api_client, temp_fp.name)
         except utils.FailToCreateError as fce:
             if not all(exc.status == http_client.CONFLICT
                        for exc in fce.api_exceptions):
                 raise
def main():
    # Configs can be set in Configuration class directly or using helper
    # utility. If no argument provided, the config will be loaded from
    # default location.

    with open('token.txt', 'r') as file:
        Token = file.read().strip('\n')

    APISERVER = 'https://10.142.0.2:6443'

    configuration = client.Configuration()

    configuration.host = APISERVER

    configuration.verify_ssl = False

    client.Configuration.set_default(configuration)

    k8s_client = client.ApiClient()

    k8s_coreapi = client.CoreV1Api(k8s_client)

    k8s_extendapi = client.ExtensionsV1beta1Api(k8s_client)

    utils.create_from_yaml(k8s_client, "deployment.yaml")

    deps = k8s_extendapi.read_namespaced_deployment("todolist", "default")

    print("Deployment {0} created".format(deps.metadata.name))

    i = 0
    while i < 10:
        pods = k8s_coreapi.list_namespaced_pod("default")

        host_ip = ""
        for item in pods.items:
            labels = item.metadata.labels
            for key in labels.keys():
                if key == "app":
                    if labels[key] == "todolist":
                        host_ip = item.status.host_ip

        if host_ip != None:
            break
        i = i + 1
        time.sleep(10)

    utils.create_from_yaml(k8s_client, "service.yaml")

    services = k8s_coreapi.read_namespaced_service("todolist", "default")

    print("Serivces {0} created".format(services.metadata.name))

    print("Host IP is %s" % host_ip)
    print("NodePort is {0}".format(services.spec.ports[0].node_port))
示例#21
0
 def test_create_namespace_from_yaml(self):
     """
     Should be able to create a namespace.
     """
     k8s_client = client.api_client.ApiClient(configuration=self.config)
     utils.create_from_yaml(k8s_client,
                            self.path_prefix + "core-namespace.yaml")
     core_api = client.CoreV1Api(k8s_client)
     nmsp = core_api.read_namespace(name="development")
     self.assertIsNotNone(nmsp)
     core_api.delete_namespace(name="development", body={})
示例#22
0
def step_impl(context):
    try:
        k8s_client = client.ApiClient()
        utils.create_from_yaml(k8s_client,
                               sample_app_yaml,
                               namespace="product-app")
        common.watch_events(context.v1.list_pod_for_all_namespaces, 10,
                            "sample-app")
        assert common.get_pod_status_from_name("sample-app", "Succeeded",
                                               "product-app")
    except Exception as err:
        raise AssertionError(f"Failed to create sample app {str(err)}")
示例#23
0
 def start(self):
     self.setup()
     res = self.appsApi.list_namespaced_deployment(
         namespace=TODO_APP_NAMESPACE,
         field_selector=f'metadata.name={self.INFRA_DB_DEPLOYMENT}')
     if len(res.items):
         print(f'{self.INFRA_DB_DEPLOYMENT} deployment is running')
     else:
         from kubernetes.utils import create_from_yaml
         create_from_yaml(k8s_client=k8s_client.ApiClient(),
                          yaml_file=f'{INFRA_DIR}/{self.INFRA_YAML}',
                          namespace=TODO_APP_NAMESPACE)
示例#24
0
 def test_create_rbac_role_from_yaml(self):
     """
     Should be able to create an rbac role.
     """
     k8s_client = client.api_client.ApiClient(configuration=self.config)
     utils.create_from_yaml(k8s_client, self.path_prefix + "rbac-role.yaml")
     rbac_api = client.RbacAuthorizationV1Api(k8s_client)
     rbac_role = rbac_api.read_namespaced_role(name="pod-reader",
                                               namespace="default")
     self.assertIsNotNone(rbac_role)
     rbac_api.delete_namespaced_role(name="pod-reader",
                                     namespace="default",
                                     body={})
示例#25
0
 def test_create_pod_from_yaml(self):
     """
     Should be able to create a pod.
     """
     k8s_client = client.api_client.ApiClient(configuration=self.config)
     utils.create_from_yaml(k8s_client, self.path_prefix + "core-pod.yaml")
     core_api = client.CoreV1Api(k8s_client)
     pod = core_api.read_namespaced_pod(name="myapp-pod",
                                        namespace="default")
     self.assertIsNotNone(pod)
     core_api.delete_namespaced_pod(name="myapp-pod",
                                    namespace="default",
                                    body={})
示例#26
0
def test__e2e__clean_setup__results_are_expected(e2e_test_case, api_client,
                                                 apps_v1):
    # get input and expected from test case name
    input_manifest = E2E_INPUT_MANIFEST.format(e2e_test_case)
    expected_yaml = E2E_EXPECTED_YAML.format(e2e_test_case)
    # create resources to test with
    utils.create_from_yaml(api_client, input_manifest)
    # wait for test resources to be ready
    wait_for_deployments_ready(e2e_test_case, api=apps_v1)
    # run illuminatio, with yaml output for later comparison
    tmp_dir = tempfile.TemporaryDirectory()
    result_file_name = f"{tmp_dir.name}/result.yaml"
    with open(result_file_name, "w") as result_file:
        cmd = [
            "illuminatio",
            "run",
            "--runner-image",
            f"{E2E_RUNNER_IMAGE}",
            "-o",
            f"{result_file.name}",
        ]
        try:
            print(subprocess.check_output(cmd, timeout=120))
        except subprocess.CalledProcessError as cpe:
            print(cpe)
    # load contents of result and expected
    result = None
    try:
        with open(result_file_name, "r") as stream:
            result = yaml.safe_load(stream)
    except OSError:
        pass
    assert result is not None, f"Could not load result from {result_file_name}"
    expected = None

    try:
        with open(expected_yaml, "r") as stream:
            expected = yaml.safe_load(stream)
    except OSError:
        pass
    assert expected is not None, f"Could not load expected from {expected_yaml}"
    # assert that the correct cases have been generated and results match
    assert "cases" in result
    try:
        assert expected == result["cases"]
    except AssertionError as e:
        print("Generated cases did not match expected. Generated:\n")
        print(yaml.dump(result["cases"]))
        print("Expected:\n")
        print(yaml.dump(expected))
        raise e
示例#27
0
def create_job(size):
    #config.load_incluster_config()
    config.load_kube_config()
    api = kclient.ApiClient()
    core = kclient.CoreV1Api()

    namespace = 'default'
    file_path = 'job.yaml'

    try:
        utils.create_from_yaml(api, file_path, namespace=namespace)
    except utils.FailToCreateError as e:
        print("already exists")

    batch = kclient.BatchV1Api()
    job = batch.read_namespaced_job(name="job-wq-2", namespace=namespace)
    controller_uid = job.metadata.labels["controller-uid"]

    pod_label_selector = "controller-uid=" + controller_uid
    pods_list = core.list_namespaced_pod(namespace=namespace,
                                         label_selector=pod_label_selector)
    pods = pods_list.items
    results = []

    w = watch.Watch()

    while True:
        for event in w.stream(func=core.list_namespaced_pod,
                              label_selector=pod_label_selector,
                              namespace=namespace,
                              timeout_seconds=30):
            print(event['object'].status.phase)
            if event['object'].status.phase in ["Succeeded", "Terminating"]:
                try:
                    pod = event['object'].metadata.name
                    pod_log_response = core.read_namespaced_pod_log(
                        name=pod,
                        namespace=namespace,
                        _return_http_data_only=True,
                        _preload_content=False)
                    pod_log = pod_log_response.data
                    data = json.loads(pod_log)
                except kclient.rest.ApiException as e:
                    print(e)
                    data = {'flow': None, 'target': None, 'score': None}
                if data not in results:
                    results.append(data)
                print(data)
                if len(results) == size:
                    w.stop()
                    return results
示例#28
0
 def test_create_service_from_yaml(self):
     """
     Should be able to create a service.
     """
     k8s_client = client.api_client.ApiClient(configuration=self.config)
     utils.create_from_yaml(k8s_client,
                            self.path_prefix + "core-service.yaml")
     core_api = client.CoreV1Api(k8s_client)
     svc = core_api.read_namespaced_service(name="my-service",
                                            namespace="default")
     self.assertIsNotNone(svc)
     core_api.delete_namespaced_service(name="my-service",
                                        namespace="default",
                                        body={})
示例#29
0
文件: jobs.py 项目: shteou/goji
def apply_job(job_name):
    try:
        log.info(f"Applying job {job_name}")
        if "IN_CLUSTER" in environ and environ["IN_CLUSTER"] == "true":
            k8s_config = config.load_incluster_config()
        else:
            k8s_config = config.load_kube_config()
        k8s_client = client.api_client.ApiClient(configuration=k8s_config)
        create_from_yaml(k8s_client, f"jobs/queued/{job_name}")
    except Exception as e:
        log.error(f"Failed to apply job {job_name}:")
        log.error(e)
        return False
    return True
示例#30
0
 def _kube_submit(self, manifests):
     try:
         utils.create_from_yaml(self.api_client, 'cm-runjob.yaml')
     except Exception as e:
         pass
     for i in range(0, len(manifests), self.limit):
         yaml = ''
         for m in manifests[i:i + self.limit]:
             yaml += "\n---\n%s" % m
         f = open('/tmp/{0}'.format(self.cluster), 'w')
         f.write(yaml)
         f.close()
         utils.create_from_yaml(self.api_client,
                                '/tmp/{0}'.format(self.cluster))
示例#31
0
 def test_deployment_in_namespace(self):
     k8s_client = client.ApiClient(configuration=self.config)
     core_api = utils.create_from_yaml(k8s_client, 
         "kubernetes/e2e_test/test_yaml/core-namespace-dep.yaml")
     self.assertEqual("v1", 
         core_api.get_api_resources().group_version)
     nmsp = core_api.read_namespace(name="dep")
     self.assertIsNotNone(nmsp)
     dep_api = utils.create_from_yaml(k8s_client, 
         "kubernetes/e2e_test/test_yaml/extensions-deployment-dep.yaml")
     dep = dep_api.read_namespaced_deployment(name="nginx-deployment", 
         namespace="dep")
     self.assertIsNotNone(dep)
     resp = dep_api.delete_namespaced_deployment(
         name="nginx-deployment", namespace="dep", 
         body={})
     resp = core_api.delete_namespace(name="dep", body={})
def main():
    # Configs can be set in Configuration class directly or using helper
    # utility. If no argument provided, the config will be loaded from
    # default location.
    config.load_kube_config()
    k8s_client = client.ApiClient()
    k8s_api = utils.create_from_yaml(k8s_client, "nginx-deployment.yaml")
    deps = k8s_api.read_namespaced_deployment("nginx-deployment", "default")
    print("Deployment {0} created".format(deps.metadata.name))
示例#33
0
 def test_core_namespace_yaml(self):
     k8s_client = client.api_client.ApiClient(configuration=self.config)
     k8s_api = utils.create_from_yaml(k8s_client, 
         "kubernetes/e2e_test/test_yaml/core-namespace.yaml")
     self.assertEqual("v1", 
         k8s_api.get_api_resources().group_version)
     nmsp = k8s_api.read_namespace(name="development")
     self.assertIsNotNone(nmsp)
     resp = k8s_api.delete_namespace(name="development", body={})
示例#34
0
 def test_api_service(self):
     k8s_client = client.api_client.ApiClient(configuration=self.config)
     k8s_api = utils.create_from_yaml(k8s_client, 
         "kubernetes/e2e_test/test_yaml/api-service.yaml")
     self.assertEqual("apiregistration.k8s.io/v1beta1", 
         k8s_api.get_api_resources().group_version)
     svc = k8s_api.read_api_service(
         name="v1alpha1.wardle.k8s.io")
     self.assertIsNotNone(svc)
     resp = k8s_api.delete_api_service(
         name="v1alpha1.wardle.k8s.io", body={})
示例#35
0
 def test_extension_yaml(self):
     k8s_client = client.api_client.ApiClient(configuration=self.config)
     k8s_api = utils.create_from_yaml(k8s_client, 
         "kubernetes/e2e_test/test_yaml/extensions-deployment.yaml")
     self.assertEqual("extensions/v1beta1", 
         k8s_api.get_api_resources().group_version)
     dep = k8s_api.read_namespaced_deployment(name="nginx-deployment", 
         namespace="default")
     self.assertIsNotNone(dep)
     resp = k8s_api.delete_namespaced_deployment(
         name="nginx-deployment", namespace="default", 
         body={})
示例#36
0
 def test_core_pod_yaml(self):
     k8s_client = client.api_client.ApiClient(configuration=self.config)
     k8s_api = utils.create_from_yaml(k8s_client, 
         "kubernetes/e2e_test/test_yaml/core-pod.yaml")
     self.assertEqual("v1", 
         k8s_api.get_api_resources().group_version)
     pod = k8s_api.read_namespaced_pod(name="myapp-pod", 
         namespace="default")
     self.assertIsNotNone(pod)
     resp = k8s_api.delete_namespaced_pod(
         name="myapp-pod", namespace="default",
         body={})