Ejemplo n.º 1
0
def main():

    contexts, active_context = config.list_kube_config_contexts()
    if not contexts:
        print("Cannot find any context in kube-config file.")
        return
    contexts = [context['name'] for context in contexts]
    active_index = contexts.index(active_context['name'])
    cluster1, first_index = pick(contexts, title="Pick the first context",
                                 default_index=active_index)
    cluster2, _ = pick(contexts, title="Pick the second context",
                       default_index=first_index)

    client1 = client.CoreV1Api(
        api_client=config.new_client_from_config(context=cluster1))
    client2 = client.CoreV1Api(
        api_client=config.new_client_from_config(context=cluster2))

    print("\nList of pods on %s:" % cluster1)
    for i in client1.list_pod_for_all_namespaces().items:
        print("%s\t%s\t%s" %
              (i.status.pod_ip, i.metadata.namespace, i.metadata.name))

    print("\n\nList of pods on %s:" % cluster2)
    for i in client2.list_pod_for_all_namespaces().items:
        print("%s\t%s\t%s" %
              (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
Ejemplo n.º 2
0
def main():
    contexts, active_context = config.list_kube_config_contexts()
    if not contexts:
        print("Cannot find any context in kube-config file.")
        return
    contexts = [context['name'] for context in contexts]
    active_index = contexts.index(active_context['name'])
    cluster1, first_index = pick(contexts,
                                 title="Pick the first context",
                                 default_index=active_index)
    cluster2, _ = pick(contexts,
                       title="Pick the second context",
                       default_index=first_index)

    client1 = client.CoreV1Api(api_client=config.new_client_from_config(
        context=cluster1))
    client2 = client.CoreV1Api(api_client=config.new_client_from_config(
        context=cluster2))

    print("\nList of pods on %s:" % cluster1)
    for i in client1.list_pod_for_all_namespaces().items:
        print("%s\t%s\t%s" %
              (i.status.pod_ip, i.metadata.namespace, i.metadata.name))

    print("\n\nList of pods on %s:" % cluster2)
    for i in client2.list_pod_for_all_namespaces().items:
        print("%s\t%s\t%s" %
              (i.status.pod_ip, i.metadata.namespace, i.metadata.name))
Ejemplo n.º 3
0
def load_kube_credentials_gcloud(credentials: Dict[str, str]) -> ApiClient:
    # Try to pull credentials from gcloud, but first checking if there
    # is a context, using their auto generated naming scheme, to avoid
    # calling `gcloud` every time, if we've already authed before.
    from subprocess import check_call, DEVNULL

    cluster = credentials["cluster"]
    project = credentials["project"]
    zone = credentials["zone"]

    context = f"gke_{project}_{zone}_{cluster}"

    try:
        return new_client_from_config(context=context)
    except (ConfigException, FileNotFoundError):
        pass

    check_call(
        [
            "gcloud",
            "container",
            "clusters",
            "get-credentials",
            cluster,
            "--zone",
            zone,
            "--project",
            project,
        ],
        stdout=DEVNULL,
        stderr=DEVNULL,
    )

    return new_client_from_config(context=context)
Ejemplo n.º 4
0
    def pick_cluster(self, cluster_name: str = None):
        """
        Pick the cluster you will scale the workload
        """
        try:
            # Load the context in kube-config file
            contexts, active_context = config.list_kube_config_contexts(config_file=self.kube_config)
            config.load_incluster_config()
            self.logger.debug(f"contexts: {contexts}, active_context: {active_context}")
            if not contexts:
                self.logger.error("Cannot locate any context in kube-config file")
                return
            contexts = [context['name'] for context in contexts]
            self.logger.debug(f"context list: {contexts}")
            if cluster_name is None:
                cluster_name = active_context['name']
            target_cluster_index = next((i for i, j in enumerate(contexts) if cluster_name in j), None)
            if target_cluster_index is None:
                self.logger.error(f"Cannot find {cluster_name} in {contexts}")
                return

            picked_cluster = contexts[target_cluster_index]
            self.logger.debug(f"Picked cluster: {picked_cluster}")

            return {
                'core_v1': client.CoreV1Api(
                    api_client=config.new_client_from_config(config_file=self.kube_config, context=picked_cluster)
                ),
                'apps_v1': client.AppsV1Api(
                    api_client=config.new_client_from_config(config_file=self.kube_config, context=picked_cluster)
                ),
            }
        except Exception as e:
            self.logger.error(f"Can't pick a cluster: {e}")
            raise e
Ejemplo n.º 5
0
def main():
    source_pods = []
    contexts, active_context = config.list_kube_config_contexts()
    if not contexts:
        print("Cannot find any context in kube-config file.")
        return
    contexts = [context['name'] for context in contexts]
    active_index = contexts.index(active_context['name'])
    cluster1, first_index = pick(contexts, title="Pick the source Cluster Context for the POD to be backed up",
                                 default_index=active_index)

    client1 = client.CoreV1Api(
        api_client=config.new_client_from_config(context=cluster1))

    # TK - Added to setup
    source_pods = [i.metadata.name for i in client1.list_pod_for_all_namespaces().items]
    print("\nList of source_pods on %s:" % source_pods)
    selected_pod = pick(source_pods, title="Pick the POD to be backed up")

    for i in client1.list_pod_for_all_namespaces().items:
        if selected_pod[0] == i.metadata.name: # Return the Kubernetes API POD object
            print("Found the POD Object for Selected POD")
            source_pod_object = i

    print(type(source_pod_object))
    print ('This is the POD you selected {0}'.format(source_pod_object.metadata.name))

    labels = source_pod_object.metadata.




    # Original Code for listing pods
    print("\nList of pods on %s:" % cluster1)

    for i in client1.list_pod_for_all_namespaces().items:
        print("%s\t%s\t%s" % (i.status.pod_ip, i.metadata.namespace, i.metadata.name))



    cluster2, _ = pick(contexts, title="Pick the target Cluster Context for the POD to be restored to",
                       default_index=first_index)


    client2 = client.CoreV1Api(
        api_client=config.new_client_from_config(context=cluster2))





    '''
Ejemplo n.º 6
0
    def delete_pipeline(self, uuid=None, name=None, namespace="moon",
                        active_context=None,
                        active_context_name=None):
        """Delete a pipeline

        :param uuid:
        :param name:
        :param namespace:
        :param active_context:
        :param active_context_name:
        :return:
        """
        name_to_delete = None
        if uuid and uuid in self.get_pods():
            name_to_delete = self.get_pods()[uuid][0]['name']
        elif name:
            for pod_key, pod_list in self.get_pods().items():
                for pod_value in pod_list:
                    if pod_value.get("name") == name:
                        name_to_delete = pod_value.get("name")
                        break
        if not name_to_delete:
            raise exceptions.PipelineUnknown
        logger.info("Will delete deployment and service named {}".format(name_to_delete))
        contexts, _active_context = self.get_contexts()
        if active_context_name:
            for _context in contexts:
                if _context["name"] == active_context_name:
                    active_context = _context
                    break
        if active_context:
            active_context = _active_context
            _config = config.new_client_from_config(
                context=active_context['name'])
            logger.debug("_config={}".format(_config))
            api_client = client.CoreV1Api(_config)
            ext_client = client.ExtensionsV1beta1Api(_config)
            self.delete_deployment(name=name_to_delete, namespace=namespace,
                                   ext_client=ext_client)
            self.delete_service(name=name_to_delete, api_client=api_client)
            return
        logger.debug("contexts={}".format(contexts))
        for _ctx in contexts:
            _config = config.new_client_from_config(context=_ctx['name'])
            logger.debug("_config={}".format(_config))
            api_client = client.CoreV1Api(_config)
            ext_client = client.ExtensionsV1beta1Api(_config)
            self.delete_deployment(name=name_to_delete, namespace=namespace,
                                   ext_client=ext_client)
            self.delete_service(name=name_to_delete, api_client=api_client)
Ejemplo n.º 7
0
def main():
    parser = argparse.ArgumentParser(
        description="ROSA Post install configuration for PerfScale CI")
    parser.add_argument(
        '--incluster',
        default="false",
        type=str,
        help='Is this running from a pod within the cluster [true|false]')
    parser.add_argument(
        '--kubeconfig',
        help='Optional kubeconfig location. Incluster cannot be true')
    args = parser.parse_args()

    if args.incluster.lower() == "true":
        config.load_incluster_config()
        k8s_config = client.Configuration()
        k8s_client = client.api_client.ApiClient(configuration=k8s_config)
    elif args.kubeconfig:
        k8s_client = config.new_client_from_config(args.kubeconfig)
    else:
        k8s_client = config.new_client_from_config()

    dyn_client = DynamicClient(k8s_client)
    nodes = dyn_client.resources.get(api_version='v1', kind='Node')

    if args.kubeconfig:
        cmd = [
            "oc get infrastructures.config.openshift.io cluster -o jsonpath={.status.infrastructureName} --kubeconfig "
            + args.kubeconfig
        ]
    else:
        cmd = [
            "oc get infrastructures.config.openshift.io cluster -o jsonpath={.status.infrastructureName}"
        ]
    process = subprocess.Popen(cmd,
                               stdout=subprocess.PIPE,
                               stderr=subprocess.PIPE,
                               shell=True)
    stdout, stderr = process.communicate()
    clustername = stdout.decode("utf-8")

    # Remove worker label from infa nodes
    _remove_worker_label(nodes)

    # Add a workload label to one of the workers
    _label_workload_machine(nodes)

    # AWS configuration
    _aws_config(nodes, clustername)
Ejemplo n.º 8
0
    def __init__(self,
                 pod_name,
                 env_pod,
                 command,
                 image_namespace=None,
                 image_tag=None,
                 env=None,
                 env_container_id=0):
        self.pod_name = pod_name if pod_name else "digdag-mds-job"
        self.env_pod = env_pod if env_pod else "digdag-mds-job"
        self.command = command if command else ["flask", "test-cli-command"]
        self.env_container_id = env_container_id

        # If env, creating container from scratch, pull from tools build suffix
        if (env):
            self.env = env
            self.image = f"docker-registry.default.svc:5000/{image_namespace}/{self.env_pod}:build{self.suffix}"

        # Else creating based on existing image and pod, requires tag
        else:
            self.env = None
            self.image = f"docker-registry.default.svc:5000/{self.namespace}/{self.env_pod}:{self.image_tag}"

        self.job_pod_name = self.pod_name + self.suffix
        self.env_pod_name = self.env_pod + self.suffix
        self.job_pod_label = f"name={self.job_pod_name}"
        self.env_pod_label = f"name={self.env_pod_name}"

        k8s_client = config.new_client_from_config(self.kube_config)
        dyn_client = DynamicClient(k8s_client)

        self.v1_pod = dyn_client.resources.get(api_version="v1", kind="Pod")
Ejemplo n.º 9
0
def getpvs(context):
    try:
        replystr = ""
        cluster = context
        api = client.CoreV1Api(api_client=config.new_client_from_config(
            context=cluster))
        pvs = api.list_persistent_volume()
        replystr += ("---- PVs ---\n")
        replystr += ("%-16s\t%-10s\t%-30s\t%-10s\t%-6s\n" %
                     ("NAME", "STATUS", "CLAIM", "STORAGECLASS", "SIZE"))
        for pv in pvs.items:
            pvandpvc = ""
            try:
                if pv.spec.claim_ref.namespace is not None:
                    pvandpvc = pv.spec.claim_ref.namespace + "/" + pv.spec.claim_ref.name
            except Exception as e:
                pvandpvc = "Unassigned"
            replystr += (
                "%-16s\t%-10s\t%-30s\t%-10s\t%-6s\n" %
                (pv.metadata.name, pv.status.phase, pvandpvc,
                 pv.spec.storage_class_name, pv.spec.capacity['storage']))
            pvandpvc = ""
        return replystr
    except Exception as e:
        print("error: ", e)
        return '"message": {"error": "something borked in getpvs"}'
Ejemplo n.º 10
0
def patch_aws_node(Context: str, **kwargs):
    while True:
        response = choice("Do you want to patch the aws-node Daemonset(Yes/No)? This will trigger a rolling restart of the networking plugin: ")
        if response == 'yes':
            break
        if response == 'no':
            exit(1)
        else:
            print('Please enter "Yes" or "No".')
    print('Patching aws-node daemonset')
    if 'Clientset' in kwargs:
        api_client = kwargs['Clientset']
        AppsV1 = client.AppsV1Api(api_client)
    else:
        AppsV1 = client.AppsV1Api(api_client=config.new_client_from_config(context=Context))

    patch = {
        "spec": {
            "template": {
                "metadata": {
                    "annotations": {
                        "irsa": "enabled"
                    }
                }
            }
        }
    }
    try:
        AppsV1.patch_namespaced_daemon_set_with_http_info(name='aws-node', namespace='kube-system', body=patch)
    except ApiException:
        print('An error occurred while patching the aws-node Daemonset')
        exit(1)
Ejemplo n.º 11
0
    def testRunInKubernetes(self):
        self._build_docker_images()

        temp_spill_dir = tempfile.mkdtemp(prefix='test-mars-k8s-')
        api_client = k8s_config.new_client_from_config()
        kube_api = k8s_client.CoreV1Api(api_client)

        cluster_client = None
        try:
            extra_vol_config = HostPathVolumeConfig('mars-src-path',
                                                    '/mnt/mars', MARS_ROOT)
            cluster_client = new_cluster(
                api_client,
                image=self._docker_image,
                worker_spill_paths=[temp_spill_dir],
                extra_volumes=[extra_vol_config],
                pre_stop_command=['rm', '/tmp/stopping.tmp'],
                timeout=600,
                log_when_fail=True)
            self.assertIsNotNone(cluster_client.endpoint)

            pod_items = kube_api.list_namespaced_pod(
                cluster_client.namespace).to_dict()

            log_processes = []
            for item in pod_items['items']:
                log_processes.append(
                    subprocess.Popen([
                        'kubectl', 'logs', '-f', '-n',
                        cluster_client.namespace, item['metadata']['name']
                    ]))

            a = mt.ones((100, 100), chunk_size=30) * 2 * 1 + 1
            b = mt.ones((100, 100), chunk_size=30) * 2 * 1 + 1
            c = (a * b * 2 + 1).sum()
            r = cluster_client.session.run(c, timeout=600)

            expected = (np.ones(a.shape) * 2 * 1 + 1)**2 * 2 + 1
            assert_array_equal(r, expected.sum())

            # turn off service processes with grace to get coverage data
            procs = []
            for item in pod_items['items']:
                p = subprocess.Popen([
                    'kubectl', 'exec', '-n', cluster_client.namespace,
                    item['metadata']['name'], '/srv/graceful_stop.sh'
                ])
                procs.append(p)
            for p in procs:
                p.wait()

            [p.terminate() for p in log_processes]
        finally:
            shutil.rmtree(temp_spill_dir)
            if cluster_client:
                try:
                    cluster_client.stop(wait=True, timeout=20)
                except TimeoutError:
                    pass
            self._remove_docker_image(False)
Ejemplo n.º 12
0
def main():
    conn = None

    device_opt = [
        "port", "namespace", "kubeconfig", "separator", "no_password"
    ]
    define_new_opts()
    options = check_input(device_opt, process_input(device_opt))

    docs = {}
    docs["shortdesc"] = "Fence agent for KubeVirt"
    docs["longdesc"] = "fence_kubevirt is an I/O Fencing agent for KubeVirt."
    docs["vendorurl"] = "https://kubevirt.io/"
    show_docs(options, docs)

    run_delay(options)

    validate_options(['--namespace'], options)

    try:
        from kubernetes import config
        from openshift.dynamic import DynamicClient
        kubeconfig = options.get('--kubeconfig')
        k8s_client = config.new_client_from_config(config_file=kubeconfig)
        conn = DynamicClient(k8s_client)
    except ImportError:
        logging.error(
            "Couldn\'t import kubernetes.config or "
            "openshift.dynamic.DynamicClient - not found or not accessible")

    # Operate the fencing device
    result = fence_action(conn, options, set_power_status, get_power_status,
                          get_nodes_list)
    sys.exit(result)
Ejemplo n.º 13
0
def create_connection(url):
    """
    :Method to create connection with client
    :param url:URL of each region
    :return:dyn_client
    """
    try:
        if url is not None:
            path = cae_lib.get_kube_config_path(url)
        print("INFO: Kube Config's Path", path)
        # looping for retrying connection attempts in case of server busy or hit 429 error due to too many requests
        for i in range(0, 10):
            try:
                k8s_client = config.new_client_from_config(path)
                dyn_client = DynamicClient(k8s_client)
            except Exception as e:
                exception_str = str(e)
                if '429' in exception_str:
                    print(
                        "LOG: RETRYING upon receiving \"HTTP error 429 (Too Many Requests)\"",
                        str(exception_str))
                    time.sleep(5)
                    continue
            break

    except Exception as e:
        print("ERROR: Fail to create connection handle: %s" % str(e))
        return None

    return dyn_client
Ejemplo n.º 14
0
    def build_chamberlain_client(self):
        """
        Build configmaps for chamberlain client.
        """

        self.define_all_client_objects()

        kube_client = k_config.new_client_from_config()
        os_client = DynamicClient(kube_client)

        if self.with_dv:
            try:
                config_map = os_client.resources.get(api_version='v1',
                                                     kind="ConfigMap")
                api_response = config_map.create(
                    namespace=self.config['namespace'],
                    body=self.dv_config_map_body)
                print("Created Dataverse ConfigMap: \n{} \n".format(
                    api_response))
            except DynamicApiError as e:
                print("Error creating Dataverse ConfigMap: \n{} \n".format(e))

        if self.with_swift:
            try:
                config_map = os_client.resources.get(api_version='v1',
                                                     kind="ConfigMap")
                api_response = config_map.create(
                    namespace=self.config['namespace'],
                    body=self.swift_config_map_body)
                print("Created Swift ConfigMap: \n{} \n".format(api_response))
            except DynamicApiError as e:
                print("Error creating Swift ConfigMap: \n{} \n".format(e))
Ejemplo n.º 15
0
def api_client(cluster_name: str, api_class: str) -> k8s.client.apis:
    """
	Creates and returns a python k8s api client of the specified class and pointing to the specified cluster.

	Usage: 	Use this function whenever you want a python k8s api client.
			Python k8s api documentation: https://github.com/kubernetes-client/python/blob/master/kubernetes/README.md

	:param (str) cluster_name
	:param (str) api_class, e.g. "CoreV1Api"
	:return: (k8s.client.apis object) python k8s api client object
	"""

    # retrieve name of context that points to the given cluster
    target_context = k8s_config.context_for_cluster(cluster_name)
    if target_context == None:
        print("No valid context could be found for cluster with name",
              cluster_name + ".")
        print(
            "As of most recent update, you have access to the following clusters:",
            k8s_config.all_cluster_names())
        return

    new_client = config.new_client_from_config(context=target_context)

    # check if given api_class is a valid k8s api class
    try:
        api_client = eval("client." + api_class + "(api_client = new_client)")
    except AttributeError as e:
        print("No known API class could be found with name", api_class + ".")
        print(
            "API classes can be found here: https://github.com/kubernetes-client/python/blob/master/kubernetes/README.md"
        )
        return
    return api_client
Ejemplo n.º 16
0
    def get_api_clients(self, resource_config):
        """Get API Clients.

        :param cloudshell.cp.kubernetes.resource_config.
            KubernetesResourceConfig resource_config:
        """
        if not os.path.isfile(resource_config.config_file_path):
            raise ValueError(
                "Config File Path is invalid. Cannot open file '{}'.".format(
                    resource_config.config_file_path))

        # todo - alexaz - Need to add support for urls so that we can
        #  download a config file from a central location and
        # todo          - also have the config file password protected.
        if resource_config.aws_access_key_id and resource_config.aws_secret_access_key:
            self._logger.debug(
                "EKS config for, key-ID {}, secret-key {}".format(
                    resource_config.aws_access_key_id,
                    resource_config.aws_secret_access_key,
                ))
            api_client = self._new_client_from_eks_config(
                resource_config.config_file_path,
                resource_config.aws_access_key_id,
                resource_config.aws_secret_access_key,
            )
        else:
            api_client = new_client_from_config(
                config_file=resource_config.config_file_path)
        core_api = CoreV1Api(api_client=api_client)
        apps_api = AppsV1Api(api_client=api_client)

        return KubernetesClients(api_client, core_api, apps_api)
Ejemplo n.º 17
0
def explore_kubernetes_system() -> DiscoveredSystemInfo:
    """
    Fetch information from the current Kubernetes context.
    """
    logger.info("Discovering Kubernetes system")
    if not has_local_config_file():
        logger.warn("Could not locate the default kubeconfig file")
        return

    api = config.new_client_from_config()
    v1core = client.CoreV1Api(api)
    v1ext = client.ExtensionsV1beta1Api(api)

    ret = v1core.list_namespace(_preload_content=False)
    namespaces = ret.read()

    info = {}
    for ns in json.loads(namespaces)["items"]:
        ret = v1core.list_namespaced_pod(namespace=ns["metadata"]["name"],
                                         _preload_content=False)
        info["pods"] = json.loads(ret.read())

        ret = v1ext.list_namespaced_deployment(
            namespace=ns["metadata"]["name"], _preload_content=False)
        info["deployments"] = json.loads(ret.read())

    return info
Ejemplo n.º 18
0
def cpvc(context, namespace, pvcname, pvname):
    try:
        cluster = context
        api = client.CoreV1Api(api_client=config.new_client_from_config(
            context=cluster))
        ns = namespace
        nm = pvcname
        vn = pvname

        my_resource = {
            "apiVersion": "v1",
            "kind": "PersistentVolumeClaim",
            "metadata": {
                "name": nm,
                "namespace": ns
            },
            "spec": {
                "volumeName": vn,
                "accessModes": ["ReadWriteMany"],
                "resources": {
                    "requests": {
                        "storage": "1Gi"
                    }
                }
            }
        }
        pvcs = api.create_namespaced_persistent_volume_claim(namespace=ns,
                                                             body=my_resource)
        output = str(pvcs) + "\n"
        return output
    except Exception as e:
        return ("Exception: ", str(e))
Ejemplo n.º 19
0
    def __init__(
        self,
        conf: str,
        namespace: str,
        meta: t.Dict,
        spec_path: str,
        override: str = None,
    ) -> None:
        """Manages deployment.

        Args:
            conf (str): Path to Kubernetes config.
            namespace (str): Kubernetes namespace.
            meta (t.Dict): Spec variables.
            spec_path (str): Location of chart to deploy.
            override (str): Override specific resource file, defaults to
             `resource.override.yaml` where one has `resource.yaml`.
        """

        self.client: k8s.ApiClient = config.new_client_from_config(conf)

        # Disable threadding pool inside swagger client
        # because it causes problems with other multithreading workers
        # as we don't use any async calls we are safe to do so.
        self.client.pool.close()
        self.client.pool.join()

        self.meta = meta
        self.namespace = namespace
        self.override = override if override else "override"
        self.spec_path = spec_path
Ejemplo n.º 20
0
    def apiclient(self):

        homedir = os.path.expanduser("˜")

        if os.path.exists(
                '/var/run/secrets/kubernetes.io/serviceaccount/token'):
            with open('/var/run/secrets/kubernetes.io/serviceaccount/token'
                      ) as tokenfile:
                token = tokenfile.read()
            aConfiguration = client.Configuration()
            aConfiguration.host = "https://kubernetes.default.svc"
            if os.path.exists(
                    '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt'):
                aConfiguration.verify_ssl = True
                aConfiguration.ssl_ca_cert = '/var/run/secrets/kubernetes.io/serviceaccount/ca.crt'
            else:
                aConfiguration.verify_ssl = False

            aConfiguration.api_key = {"authorization": "Bearer " + token}
            logging.info('Initiating kubeapi session with auth token...')
            apiclient = client.ApiClient(aConfiguration)
            return apiclient
        elif os.path.exists(homedir + '/.kube/config'):
            config.verify_ssl = False
            k8sclient = config.new_client_from_config()
            logging.info('Found kubeconfig file on ' + homedir +
                         '/.kube/config')
            return k8sclient
        else:
            raise Exception(
                'Unauthorized: No kubeconfig file or bearer token found so cannot instantiate client instance'
            )
Ejemplo n.º 21
0
 def create_wrappers(self, slave_name=None):
     contexts, active_context = self.get_contexts()
     logger.debug("contexts: {}".format(contexts))
     logger.debug("active_context: {}".format(active_context))
     conf = configuration.get_configuration("components/wrapper")
     hostname = conf["components/wrapper"].get(
         "hostname", "wrapper")
     port = conf["components/wrapper"].get("port", 80)
     container = conf["components/wrapper"].get(
         "container",
         "wukongsun/moon_wrapper:v4.3")
     for _ctx in contexts:
         if slave_name and slave_name != _ctx['name']:
             continue
         _config = config.new_client_from_config(context=_ctx['name'])
         logger.debug("_config={}".format(_config))
         api_client = client.CoreV1Api(_config)
         ext_client = client.ExtensionsV1beta1Api(_config)
         data = [{
             "name": hostname + "-" + get_random_name(),
             "container": container,
             "port": port,
             "namespace": "moon",
             "slave_name": _ctx['name']
         }, ]
         self.load_deployment_and_service(data, api_client, ext_client, expose=True)
Ejemplo n.º 22
0
    def checkMe(self):
        ret = Service.FAIL
        extra = None

        try:
            k8s = client.CoreV1Api(api_client=config.new_client_from_config(
                context=self.context))
            list_nodes = k8s.list_node(watch=False)
            unknowns = 0
            nodes = 0
            for item in list_nodes.items:
                if 'Unknown' in [x.status for x in item.status.conditions]:
                    unknowns = unknowns + 1
                nodes = nodes + 1

            if nodes > 0 and (100 -
                              (unknowns * 100 / nodes)) >= self.availability:
                ret = Service.OK
            else:
                # FAIL so inform about the number of nodes availables or not
                extra = {"ready": nodes - unknowns, "unknown": unknowns}
        except Exception as e:
            extra = {"exception": str(e)}

        return super().checkMe(ret, extra)
Ejemplo n.º 23
0
def _start_kube_cluster(**kwargs):
    image_name = _build_docker_images()

    temp_spill_dir = tempfile.mkdtemp(prefix='test-mars-k8s-')
    api_client = k8s_config.new_client_from_config()
    kube_api = k8s_client.CoreV1Api(api_client)

    cluster_client = None
    try:
        extra_vol_config = HostPathVolumeConfig('mars-src-path', '/mnt/mars',
                                                MARS_ROOT)
        cluster_client = new_cluster(
            api_client,
            image=image_name,
            worker_spill_paths=[temp_spill_dir],
            extra_volumes=[extra_vol_config],
            pre_stop_command=['rm', '/tmp/stopping.tmp'],
            timeout=600,
            log_when_fail=True,
            **kwargs)

        assert cluster_client.endpoint is not None

        pod_items = kube_api.list_namespaced_pod(
            cluster_client.namespace).to_dict()

        log_processes = []
        for item in pod_items['items']:
            log_processes.append(
                subprocess.Popen([
                    'kubectl', 'logs', '-f', '-n', cluster_client.namespace,
                    item['metadata']['name']
                ]))

        yield

        # turn off service processes with grace to get coverage data
        procs = []
        pod_items = kube_api.list_namespaced_pod(
            cluster_client.namespace).to_dict()
        for item in pod_items['items']:
            p = subprocess.Popen([
                'kubectl', 'exec', '-n', cluster_client.namespace,
                item['metadata']['name'], '--', '/srv/graceful_stop.sh'
            ])
            procs.append(p)
        for p in procs:
            p.wait()

        [p.terminate() for p in log_processes]
    finally:
        shutil.rmtree(temp_spill_dir)
        if cluster_client:
            try:
                cluster_client.stop(wait=True, timeout=20)
            except TimeoutError:
                pass
        _collect_coverage()
        _remove_docker_image(image_name, False)
Ejemplo n.º 24
0
 def k8s_client(self):
     """
     Return k8s_client instance for specific openshift cluster based on kube_config_file attribute
     :return: Instance of K8s_client
     """
     if self.kube_config_file and self.kube_config_file not in OcpBase.k8s_clients:
         OcpBase.k8s_clients[self.kube_config_file] = config.new_client_from_config(str(self.kube_config_file))
     return OcpBase.k8s_clients.get(self.kube_config_file)
Ejemplo n.º 25
0
    def __init__(self, config_file='/etc/kubernetes/admin.conf', logger=None):
        self.api_client = config.new_client_from_config(config_file)
        self.api_client.config.assert_hostname = False
        self.v1_h = client.CoreV1Api(self.api_client)
        self.v1_h.read_namespace('default')
        self.v1_beta_h = client.ExtensionsV1beta1Api(self.api_client)

        self.logger = logger or contrail_logging.getLogger(__name__)
Ejemplo n.º 26
0
 def __init__(self, spec, name_generator=None, atomic_inst=None):
     super(KubernetesService, self).__init__(None,
                                             name_generator=name_generator,
                                             atomic_inst=atomic_inst)
     self._spec = spec
     apiclient = config.new_client_from_config(spec.get("config_file"))
     self.api = core_v1_api.CoreV1Api(api_client=apiclient)
     self.events = []
Ejemplo n.º 27
0
 def client_from_config(config_file, context):
     # TODO(fabianvf): probably want to break this branch out or refactor method names
     try:
         return config.new_client_from_config(config_file, context)
     except (IOError, config.ConfigException):
         if not config_file:
             return ApiClient(configuration=Configuration())
         else:
             raise
Ejemplo n.º 28
0
    def __init__(self, config_file='/etc/kubernetes/admin.conf', logger=None):
        self.api_client = config.new_client_from_config(config_file)
        self.api_client.configuration.assert_hostname = False
        self.v1_h = client.CoreV1Api(self.api_client)
        self.v1_h.read_namespace('default')
        self.v1_beta_h = client.ExtensionsV1beta1Api(self.api_client)
        self.apps_v1_beta1_h = client.AppsV1beta1Api(self.api_client)

        self.logger = logger or contrail_logging.getLogger(__name__)
Ejemplo n.º 29
0
def get_pods_for_context(context, name):
    try:
        kube = client.CoreV1Api(config.new_client_from_config(context=context))
        pod_dicts = [pod.to_dict()
                     for pod in kube.list_pod_for_all_namespaces(watch=False,
                                                                 field_selector='metadata.namespace!=kube-system').items]
    except (kubernetes.client.rest.ApiException, kubernetes.config.config_exception.ConfigException):
        return []
    return [pod for pod in pod_dicts if re.match(name, pod['metadata']['name'])]
Ejemplo n.º 30
0
 def _load_user_config(self, user):
     if user not in self.user_configs:
         raise Exception("{} not found in configs".format(user))
     self.user = user
     if user not in self.api_cache:
         c = config.new_client_from_config(self.user_configs[self.user])
         self.api_cache[user] = (client.CoreV1Api(c),
                                 client.ExtensionsV1beta1Api(c))
     self.apiV1, self.apiV1beta1 = self.api_cache[user]
Ejemplo n.º 31
0
def dpv(cluster, pvname):    
    try: 
        api = client.CoreV1Api(api_client=config.new_client_from_config(context=cluster))
        pvn = pvname
        dpvs = api.delete_persistent_volume(name=pvn)
        output = str(dpvs) + "\n"
        return output
    except Exception as e:
        return("Exception e: ", str(e))
Ejemplo n.º 32
0
def get_ocp_secrets(namespace, in_cluster):
    if in_cluster:
        config.load_incluster_config()
        k8s_client = client.ApiClient()
    else:
        k8s_client = config.new_client_from_config()
    _client = DynamicClient(k8s_client)
    v1_secrets = _client.resources.get(api_version='v1', kind='Secret')
    return v1_secrets.get(namespace=namespace).items
Ejemplo n.º 33
0
 def __init__(self, config_file='/etc/kubernetes/admin.conf', logger=None):
     super(Client, self).__init__(config_file, logger)
     #Creating Dynamic API client
     k8s_client = config.new_client_from_config(config_file)
     dyn_client = DynamicClient(k8s_client)
     self.pod_h = dyn_client.resources.get(api_version='v1', kind='Pod')
     self.namespace_h = dyn_client.resources.get(api_version='v1', kind='Namespace')
     self.network_policy_h = dyn_client.resources.get(api_version='v1', kind='NetworkPolicy')
     self.deployment_h = dyn_client.resources.get(api_version='v1', kind='Deployment')
     self.service_h = dyn_client.resources.get(api_version='v1', kind='Service')
Ejemplo n.º 34
0
def _load_kube_config(in_cluster, cluster_context):
    from kubernetes import config, client
    if in_cluster:
        config.load_incluster_config()
        return client.CoreV1Api()
    else:
        if cluster_context is None:
            config.load_kube_config()
            return client.CoreV1Api()
        else:
            return client.CoreV1Api(
                api_client=config.new_client_from_config(context=cluster_context))
def get_fernet_key(composer_env):
    print("Retrieving fernet key for Composer Environment {}.".format(
        composer_env.get('name', '')))
    gke_cluster_resource = composer_env.get("config", {}).get("gkeCluster")
    project_zone_cluster = re.match(
        "projects/([^/]*)/zones/([^/]*)/clusters/([^/]*)", gke_cluster_resource
    ).groups()
    tmp_dir_name = None
    try:
        print("Getting cluster credentials {} to retrieve fernet key.".format(
            gke_cluster_resource))
        tmp_dir_name = tempfile.mkdtemp()
        kubeconfig_file = tmp_dir_name + "/config"
        os.environ["KUBECONFIG"] = kubeconfig_file
        if subprocess.call(
            [
                "gcloud",
                "container",
                "clusters",
                "get-credentials",
                project_zone_cluster[2],
                "--zone",
                project_zone_cluster[1],
                "--project",
                project_zone_cluster[0]
            ]
        ):
            print("Failed to retrieve cluster credentials: {}.".format(
                gke_cluster_resource))
            sys.exit(1)

        kubernetes_client = client.CoreV1Api(
            api_client=config.new_client_from_config(
                config_file=kubeconfig_file))
        airflow_configmap = kubernetes_client.read_namespaced_config_map(
            "airflow-configmap", "default")
        config_str = airflow_configmap.data['airflow.cfg']
        with contextlib.closing(six.StringIO(config_str)) as config_buffer:
            config_parser = configparser.ConfigParser()
            config_parser.readfp(config_buffer)
            return config_parser.get("core", "fernet_key")
    except Exception as exc:
        print("Failed to get fernet key for cluster: {}.".format(str(exc)))
        sys.exit(1)
    finally:
        if tmp_dir_name:
            shutil.rmtree(tmp_dir_name)