Ejemplo n.º 1
0
def get_resource_count(cluster_id, namespace_id=None):
    """
    Get count of resources for requested cluster and namespace
    :param cluster_id:
    :param namespace_id:
    :return:
    """
    # fetching namespaced resource count
    if namespace_id:
        # Deployment count
        deployment_count = len(
            client.AppsV1beta2Api().list_namespaced_deployment(
                namespace_id).items)
        # Pod count
        pod_items = client.CoreV1Api().list_namespaced_pod(namespace_id).items
        pod_count = len([
            pod_item for pod_item in pod_items
            if pod_item.status.phase == 'Running'
        ])
        # Cell count
        cell_pod_map = get_cell_pod_map(cluster_id)
        pods_list = [pod_item.metadata.name for pod_item in pod_items]
        cell_count = len(
            [cell_pod_map[pods] for pods in pods_list if pods in cell_pod_map])
    # fetching resource count for entire cluster
    else:
        # Deployment count
        deployment_count = len(
            client.AppsV1beta2Api().list_deployment_for_all_namespaces().items)
        # Pod count
        pod_count = len(client.CoreV1Api().list_pod_for_all_namespaces().items)
        # Cell count
        cell_count = len(get_compute_cell_data()['items'])
        # Removing resources related to the excluded namespace
        if namespace_is_hidden == 'true':
            resources_to_hide = get_hidden_namespace_resources(
                cluster_id, 'dashboard')
            deployment_count = 0 if deployment_count == 0 else deployment_count - resources_to_hide[
                'deployments']
            pod_count = 0 if pod_count == 0 else pod_count - resources_to_hide[
                'pods']
            cell_count = 0 if cell_count == 0 else cell_count - resources_to_hide[
                'cells']
    return {
        "deployments": deployment_count,
        "pods": pod_count,
        'cells': cell_count
    }
Ejemplo n.º 2
0
Archivo: main.py Proyecto: gortyy/k8s
    def create_deployment(self, deployment_object):
        container_port = client.V1ContainerPort(
            container_port=deployment_object.container_port, name=deployment_object.name
        )

        container = client.V1Container(
            image=deployment_object.image,
            name=deployment_object.name,
            env=deployment_object.env,
            ports=[container_port],
        )

        metadata = client.V1ObjectMeta(
            name=deployment_object.name, labels=deployment_object.labels
        )
        template = client.V1PodTemplateSpec(
            metadata=metadata, spec=client.V1PodSpec(containers=[container])
        )

        strategy = client.ExtensionsV1beta1DeploymentStrategy(
            type=deployment_object.strategy_type
        )
        selector = client.V1LabelSelector(match_labels=deployment_object.labels)
        spec = client.ExtensionsV1beta1DeploymentSpec(
            selector=selector, strategy=strategy, template=template
        )

        deployment = client.ExtensionsV1beta1Deployment(
            api_version="apps/v1beta2", kind="Deployment", metadata=metadata, spec=spec
        )

        api_client = client.AppsV1beta2Api()
        api_client.create_namespaced_deployment(body=deployment, namespace="default")
Ejemplo n.º 3
0
    def __init__(self, name, queue, timer_timeout=300):
        """
        For each controller that needs updated, write a custom annotation
        and maybe restart the necessary pods

        Receives a queue with what needs updating

        Main run thread is the only worker. Another thread is spawn for the update function.
        The main class thread and the update thread both use for_update dict
        to write/delete, so a lock is used to ensure they don't step on each other toes
        """

        threading.Thread.__init__(self)
        self.log = logging.getLogger(__name__ + " " + name)
        self.log.info("Init threadApplyChanges")
        global opsguru_signature
        self.timer_timeout = timer_timeout
        self.q = queue
        self.v1 = client.CoreV1Api()
        self.v1b1 = client.AppsV1beta1Api()
        self.v1b2 = client.AppsV1beta2Api()
        self.v1b1e = client.ExtensionsV1beta1Api()
        self.ann = annotations.Annotations()
        self.for_update = {}
        self.for_update_lock = threading.Lock()

        t = threading.Thread(target=self.update)
        t.daemon = True
        t.start()
Ejemplo n.º 4
0
    def post(self, request, types):
        if types == 'img':
            pj_id = request.POST.get('pid', None)
            print(pj_id)
            a = repitl.get_image_name(project_id=pj_id)
            print(a)
            return JsonResponse(a, safe=False)

        if types == 'tags':
            repo_name = request.POST.get('image')
            tags = repitl.get_tags(repo_name)
            print('tags:', tags)

            return JsonResponse(tags, safe=False)

        if types == 'dep':
            ns = request.POST.get('ns')
            config.load_kube_config()
            v1 = client.AppsV1beta2Api()
            ret = []
            tmp = v1.list_namespaced_deployment(namespace=ns).items
            for i in tmp:
                ret.append(i.metadata.name)
            return JsonResponse(ret, safe=False)

        if types == 'svc':
            ns = request.POST.get('ns')
            print(ns)
            config.load_kube_config()
            v1 = client.CoreV1Api()
            ret = []
            tmp = v1.list_namespaced_service(ns).items
            for i in tmp:
                ret.append(i.metadata.name)
            return JsonResponse(ret, safe=False)
