前言
一、
需要准备一个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"`
}