gpt4 book ai didi

go - 使用 kubeconfig 进行 helm 客户端安装挂起

转载 作者:行者123 更新时间:2023-12-03 17:09:02 28 4
gpt4 key购买 nike

我正在使用掌 Helm SDK它工作得很好,对于我使用有效的假选项(对于 kubeconfig)进行的测试,
现在当我更新 kubeconfig在我的集群中,我注意到在安装过程中图表是 卡住 状态待定 ,
永远卡住在这种状态下,直到我再次删除并安装它,(手动)
我的问题是如何解决这个问题
Helm SDK (仅通过代码)https://pkg.go.dev/helm.sh/helm/v3 ,
我的意思是稍等片刻,如果状态在 3 分钟后挂起,请删除并重新安装...或在此之前尝试升级
这是代码

    kubeConfigPath, err := findKubeConfig()
if err != nil {
fmt.Println()
}

actionConfig := &action.Configuration{
}

cfg := cli.New()
clientGetter := genericclioptions.NewConfigFlags(false)
clientGetter.KubeConfig = &kubeConfigPath

actionConfig.Init(clientGetter, "def", "memory", log.Printf)
if err != nil {
fmt.Println(err)
}

chart, err := installation.InstallChart(cfg, "test", "chart1", "./charts/dns", nil, actionConfig)
if err != nil {
fmt.Println(err)
}
fmt.Println(chart)
}

func findKubeConfig() (string, error) {
env := os.Getenv("KUBECONFIG")
if env != "" {
return env, nil
}
path, err := homedir.Expand("~/.kube/config")
if err != nil {
return "", err
}
return path, nil
}

最佳答案

看例子,不知道是什么installation包是但我觉得你需要使用 Loader (也许你正在那个包中使用它)
通过快速搜索 github issues我发现有人有类似的问题 - 他们得到了同样的建议。这是一个派生的例子:

package main

import (
"fmt"
"os"

"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/kube"
"helm.sh/helm/v3/pkg/release"
_ "k8s.io/client-go/plugin/pkg/client/auth"
)

func main() {
chartPath := "./charts/dns"
chart, err := loader.Load(chartPath)
if err != nil {
panic(err)
}

kubeconfigPath := findKubeConfig()
releaseName := "test"
releaseNamespace := "default"
actionConfig := new(action.Configuration)
if err := actionConfig.Init(kube.GetConfig(kubeconfigPath, "", releaseNamespace), releaseNamespace, os.Getenv("HELM_DRIVER"), func(format string, v ...interface{}) {
fmt.Sprintf(format, v)
}); err != nil {
panic(err)
}

iCli := action.NewInstall(actionConfig)
iCli.Namespace = releaseNamespace
iCli.ReleaseName = releaseName
rel, err := iCli.Run(chart, nil)
if err != nil {
panic(err)
}
fmt.Println("Successfully installed release: ", rel.Name)
rel.Info.Status
// check Release Status, feel free to run it in a go routine along the deletetion logic
upCli := action.NewUpgrade(actionConfig)
upgradedRel, err := pollAndUpdate(rel.Info.Status, upCli) // see if its better to just run that code here directly :shrug:

// if we still on pending, then delete it
if upgradedRel.Info.Status.IsPending() {
unCli := action.NewUninstall(actionConfig)
res, err := unCli.Run(rel.Name)
if err != nil {
panic(err)
}
}
}

func pollAndUpdate(originalRel *release.Release, upgradeCli *action.Upgrade) (*release.Release, error) {
if !originalRel.Info.Status.IsPending() {
return originalRel, nil
}
c := time.Tick(10 * time.Second) // we gonna time it out besides checking repeatedly
var rel *release.Release = originalRel
for _ = range c {
//check the status and try and upgrade
for rel.Info.Status.IsPending() { // https://pkg.go.dev/helm.sh/helm/v3@v3.5.4/pkg/release#Status.IsPending
// run the upgrade command you have
// its this function: https://github.com/helm/helm/blob/main/pkg/action/upgrade.go#L111
rel, err := upgradeCli.Run(/*you gotta get all the values this needs*/)
if err != nil {
panic(err)
}
}
}
return rel, nil
}

关于go - 使用 kubeconfig 进行 helm 客户端安装挂起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67415405/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com