Ejemplo n.º 5
0
 def createDeployment(applyName, image, servicePort):
     V1ContainerPort = client.V1ContainerPort(
         container_port=int(servicePort), protocol="TCP")
     V1Container = client.V1Container(name=applyName,
                                      image=image,
                                      ports=[V1ContainerPort])
     V1PodTemplateSpec = client.V1PodTemplateSpec(
         spec=client.V1PodSpec(containers=[V1Container]),
         metadata={"labels": {
             'k8s-app': applyName
         }})
     V1beta2DeploymentSpec = client.V1beta2DeploymentSpec(
         replicas=1,
         selector=client.V1LabelSelector(
             match_labels={"k8s-app": applyName}),
         template=V1PodTemplateSpec)
     V1ObjectMeta = client.V1ObjectMeta(namespace="default", name=applyName)
     V1beta2Deployment = client.V1beta2Deployment(
         spec=V1beta2DeploymentSpec,
         api_version="apps/v1beta2",
         kind="Deployment",
         metadata=V1ObjectMeta)
     pprint(V1beta2Deployment)
     k8s_api = client.AppsV1beta2Api()
     deployment = k8s_api.create_namespaced_deployment(
         body=V1beta2Deployment, namespace="default")
     pprint("Deployment create. status= '%s'" % deployment)
     return deployment
Ejemplo n.º 6
0
 def get(self, request, *args, **kwargs):
     config.load_kube_config()
     k8s_api = client.AppsV1beta2Api()
     deployments = k8s_api.list_deployment_for_all_namespaces().items
     return render(request,
                   template_name='dashboard/kubernetes/applications.html',
                   context={"deployments": deployments})
Ejemplo n.º 7
0
 def get_context_data(self, **kwargs):
     config.load_kube_config()
     v1 = client.AppsV1beta2Api()
     context = super(Dp_list, self).get_context_data(**kwargs)
     dp_list = v1.list_deployment_for_all_namespaces().items
     context['dp_list'] = dp_list
     return context
Ejemplo n.º 8
0
def check_existing():
    try:
        cluster_list = list_cluster_mongodb_object()
    except client.rest.ApiException as e:
        # If for any reason, k8s api gives us an error here, there is
        # nothing for us to do but retry later
        logging.exception(e)
        return False

    core_api = client.CoreV1Api()
    apps_api = client.AppsV1beta2Api()
    for cluster_object in cluster_list['items']:
        name = cluster_object['metadata']['name']
        namespace = cluster_object['metadata']['namespace']

        # Check service exists
        try:
            service = core_api.read_namespaced_service(name, namespace)
        except client.rest.ApiException as e:
            if e.status == 404:
                # Create missing service
                created_service = create_service(cluster_object)
                if created_service:
                    # Store latest version in cache
                    cache_version(created_service)
            else:
                logging.exception(e)
        else:
            if not is_version_cached(service):
                # Update since we don't know if it's configured correctly
                updated_service = update_service(cluster_object)
                if updated_service:
                    # Store latest version in cache
                    cache_version(updated_service)

        # Check statefulset exists
        try:
            statefulset = apps_api.read_namespaced_stateful_set(
                name, namespace)
        except client.rest.ApiException as e:
            if e.status == 404:
                # Create missing statefulset
                created_statefulset = create_statefulset(cluster_object)
                if created_statefulset:
                    # Store latest version in cache
                    cache_version(created_statefulset)
            else:
                logging.exception(e)
        else:
            if not is_version_cached(statefulset):
                # Update since we don't know if it's configured correctly
                updated_statefulset = update_statefulset(cluster_object)
                if updated_statefulset:
                    # Store latest version in cache
                    cache_version(updated_statefulset)

        # Check replica set status
        check_if_replicaset_needs_setup(cluster_object)
Ejemplo n.º 9
0
def delete_deploy(namespace, name):
    config.load_kube_config()
    k8s_api = client.AppsV1beta2Api()
    body = client.V1DeleteOptions(api_version="apps/v1beta2",
                                  propagation_policy='Foreground')
    V1status = k8s_api.delete_namespaced_deployment(body=body,
                                                    name=name,
                                                    namespace=namespace)
    return V1status
Ejemplo n.º 10
0
    def __init__(self, host, api_key=None, namespace='default'):
        self._config = Configuration()
        self._config.host = host
        if api_key:
            self._config.api_key['authorization'] = api_key
        self._api_client = ApiClient(self._config)

        self.apps_v1_beta2 = client.AppsV1beta2Api(api_client=self._api_client)
        self.core_v1 = client.CoreV1Api(api_client=self._api_client)
        self.batch_v1 = client.BatchV1Api(api_client=self._api_client)
        self.namespace = namespace
