Beispiel #1
0
def get_svc_affinity(svc):
    is_app = True if svc["app_name"] else True
    pod_affinity = svc["kube_config"]["pod"]["podAffinity"]
    if "requiredDuringSchedulingIgnoredDuringExecution" in pod_affinity:
        for item in pod_affinity[
                "requiredDuringSchedulingIgnoredDuringExecution"]:
            if "labelSelector" in item:
                match_expressions = item["labelSelector"]
                for expression in match_expressions["matchExpressions"]:
                    if "key" in expression and expression[
                            "key"] == "alauda_service_id":
                        expression["key"] = "service.alauda.io/name"
                    if "values" in expression:
                        tmp = []
                        for value in expression["values"]:
                            if is_app:
                                tmp.append(
                                    applications.get_app_service_detail(value)
                                    ["service_name"].lower())
                            else:
                                tmp.append(
                                    get_service_detail(value)
                                    ["service_name"].lower())
                        expression["values"] = tmp

    pod_antiAffinity = svc["kube_config"]["pod"]["podAntiAffinity"]
    if "requiredDuringSchedulingIgnoredDuringExecution" in pod_antiAffinity:
        for item in pod_antiAffinity[
                "requiredDuringSchedulingIgnoredDuringExecution"]:
            if "labelSelector" in item:
                match_expressions = item["labelSelector"]
                for expression in match_expressions["matchExpressions"]:
                    if "key" in expression and expression[
                            "key"] == "alauda_service_id":
                        expression["key"] = "service.alauda.io/name"
                    if "values" in expression:
                        tmp = []
                        for value in expression["values"]:
                            if is_app:
                                tmp.append(
                                    applications.get_app_service_detail(value)
                                    ["service_name"].lower())
                            else:
                                tmp.append(
                                    get_service_detail(value)
                                    ["service_name"].lower())
                        expression["values"] = tmp

    return {"podAffinity": pod_affinity, "podAntiAffinity": pod_antiAffinity}
Beispiel #2
0
def create_cm():
    current_folder = utils.get_current_folder()
    print "begin create configMap "
    for filename in os.listdir(current_folder):
        if filename.startswith(consts.Prefix["cm_name_prefix"]):
            cm_file = filename
            print "find configMap file " + filename
            if not os.path.exists(current_folder + cm_file):
                raise "configMap file for {} not exists! please check task of init cm".format(
                    current_folder + cm_file)
            cm_data = utils.file_reader(current_folder + cm_file)
            print "config map data for file {},{}".format(
                filename, json.dumps(cm_data))
            is_app = False
            if filename.startswith(consts.Prefix["cm_name_prefix"] +
                                   consts.Prefix["app_cm_flag_file"]):
                is_app = True
            filename_pre = consts.Prefix["cm_name_prefix"] + consts.Prefix["app_cm_flag_file"] if is_app\
                else consts.Prefix["cm_name_prefix"]
            service_uuid = filename.replace(filename_pre, "")
            print "find cm service uuid {}, type is {}".format(
                service_uuid, "app" if is_app else "service")
            svc_detail = applications.get_app_service_detail(service_uuid) if is_app \
                else services.get_service_detail(service_uuid)
            data = {
                "apiVersion": "v1",
                "kind": "ConfigMap",
                "metadata": {
                    "annotations": {},
                    "namespace":
                    applications.get_app_svc_namespace(svc_detail)
                    if is_app else svc_detail["space_name"],
                    "name":
                    filename.replace("_", "-")
                },
                "data": cm_data
            }
            print "begin create configMap for {}".format(current_folder +
                                                         filename)
            res = utils.send_request("POST", consts.URLS["create_cm"], data)
            if isinstance(res, list) and len(res) > 0:
                print "configMap {} create success,list".format(
                    current_folder + filename)
            elif isinstance(
                    res, dict) and "result" in res and len(res["result"]) > 0:
                print "create namespace for {} success".format(current_folder +
                                                               filename)
            elif isinstance(res, dict) and "errors" in res and \
                            res["errors"][0]["message"].find("already exists") >= 0:
                print res["errors"][0]["message"] + ", will be skipped"
            else:
                print "configMap {} create ERROR!!!".format(current_folder +
                                                            filename)
                exit(1)
Beispiel #3
0
def init_app_cm():
    """
    cm needs to be created
    :return:
    """
    app_list = applications.get_app_list()
    for app in app_list:
        for app_service in app["services"]:
            svc_detail = applications.get_app_service_detail(
                app_service["uuid"])
            init_cm_handler(svc_detail)
Beispiel #4
0
def get_svc_labels(svc_id, is_app=False):
    if is_app:
        svc_detail = applications.get_app_service_detail(svc_id)
    else:
        svc_detail = get_service_detail(svc_id)
    lables = {
        "service.alauda.io/name": svc_detail["service_name"].lower(),
    }
    for lable in svc_detail["labels"]:
        lables[lable["key"]] = lable["value"]
    return lables
