Golang 對接并部署 helm charts
共 5538字,需瀏覽 12分鐘
·
2024-07-01 19:51
Helm
Helm[1] 旨在簡化 Kubernetes 中復(fù)雜應(yīng)用工作負(fù)載的部署和管理。它的功能類似于 Kubernetes 的軟件包管理器,其中的軟件包被稱為 Helm Charts。Helm Chart 結(jié)合了一個模板文件和一個值文件,前者概述了要部署的 Kubernetes 資源,后者則為模板提供必要的配置。Helm Charts 中的模板系統(tǒng)可實現(xiàn)軟件包的可重用性。
Helm Go SDK
Helm 包含一個方便的 CLI,這是一個強大的命令行工具,可幫助最終用戶管理圖表生命周期的各個階段。不過,使用 CLI 在 Kubernetes 集群上以編程方式安裝 Helm 圖表,對于構(gòu)建可靠、可預(yù)測的應(yīng)用程序來說并不理想。幸運的是,Helm 開發(fā)人員將 CLI 設(shè)計為 Go SDK 的接口,我們可以在應(yīng)用中方便對接。
安裝使用 Helm Client
首先,我們需要安裝 go-helm-client,可以使用下面的命令將該模塊添加到 go.mod 文件中。
$ go get github.com/mittwald/go-helm-client
現(xiàn)在,我們可以像導(dǎo)入 helmcilent github.com/mmittwald/go-helm-client 那樣導(dǎo)入它,并在應(yīng)用程序中使用它。
package main
import (
"fmt"
"os"
helmclient "github.com/mittwald/go-helm-client"
)
func GetHelmClient(kubeConfig string, namespace string) (helmclient.Client) {
opt := &helmclient.KubeConfClientOptions{
Options: &helmclient.Options{
Namespace: namespace, // Change this to the namespace you wish to install the chart in.
RepositoryCache: "/tmp/.helmcache",
RepositoryConfig: "/tmp/.helmrepo",
Debug: true,
},
KubeContext: "",
KubeConfig: []byte(kubeConfig),
}
helmClient, err := helmclient.NewClientFromKubeConf(opt)
if err != nil {
fmt.Printf("Failed to initialize Helm Client: %v", err)
return nil
}
return helmClient
}
func main() {
dirname, err := os.UserHomeDir()
kubeConfig, err := os.ReadFile(fmt.Sprintf("%s/.kube/config", dirname))
if err != nil {
t.Fatalf("Not able to read File: %v", err)
}
helmClient := helmc.GetHelmClient(string(kubeConfig), "default")
}
如果啟用了 docker-kubernetes,就可以在本地 Kubernetes 上運行和測試。提供 kubeConfig 和命名空間后,就可以創(chuàng)建 helmClient 對象,并使用該對象執(zhí)行 helm 操作。
下面是提供的方法接口:
type Client interface {
AddOrUpdateChartRepo(entry repo.Entry) error
UpdateChartRepos() error
InstallOrUpgradeChart(ctx context.Context, spec *ChartSpec, opts *GenericHelmOptions) (*release.Release, error)
InstallChart(ctx context.Context, spec *ChartSpec, opts *GenericHelmOptions) (*release.Release, error)
UpgradeChart(ctx context.Context, spec *ChartSpec, opts *GenericHelmOptions) (*release.Release, error)
ListDeployedReleases() ([]*release.Release, error)
ListReleasesByStateMask(action.ListStates) ([]*release.Release, error)
GetRelease(name string) (*release.Release, error)
// RollBack is an interface to abstract a rollback action.
RollBack
GetReleaseValues(name string, allValues bool) (map[string]interface{}, error)
GetSettings() *cli.EnvSettings
GetProviders() getter.Providers
UninstallRelease(spec *ChartSpec) error
UninstallReleaseByName(name string) error
TemplateChart(spec *ChartSpec, options *HelmTemplateOptions) ([]byte, error)
LintChart(spec *ChartSpec) error
SetDebugLog(debugLog action.DebugLog)
ListReleaseHistory(name string, max int) ([]*release.Release, error)
GetChart(chartName string, chartPathOptions *action.ChartPathOptions) (*chart.Chart, string, error)
RunChartTests(releaseName string) (bool, error)
}
Example
下面舉例說明如何使用 helmClient, 從本地目錄安裝 helm chart。
import (
"context"
)
func main() {
// using old helmClient that we have created before
helmClient := helmc.GetHelmClient(string(kubeConfig), "default")
// Define the chart to be installed
chartSpec := ChartSpec{
ReleaseName: "nginx",
ChartName: "/tmp/nginx",
Namespace: "default",
UpgradeCRDs: true,
Wait: true,
}
// Install a chart release.
// Note that helmclient.Options.Namespace should ideally match
// the namespace in chartSpec.Namespace.
if _, err := helmClient.InstallChart(context.Background(), &chartSpec,
nil); err != nil {
panic(err)
}
}
Helm: https://helm.sh/