Ejemplo n.º 11
0
def create_resources(args):
    print("Creating cluster...")

    apps_v1beta2 = client.AppsV1beta2Api()
    core_v1 = client.CoreV1Api()

    create_secret_from_file(core_v1,
                            "scripts/kubernetes/kube-bootstrap-node.config")
    create_headless_service(core_v1)
    create_pod_service(core_v1, "us1", "chainweb-0")
    create_stateful_set(apps_v1beta2, args)
Ejemplo n.º 12
0
 def create_deployment(self, body):
     try:
         dep = yaml.load(body)
         k8s = client.AppsV1beta2Api()
         resp = k8s.create_namespaced_deployment(body=dep,
                                                 namespace=os.environ.get(
                                                     "NAMESPACE",
                                                     "default"))
         return resp
     except Exception as e:
         return e
Ejemplo n.º 13
0
def delete_resources(args):
    print("Deleting cluster...")

    apps_v1beta2 = client.AppsV1beta2Api()
    core_v1 = client.CoreV1Api()

    try_delete(delete_stateful_set, apps_v1beta2)
    try_delete(delete_secret, core_v1)
    try_delete(delete_headless_service, core_v1)
    try_delete(delete_pod_service, core_v1, "chainweb-0")
    try_delete(delete_persistent_volume, core_v1, "data-chainweb-0")
Ejemplo n.º 14
0
 def __init__(self, namespace='default'):
     self.client = kubernetes_client
     self.namespace = namespace
     kubernetes_config.load_incluster_config()
     self.v1 = kubernetes_client.CoreV1Api()
     self.v1b1 = kubernetes_client.AppsV1beta1Api()
     self.v1b2 = kubernetes_client.AppsV1beta2Api()
     self.v1b1Storage = kubernetes_client.StorageV1beta1Api()
     self.v1b1Extensions = kubernetes_client.ExtensionsV1beta1Api()
     self.AppsV1Api = kubernetes_client.AppsV1Api()
     self.v1_body_delete = kubernetes_client.V1DeleteOptions()
     self.batch_api = kubernetes_client.BatchV1Api()
Ejemplo n.º 15
0
def k8s_configuration():
    eks = auth.EKSAuth(cluster_id=CLUSTER_NAME)
    token = eks.get_token()

    # Configure
    config.load_kube_config(KUBE_FILEPATH)
    configuration = client.Configuration()
    configuration.api_key['authorization'] = K8S_TOKEN
    configuration.api_key_prefix['authorization'] = 'Bearer'
    # API
    api = client.ApiClient(configuration)
    v1 = client.CoreV1Api(api)
    v1_beta = client.AppsV1beta1Api(api)
    api_v1=client.AppsV1Api(api)
    v1_beta2 = client.AppsV1beta2Api(api)
    return v1, v1_beta, api_v1, v1_beta2
Ejemplo n.º 16
0
 def changeDeployment(deployment, namespace, image, port):
     config.load_kube_config()
     k8s_api = client.AppsV1beta2Api()
     body = k8s_api.read_namespaced_deployment(name=deployment,
                                               namespace=namespace)
     if image != None:
         body.spec.template.spec.containers[0].image = image
     else:
         print(image + "is none")
     if port != None:
         body.spec.template.spec.containers[0].ports[
             0].container_port = int(port)
     else:
         print(port + "is none")
     resp = k8s_api.patch_namespaced_deployment(name=deployment,
                                                namespace=namespace,
                                                body=body)
     return resp
Ejemplo n.º 17
0
    def kube_controller(self):
        # configuration = client.Configuration()
        # configuration.host = "https://c5b1d0b4a27474d47adeeb5132a397197.k8s-g1.cn-hangzhou.aliyuncs.com:6443"
        # configuration.verify_ssl = False
        # client.Configuration.set_default(configuration)
        config.load_kube_config()
        k8s_beta = client.AppsV1beta2Api()
        k8s_core = client.CoreV1Api()
        # deployment_list = self.kube_get_dep_list(k8s_beta)
        # return deployment_list

        # 处理deployment
        deployment_list = self.kube_get_dep_list(k8s_beta)
        if self.app_name in deployment_list:
            print("Deployment already exists, replace ...")
            try:
                self.kube_replace_dep(k8s_beta)
            except Exception as e:
                print("Deployment replace Failed!, message:")
                print(e)
        else:
            print("Deployment not exists, creat ...")
            try:
                self.kube_create_dep(k8s_beta)
            except Exception as e:
                print("Deployment create Failed! message:")
                print(e)

        # 处理Service
        # svc = v1.list_namespaced_service(namespace=self.namespace)
        # for i in svc.items:
        #     if re.search():
        #         pass
        service_list = self.kube_get_svc_list(k8s_core)
        if self.app_name in service_list:
            print("Service already exists, if not necessary, do not change!")
        else:
            print("None Services exists, create")
            try:
                self.kube_create_svc(k8s_core)
            except Exception as e:
                print("Service create Failed! message:")
                print(e)