Beispiel #5
0
def get_svc_env(svc_id, is_app=False):
    if is_app:
        svc_detail = applications.get_app_service_detail(svc_id)
    else:
        svc_detail = get_service_detail(svc_id)
    result = []
    env_objs = {}
    if "envfiles" in svc_detail:
        for envfile in svc_detail["envfiles"]:
            content = envfile["content"]
            for env in content:
                env_objs[env[0]] = env[1]

    if "instance_envvars" in svc_detail:
        for key in svc_detail["instance_envvars"]:
            env_objs[key] = svc_detail["instance_envvars"][key]
    for key in env_objs:
        result.append({"name": key, "value": env_objs[key]})
    return result
Beispiel #6
0
def get_health_check(svc_id, is_app=False):
    if is_app:
        svc_detail = applications.get_app_service_detail(svc_id)
    else:
        svc_detail = get_service_detail(svc_id)
    hc = svc_detail["health_checks"][0]
    # {"protocol": "TCP", "timeout_seconds": 15, "interval_seconds": 2, "max_consecutive_failures": 2,
    #  "port": 2182, "grace_period_seconds": 100}
    result = {
        "initialDelaySeconds": hc["grace_period_seconds"],
        "periodSeconds": hc["interval_seconds"],
        "timeoutSeconds": hc["timeout_seconds"],
        "successThreshold": 1,
        "failureThreshold": hc["max_consecutive_failures"]
    }
    if hc["protocol"] in ["HTTP", "HTTPS"]:
        result["httpGet"] = {
            "path": hc["path"],
            "scheme": hc["protocol"],
            "port": hc["port"]
        }
    if hc["protocol"] == "TCP":
        result["tcpSocket"] = {"port": hc["port"]}
    return result
Beispiel #7
0
def get_volume_mounts(svc_id, is_app=False):
    if is_app:
        svc = applications.get_app_service_detail(svc_id)
    else:
        svc = get_service_detail(svc_id)
    mount_points = svc["mount_points"]
    svc_name = svc["service_name"].lower()
    svc_uuid = svc["uuid"]
    volume_mounts = []
    volumes = []
    index = 0
    k8_cm_name = consts.Prefix["cm_name_prefix"] + consts.Prefix["app_cm_flag_file"] + svc_uuid if svc["app_name"] \
        else consts.Prefix["cm_name_prefix"] + svc_uuid
    for mp in mount_points:
        if mp["type"] == "config":
            value = mp["value"]
            config_key = value["key"]
            volume_obj = {
                "name": "configmap-" + svc_name + "-" + str(index),
                "configMap": {
                    "name": k8_cm_name,
                    "items": [{
                        "key": config_key,
                        "path": config_key
                    }]
                }
            }

            mount_obj = {
                "mountPath": mp["path"],
                "name": "configmap-" + svc_name + "-" + str(index),
                "subPath": config_key
            }
            volumes.append(volume_obj)
            volume_mounts.append(mount_obj)
        elif mp["type"] == "raw" or mp["type"] == "text":
            raw_key = "key" + mp["path"].replace("/", "-")
            volume_obj = {
                "name": "configmap-" + svc_name + "-" + str(index),
                "configMap": {
                    "name": k8_cm_name,
                    "items": [{
                        "key": raw_key,
                        "path": raw_key
                    }]
                }
            }
            mount_obj = {
                "mountPath": mp["path"],
                "name": "configmap-" + svc_name + "-" + str(index),
                "subPath": raw_key
            }
            volumes.append(volume_obj)
            volume_mounts.append(mount_obj)
        else:
            raise "Unknown config type " + mp["type"]
        index += 1
    # handle host_path, gfs, not include  ebs
    if "volumes" in svc:
        for vol in svc["volumes"]:
            driver_name = vol["driver_name"]
            if driver_name == "":  # host path
                volume_obj = {
                    "name": "hostpath-" + svc_name + "-" + str(index),
                    "hostPath": {
                        "path": vol["volume_name"]
                    }
                }
                mount_obj = {
                    "mountPath": vol["app_volume_dir"],
                    "name": "hostpath-" + svc_name + "-" + str(index)
                }
                volumes.append(volume_obj)
                volume_mounts.append(mount_obj)
            if driver_name == "glusterfs":  # glusterfs
                volume_obj = {
                    "name": "glusterfs-" + svc_name + "-" + str(index),
                    "glusterfs": {
                        "endpoints": "glusterfs-endpoints",
                        "path": vol["volume_name"]
                    }
                }
                mount_obj = {
                    "mountPath": vol["app_volume_dir"],
                    "name": "glusterfs-" + svc_name + "-" + str(index)
                }
                volumes.append(volume_obj)
                volume_mounts.append(mount_obj)
            index += 1
    print "volumes, volume_mounts foo svc {}".format(svc_name)
    print {"mounts": volume_mounts, "volumes": volumes}
    return {"mounts": volume_mounts, "volumes": volumes}