helm项目主要目录
1 | pkg/action: 包含执行Helm操作的主要客户端。和CLI处理过程中使用的是同一个包。如果你仅仅需要执行来自另一个Go程序的最基本的Helm命令,这个包会很适合你 |
示例
helm version: v3.10.1
通过SDK
安装一个指定的chart包1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52package main
import (
"context"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/cli"
"k8s.io/client-go/rest"
"log"
"os"
"time"
)
func main() {
// must be run in k8s cluster
kubeConfig, _ := rest.InClusterConfig()
// init action config
actionConfig := new(action.Configuration)
settings := cli.New()
helmDriver := os.Getenv("HELM_DRIVER")
settings.KubeAPIServer = kubeConfig.Host
settings.KubeToken = kubeConfig.BearerToken
settings.KubeInsecureSkipTLSVerify = true
err := actionConfig.Init(settings.RESTClientGetter(), "namespace", helmDriver, log.Printf)
if err != nil {
panic(err)
}
// create install client and set
client := action.NewInstall(actionConfig)
client.CreateNamespace = true
client.Namespace = "namespace"
client.Timeout = 300 * time.Second
client.RepoURL = "change to your repoURL"
client.ReleaseName = "release name"
cp, err := client.ChartPathOptions.LocateChart("chart name", settings)
if err != nil {
panic(err)
}
chartRequested, err := loader.Load(cp)
if err != nil {
panic(err)
}
// values is the value set in Values.yaml or --set command
values := make(map[string]interface{})
_, err = client.RunWithContext(context.TODO(), chartRequested, values)
if err != nil {
panic(err)
}
}
源码分析install
RunWithContext
执行安装操作
1 | // pkg/action/install.go |
Load
将chart转换成chart.Chart对象
1 | // pkg/chart/loader/load.go |
1 | // pkg/chart/loader/directory.go |
ToRenderValues
将合并后values值设置到Values中
1 | // pkg/chartutil/values.go |
renderResources
对资源进行渲染,因为chart支持了模版,用户可以自己配置一些值,所以在安装时需要对这些
参数渲染成实际的值
1 | func (cfg *Configuration) renderResources(ch *chart.Chart, values chartutil.Values, releaseName, outputDir string, subNotes, useReleaseName, includeCrds bool, pr postrender.PostRenderer, dryRun bool) ([]*release.Hook, *bytes.Buffer, string, error) { |
KindSortOrder
k8s资源的创建顺序
1 | // pkg/releaseutil/kind_sorter.go |
performInstall
真正执行资源创建的函数,将渲染后的chart转换成k8s资源yaml,并发送请求给kubernetes
1 | func (i *Install) performInstall(c chan<- resultMessage, rel *release.Release, toBeAdopted kube.ResourceList, resources kube.ResourceList) { |
源码分析uninstall
1 |
|