使用 kubeconfig 进行 helm 客户端安装 panding

Use kubeconfig for helm client install panding

我正在使用 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 (也许你在那个 pkg 中使用它)

通过对 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
}