Go install chart and check if app is up and running

3/8/2021

I've the following code which works,I was able to create a helm chart in the target cluster. when you install some chart until the application (within the chart) is available it takes time , How can I check if the application that installed via helm chart is up and running ? is there a way to do it with the helm client (we are using helm 3.5.2)

tmpfile, err := ioutil.TempFile(kp, kcp)
if err != nil {
	log.Error(err, "error")
}

defer os.Remove(tmpfile.Name()) 

if _, err := tmpfile.Write(cfg); err != nil {
	return err
}
if err := tmpfile.Close(); err != nil {
	return err
}

kcfgFilePath := tmpfile.Name()
settings := cli.New()
ac := new(action.Configuration)
clientGetter := genericclioptions.NewConfigFlags(false)
clientGetter.KubeConfig = &kcfgFilePath



for _, chartInstallation := range charts {
	chart, err := loader.Load(chartInstallation.Path)
	if err != nil {
		return err
	}

	releaseName := releaseName + "-" + chartInstallation.Name
	if err := ac.Init(clientGetter, settings.Namespace(), os.Getenv("HELM_DRIVER"), func(format string, v ...interface{}) {
		
	}); err != nil {
		return err
	}
	releasePresent := true
	statusAction := action.NewStatus(ac)
	status, err := statusAction.Run(releaseName)
	if err != nil {
		if strings.Contains(err.Error(), driver.ErrReleaseNotFound.Error()) {
			releasePresent = false
		} else {
			return err
		}
	}

	if !releasePresent {
		// install chart
		installAction := action.NewInstall(ac)
		installAction.CreateNamespace = true
		installAction.Namespace = chartInstallation.Namespace
		installAction.ReleaseName = releaseName

		_, err := installAction.Run(chart, nil)
		if err != nil {
			return err
		}
		log.Info(“chart installed: ", "releaseName", releaseName)
	}

	if status != nil {
		if releasePresent && status.Info.Status.String() == release.StatusFailed.String() { 
			upgradeAction := action.NewUpgrade(ac)
			upgradeAction.Wait = true
			upgradeAction.ReuseValues = false
			upgradeAction.Recreate = false
			_, err := upgradeAction.Run(releaseName, chart, nil)
			if err != nil {
				return err
			}
		}
	}
-- JJD
go
kubernetes
kubernetes-helm

1 Answer

3/8/2021

You need to helm install (or upgrade) using the --wait flag:

--wait: Waits until all Pods are in a ready state, PVCs are bound, Deployments have minimum (Desired minus maxUnavailable) Pods in ready state and Services have an IP address (and Ingress if a LoadBalancer) before marking the release as successful. It will wait for as long as the --timeout ...

Make sure that your pods have Liveness and Readiness checks. Using the --wait the release will only return successfully after these checks are passing.

-- Eduardo Baitello
Source: StackOverflow