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.

99
main.go
View File

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