Skip to content

Commit

Permalink
Fix/Hack for helm cli bug
Browse files Browse the repository at this point in the history
Signed-off-by: revolyssup <[email protected]>
  • Loading branch information
Revolyssup committed Oct 6, 2022
1 parent d735cb7 commit 71a5148
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
6 changes: 1 addition & 5 deletions models/controllers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/layer5io/meshery-operator/api/v1alpha1"
"github.com/layer5io/meshkit/utils"
mesherykube "github.com/layer5io/meshkit/utils/kubernetes"
"github.com/spf13/viper"
)

const BrokerPingEndpoint = "8222/connz"
Expand Down Expand Up @@ -66,10 +65,7 @@ func applyOperatorHelmChart(chartRepo string, client mesherykube.Client, meshery
act = mesherykube.INSTALL
chart = "meshery-operator"
)
if viper.GetString("KUBERNETES_SERVICE_HOST") != "" {
act = mesherykube.UPGRADE
chart = "meshery"
} else if delete {
if delete {
act = mesherykube.UNINSTALL
}
err := client.ApplyHelmChart(mesherykube.ApplyHelmChartConfig{
Expand Down
41 changes: 27 additions & 14 deletions utils/kubernetes/apply-helm-chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ package kubernetes

import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"sync"

"github.com/layer5io/meshkit/utils"
"gopkg.in/yaml.v2"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/cli"
"helm.sh/helm/v3/pkg/getter"
"helm.sh/helm/v3/pkg/repo"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/rest"
)

Expand Down Expand Up @@ -401,26 +403,37 @@ func checkIfInstallable(ch *chart.Chart) error {
return ErrApplyHelmChart(fmt.Errorf("%s charts are not installable", ch.Metadata.Type))
}

var mx sync.Mutex

// createHelmActionConfig generates the actionConfig with the appropriate defaults
func createHelmActionConfig(restConfig rest.Config, cfg ApplyHelmChartConfig) (*action.Configuration, error) {
mx.Lock() //We need the lock here because we are setting environment variables to signal helm about kubeconfig data and two functions might concurrently try to set these variable causing issues.
defer mx.Unlock() //Once we exist this function, we can release the lock and environment variables can be reset by another function as the input to helm would have been set by the end of this function.
// Set the environment variable needed by the Init method
os.Setenv("HELM_DRIVER_SQL_CONNECTION_STRING", cfg.SQLConnectionString)

os.Setenv("HELM_KUBEAPISERVER", restConfig.Host)
os.Setenv("HELM_KUBETOKEN", restConfig.BearerToken)
f, err := os.CreateTemp("", "*")
if err != nil {
return nil, fmt.Errorf("could not create temporary CAFile: " + err.Error())
}
defer func() {
_ = os.Remove(f.Name())
}()
_, err = io.WriteString(f, restConfig.CAFile)
if err != nil {
return nil, fmt.Errorf("could not create temporary CAFile: " + err.Error())
}
os.Setenv("HELM_KUBECAFILE", f.Name())
// KubeConfig setup
kubeConfig := cli.New()
kubeConfig.KubeAPIServer = restConfig.Host
kubeConfig.KubeToken = restConfig.BearerToken
kubeConfig.KubeCaFile = restConfig.CAFile
kubeConfig.SetNamespace(cfg.Namespace)
kubeConfig := genericclioptions.NewConfigFlags(false)
//We were setting the below fields earlier but it is of no use as helm cli function doesn't respect these passed fields.
// kubeConfig.APIServer = &restConfig.Host
// kubeConfig.BearerToken = &restConfig.BearerToken
// kubeConfig.CAFile = &restConfig.CAFile

actionConfig := new(action.Configuration)
err := actionConfig.Init(
kubeConfig.RESTClientGetter(),
kubeConfig.Namespace(),
string(cfg.HelmDriver),
cfg.Logger,
)
if err != nil {
if err := actionConfig.Init(kubeConfig, cfg.Namespace, string(cfg.HelmDriver), cfg.Logger); err != nil {
return nil, ErrApplyHelmChart(err)
}

Expand Down

0 comments on commit 71a5148

Please sign in to comment.