Ejemplo n.º 18
0
def get_hidden_namespace_resources(cluster_id, namespace_id):
    """
    Get the resource details for the namespace to be excluded
    :param cluster_id:
    :param namespace_id:
    :return:
    """
    # Deployment count
    deployment_count = len(
        client.AppsV1beta2Api().list_namespaced_deployment(namespace_id).items)
    # Pod count
    pod_items = client.CoreV1Api().list_namespaced_pod(namespace_id).items
    pod_count = len(pod_items)
    # Cell count
    cell_pod_map = get_cell_pod_map(cluster_id)
    pods_list = [pod_item.metadata.name for pod_item in pod_items]
    cell_count = len([cell_pod_map[pods] for pods in pods_list])
    return {
        'deployments': deployment_count,
        'pods': pod_count,
        'cells': cell_count
    }
Ejemplo n.º 19
0
def main():
    config_logger()
    log().info('Starting kube-job-cleaner')
    parser = argparse.ArgumentParser()
    parser.add_argument('--dry-run', action='store_true', help='Dry run mode')
    args = parser.parse_args()

    if os.getenv('IN_CLUSTER', 'true') in ("yes", "true"):
        config.load_incluster_config()
    else:
        config.load_kube_config()

    log().info('Looking for expired "Deployment" objects')
    # create an instance of the API class
    api_instance = client.AppsV1beta2Api()
    # label_selector = 'label_selector_example'

    try: 
        api_response = api_instance.list_deployment_for_all_namespaces(watch=False)
        for deployment in api_response.items:
            log().info("Checking namespace {} deployment {} for expiration".format(deployment.metadata.namespace, deployment.metadata.name))
            delete_if_expired(args.dry_run, deployment, deployment_expired(deployment), api_instance)
    except ApiException as e:
        log().error("Exception when calling AppsV1beta2Api->list_deployment_for_all_namespaces: {}\n".format(e))
Ejemplo n.º 20
0
 def list_replica_sets(self):
     k8s = client.AppsV1beta2Api()
     return k8s.list_namespaced_replica_set(namespace=os.environ.get(
         "NAMESPACE", "default"),
                                            pretty=True)
Ejemplo n.º 21
0
def createDeployment(name, namespace, image, port, protocol, **kwargs):
    print(name, namespace, image, port, protocol, kwargs)
    labels = {}
    pprint(kwargs)
    if isinstance(kwargs['labelKey'], list) and isinstance(
            kwargs['labelValue'], list):
        for (key, value) in zip(kwargs['labelKey'], kwargs['labelValue']):
            labels[key] = value
    else:
        labels[kwargs['labelKey']] = kwargs['labelValue']
    V1ObjectMeta = client.V1ObjectMeta(namespace="default",
                                       name=name,
                                       labels=labels)
    ports = []
    if isinstance(port, str):
        ports.append(
            client.V1ContainerPort(container_port=int(port),
                                   protocol=protocol))
    else:
        for (Port, Protocol) in zip(port, protocol):
            ports.append(
                client.V1ContainerPort(container_port=int(Port),
                                       protocol=Protocol))
    env = []
    if 'envKay' in kwargs.keys() and 'envValue' in kwargs.keys():
        for (k, v) in zip(kwargs['envKey'], ['envValue']):
            env.append(client.V1EnvVar(name=k, value=v))
    if kwargs['command']:
        command = kwargs['command']
        print('命令错误')
    else:
        command = None
    if kwargs['args']:
        args = kwargs['args']
    else:
        args = None
    V1Container = client.V1Container(name=name,
                                     image=image,
                                     ports=ports,
                                     command=command,
                                     args=args,
                                     env=env)
    V1PodTemplateSpec = client.V1PodTemplateSpec(
        spec=client.V1PodSpec(containers=[V1Container]), metadata=V1ObjectMeta)
    V1beta2DeploymentSpec = client.V1beta2DeploymentSpec(
        replicas=int(kwargs['rc']),
        selector=client.V1LabelSelector(match_labels=labels),
        template=V1PodTemplateSpec)

    V1beta2Deployment = client.V1beta2Deployment(
        spec=V1beta2DeploymentSpec,
        # api_version="apps/v1beta2",
        kind="Deployment",
        metadata=V1ObjectMeta)
    pprint(V1beta2Deployment)
    config.load_kube_config()
    k8s_api = client.AppsV1beta2Api()
    deployment = k8s_api.create_namespaced_deployment(body=V1beta2Deployment,
                                                      namespace=namespace)
    pprint("Deployment create. status= '%s'" % deployment)
    return deployment
