import (
"bytes"
"fmt"
+ "os"
+
"github.com/sirupsen/logrus"
"helm.sh/helm/v3/pkg/action"
"helm.sh/helm/v3/pkg/chart/loader"
"helm.sh/helm/v3/pkg/kube"
- "os"
)
// Variables to be defined in deployment file
var (
- chartPath = os.Getenv("CHART_PATH")
- kubeconfigPath = os.Getenv("KUBECONFIG_DIR_PATH")
+ chartPath = os.Getenv("CHART_PATH")
+ kubeconfigPath = os.Getenv("KUBECONFIG_DIR_PATH")
releaseNamespace = os.Getenv("RELEASE_NAMESPACE")
)
-
// Helm client
type HelmClient struct {
- hostIP string
+ hostIP string
kubeconfig string
- logger *logrus.Logger
+ logger *logrus.Logger
}
// Constructor of helm client for a given host IP
if exists {
return &HelmClient{hostIP: hostIP, kubeconfig: kubeconfigPath + hostIP, logger: logger}, nil
} else {
- logger.Errorf("No file exist with name: %s. Err: %s", kubeconfigPath + hostIP)
+ logger.Errorf("No file exist with name: %s. Err: %s", kubeconfigPath+hostIP)
return nil, err
}
}
// Create temporary file to hold helm chart
file, err := os.Create(chartPath + "temp.tar.gz")
if err != nil {
- hc.logger.Errorf("Unable to create file: %s. Err: %s", chartPath + "temp.tar.gz", err)
+ hc.logger.Errorf("Unable to create file: %s. Err: %s", chartPath+"temp.tar.gz", err)
return "", err
}
defer os.Remove(chartPath + "temp.tar.gz")
// Write input bytes to temp file
_, err = helmPkg.WriteTo(file)
if err != nil {
- hc.logger.Errorf("Unable to write to file: %s. Err: %s", chartPath + "temp.tar.gz", err)
+ hc.logger.Errorf("Unable to write to file: %s. Err: %s", chartPath+"temp.tar.gz", err)
return "", err
}
// Load the file to chart
chart, err := loader.Load(chartPath + "temp.tar.gz")
if err != nil {
- hc.logger.Errorf("Unable to load chart from file: %s. Err: %s", chartPath + "temp.tar.gz", err)
+ hc.logger.Errorf("Unable to load chart from file: %s. Err: %s", chartPath+"temp.tar.gz", err)
return "", err
}
hc.logger.Errorf("Unable to install chart with release name: %s. Err: %s", relName, err)
return "", err
}
- hc.logger.Info("Successfully create chart with release name: %s", relName)
+ hc.logger.Infof("Successfully create chart with release name: %s", relName)
return rel.Name, err
}
// Un-Install a given helm chart
-func (hc *HelmClient) uninstallChart(relName string) (error) {
+func (hc *HelmClient) uninstallChart(relName string) error {
// Prepare action config and uninstall chart
actionConfig := new(action.Configuration)
if err := actionConfig.Init(kube.GetConfig(hc.kubeconfig, "", releaseNamespace), releaseNamespace,
}
ui := action.NewUninstall(actionConfig)
- res, err := ui.Run(relName);
+ res, err := ui.Run(relName)
if err != nil {
hc.logger.Errorf("Unable to uninstall chart with release name: %s. Err: %s", relName, err)
return err
}
- hc.logger.Info("Successfully uninstalled chart with release name: %s. Response Info: %s", res.Release.Name, res.Info)
+ hc.logger.Infof("Successfully uninstalled chart with release name: %s. Response Info: %s", res.Release.Name, res.Info)
return nil
}
// Query a given chart
-func (hc *HelmClient) queryChart(relName string) (string, error) {
+func (hc *HelmClient) queryChart(relName string) (string, error) {
actionConfig := new(action.Configuration)
if err := actionConfig.Init(kube.GetConfig(hc.kubeconfig, "", releaseNamespace), releaseNamespace,
os.Getenv("HELM_DRIVER"), func(format string, v ...interface{}) {
return false, err
}
return !info.IsDir(), nil
-}
\ No newline at end of file
+}