diff --git a/apps/helm_utils.go b/apps/helm_utils.go index 7f01202..c5ab1cc 100644 --- a/apps/helm_utils.go +++ b/apps/helm_utils.go @@ -171,3 +171,49 @@ func UninstallHelmRelease(releaseName, namespace string) error { return nil } + +// Get values for a release from helm command +func HelmGetValues(releaseName string, namespace string) ([]byte, error) { + // Define the Helm installation command + cmd := exec.Command("helm", "get", "values", releaseName, "-n", namespace) + // Execute the command + output, err := cmd.CombinedOutput() + if err != nil { + return output,fmt.Errorf("failed to helm get values for release %s : %v\n%s", releaseName, err, output) + } + return output, nil +} +// Save values for a release from helm command to a yaml file +func SaveHelmValues(releaseName string, namespace string, filePath string) error { + output, err := HelmGetValues(releaseName, namespace) + if err != nil { + return err + } + dir := filepath.Dir(filePath) + err = os.MkdirAll(dir, 0755) // Ensure intermediate directories are created + if err != nil { + return err + } + // Write the output to a YAML file. + if err := os.WriteFile(filePath, output, 0644); err != nil { + return fmt.Errorf("failed to write output to file %s: %v", filePath, err) + } + return nil +} +// Upgrade a chart from a yaml file using helm command +func HelmUpgradeChartfromYaml(helmChart string, namespace string, releaseName string, setValues map[string]interface{}, filePath string, version string) ([]byte, error) { + var vals []string + for k, v := range setValues { + vals = append(vals, fmt.Sprintf("%s=%v", k, v)) + } + setVals := strings.Join(vals, ",") + logf.Log.Info("executing helm upgrade ", "releaseName: ", releaseName, ", chart: ", helmChart, ", namespace: ", namespace, ", old-values: ", filePath, ", version: ", version, ", values: ", setVals) + // Define the Helm upgrade command + cmd := exec.Command("helm", "upgrade", releaseName, helmChart, "-n", namespace, "-f", filePath, "--version", version,"--set", setVals) + // Execute the command + output, err := cmd.CombinedOutput() + if err != nil { + return output,fmt.Errorf("failed to upgrade with Helm: %v\n%s", err, output) + } + return output, nil +} diff --git a/common/k8sinstall/util.go b/common/k8sinstall/util.go index c4ec90e..51687eb 100644 --- a/common/k8sinstall/util.go +++ b/common/k8sinstall/util.go @@ -228,7 +228,7 @@ func SetRdmaViaHelm(enableRdma bool, iface string, helmChart, helmRelease, helmV "io_engine.target.nvmf.iface": iface, } - err := k8stest.UpgradeHelmChart(helmChart, + _, err := k8stest.UpgradeHelmChart(helmChart, common.NSMayastor(), helmRelease, helmVersion, diff --git a/common/k8stest/util_helm.go b/common/k8stest/util_helm.go index 7639bab..9e5eda9 100644 --- a/common/k8stest/util_helm.go +++ b/common/k8stest/util_helm.go @@ -8,7 +8,7 @@ import ( logf "sigs.k8s.io/controller-runtime/pkg/log" ) -func UpgradeHelmChart(helmChart, namespace, releaseName, version string, values map[string]interface{}) error { +func UpgradeHelmChart(helmChart, namespace, releaseName, version string, values map[string]interface{}) ([]byte, error) { var vals []string for k, v := range values { vals = append(vals, fmt.Sprintf("%s=%v", k, v)) @@ -20,7 +20,7 @@ func UpgradeHelmChart(helmChart, namespace, releaseName, version string, values // Execute the command. output, err := cmd.CombinedOutput() if err != nil { - return fmt.Errorf("failed to upgrade with Helm: %v\n%s", err, output) + return output, fmt.Errorf("failed to upgrade with Helm: %v\n%s", err, output) } - return nil + return output, nil }