Ejemplo n.º 22
0
def update_cluster_config():
    #获取环境变量
    namespace = os.environ['POD_NAMESPACE']
    service_name = os.environ['REDIS_SERVICE_NAME']
    conf_file = os.environ['REDIS_CONFIG_FILE']
    apiserver = 'https://' + os.environ[
        'KUBERNETES_SERVICE_HOST'] + ":" + os.environ['KUBERNETES_SERVICE_PORT']
    pod_name = os.environ['POD_NAME']
    st_name = pod_name[0:pod_name.rfind('-')]

    redis_conf = get_redis_conf(conf_file)
    #调用API获取service所有endpoint
    with open('/var/run/secrets/kubernetes.io/serviceaccount/token',
              'r') as file:
        Token = file.read().strip('\n')

    configuration = client.Configuration()
    configuration.host = apiserver
    configuration.verify_ssl = False
    configuration.api_key = {"authorization": "Bearer " + Token}
    client.Configuration.set_default(configuration)
    v1 = client.CoreV1Api()
    endpoints = v1.list_namespaced_endpoints(namespace,
                                             field_selector='metadata.name=' +
                                             service_name)
    redis_port = 0
    pods = []
    if endpoints.items[0].subsets and endpoints.items[0].subsets != 'None':
        redis_port = endpoints.items[0].subsets[0].ports[0].port
        pods = endpoints.items[0].subsets[0].addresses

    myip = get_myself_ip()
    myport = redis_conf['port']
    if 'requirepass' in redis_conf:
        redis_passwd = redis_conf['requirepass']
    else:
        redis_passwd = ''
    #读取cluter.conf文件
    cluster_file = redis_conf['cluster-config-file']
    lines = ''
    line_new = ''
    if os.path.exists(cluster_file):
        conf = open(cluster_file, 'r+')
        lines = conf.read()
        line_new = lines

    #本脚本先于redis-server启动,判断本节点是否已加入集群
    if not os.path.exists(cluster_file) or (lines
                                            and len(lines.split('\n')) < 4):
        print("%s [%s] %s" %
              (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())),
               'info', 'this node is not a part of cluster yet!'))
        #判断是否已存在集群
        clusters = []
        cluster_exists = 0
        if pods:
            for pod in pods:
                cluster_info = get_redis_cluster_info(pod.ip, redis_port,
                                                      redis_passwd)
                if cluster_info['in_cluster'] == 1: cluster_exists = 1
                clusters.append(cluster_info)
        print(clusters)
        if cluster_exists == 1:
            print("%s [%s] %s" %
                  (time.strftime('%Y-%m-%d %H:%M:%S',
                                 time.localtime(time.time())), 'info',
                   'there is a redis cluster exists,do nothing'))
            return
        #判断当前是否最后一个启动的Pod
        v1b2 = client.AppsV1beta2Api()
        st = v1b2.list_namespaced_stateful_set(
            namespace, field_selector='metadata.name=' + st_name)
        if not st.items:
            print(
                "%s [%s] %s" %
                (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(
                    time.time())), 'error', st_name + ' not exists'))
            return
        replicas = st.items[0].spec.replicas
        current_replicas = st.items[0].status.current_replicas
        if current_replicas == replicas:
            print("%s [%s] %s" %
                  (time.strftime('%Y-%m-%d %H:%M:%S',
                                 time.localtime(time.time())), 'info',
                   'this pod is the last replica,begin create cluster!'))
            cluster_instances = [myip + ':' + str(myport)]
            for pod in pods:
                if pod.ip + ':' + str(redis_port) not in cluster_instances:
                    cluster_instances.append(pod.ip + ':' + str(redis_port))
            start_redis = os.system("echo 'daemonize yes' >> " + conf_file)
            start_redis = os.system('redis-server ' + conf_file)
            rs = os.popen(
                "echo 'yes'|redis-cli --cluster create --cluster-replicas 1 -a %s --no-auth-warning %s"
                % (redis_passwd, (' ').join(cluster_instances)))
            res = rs.read()
            print("%s [%s] %s" %
                  (time.strftime('%Y-%m-%d %H:%M:%S',
                                 time.localtime(time.time())), 'info', res))
            os.system('pkill redis-server')
            start_redis = os.system("echo 'daemonize no' >> " + conf_file)
        else:
            print("%s [%s] %s" %
                  (time.strftime('%Y-%m-%d %H:%M:%S',
                                 time.localtime(time.time())), 'info',
                   'this pod is not the last replica,do nothing!'))

        return

    redis_nodes = []
    if myip:
        for line in lines.split('\n'):
            row = line.split(' ')
            if len(row) > 7 and row[2].split(',')[0] == 'myself':
                line_new = re.sub(
                    row[0] + ' ' + row[1], row[0] + ' ' + myip + ':' +
                    str(myport) + '@' + row[1].split('@')[1], line_new)

    try:
        #把IP和Node_id对应起来
        print("%s [%s] %s" % (time.strftime(
            '%Y-%m-%d %H:%M:%S', time.localtime(time.time())), 'info', pods))

        if pods:
            for pod in pods:
                cluster_info = get_redis_cluster_info(pod.ip, redis_port,
                                                      redis_passwd)
                if cluster_info:
                    redis_nodes.append({
                        'host': pod.ip,
                        'port': redis_port,
                        'node_id': cluster_info['node_id']
                    })
            print(
                "%s [%s] %s" %
                (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(
                    time.time())), 'info', redis_nodes))
            #根据node_id替换cluster.conf文件中的ip
            for line in lines.split('\n'):
                row = line.split(' ')

                for node in redis_nodes:
                    if len(row) > 7 and node['node_id'] == row[0]:
                        line_new = re.sub(
                            row[0] + ' ' + row[1],
                            row[0] + ' ' + node['host'] + ':' +
                            str(node['port']) + '@' + row[1].split('@')[1],
                            line_new)
    except Exception as e:
        print("%s [%s] %s" %
              (time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(
                  time.time())), 'error', str(e)))

    #重写cluster.conf文件
    conf.seek(0)
    conf.truncate()
    conf.write(line_new)
    conf.close()
