Add subchart cli argument

This is necessary for the umbrella chart
which contains overrides for multiple subcharts.

Change-Id: Ic36c807b12abfb3147912ecee1626c54e781ef73
This commit is contained in:
Vladimir Kozhukalov 2024-04-18 17:26:06 -05:00
parent 4b4f8c3412
commit 611f0cefdc
2 changed files with 70 additions and 45 deletions

Binary file not shown.

115
main.go
View File

@ -27,30 +27,44 @@ import (
"github.com/spf13/cobra"
)
const BaseUrl string = "https://opendev.org/openstack/openstack-helm/raw/branch/master"
const DefaultBaseUrl string = "https://opendev.org/openstack/openstack-helm/raw/branch/master"
var Cwd string
var Download bool
var OverridesPath string
var DownloadBaseUrl string
func newRootCommand() *cobra.Command {
var cwd string
var download bool
var baseUrl string
var basePath string
var subchart string
var chart string
var rootCmd = &cobra.Command{
Use: "get-values-overrides",
Args: cobra.MinimumNArgs(2),
Run: func(cmd *cobra.Command, args []string) {
chart := args[0]
features := args[1:]
overrideCandidates := generateOverrideCandidates(chart, features)
overrideArgs := getOverrideHelmArguments(chart, overrideCandidates)
fmt.Println(strings.Join(overrideArgs, " "))
},
}
func init() {
Cwd, _ = os.Getwd()
rootCmd.Flags().BoolVarP(&Download, "download", "d", false, "Download the overrides from the internet if does not exist in the path (default: false)")
rootCmd.Flags().StringVarP(&DownloadBaseUrl, "url", "u", BaseUrl, fmt.Sprintf("Base url to download overrides (default: %s)", BaseUrl))
rootCmd.Flags().StringVarP(&OverridesPath, "path", "p", Cwd, "Path to the overrides (default: current directory)")
cwd, _ = os.Getwd()
rootCmd := &cobra.Command{
Use: "get-values-overrides",
Run: func(cmd *cobra.Command, args []string) {
features := args[0:]
if len(features) == 0 {
fmt.Fprintln(os.Stderr, "No features provided")
os.Exit(0)
}
fmt.Fprintf(os.Stderr, "Base URL: %s\nBase path: %s\n", baseUrl, basePath)
fmt.Fprintf(os.Stderr, "Chart: %s\n", chart)
if subchart != "" {
fmt.Fprintf(os.Stderr, " Subchart: %s\n", subchart)
}
fmt.Fprintf(os.Stderr, "Features: %s\n", strings.Join(features, " "))
overrideCandidates := generateOverrideCandidates(features)
overrideArgs := getOverrideArgs(baseUrl, basePath, chart, subchart, overrideCandidates, download)
fmt.Fprintf(os.Stderr, "Resulting override args: %s\n", strings.Join(overrideArgs, " "))
fmt.Println(strings.Join(overrideArgs, " "))
},
}
rootCmd.Flags().BoolVarP(&download, "download", "d", false, "Download the overrides from the internet if does not exist in the path (default: false)")
rootCmd.Flags().StringVarP(&baseUrl, "url", "u", DefaultBaseUrl, "Base url to download overrides")
rootCmd.Flags().StringVarP(&basePath, "path", "p", cwd, "Path to the overrides")
rootCmd.Flags().StringVarP(&subchart, "subchart", "s", "", "Subchart to get the overrides for")
rootCmd.Flags().StringVarP(&chart, "chart", "c", "", "Chart to get the overrides for")
rootCmd.MarkFlagRequired("chart")
return rootCmd
}
func sliceReverse[T comparable](s []T) {
@ -70,13 +84,23 @@ func num2items(num uint32, power int) []int {
return featureNums
}
func overrideFile(chart, overrideName string) string {
return filepath.Join(OverridesPath, chart, "values_overrides", overrideName)
func overrideFile(basePath, chart, subchart, overrideName string) string {
if subchart != "" {
return filepath.Join(basePath, chart, "values_overrides", subchart, overrideName)
}
return filepath.Join(basePath, chart, "values_overrides", overrideName)
}
func downloadOverride(chart, overrideName string) error {
fullUrl := fmt.Sprintf("%s/%s/values_overrides/%s", DownloadBaseUrl, chart, overrideName)
filename := overrideFile(chart, overrideName)
func overrideUrl(baseUrl, chart, subchart, overrideName string) string {
if subchart != "" {
return fmt.Sprintf("%s/%s/values_overrides/%s/%s", baseUrl, chart, subchart, overrideName)
}
return fmt.Sprintf("%s/%s/values_overrides/%s", baseUrl, chart, overrideName)
}
func downloadOverride(baseUrl, basePath, chart, subchart, overrideName string) error {
fullUrl := overrideUrl(baseUrl, chart, subchart, overrideName)
filename := overrideFile(basePath, chart, subchart, overrideName)
fmt.Fprintf(os.Stderr, "Trying to download %s\n", fullUrl)
resp, err := http.Get(fullUrl)
if err != nil {
@ -85,7 +109,6 @@ func downloadOverride(chart, overrideName string) error {
if resp.StatusCode != 200 {
return fmt.Errorf("failed to download %s: %s", fullUrl, resp.Status)
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
@ -102,8 +125,7 @@ func downloadOverride(chart, overrideName string) error {
return nil
}
func generateOverrideCandidates(chart string, features []string) []string {
fmt.Fprintf(os.Stderr, "Chart: %s Features: %s\n", chart, strings.Join(features, " "))
func generateOverrideCandidates(features []string) []string {
sliceReverse(features)
overrideCandidates := make([]string, 0)
for num := uint32(1); num < uint32(math.Pow(2, float64(len(features)))); num++ {
@ -117,31 +139,34 @@ func generateOverrideCandidates(chart string, features []string) []string {
return overrideCandidates
}
func getOverrideHelmArguments(chart string, overrideCandidates []string) []string {
overrides := make([]string, 0)
func getOverrideArgs(baseUrl, basePath, chart, subchart string, overrideCandidates []string, download bool) []string {
overrideArgs := make([]string, 0)
for _, overrideCandidate := range overrideCandidates {
overrideCandidateFile := overrideFile(chart, overrideCandidate)
overrideCandidateFile := overrideFile(basePath, chart, subchart, overrideCandidate)
fmt.Fprintf(os.Stderr, "Override candidate: %s\n", overrideCandidateFile)
_, err := os.Stat(overrideCandidateFile)
if err == nil {
fmt.Fprintf(os.Stderr, "File found: %s\n", overrideCandidateFile)
overrides = append(overrides, fmt.Sprintf("--values %s", overrideCandidateFile))
} else if Download {
overrideArgs = append(overrideArgs, fmt.Sprintf("--values %s", overrideCandidateFile))
} else {
fmt.Fprintf(os.Stderr, "File not found: %s\n", overrideCandidateFile)
err = downloadOverride(chart, overrideCandidate)
if err != nil {
fmt.Fprintln(os.Stderr, err)
} else {
fmt.Fprintf(os.Stderr, "Successfully downloaded %s\n", overrideCandidate)
overrides = append(overrides, fmt.Sprintf("--values %s", overrideCandidateFile))
if download {
err = downloadOverride(baseUrl, basePath, chart, subchart, overrideCandidate)
if err != nil {
fmt.Fprintln(os.Stderr, err)
} else {
fmt.Fprintf(os.Stderr, "Successfully downloaded %s\n", overrideCandidate)
overrideArgs = append(overrideArgs, fmt.Sprintf("--values %s", overrideCandidateFile))
}
}
}
}
fmt.Fprintln(os.Stderr, "Resulting override Helm arguments:")
fmt.Fprintln(os.Stderr, strings.Join(overrides, " "))
return overrides
return overrideArgs
}
func main() {
rootCmd.Execute()
if err := newRootCommand().Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}