goalng_helm_crd

前言

k8集群用的这个


一、

需要准备一个k8集群,以及admin.conf(如果用kubeadm方式创建集群的话),在/etc/kubernetes,以及一个chart包

二、

以nginx为例创建一个chart

#用一个nginx测试
kubectl create deployment nginxdeployment --image=nginx:latest -o yaml > nginxdeployment.yaml
kubectl expose deployment nginxdeployment --port=80 --target-port=8080 --type=NodePort -o yaml > nginxservice.yaml

然后

helm create web80

将templates下清空 mv *.yaml /web80/templates/

三、

package main

import (
	"fmt"
	"github.com/ghodss/yaml"
	"helm.sh/helm/v3/pkg/action"
	"helm.sh/helm/v3/pkg/chart/loader"
	"helm.sh/helm/v3/pkg/kube"
	"strings"
)

var (
	kubeconfigPath = "E:\\Users\\coding\\Desktop\\"
	ns             = "default"
	chart_name     = "web80"
)

func main() {
	//helm_install()
	//helm_query()
	helm_delete()
}
func helm_query() {
	println("start helm query  ...")
	// Initialize action config
	actionConfig := new(action.Configuration)
	actionConfig.Init(kube.GetConfig(kubeconfigPath+"admin.conf", "", ns), ns, "", func(format string, v ...interface{}) {
		_ = fmt.Sprintf(format, v)
	})
	installer := action.NewStatus(actionConfig)
	res, err := installer.Run(chart_name)
	if err != nil {
		println(err.Error())
		return
	}
	manifest, err := splitManifestYaml([]byte(res.Manifest))
	for _, m := range manifest {
		println(m.Metadata.Name)
	}
}
func helm_delete() {
	println("start helm delete  ...")
	// Initialize action config
	actionConfig := new(action.Configuration)
	actionConfig.Init(kube.GetConfig(kubeconfigPath+"admin.conf", "", ns), ns, "", func(format string, v ...interface{}) {
		_ = fmt.Sprintf(format, v)
	})
	installer := action.NewUninstall(actionConfig)
	_, err := installer.Run(chart_name)
	if err != nil {
		println(err.Error())
		return
	}
	println("helm uninstall is successful ...")
}
func helm_install() {
	println("start helm install  ...")
	// Initialize action config
	actionConfig := new(action.Configuration)
	actionConfig.Init(kube.GetConfig(kubeconfigPath+"admin.conf", "", ns), ns, "", func(format string, v ...interface{}) {
		_ = fmt.Sprintf(format, v)
	})
	installer := action.NewInstall(actionConfig)
	installer.Namespace = ns // so if we want to deploy helm charts via k8splugin.. first namespace should be created or exist then we can deploy helm charts in that namespace
	installer.ReleaseName = chart_name
	//load chart
	chart, err := loader.Load(kubeconfigPath + chart_name)
	if err != nil {
		println(err.Error())
		return

	}
	//do NewInstall
	run, err := installer.Run(chart, nil)
	if err != nil {
		println(err.Error())
		return
	}
	println(run.Name + "is ok!")
}
func splitManifestYaml(data []byte) (manifest []Manifest, err error) {
	manifestBuf := []Manifest{}

	yamlSeparator := "\n---"
	yamlString := string(data)

	yamls := strings.Split(yamlString, yamlSeparator)
	for k := 0; k < len(yamls); k++ {
		var manifest Manifest
		err := yaml.Unmarshal([]byte(yamls[k]), &manifest)
		if err != nil {
			return nil, err
		}
		manifestBuf = append(manifestBuf, manifest)
	}
	return manifestBuf, nil
}

// Manifest file
type Manifest struct {
	APIVersion string `yaml:"apiVersion"`
	Kind       string `yaml:"kind"`
	Metadata   struct {
		Name      string `yaml:"name"`
		Namespace string `yaml:"namespace"`
		Labels    struct {
			App       string `yaml:"app"`
			Component string `yaml:"component"`
		} `yaml:"labels"`
	} `yaml:"metadata"`
	Spec struct {
		Selector struct {
			MatchLabels struct {
				App string `yaml:"app"`
			} `yaml:"matchLabels"`
		} `yaml:"selector"`
		Replicas int `yaml:"replicas"`
		Template struct {
			Metadata struct {
				Labels struct {
					App string `yaml:"app"`
				} `yaml:"labels"`
			} `yaml:"metadata"`
		} `yaml:"template"`
	} `yaml:"spec"`
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值