Ejemplo n.º 23
0
def deployments_list(request):
    config.load_kube_config()
    k8s_api = client.AppsV1beta2Api()
    deployments = k8s_api.list_deployment_for_all_namespaces().items
    return render(request, 'dashboard/kubernetes/deployments.html',
                  {"deployments": deployments})
Ejemplo n.º 24
0
def getDeployment(name, namespace):
    config.load_kube_config()
    k8s_api = client.AppsV1beta2Api()
    resp = k8s_api.read_namespaced_deployment(name, namespace)
    return resp
Ejemplo n.º 25
0
def get_deployment_data(cluster_id, namespace_id=None, deployment_id=None):
    """
    Fetch deployment listing and detail
    :param deployment_id:
    :param namespace_id:
    :param cluster_id
    :return:
    """
    # global pods_for_resource_calculation
    # deployment detail
    if deployment_id and namespace_id is not None:
        # creating cell-pod mapping for getting cell details
        cell_pod_map = get_cell_pod_map(cluster_id)
        # getting pod count
        pods_data = [
            pod
            for pod in get_pod_data(cluster_id, namespace_id, deployment_id)
        ]
        core_api = client.CoreV1Api()
        apps_api = client.AppsV1Api()
        deployment_cell_list = list()
        deployment_pod_list = list()
        for pod_name in pods_data:
            if pod_name['name'] in cell_pod_map:
                namespaced_pod_info = core_api.read_namespaced_pod(
                    pod_name['name'],
                    namespace_id).metadata.owner_references[0]
                if namespaced_pod_info.kind == 'ReplicaSet':
                    replica_set = apps_api.read_namespaced_replica_set(
                        core_api.read_namespaced_pod(
                            pod_name['name'],
                            namespace_id).metadata.owner_references[0].name,
                        namespace_id)
                    if replica_set.metadata.owner_references[0].name == deployment_id and pod_name['name'] in cell_pod_map \
                            and pod_name['status']=='Running':
                        # fetching pods based on deployment
                        deployment_pod_list.append(pod_name['name'])
                        # fetching cells based on pods and deployment
                        deployment_cell_list.append(
                            cell_pod_map[pod_name['name']]['cell_name'])
                else:
                    continue
            else:
                continue
        # if there are no pods for the passed deployment
        if len(deployment_pod_list) == 0:
            pods_for_resource_calculation = 'no_pod_resource'
        else:
            pods_for_resource_calculation = deployment_pod_list

        deployments_info = {
            'resource_count': {
                'cells': len(deployment_cell_list),
                'pods': len(deployment_pod_list)
            },
            'resource_info':
            get_resource_info(cluster_id, 'pods', namespace_id,
                              pods_for_resource_calculation)
        }

    # deployment listing
    else:
        if namespace_id:
            deployments_info = [{
                'value': deployment_item.metadata.name,
                'label': deployment_item.metadata.name
            } for deployment_item in client.AppsV1beta2Api().
                                list_namespaced_deployment(namespace_id).items]
        else:
            deployments_info = [{
                'value': deployment_item.metadata.name,
                'label': deployment_item.metadata.name
            } for deployment_item in client.AppsV1beta2Api().
                                list_deployment_for_all_namespaces().items]
    return deployments_info
