【亲测】Helm Java Api

发布于:2024-04-12 ⋅ 阅读:(242) ⋅ 点赞:(0)

前言

最近在做云原生运维相关系统开发,需要api操作helm,也是调研找了许久,找到个不错的开源项目,分享一下叫helm-java

链接https://github.com/manusa/helm-java

这个项目是之前分享过的文章Fabric8io团队24年初开源的,目前找了网上各个博客还都没有,都是操作helm比较麻烦的。这个Helm-Java使用简单,和Fabirc8差不多,该项目也有详细api使用讲解

注:有兴趣可以研究研究,这里和大家简单分享一下这个库

1、个人简单Demo使用

根据官网下方对每个命令对应api的讲解,做了demo,亲测有效

<dependency>
    <groupId>com.marcnuri.helm-java</groupId>
    <artifactId>helm-java</artifactId>
    <version>0.0.6</version>
</dependency>

个人demo代码,这里不做详细讲解,因为开源讲解已经很详细,自己可以去试着运行一下,就懂了,链式编程填参数也好理解

package com.yx;

import com.marcnuri.helm.*;

import java.nio.file.Paths;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * Author:yx5411
 * Date:2024-03-27
 * Description: helm-java-api测试
 */
public class Main {
    public static void main(String[] args) {
        // 集群中的~/.kube/config连接文件下载放到本地
        String kubeConfigPath  = "D:\\IDEAProject\\helm_java_api\\src\\main\\resources\\conf\\config";
        String namespace = "kubeapps";
        String uuid = UUID.randomUUID().toString();
        String name = "nginx-test-" + uuid.substring(0, 4);
        System.out.println("name: " + name);
//        findNameSpaceAllChart(namespace, kubeConfigPath);
        installHelm(namespace, kubeConfigPath, name);
//        createChartDir();
//        lintHelm();
//        uninstallReleaseByName(namespace, kubeConfigPath);
    }

    // helm create:在指定位置创建一个包含chart包公共文件的目录,即初始化创建
    private static void createChartDir() {
        Helm.create()
            // Name of the chart to create
            .withName("test")
            // Path to the directory where the new chart directory will be created
            .withDir(Paths.get("D:\\IDEAProject\\helm_java_api\\src\\main\\resources\\tmp"))
            .call();
    }

    // helm list:查询某个命名空间下的所有已发布的chart
    private static void findNameSpaceAllChart(String namespace, String kubeConfigPath) {
        List<Release> releases = Helm.list()
                .withNamespace(namespace)
                .withKubeConfig(Paths.get(kubeConfigPath))
                .all()
//                .allNamespaces()
                .deployed()
                .failed()
                .pending()
                .superseded()
                .call();
        for (Release release : releases) {
            System.out.println("Name: " + release.getName());
            System.out.println("Namespace: " + release.getNamespace());
            System.out.println("Revision: " + release.getRevision());
            System.out.println("Status: " + release.getStatus());
            System.out.println("Chart: " + release.getChart());
            System.out.println("App Version: " + release.getAppVersion());
            System.out.println("-----------------------------");
        }
    }

    // helm lint:检查图表是否存在可能的问题,即验证
    public static void lintHelm() {
        String chatPath = "D:\\IDEAProject\\helm_java_api\\src\\main\\resources\\conf\\nginx-0.1.0.tgz";
        LintResult result = new Helm(Paths.get(chatPath))
                .lint()
                .strict()
                .quiet()
                .call();
        System.out.println(result.isFailed());
        System.out.println(result.getMessages());
    }

    // helm uninstall:删除某个命名空间下的release,可helm list -n [NAMESPACE]查看已发布的chart
    private static void uninstallReleaseByName(String namespace, String kubeConfigPath){
        // 假设先查询helm list
        List<Release> releases = Helm.list()
                .withNamespace(namespace)
                .withKubeConfig(Paths.get(kubeConfigPath))
                .all()
                // 下面是筛选查各种状态的release
//                .deployed()
//                .failed()
//                .pending()
//                .superseded()
//                .uninstalled()
                .call();
        // 删除状态为失败和挂起的release
        List<String> list = releases.stream().map(Release::getName).collect(Collectors.toList());
        for (String name: list){
            System.out.println("helm uninstall " + name + " -n " + namespace);
            String res = Helm.uninstall(name)
                    .dryRun() // 模拟卸载,不真实卸载,真卸载需将其注释
                    .noHooks()
                    .ignoreNotFound()
                    .keepHistory()
                    .withCascade(UninstallCommand.Cascade.BACKGROUND)
                    .withNamespace(namespace)
                    .withKubeConfig(Paths.get(kubeConfigPath))
                    .debug()
                    .call();
            System.out.println(res);
        }
    }

    // helm install:在某个命名空间下安装部署chart
    public static void installHelm(String namespace, String kubeConfigPath, String name){
        // 通过下面方式nginx.tgz安装正常running,mysql.tgz安装却是pending
        // 原因是mysql和redis均默认要1个master和3个pod进行部署,本地只部署1master和2node导致,资源问题
        String desc = "helm install " + name;
        String chatPath = "D:\\IDEAProject\\helm_java_api\\src\\main\\resources\\conf\\nginx-0.1.0.tgz";
        InstallCommand installCommand = new Helm(Paths.get(chatPath)).install();
        Release result = installCommand
                .withName(name)
                .withNamespace(namespace)
                .withKubeConfig(Paths.get(kubeConfigPath))
                .createNamespace()
                .withDescription(desc)
                .dependencyUpdate()
                .waitReady()
                .plainHttp()
                .debug()
                .call();
        System.out.println("Success");
    }
    
}


网站公告

今日签到

点亮在社区的每一天
去签到