Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the early parsing of platform flag in init() #1451

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion cmd/skupper/skupper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1039,12 +1039,27 @@ func addCommands(skupperCli SkupperClient, rootCmd *cobra.Command, cmds ...*cobr
}
}

func parsePlatformFlagOnly(args []string) {
// This function is called when not every supported command flag is defined yet
for i, p := range args {
if p == "--platform" && i < len(args)-1 {
config.Platform = args[i+1]
break
} else if strings.HasPrefix(p, "--platform=") {
_, v, _ := strings.Cut(p, "=")
config.Platform = v
break
}
}
}

func init() {
rootCmd = &cobra.Command{Use: "skupper"}
routev1.AddToScheme(scheme.Scheme)

rootCmd.PersistentFlags().StringVarP(&config.Platform, "platform", "", "", "The platform type to use [kubernetes, podman]")
rootCmd.ParseFlags(os.Args)
// Only the "platform" flag is defined at this point so it is unsafe to call rootCmd.ParseFlags(os.Args) here
parsePlatformFlagOnly(os.Args)

var skupperCli SkupperClient
switch config.GetPlatform() {
Expand Down
29 changes: 29 additions & 0 deletions cmd/skupper/skupper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/skupperproject/skupper/api/types"
"github.com/skupperproject/skupper/pkg/config"

"gotest.tools/assert"
)
Expand Down Expand Up @@ -317,3 +318,31 @@ func TestSkupperInitFlowCollectorParseArgs(t *testing.T) {
assert.Equal(t, routerCreateOpts.FlowCollector.MemoryLimit, "3G")
assert.Equal(t, routerCreateOpts.FlowCollector.FlowRecordTtl, time.Minute*15)
}

func TestParsePlatformFlagOnly(t *testing.T) {
config.Platform = ""
args := []string{"skupper", "expose", "host", "10.0.0.1", "--address", "test-service", "--port", "8080", "--platform", "podman"}
parsePlatformFlagOnly(args)
assert.Equal(t, config.Platform, types.PlatformPodman)

config.Platform = ""
args = []string{"skupper", "expose", "host", "10.0.0.1", "--address", "test-service", "--port", "8080", "--platform=podman"}
parsePlatformFlagOnly(args)
assert.Equal(t, config.Platform, types.PlatformPodman)

config.Platform = ""
args = []string{"skupper", "--platform", "podman", "expose", "host", "10.0.0.1", "--address", "test-service", "--port", "8080"}
parsePlatformFlagOnly(args)
assert.Equal(t, config.Platform, types.PlatformPodman)

config.Platform = ""
args = []string{"skupper", "--platform=podman", "expose", "host", "10.0.0.1", "--address", "test-service", "--port", "8080"}
parsePlatformFlagOnly(args)
assert.Equal(t, config.Platform, types.PlatformPodman)

// Bad value
config.Platform = ""
args = []string{"skupper", "--platform=", "expose", "host", "10.0.0.1", "--address", "test-service", "--port", "8080"}
parsePlatformFlagOnly(args)
assert.Equal(t, config.Platform, "")
}