Ejemplo n.º 26
0
def collect_garbage():
    core_api = client.CoreV1Api()
    apps_api = client.AppsV1beta2Api()
    label_selector = get_default_label_selector()

    # Find all services that match our labels
    try:
        service_list = core_api.list_service_for_all_namespaces(
            label_selector=label_selector)
    except client.rest.ApiException as e:
        logging.exception(e)
    else:
        # Check if service belongs to an existing cluster
        for service in service_list.items:
            name = service.metadata.name
            namespace = service.metadata.namespace

            try:
                get_namespaced_mongodb_object(name, namespace)
            except client.rest.ApiException as e:
                if e.status == 404:
                    # Delete service
                    delete_service(name, namespace)
                else:
                    logging.exception(e)

    # Find all statefulsets that match our labels
    try:
        statefulset_list = apps_api.list_stateful_set_for_all_namespaces(
            label_selector=label_selector)
    except client.rest.ApiException as e:
        logging.exception(e)
    else:
        # Check if statefulsets belongs to an existing cluster
        for statefulset in statefulset_list.items:
            name = statefulset.metadata.name
            namespace = statefulset.metadata.namespace

            try:
                get_namespaced_mongodb_object(name, namespace)
            except client.rest.ApiException as e:
                if e.status == 404:
                    # Gracefully delete statefulsets and pods
                    delete_statefulset(name, namespace)
                else:
                    logging.exception(e)

    # Find all secrets that match our labels
    try:
        secret_list = core_api.list_secret_for_all_namespaces(
            label_selector=label_selector)
    except client.rest.ApiException as e:
        logging.exception(e)
    else:
        # Check if secrets belongs to an existing cluster
        for secret in secret_list.items:
            cluster_name = secret.metadata.labels['cluster']
            secret_name = secret.metadata.name
            namespace = secret.metadata.namespace

            try:
                get_namespaced_mongodb_object(cluster_name, namespace)
            except client.rest.ApiException as e:
                if e.status == 404:
                    # Delete service
                    delete_secret(secret_name, namespace)
                else:
                    logging.exception(e)
Ejemplo n.º 27
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'])
    clusterName, _ = pick(contexts,
                          title="Pick the context to load",
                          default_index=active_index)

    config.load_kube_config(context=clusterName)

    all_rows = []
    new_row = {}

    appsV1Beta2Api = client.AppsV1beta2Api()
    coreV1Api = client.CoreV1Api()
    networkingV1Api = client.NetworkingV1Api()
    networkingV1beta1Api = client.NetworkingV1beta1Api()
    extensionsV1beta1Api = client.ExtensionsV1beta1Api()

    api_response = coreV1Api.list_namespace(watch=False,
                                            _preload_content=False)

    api_response = json.loads(api_response.data)
    namespaces = api_response["items"]

    api_response = coreV1Api.list_endpoints_for_all_namespaces(
        watch=False, _preload_content=False)

    api_response = json.loads(api_response.data)
    endpoints = api_response["items"]

    api_response = extensionsV1beta1Api.list_ingress_for_all_namespaces(
        watch=False, _preload_content=False)

    api_response = json.loads(api_response.data)
    ingresses = api_response["items"]

    for namespace in namespaces:
        ingress_row = {
            'kind': 'Ingress',
            'name': "missing",
            'className': 'missing',
            'children': []
        }
        new_row = {
            "kind": "Namespace",
            "name": namespace["metadata"]["name"],
            "className": "namespace",
            "children": []
        }
        new_row["children"].append(ingress_row)
        all_rows.append(new_row)

    for ingress in ingresses:
        namespace = ingress["metadata"]["namespace"]

        for idx, item in enumerate(all_rows):
            if item["className"] == "namespace" and item["name"] == namespace:
                if ingress["metadata"]["name"] not in [
                        x['name'] for x in item["children"]
                ]:
                    new_row = {
                        'kind': 'Ingress',
                        'name': ingress["metadata"]["name"],
                        'className': 'ingress',
                        'children': []
                    }
                    item["children"].append(new_row)

        for rule in ingress["spec"]["rules"]:
            for service in rule["http"]["paths"]:
                serviceName = service["backend"]["serviceName"]
                path = service["path"]

                for namespace_idx, namespace_item in enumerate(all_rows):
                    if namespace_item[
                            "className"] == "namespace" and namespace_item[
                                "name"] == namespace:
                        for ingress_idx, ingress_item in enumerate(
                                all_rows[namespace_idx]["children"]):
                            if ingress_item[
                                    "className"] == "ingress" and ingress_item[
                                        "name"] == ingress["metadata"]["name"]:
                                if serviceName not in [
                                        x['name']
                                        for x in ingress_item["children"]
                                ]:
                                    new_row = {
                                        'kind': 'Service',
                                        'name': serviceName,
                                        'ingressPath': path,
                                        'className': 'service',
                                        'children': []
                                    }
                                    ingress_item["children"].append(new_row)
    '''
        The endpoints response contains namespace, services name, pod name, node name and endpoint IP so we 
        will parse all endpoints and add them to the required structure for the org chart
    '''

    for endpoint in endpoints:
        namespace = endpoint["metadata"]["namespace"]

        if namespace not in [x['name'] for x in all_rows]:
            new_row = {
                'kind': 'Namespace',
                'name': namespace,
                'className': 'namespace',
                'children': []
            }
            all_rows.append(new_row)

        serviceName = endpoint["metadata"]["name"]

        missing_ingress_idx = 0
        ingress_missing = True

        for namespace_idx, namespace_item in enumerate(all_rows):
            if namespace_item["className"] == "namespace" and namespace_item[
                    "name"] == namespace:
                for ingress_idx, ingress_item in enumerate(
                        all_rows[namespace_idx]["children"]):
                    for service_idx, service_item in enumerate(
                            all_rows[namespace_idx]["children"][ingress_idx]
                        ["children"]):
                        if serviceName in service_item["name"]:
                            ingress_missing = False
                            if "subsets" in endpoint:
                                for subset in endpoint["subsets"]:
                                    for address in subset["addresses"]:
                                        if "ip" in address:
                                            ip = address["ip"]
                                        if "nodeName" in address:
                                            nodeName = address["nodeName"]
                                        if "targetRef" in address:
                                            podName = address["targetRef"][
                                                "name"]

                                        new_row = {
                                            'kind': 'Pod',
                                            'name': podName,
                                            'node': nodeName,
                                            'ip': ip,
                                            "className": "pod"
                                        }
                                        service_item["children"].append(
                                            new_row)

                # No ingress was found with this service name so add it to the  "Missing" ingress which will be invisible
                if ingress_missing:
                    if serviceName not in [
                            x['name'] for x in all_rows[namespace_idx]
                        ["children"][missing_ingress_idx]["children"]
                    ]:
                        pod_row = {}
                        if "subsets" in endpoint:
                            for subset in endpoint["subsets"]:
                                for address in subset["addresses"]:
                                    if "ip" in address:
                                        ip = address["ip"]
                                    if "nodeName" in address:
                                        nodeName = address["nodeName"]
                                    if "targetRef" in address:
                                        podName = address["targetRef"]["name"]

                                    pod_row = {
                                        'kind': 'Pod',
                                        'name': podName,
                                        'node': nodeName,
                                        'ip': ip,
                                        "className": "pod"
                                    }

                        new_row = {
                            'kind': 'Service',
                            'name': serviceName,
                            'className': 'service',
                            'children': []
                        }
                        new_row["children"].append(pod_row)
                        all_rows[namespace_idx]["children"][
                            missing_ingress_idx]["children"].append(new_row)

    cluster_row = {
        "kind": "K8s Cluster",
        "name": clusterName,
        "children": all_rows
    }

    final_output = "var datasource = {};".format(cluster_row)

    with open("assets/data/k8s_data.js", "w") as out:
        out.write(final_output)

    webbrowser.open('file://' + os.path.realpath("index.html"))
Ejemplo n.º 28
0
 def delete_replica_set(self, name):
     k8s = client.AppsV1beta2Api()
     resp = k8s.delete_namespaced_replica_set(name=name,
                                              namespace=os.environ.get(
                                                  "NAMESPACE", "default"))
     return resp
Ejemplo n.º 29
0
try:
    config.load_incluster_config()
except config.config_exception.ConfigException:
    config.load_kube_config()

namespace = os.environ.get('NAMESPACE', 'default')

kinds_lookup = {
    'pods': {
        'should_watch': os.environ.get('WATCH_PODS', False),
        'stream': partial(client.CoreV1Api().list_namespaced_pod, namespace=namespace)
    },
    'deployments': {
        'should_watch': os.environ.get('WATCH_DEPLOYMENTS', True),
        'stream': partial(client.AppsV1beta2Api().list_namespaced_deployment, namespace=namespace)
    },
    'services': {
        'should_watch': os.environ.get('WATCH_SERVICES', False),
        'stream': partial(client.CoreV1Api().list_namespaced_service, namespace=namespace)
    },
    'configmaps': {
        'should_watch': os.environ.get('WATCH_CONFIGMAPS', False),
        'stream': partial(client.CoreV1Api().list_namespaced_config_map, namespace=namespace)
    },
    'secrets': {
        'should_watch': os.environ.get('WATCH_SECRETS', False),
        'stream': partial(client.CoreV1Api().list_namespaced_secret, namespace=namespace)
    },
    'serviceaccounts': {
        'should_watch': os.environ.get('WATCH_SERVICEACCOUNTS', False),
Ejemplo n.º 30
0
 def __init__(self, kubernetes_admin_conf):
     config.load_kube_config(config_file=kubernetes_admin_conf)
     self.core_v1 = client.CoreV1Api()
     self.apps_v1 = client.AppsV1beta2Api()