Search in sources :

Example 1 with HorizontalpodAutoscaler

use of com.qlangtech.tis.config.k8s.HorizontalpodAutoscaler in project plugins by qlangtech.

the class K8SDataXJobWorker method launchService.

@Override
public void launchService() {
    if (inService()) {
        throw new IllegalStateException("k8s instance of:" + KEY_FIELD_NAME + " is running can not relaunch");
    }
    try {
        // 启动服务
        // ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, 3);
        // CuratorFrameworkFactory.Builder curatorBuilder = CuratorFrameworkFactory.builder();
        // curatorBuilder.retryPolicy(retryPolicy);
        // this.client = curatorBuilder.connectString(this.zkAddress).build();
        K8sImage k8sImage = this.getK8SImage();
        // this.k8sClient = k8SImage.createApiClient();
        ReplicasSpec replicasSpec = this.getReplicasSpec();
        Objects.requireNonNull(replicasSpec, "replicasSpec can not be null");
        EnvVarsBuilder varsBuilder = new EnvVarsBuilder("tis-datax-executor") {

            @Override
            public String getAppOptions() {
                // return "-D" + DataxUtils.DATAX_QUEUE_ZK_PATH + "=" + getZkQueuePath() + " -D" + DataxUtils.DATAX_ZK_ADDRESS + "=" + getZookeeperAddress();
                return getZookeeperAddress() + " " + getZkQueuePath();
            }

            @Override
            public String getExtraSysProps() {
                return "-D" + CuratorDataXTaskMessage.SYSTEM_KEY_LOGBACK_PATH_KEY + "=" + CuratorDataXTaskMessage.SYSTEM_KEY_LOGBACK_PATH_VALUE;
            }

            @Override
            protected String processHost(String address) {
                return processDefaultHost(address);
            }
        };
        // K8sImage config, CoreV1Api api, String name, ReplicasSpec incrSpec, List< V1EnvVar > envs
        // CoreV1Api k8sV1Api = new CoreV1Api(k8sClient);
        // K8sImage k8sImage = this.getK8SImage();
        this.getK8SController().createReplicationController(K8S_DATAX_INSTANCE_NAME, replicasSpec, varsBuilder.build());
        if (supportHPA()) {
            HorizontalpodAutoscaler hap = this.getHpa();
            createHorizontalpodAutoscaler(k8sImage, hap);
        }
        writeLaunchToken();
    } catch (ApiException e) {
        logger.error(e.getResponseBody(), e);
        throw K8sExceptionUtils.convert(e);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : EnvVarsBuilder(com.qlangtech.tis.plugin.k8s.EnvVarsBuilder) HorizontalpodAutoscaler(com.qlangtech.tis.config.k8s.HorizontalpodAutoscaler) ReplicasSpec(com.qlangtech.tis.config.k8s.ReplicasSpec) ApiException(io.kubernetes.client.openapi.ApiException) K8sImage(com.qlangtech.tis.plugin.k8s.K8sImage) ApiException(io.kubernetes.client.openapi.ApiException)

Example 2 with HorizontalpodAutoscaler

use of com.qlangtech.tis.config.k8s.HorizontalpodAutoscaler in project tis by qlangtech.

the class IncrUtils method parseIncrSpec.

/**
 * @param context
 * @param form
 * @param msg
 * @return
 */
public static IncrSpecResult parseIncrSpec(Context context, final JSONObject form, IControlMsgHandler msg) {
    ReplicasSpec spec = new ReplicasSpec();
    IncrSpecResult result = new IncrSpecResult(spec, context, msg);
    result.success = false;
    String fieldPods = "pods";
    int replicCount = form.getIntValue(fieldPods);
    if (replicCount < 1) {
        // msg.addErrorMessage(context, "请设置Pods,不能小于1");
        msg.addFieldError(context, fieldPods, "请设置Pods,不能小于1");
    // return result;
    } else if (replicCount > 20) {
        msg.addFieldError(context, fieldPods, "不能大于20");
    // return result;
    }
    spec.setReplicaCount(replicCount);
    Specification s = null;
    String fieldCuprequest = "cuprequest";
    String cpurequest = StringUtils.trimToEmpty(form.getString(fieldCuprequest));
    String cpuRequestUnit = form.getString("cuprequestunit");
    if (StringUtils.isEmpty(cpuRequestUnit)) {
        msg.addFieldError(context, fieldCuprequest, "请填写CPU请求单位");
    }
    cpuRequestUnit = "cores".equals(cpuRequestUnit) ? StringUtils.EMPTY : cpuRequestUnit;
    if (StringUtils.isEmpty(cpurequest)) {
        msg.addFieldError(context, fieldCuprequest, "请填写");
    } else if (!IncrUtils.isNumber(cpurequest)) {
        // msg.addErrorMessage(context, "cpurequest must be " + IncrUtils.PATTERN_NUMBER);
        msg.addFieldError(context, fieldCuprequest, "必须是非负整数");
    } else {
        s = new Specification();
        s.setVal(Integer.parseInt(cpurequest));
        s.setUnit(cpuRequestUnit);
        int maxCpuCoresRequest = 4;
        if (s.normalizeCPU() > (maxCpuCoresRequest * 1024)) {
            msg.addFieldError(context, fieldCuprequest, "请检查CPU请求资源,不能大于" + maxCpuCoresRequest + "cores");
        // return result;
        }
        spec.setCpuRequest(s);
    }
    String filedCpuLimit = "cuplimit";
    String cupLimit = StringUtils.trimToEmpty(form.getString(filedCpuLimit));
    String cupLimitUnit = form.getString("cuplimitunit");
    if (StringUtils.isEmpty(cupLimitUnit)) {
        msg.addFieldError(context, filedCpuLimit, "请填写CPU最大请求单位");
    }
    cupLimitUnit = "cores".equals(cupLimitUnit) ? StringUtils.EMPTY : cupLimitUnit;
    if (StringUtils.isEmpty(cupLimit)) {
        msg.addFieldError(context, filedCpuLimit, "请填写");
    } else if (!IncrUtils.isNumber(cupLimit)) {
        // msg.addErrorMessage(context, "CPU limit must be " + IncrUtils.PATTERN_NUMBER);
        msg.addFieldError(context, filedCpuLimit, "必须是非负整数");
    // return result;
    } else {
        s = new Specification();
        s.setVal(Integer.parseInt(cupLimit));
        s.setUnit(cupLimitUnit);
        int maxCpuCoresLimit = 8;
        if (s.normalizeCPU() > maxCpuCoresLimit * 1024) {
            msg.addFieldError(context, filedCpuLimit, "请检查CPU最大申请资源,不能大于" + maxCpuCoresLimit + "cores");
        // return result;
        }
        spec.setCpuLimit(s);
    }
    if (spec.getCpuRequest() != null && spec.getCpuLimit() != null && spec.getCpuRequest().cpuBigThan(spec.getCpuLimit())) {
        msg.addFieldError(context, fieldCuprequest, "请检查CPU`申请资源`,不能大于`最大申请资源`");
        msg.addFieldError(context, filedCpuLimit, "请检查CPU`最大申请资源`,不能小于`申请资源`");
    }
    // cupLimit = cupLimit + cupLimitUnit;
    String fieldMemoryRequest = "memoryrequest";
    String memoryRequest = form.getString(fieldMemoryRequest);
    String memoryRequestUnit = form.getString("memoryrequestunit");
    if (StringUtils.isEmpty(memoryRequest)) {
        msg.addFieldError(context, fieldMemoryRequest, "请填写");
    } else if (!IncrUtils.isNumber(memoryRequest)) {
        // msg.addErrorMessage(context, "内存格式" + IncrUtils.PATTERN_NUMBER);
        msg.addFieldError(context, fieldMemoryRequest, "必须是非负整数");
    // return result;
    } else {
        s = new Specification();
        s.setVal(Integer.parseInt(memoryRequest));
        s.setUnit(memoryRequestUnit);
        int maxMemoryRequest = 4;
        if (s.normalizeMemory() > (maxMemoryRequest * 1024)) {
            msg.addFieldError(context, fieldMemoryRequest, "请检查内存申请资源,不能大于" + maxMemoryRequest + "G");
        // return result;
        }
        spec.setMemoryRequest(s);
    }
    if (StringUtils.isEmpty(memoryRequestUnit)) {
        // msg.addErrorMessage(context, "请填写内存请求单位");
        msg.addFieldError(context, fieldMemoryRequest, "请填写内存请求单位");
    // return result;
    }
    String fieldMemorylimit = "memorylimit";
    String memoryLimit = (form.getString(fieldMemorylimit));
    String memoryLimitUnit = form.getString("memorylimitunit");
    if (StringUtils.isEmpty(memoryLimit)) {
        msg.addFieldError(context, fieldMemorylimit, "请填写");
    } else if (!IncrUtils.isNumber(memoryLimit)) {
        // msg.addErrorMessage(context, "内存上限" + IncrUtils.PATTERN_NUMBER);
        msg.addFieldError(context, fieldMemorylimit, "必须为非负整数");
    // return result;
    } else {
        int maxMemorylimit = 8;
        s = new Specification();
        s.setVal(Integer.parseInt(memoryLimit));
        s.setUnit(memoryLimitUnit);
        if (s.normalizeMemory() > (maxMemorylimit * 1024)) {
            msg.addFieldError(context, fieldMemorylimit, "请检查内存最大申请资源,不能大于" + maxMemorylimit + "G");
        // return result;
        }
        spec.setMemoryLimit(s);
    }
    if (StringUtils.isEmpty(memoryLimitUnit)) {
        msg.addFieldError(context, fieldMemorylimit, "请填写内存上限单位");
    // return result;
    }
    if (spec.getMemoryRequest() != null && spec.getMemoryLimit() != null && spec.getMemoryRequest().memoryBigThan(spec.getMemoryLimit())) {
        msg.addFieldError(context, fieldMemoryRequest, "请检查内存`申请资源`,不能大于`最大申请资源`");
        msg.addFieldError(context, fieldMemorylimit, "请检查内存`最大申请资源`,不能小于`申请资源`");
    // return result;
    }
    final String KEY_MIN_HPA_POD = "minHpaPod";
    final String KEY_MAX_HPA_POD = "maxHpaPod";
    final String KEY_CPU_AVERAGE_UTILIZATION = "cpuAverageUtilization";
    boolean supportHpa = form.getBooleanValue("supportHpa");
    if (supportHpa) {
        // 支持弹性扩容
        Integer maxHpaPod = form.getInteger(KEY_MAX_HPA_POD);
        Integer minHpaPod = form.getInteger(KEY_MIN_HPA_POD);
        if (minHpaPod == null) {
            msg.addFieldError(context, KEY_MIN_HPA_POD, "请填写'最小Pods'");
        }
        if (maxHpaPod == null) {
            msg.addFieldError(context, KEY_MAX_HPA_POD, "请填写'最大Pods'");
        }
        Integer cpuAverageUtilization = form.getInteger(KEY_CPU_AVERAGE_UTILIZATION);
        if (cpuAverageUtilization == null) {
            msg.addFieldError(context, KEY_CPU_AVERAGE_UTILIZATION, "请填写'CPU平均利用率'");
        }
        if (context.hasErrors()) {
            result.success = false;
            return result;
        }
        boolean hasErr = false;
        if (cpuAverageUtilization < 1 || cpuAverageUtilization >= 100) {
            msg.addFieldError(context, KEY_CPU_AVERAGE_UTILIZATION, "必须大于0且小于100");
            hasErr = true;
        }
        if (minHpaPod < 1) {
            msg.addFieldError(context, KEY_MIN_HPA_POD, "最小弹性Pod数必须大于0");
            hasErr = true;
        }
        if (maxHpaPod <= minHpaPod) {
            msg.addFieldError(context, KEY_MAX_HPA_POD, "必须大于最小弹性Pod数");
            msg.addFieldError(context, KEY_MIN_HPA_POD, "必须小于最大弹性Pod数");
            hasErr = true;
        }
        if (hasErr) {
            return result;
        }
        HorizontalpodAutoscaler hpa = new HorizontalpodAutoscaler();
        hpa.setMaxPod(maxHpaPod);
        hpa.setMinPod(minHpaPod);
        hpa.setCpuAverageUtilization(cpuAverageUtilization);
        if (hpa.getMinPod() > spec.getReplicaCount()) {
            // msg.addFieldError(context, KEY_MAX_HPA_POD, "必须大于最小弹性Pod数");
            // msg.addFieldError(context, KEY_MIN_HPA_POD, "必须小于最大弹性Pod数");
            msg.addFieldError(context, KEY_MIN_HPA_POD, "不能大于'Pods'数");
            msg.addFieldError(context, fieldPods, "不能小于'弹性扩容最小Pods数'");
            // msgHandler.(context, resourceLiteria + " Request Is inValid " + request);
            return result;
        }
        result.hpa = hpa;
    }
    result.success = !context.hasErrors();
    return result;
}
Also used : HorizontalpodAutoscaler(com.qlangtech.tis.config.k8s.HorizontalpodAutoscaler) ReplicasSpec(com.qlangtech.tis.config.k8s.ReplicasSpec)

Aggregations

HorizontalpodAutoscaler (com.qlangtech.tis.config.k8s.HorizontalpodAutoscaler)2 ReplicasSpec (com.qlangtech.tis.config.k8s.ReplicasSpec)2 EnvVarsBuilder (com.qlangtech.tis.plugin.k8s.EnvVarsBuilder)1 K8sImage (com.qlangtech.tis.plugin.k8s.K8sImage)1 ApiException (io.kubernetes.client.openapi.ApiException)1