All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alogic.kube.xscript.KubeCall Maven / Gradle / Ivy

package com.alogic.kube.xscript;

import com.alogic.kube.KubeApiClients;
import com.alogic.remote.Request;
import com.alogic.remote.Response;
import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.plugins.Segment;
import com.anysoft.util.BaseException;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import io.kubernetes.client.openapi.ApiClient;
import io.kubernetes.client.openapi.auth.ApiKeyAuth;
import org.apache.commons.lang3.StringUtils;

/**
 * Kube call
 *
 * @since 1.6.16.14
 */
public class KubeCall extends Segment {

    /**
     * Response对象的上下文id
     */
    protected String cid = "remote-res";

    /**
     * Request对象的上下文id
     */
    protected String pid = "remote-req";

    /**
     * 服务路径
     */
    protected String path;

    protected String rpcId;

    protected String $kubeClientId = KubeApiClients.DEFAULT;

    public KubeCall(String tag, Logiclet p) {
        super(tag, p);
    }

    @Override
    public void configure(Properties p){
        super.configure(p);
        cid = PropertiesConstants.getString(p, "cid", cid,true);
        pid = PropertiesConstants.getString(p, "pid", pid,true);
        path = PropertiesConstants.getRaw(p,"path",path);
        rpcId = PropertiesConstants.getRaw(p,"rpcId","");
        $kubeClientId = PropertiesConstants.getRaw(p,"kubeId",$kubeClientId);
    }

    @Override
    protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
        Request req = ctx.getObject(pid);
        if (req == null){
            throw new BaseException("core.e1001","It must be in a remote-request context,check your script.");
        }

        Response response = null;
        try {
            String p = ctx.transform(path);
            if (StringUtils.isEmpty(p)){
                throw new BaseException("core.e1003","The url path to call is null.");
            }
            
            String id = PropertiesConstants.transform(ctx, rpcId, "");

            ApiClient apiClient = KubeApiClients.getClient(
                    PropertiesConstants.transform(ctx,$kubeClientId,KubeApiClients.DEFAULT),false);

            p = apiClient.getBasePath() + p;

            ApiKeyAuth auth = (ApiKeyAuth)apiClient.getAuthentication("BearerToken");
            if (auth == null){
                throw new BaseException("core.e1003","No BearerToken authentication configured!");
            }
            req.setHeader("authorization",String.format("%s %s",auth.getApiKeyPrefix(),auth.getApiKey()));
            response = req.execute(p,id);
            ctx.setObject(cid, response);
            super.onExecute(root, current, ctx, watcher);
        }finally{
            ctx.removeObject(cid);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy