Exemple #1
0
 def _detect_api_object(self, api_version):
     # Due to https://github.com/kubernetes-client/python/issues/387
     if api_version == 'apps/v1beta1':
         return client.AppsV1beta1Api()
     if api_version == 'v1':
         return client.CoreV1Api()
     if api_version == 'extensions/v1beta1':
         return client.ExtensionsV1beta1Api()
     if api_version == 'batch/v1':
         return client.BatchV1Api()
     if api_version == 'batch/v2alpha1':
         return client.BatchV2alpha1Api()
     if api_version == 'batch/v1beta1':
         return client.BatchV1beta1Api()
     if api_version == 'policy/v1beta1':
         return client.PolicyV1beta1Api()
     if api_version == 'storage.k8s.io/v1':
         return client.StorageV1Api()
     if api_version == 'apps/v1':
         return client.AppsV1Api()
     if api_version == 'autoscaling/v1':
         return client.AutoscalingV1Api()
     if api_version == 'rbac.authorization.k8s.io/v1':
         return client.RbacAuthorizationV1Api()
     if api_version == 'scheduling.k8s.io/v1alpha1':
         return client.SchedulingV1alpha1Api()
     if api_version == 'scheduling.k8s.io/v1beta1':
         return client.SchedulingV1beta1Api()
     if api_version == 'test/test':
         return K8sClientMock(self.name)
 def get_resource_api(api_client: client.ApiClient = None,
                      **kwargs) -> "client.BatchV2alpha1Api":
     """
     Returns an instance of the kubernetes API client associated with
     this object.
     """
     if api_client:
         kwargs["apl_client"] = api_client
     return client.BatchV2alpha1Api(**kwargs)
Exemple #3
0
def get_cronjobs(request):
    user = user_get(request)
    hostname = request.GET.get('hostname')

    auth_config(user, hostname)
    batch_api = client.BatchV2alpha1Api()

    namespace = request.GET.get("namespace")
    if request.method == "GET":
        pageIndex = request.GET.get("pageIndex")
        pageSize = request.GET.get("pageSize")

        list = []
        res = []
        try:
            for cj in batch_api.list_namespaced_cron_job(namespace).items:
                dict = {}
                dict["name"] = cj.metadata.name
                dict["namespace"] = cj.metadata.namespace
                dict["schedule"] = cj.spec.schedule
                if cj.status.last_schedule_time:
                    dict["lastScheduleTime"] = dt_format(
                        cj.status.last_schedule_time)
                else:
                    dict["lastScheduleTime"] = ""
                print(cj.status.last_schedule_time)
                dict["create_time"] = dt_format(cj.metadata.creation_timestamp)
                list.append(dict)

            pageInator = Paginator(list, pageSize)
            context = pageInator.page(pageIndex)
            for item in context:
                res.append(item)
            data = {
                "code": 0,
                "msg": "ok",
                "DataCount": len(list),
                "data": res
            }
        except Exception as e:
            data = error(e)
        return JsonResponse(data)
    elif request.method == "DELETE":
        request_data = QueryDict(request.body)
        name = request_data.get("name")
        namespace = request_data.get("namespace")
        try:
            batch_api.delete_namespaced_cron_job(name, namespace)
            data = {"code": 0, "msg": "删除成功."}
        except Exception as e:
            data = error(e)
        return JsonResponse(data)
