rancher-api 客户端工具类

package com.cwp.data.service.utils;

/**
 * @ClassName RancherClientUtils
 * @Description TODO
 * @Author Getech
 * @Date 2021/9/23 10:20
 */
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.Charset;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.X509Certificate;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author gflourenco
 */
public class RancherClientUtils {

    /**
     *  bearer token
     */
    private String rancherBearerToken;

    /**
     * rancher地址
     */
    private String rancherAddress;

    /**
     *  项目地址前缀
     */
    private static String rancherProjects = "/v3/projects/";

    /**
     *  工作负载地址前缀
     */
    private static String rancherWorkloads = "/workloads/";

    /**
     *  节点地址前缀
     */
    private static String rancherNodes = "/v3/nodes/";

    /**
     * 默认构造方法
     */
    public RancherClientUtils() {
        this("cwp.com", "token-abc:123456");
    }

    /**
     * @Description 通过地址和 bearer token设置token
     * @Author  chengweiping
     * @Date   2021/9/23 14:17
     */
    public RancherClientUtils(String rancherAddress, String token) {
        this.rancherAddress = rancherAddress;
        this.rancherBearerToken = token;
    }


    /**
     *  新增工作负载
     */
    public JSONObject addWorkload(String projectId, com.alibaba.fastjson.JSONObject workloadAdd) throws Exception {
        String addWorkloadUrl="https://" + rancherAddress + rancherProjects + projectId +"/workload";
        return postDataGetJson(addWorkloadUrl, workloadAdd);
    }

    /**
     *  更新工作负载
     */
    public JSONObject updateWorkload(String projectId, String workloadId, JSONObject containerUpdt) throws Exception {
        JSONObject workloadObj = getWorkload(projectId, workloadId, true);

        try {

            for (int i = 0; i < workloadObj.getJSONArray("containers").length(); i++) {

                if (workloadObj.getJSONArray("containers").getJSONObject(i).get("name").equals(containerUpdt.get("name"))) {
                    workloadObj.getJSONArray("containers").getJSONObject(i).put("image", containerUpdt.get("image"));
                    if (containerUpdt.has("environment")) {
                        workloadObj.getJSONArray("containers").getJSONObject(i).put("environment", containerUpdt.get("environment"));
                    }

                }
            }
        } catch (Exception ex) {
           ex.printStackTrace();
           throw  ex;
        }
        String updateWorkloadUrl="https://" + rancherAddress + rancherProjects + projectId + rancherWorkloads + "/" + workloadId;
        return putDataGetJson(updateWorkloadUrl, workloadObj);
    }

    /**
     * @Description 获取节点
     * @Author  chengweiping
     * @Date   2021/9/23 14:13
     */
    public JSONObject getNode(String nodeID) throws Exception {
        JSONObject returnObj = new JSONObject();

        try {

            JSONObject workloadObj = readJsonFromUrl("https://" + rancherAddress + rancherNodes + nodeID);
            returnObj.put("id", workloadObj.get("id"));
            returnObj.put("hostname", workloadObj.get("hostname"));
            returnObj.put("externalIpAddress", workloadObj.get("externalIpAddress"));
        } catch (Exception ex) {
            ex.printStackTrace();
            throw  ex;
        }
        return returnObj;
    }

    /**
     * @Description  获取工作负载,raw:true,返回原始数据
     * @Author  chengweiping
     * @Date   2021/9/23 14:14
     */
    public JSONObject getWorkload(String projectId, String workloadId, boolean raw) throws Exception {
        if (raw) {
            return readJsonFromUrl("https://" + rancherAddress + rancherProjects + projectId + rancherWorkloads + "/" + workloadId);
        }
        return getWorkload(projectId, workloadId);
    }

    /**
     * @Description 获取工作负载
     * @Author  chengweiping
     * @Date   2021/9/23 14:14
     */
    public JSONObject getWorkload(String projectId, String workloadId) throws Exception {
        JSONObject returnObj = new JSONObject();
        try {
            JSONObject workloadObj = readJsonFromUrl("https://" + rancherAddress + rancherProjects + projectId + rancherWorkloads + "/" + workloadId);
            returnObj.put("name", workloadObj.get("name"));
            returnObj.put("id", workloadObj.get("id"));
            returnObj.put("created", workloadObj.get("created"));
            returnObj.put("createdTS", workloadObj.get("createdTS"));
            returnObj.put("projectId", workloadObj.get("projectId"));
            /*  returnObj.put("daemonSetStatus", workloadObj.get("daemonSetStatus"));*/

            returnObj.put("state", workloadObj.get("state"));
            if (workloadObj.has("publicEndpoints")) {
                returnObj.put("publicEndpoints", workloadObj.get("publicEndpoints"));
            }
            JSONArray containersArr = workloadObj.getJSONArray("containers");
            JSONArray returnContainersArr = new JSONArray();

            for (int i = 0; i < containersArr.length(); i++) {
                JSONObject containerObj = containersArr.getJSONObject(i);
                JSONObject returnContainerObj = new JSONObject();
                if (containerObj.has("environment")) {
                    returnContainerObj.put("environment", containerObj.get("environment"));
                }
                if (containerObj.has("image")) {
                    returnContainerObj.put("image", containerObj.get("image"));
                }

                if (containerObj.has("ports")) {
                    returnContainerObj.put("ports", containerObj.get("ports"));
                }
                returnContainersArr.put(returnContainerObj);
            }
            returnObj.put("containers", returnContainersArr);
        } catch (Exception ex) {
           ex.printStackTrace();
           throw  ex;
        }
        return returnObj;
    }

    /**
     *  获取项目下的所有工作负载
     */
    public JSONArray getWorkloads(String projectId) throws Exception {
        JSONArray returnArr = new JSONArray();

        try {
            JSONObject json = readJsonFromUrl("https://" + rancherAddress + rancherProjects + projectId + rancherWorkloads);
            JSONArray dataArr = json.getJSONArray("data");
            for (int i = 0; i < dataArr.length(); i++) {
                JSONObject dataObj = dataArr.getJSONObject(i);
                JSONObject returnObj = new JSONObject();
                returnObj.put("name", dataObj.get("name"));
                returnObj.put("created", dataObj.get("created"));
                returnObj.put("createdTS", dataObj.get("createdTS"));
                returnObj.put("id", dataObj.get("id"));

                returnArr.put(returnObj);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            throw  ex;
        }
        return returnArr;

    }

    /**
     * @Description  获取所有项目
     * @Author  chengweiping
     * @Date   2021/9/23 14:14
     */
    public JSONArray getProjects() throws Exception {
        JSONArray returnArr = new JSONArray();
        try {
            JSONObject json = readJsonFromUrl("https://" + rancherAddress + rancherProjects);

            JSONArray dataArr = json.getJSONArray("data");
            for (int i = 0; i < dataArr.length(); i++) {
                JSONObject dataObj = dataArr.getJSONObject(i);
                JSONObject returnObj = new JSONObject();
                returnObj.put("name", dataObj.get("name"));
                returnObj.put("created", dataObj.get("created"));
                returnObj.put("createdTS", dataObj.get("createdTS"));
                returnObj.put("creatorId", dataObj.get("creatorId"));
                returnObj.put("description", dataObj.get("description"));
                returnObj.put("id", dataObj.get("id"));
                returnArr.put(returnObj);
            }
        } catch (JSONException ex) {
            ex.printStackTrace();
            throw  ex;
        }
        return returnArr;

    }

    /**
     * @Description 读取记录
     * @Author  chengweiping
     * @Date   2021/9/23 14:15
     */
    private String readAll(Reader rd) throws IOException {
        StringBuilder sb = new StringBuilder();
        int cp;
        while ((cp = rd.read()) != -1) {
            sb.append((char) cp);
        }
        return sb.toString();
    }

    /**
     *  从url地址中获取json数据
     */
    public JSONObject readJsonFromUrl(String url) throws Exception {
        try {
            // Create a trust manager that does not validate certificate chains
            TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }

                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            }
            };

            // Install the all-trusting trust manager
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

            URLConnection connection = new URL(url).openConnection();
            HttpsURLConnection httpConn = (HttpsURLConnection) connection;
            httpConn.setRequestProperty("Authorization", "Bearer " + rancherBearerToken);

            InputStream is = httpConn.getInputStream();

            try {
                BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
                String jsonText = readAll(rd);
                JSONObject json = new JSONObject(jsonText);
                return json;
            } catch (JSONException ex) {
                Logger.getLogger(RancherClientUtils.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                is.close();
            }
        } catch (NoSuchAlgorithmException | KeyManagementException | IOException ex) {
            Logger.getLogger(RancherClientUtils.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }


    /**
     * @Description 通过put请求设置数据
     * @Author  chengweiping
     * @Date   2021/9/23 14:16
     */
    public JSONObject putDataGetJson(String url, JSONObject data) throws Exception{
        try {
            // Create a trust manager that does not validate certificate chains
            TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }

                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            }
            };
            // Install the all-trusting trust manager
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

            URLConnection connection = new URL(url).openConnection();
            HttpsURLConnection httpConn = (HttpsURLConnection) connection;

            httpConn.setRequestMethod("PUT");
            httpConn.setDoOutput(true);
            httpConn.setRequestProperty("Authorization", "Bearer " + rancherBearerToken);
            httpConn.setRequestProperty("Accept", "application/json");
            httpConn.setRequestProperty("Content-Type", "application/json");

            OutputStreamWriter osw = new OutputStreamWriter(httpConn.getOutputStream());
            osw.write(data.toString());
            osw.flush();
            osw.close();
            InputStream is = httpConn.getInputStream();

            try {
                BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
                String jsonText = readAll(rd);
                JSONObject json = new JSONObject(jsonText);
                return json;
            } catch (JSONException ex) {
                Logger.getLogger(RancherClientUtils.class.getName()).log(Level.SEVERE, null, ex);
            } finally {
                is.close();
            }
        } catch (Exception ex) {
           ex.printStackTrace();
           throw  ex;
        }
        return null;
    }


    /**
     * @Description 通过post请求设置数据
     * @Author  chengweiping
     * @Date   2021/9/23 14:16
     */
    public JSONObject postDataGetJson(String url, com.alibaba.fastjson.JSONObject data) throws Exception {
        try {
            // Create a trust manager that does not validate certificate chains
            TrustManager[] trustAllCerts = new TrustManager[]{new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }

                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }

                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            }
            };

            // Install the all-trusting trust manager
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

            URLConnection connection = new URL(url).openConnection();
            HttpsURLConnection httpConn = (HttpsURLConnection) connection;

            httpConn.setRequestMethod("POST");
            httpConn.setDoOutput(true);
            httpConn.setRequestProperty("Authorization", "Bearer " + rancherBearerToken);
            httpConn.setRequestProperty("Accept", "application/json");
            httpConn.setRequestProperty("Content-Type", "application/json");

            OutputStreamWriter osw = new OutputStreamWriter(httpConn.getOutputStream());
            osw.write(data.toString());
            osw.flush();
            osw.close();
            InputStream is = httpConn.getInputStream();

            try {
                BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
                String jsonText = readAll(rd);
                JSONObject json = new JSONObject(jsonText);
                return json;
            } catch (JSONException ex) {
               ex.printStackTrace();
               throw ex;
            } finally {
                is.close();
            }
        } catch (Exception ex) {
           ex.printStackTrace();
           throw  ex;
        }
    }



    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        try {

            RancherClientUtils rancherClient = new RancherClientUtils();

            System.out.println("\nProjects");
            JSONArray projects = rancherClient.getProjects();
            for (int i = 0; i < projects.length(); i++) {
                System.out.println(projects.getJSONObject(i).toString());
            }

            System.out.println("\nWorkloads");
            JSONArray workloads = rancherClient.getWorkloads("c-v69ln:p-sgwmx");
            for (int i = 0; i < workloads.length(); i++) {
                System.out.println(workloads.getJSONObject(i).toString());
            }

            System.out.println("\nWorkload");
            JSONObject workload = rancherClient.getWorkload("c-v69ln:p-sgwmx", "deployment:dip-ims:data-service-job");
            System.out.println(workload.toString());

         /*   System.out.println("\nNode");
            JSONObject node = rancherClient.getNode("c-sm5kw:m-81dd8018f312");
            System.out.println(node.toString());*/

            System.out.println("\nWorkload Raw");
            JSONObject workloadRaw = rancherClient.getWorkload("c-v69ln:p-sgwmx", "deployment:dip-ims:data-service-job", true);
            System.out.println(workloadRaw.toString());

            System.out.println("\nUpdate Workload");
            JSONObject updateContainer = new JSONObject();

          /*  updateContainer.put("name", "data-service-job");
            updateContainer.put("image", "registry.getech.cn/dip/data-service-job-ims:2.3.34");
            JSONObject environment = new JSONObject();
            environment.put("VAR1", "false");
            environment.put("VAR2", "Teste222");
            updateContainer.put("environment", environment);
            System.out.println(rancherClient.updateWorkload("c-v69ln:p-sgwmx", "deployment:dip-ims:data-service-job", updateContainer).toString());
*/
        } catch (Exception ex) {
          ex.printStackTrace();
        }

    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

成伟平2022

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值