Exemple #4
0
def yaml_json_add(request):
    user = user_get(request)
    hostname = request.POST.get('hostname')
    auth_config(user, hostname)
    core_api = client.CoreV1Api()  # namespace,pod,service,pv,pvc
    apps_api = client.AppsV1Api()  # deployment
    batch1_api = client.BatchV1Api()
    batch2_api = client.BatchV2alpha1Api()
    networking_api = client.NetworkingV1beta1Api()  # ingress
    storage_api = client.StorageV1Api()  # storage_class
    namespace = request.POST.get('namespace', None)
    resource = request.POST.get('resource', None)
    name = request.POST.get('name', None)
    types = request.POST.get('types')
    if types == "yaml":
        body = request.POST.get('yaml', None)
        body = yaml.safe_load(body)
    else:
        body = request.POST.get('json', None)
    try:
        if body['kind'] == "Namespace":
            core_api.create_namespace(body)
        elif body['kind'] == "Deployment":
            apps_api.create_namespaced_deployment(namespace, body)
        elif body['kind'] == "DaemonSet":
            apps_api.create_namespaced_daemon_set(namespace, body)
        elif body['kind'] == "StatefulSet":
            apps_api.create_namespaced_stateful_set(namespace, body)
        elif body['kind'] == "Service":
            core_api.create_namespaced_service(namespace, body)
        elif body['kind'] == "Pod":
            core_api.create_namespaced_pod(namespace, body)
        elif body['kind'] == "Ingress":
            networking_api.create_namespaced_ingress(namespace, body)
        elif body['kind'] == "PersistentVolume":
            core_api.create_persistent_volume(body)
        elif body['kind'] == "PersistentVolumeClaim":
            core_api.create_namespaced_persistent_volume_claim(namespace, body)
        elif body['kind'] == "ConfigMap":
            core_api.create_namespaced_config_map(namespace, body)
        elif body['kind'] == "Secret":
            core_api.create_namespaced_secret(namespace, body)
        elif body['kind'] == "CronJob":
            batch2_api.create_namespaced_cron_job(namespace, body)
        elif body['kind'] == "Job":
            batch1_api.create_namespaced_job(namespace, body)
        data = {"code": 0, "msg": f"{body['kind']}创建成功"}
        return data
    except Exception as e:
        data = error(e)
        return data
Exemple #5
0
    def __init__(self):
        self.logger = logging.getLogger(__name__)
        try:
            config.load_kube_config()
        except:
            self.logger.warning("unable to load kube-config")

        self.v1 = client.CoreV1Api()
        self.v1Beta1 = client.AppsV1beta1Api()
        self.extensionsV1Beta1 = client.ExtensionsV1beta1Api()
        self.autoscalingV1Api = client.AutoscalingV1Api()
        self.rbacApi = client.RbacAuthorizationV1beta1Api()
        self.batchV1Api = client.BatchV1Api()
        self.batchV2Api = client.BatchV2alpha1Api()
Exemple #6
0
    def __init__(self):
        self.logger = logging.getLogger(__name__)
        try:
            config_file = os.path.expanduser(kubeconfig_filepath)
            config.load_kube_config(config_file=config_file)
        except:
            self.logger.warning("unable to load kube-config")

        self.v1 = client.CoreV1Api()
        self.v1Beta1 = client.AppsV1beta1Api()
        self.extensionsV1Beta1 = client.ExtensionsV1beta1Api()
        self.autoscalingV1Api = client.AutoscalingV1Api()
        self.rbacApi = client.RbacAuthorizationV1beta1Api()
        self.batchV1Api = client.BatchV1Api()
        self.batchV2Api = client.BatchV2alpha1Api()

        self._namespace = self._parse_namespace()
        self._cached_resources: Dict[str, List[str]] = {}
        self._init_cached_resources(self._namespace)
Exemple #7
0
before = int(os.environ['BEFORE']) if 'BEFORE' in os.environ else 3
timezone = os.environ['TZ'] if 'TZ' in os.environ else 'Europe/Madrid'

if __name__ == "__main__":
    if 'PATTERNS' not in os.environ:
        print("Missing patterns.Leaving...")
        os._exit(1)
    else:
        patterns = os.environ['PATTERNS'].split(',')
    if 'KUBERNETES_PORT' in os.environ:
        config.load_incluster_config()
        template = "/tmp/crontemplate.yml"
    else:
        config.load_kube_config()
        template = "crontemplate.yml"
    cli = client.BatchV2alpha1Api()
    # clean old crons
    old_crons = [
        item.metadata.name
        for item in cli.list_namespaced_cron_job(namespace=namespace).items
    ]
    for old in cli.list_namespaced_cron_job(namespace=namespace).items:
        if old.metadata.name == cronmaster:
            continue
        print("Removing old cronjob %s" % old.metadata.name)
        cli.delete_namespaced_cron_job(old.metadata.name, namespace, {})
    with open(template) as data:
        oribody = yaml.load(data)
    newentries = googlecalendar.get_events(timezone=timezone,
                                           days=days,
                                           patterns=patterns,
Exemple #8
0
def main():
    config.load_incluster_config()
    #config.load_kube_config()

    apps_beta1 = client.AppsV1beta1Api()
    crds = client.CustomObjectsApi()
    v1 = client.CoreV1Api()
    batch = client.BatchV2alpha1Api()

    def create_meta(app):
        controller_ref = {
            "apiVersion": app._apiversion.rstrip("/v1"),
            "blockOwnerDeletion": True,
            "kind": app._kind,
            "name": app.crd_name(),
            "uid": app._metadata["uid"],
        }
        job = batch.create_namespaced_cron_job(namespace="default",
                                               body=app.cronjob(
                                                   [controller_ref]))
        logging.warning("Created CronJob for App: %s", job.metadata.name)
        logging.warning("Owner's reference: %s", json.dumps(controller_ref))

    def update_meta(app):
        try:
            create_meta(app)
        except ApiException as e:
            if e.status != httplib.CONFLICT:
                raise e

        # Tear down any versions that shouldn't exist.
        #delete_meta(app.other_versions())

    def delete_meta(selector):
        # Handle random namespace later...
        namespace = "default"
        for job in batch.list_namespaced_cron_job(
                namespace, label_selector=selector).items:
            batch.delete_namespaced_cron_job(
                job.metadata.name,
                namespace,
                body=client.V1DeleteOptions(propagation_policy='Foreground',
                                            grace_period_seconds=5))
            logging.warning("Deleted the CronJob for: %s", job.metadata.name)

    def process_meta(t, app, obj):
        if t == "DELETED":
            delete_meta(app.any_versions())
            logging.warning("Deleted CRD, check garbage collection")
        elif t in ["MODIFIED", "ADDED"]:
            update_meta(app)
        else:
            logging.error("Unrecognized type: %s", t)

    # hack, using default namespace, default service account to get a token for kubecfg to work
    token = v1.read_namespaced_service_account(namespace="default",
                                               name="default").secrets[0].name
    resource_version = ""
    while True:
        stream = watch.Watch().stream(crds.list_cluster_custom_object,
                                      DOMAIN,
                                      VERSION,
                                      PLURAL,
                                      resource_version=resource_version)
        for event in stream:
            try:
                t = event["type"]
                obj = event["object"]
                print obj
                app = App(obj, token)
                logging.warning("Apps %s, %s" % (app.crd_name(), t))
                process_meta(t, app, obj)

                # Configure where to resume streaming.
                metadata = obj.get("metadata")
                if metadata:
                    resource_version = metadata["resourceVersion"]
            except:
                logging.exception("Error handling event")
Exemple #9
0
    def get_resources(self, resource, namespace):
        names = []
        config.load_kube_config()

        v1 = client.CoreV1Api()
        v1Beta1 = client.AppsV1beta1Api()
        extensionsV1Beta1 = client.ExtensionsV1beta1Api()
        autoscalingV1Api = client.AutoscalingV1Api()
        rbacAPi = client.RbacAuthorizationV1beta1Api()
        batchV1Api = client.BatchV1Api()
        batchV2Api = client.BatchV2alpha1Api()

        if resource == "pod":
            ret = v1.list_pod_for_all_namespaces(watch=False)
        elif resource == "service":
            ret = v1.list_service_for_all_namespaces(watch=False)
        elif resource == "deployment":
            ret = v1Beta1.list_deployment_for_all_namespaces(watch=False)
        elif resource == "statefulset":
            ret = v1Beta1.list_stateful_set_for_all_namespaces(watch=False)
        elif resource == "node":
            ret = v1.list_node(watch=False)
        elif resource == "namespace":
            ret = v1.list_namespace(watch=False)
        elif resource == "daemonset":
            ret = extensionsV1Beta1.list_daemon_set_for_all_namespaces(watch=False)
        elif resource == "networkpolicy":
            ret = extensionsV1Beta1.list_network_policy_for_all_namespaces(watch=False)
        elif resource == "thirdpartyresource":
            ret = extensionsV1Beta1.list_third_party_resource(watch=False)
        elif resource == "replicationcontroller":
            ret = v1.list_replication_controller_for_all_namespaces(watch=False)
        elif resource == "replicaset":
            ret = extensionsV1Beta1.list_replica_set_for_all_namespaces(watch=False)
        elif resource == "ingress":
            ret = extensionsV1Beta1.list_ingress_for_all_namespaces(watch=False)
        elif resource == "endpoints":
            ret = v1.list_endpoints_for_all_namespaces(watch=False)
        elif resource == "configmap":
            ret = v1.list_config_map_for_all_namespaces(watch=False)
        elif resource == "event":
            ret = v1.list_event_for_all_namespaces(watch=False)
        elif resource == "limitrange":
            ret = v1.list_limit_range_for_all_namespaces(watch=False)
        elif resource == "configmap":
            ret = v1.list_config_map_for_all_namespaces(watch=False)
        elif resource == "persistentvolume":
            ret = v1.list_persistent_volume(watch=False)
        elif resource == "secret":
            ret = v1.list_secret_for_all_namespaces(watch=False)
        elif resource == "resourcequota":
            ret = v1.list_resource_quota_for_all_namespaces(watch=False)
        elif resource == "componentstatus":
            ret = v1.list_component_status(watch=False)
        elif resource == "podtemplate":
            ret = v1.list_pod_template_for_all_namespaces(watch=False)
        elif resource == "serviceaccount":
            ret = v1.list_service_account_for_all_namespaces(watch=False)
        elif resource == "horizontalpodautoscaler":
            ret = autoscalingV1Api.list_horizontal_pod_autoscaler_for_all_namespaces(watch=False)
        elif resource == "clusterrole":
            ret = rbacAPi.list_cluster_role(watch=False)
        elif resource == "clusterrolebinding":
            ret = rbacAPi.list_cluster_role_binding(watch=False)
        elif resource == "job":
            ret = batchV1Api.list_job_for_all_namespaces(watch=False)
        elif resource == "cronjob":
            ret = batchV2Api.list_cron_job_for_all_namespaces(watch=False)
        elif resource == "scheduledjob":
            ret = batchV2Api.list_scheduled_job_for_all_namespaces(watch=False)
        for i in ret.items:
            names.append((i.metadata.name, i.metadata.namespace))
        return names
Exemple #10
0
def yaml_json(request):
    user = user_get(request)
    hostname = request.GET.get('hostname')
    auth_config(user, hostname)
    core_api = client.CoreV1Api()  # namespace,pod,service,pv,pvc
    apps_api = client.AppsV1Api()  # deployment
    batch1_api = client.BatchV1Api()
    batch2_api = client.BatchV2alpha1Api()
    networking_api = client.NetworkingV1beta1Api()  # ingress
    storage_api = client.StorageV1Api()  # storage_class

    if request.method == "GET":
        namespace = request.GET.get('namespace', None)
        resource = request.GET.get('resource', None)
        name = request.GET.get('name', None)
        types = request.GET.get('types')
        try:
            if resource == "namespace":
                result = core_api.read_namespace(
                    name=name, _preload_content=False).read()
            elif resource == "deployment":
                result = apps_api.read_namespaced_deployment(
                    name=name, namespace=namespace,
                    _preload_content=False).read()
            elif resource == "replicaset":
                result = apps_api.read_namespaced_replica_set(
                    name=name, namespace=namespace,
                    _preload_content=False).read()
            elif resource == "daemonset":
                result = apps_api.read_namespaced_daemon_set(
                    name=name, namespace=namespace,
                    _preload_content=False).read()
            elif resource == "statefulset":
                result = apps_api.read_namespaced_stateful_set(
                    name=name, namespace=namespace,
                    _preload_content=False).read()
            elif resource == "pod":
                result = core_api.read_namespaced_pod(
                    name=name, namespace=namespace,
                    _preload_content=False).read()
            elif resource == "service":
                result = core_api.read_namespaced_service(
                    name=name, namespace=namespace,
                    _preload_content=False).read()
            elif resource == "ingress":
                result = networking_api.read_namespaced_ingress(
                    name=name, namespace=namespace,
                    _preload_content=False).read()
            elif resource == "cronjob":
                result = batch2_api.read_namespaced_cron_job(
                    name=name, namespace=namespace,
                    _preload_content=False).read()
            elif resource == "job":
                result = batch1_api.read_namespaced_job(
                    name=name, namespace=namespace,
                    _preload_content=False).read()
            elif resource == "pvc":
                result = core_api.read_namespaced_persistent_volume_claim(
                    name=name, namespace=namespace,
                    _preload_content=False).read()
            elif resource == "pv":
                result = core_api.read_persistent_volume(
                    name=name, _preload_content=False).read()
            elif resource == "node":
                result = core_api.read_node(name=name,
                                            _preload_content=False).read()
            elif resource == "configmap":
                result = core_api.read_namespaced_config_map(
                    name=name, namespace=namespace,
                    _preload_content=False).read()
            elif resource == "secret":
                result = core_api.read_namespaced_secret(
                    name=name, namespace=namespace,
                    _preload_content=False).read()
            result = str(result, "utf-8")  # bytes转字符串
            if types == "yaml":
                result = yaml.safe_dump(
                    json.loads(result))  # str/dict -> json -> yaml
            else:
                result = json.dumps(json.loads(result),
                                    sort_keys=True,
                                    indent=2)
            data = {"code": 0, "msg": "", "data": result}
            return data
        except Exception as e:
            data = error(e)
            return data

    elif request.method == "POST":
        namespace = request.POST.get('namespace', None)
        resource = request.POST.get('resource', None)
        name = request.POST.get('name', None)
        types = request.POST.get('types')
        if types == "yaml":
            body = request.POST.get('yaml', None)
            body = yaml.load(body)
        else:
            body = request.POST.get('json', None)
        try:
            if resource == "namespace":
                if name == body['metadata']['name']:
                    # core_api.replace_namespace(name, yaml.load(body))  # 全局更新
                    core_api.patch_namespace(name, body)  # 局部更新
                    data = {"code": 0, "msg": "更新成功"}
                else:
                    data = {"code": 1, "msg": "名字不对"}
            elif resource == "deployment":
                if name == body['metadata']['name']:
                    apps_api.patch_namespaced_deployment(
                        name, namespace, body)  # 局部更新
                    data = {"code": 0, "msg": "更新成功"}
                else:
                    data = {"code": 1, "msg": "名字不对"}
            elif resource == "pod":
                if name == body['metadata']['name']:
                    core_api.patch_namespaced_pod(name, namespace,
                                                  body)  # 局部更新
                    data = {"code": 0, "msg": "更新成功"}
                else:
                    data = {"code": 1, "msg": "名字不对"}
            elif resource == "service":
                if name == body['metadata']['name']:
                    core_api.patch_namespaced_service(name, namespace,
                                                      body)  # 局部更新
                    data = {"code": 0, "msg": "更新成功"}
                else:
                    data = {"code": 1, "msg": "名字不对"}
            elif resource == "statefulset":
                if name == body['metadata']['name']:
                    apps_api.patch_namespaced_stateful_set(
                        name, namespace, body)  # 局部更新
                    data = {"code": 0, "msg": "更新成功"}
                else:
                    data = {"code": 1, "msg": "名字不对"}
            return data
        except Exception as e:
            data = error(e)
            return data
def _make_dir(directory):
    try:
        os.makedirs(directory)
    except OSError as e:
        if e.errno != errno.EEXIST:
            raise


local_client = config.new_client_from_config(
    expanduser("~/.kube/config-collect"))
k8s_apis = {
    "CoreV1Api":
    client.CoreV1Api(local_client),
    "BatchV2alpha1Api":
    client.BatchV2alpha1Api(local_client),
    "ExtensionsV1beta1Api":
    client.ExtensionsV1beta1Api(local_client),
    "AppsV1beta1Api":
    client.AppsV1beta1Api(local_client),
    "StorageV1Api":
    client.StorageV1Api(local_client),
    "RbacAuthorizationV1alpha1Api":
    client.RbacAuthorizationV1alpha1Api(local_client),
}
api_map = {
    "cron_job": 'BatchV2alpha1Api',
    "daemon_set": 'ExtensionsV1beta1Api',
    "deployment": 'ExtensionsV1beta1Api',
    "replica_set": 'ExtensionsV1beta1Api',
    "stateful_set": 'AppsV1beta1Api',