Skip to content

Commit

Permalink
Fix the early parsing of platform flag in init()
Browse files Browse the repository at this point in the history
Fixes #1147

Signed-off-by: Gabor Dozsa <[email protected]>
  • Loading branch information
gabordozsa committed Apr 23, 2024
1 parent a1c5dd9 commit 906d137
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
26 changes: 24 additions & 2 deletions cmd/skupper/skupper.go
Original file line number Diff line number Diff line change
Expand Up @@ -1039,12 +1039,34 @@ 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.Split(p,"=")
if len(v) == 2 {
config.Platform = v[1]
}
break
}
}
}

func init() {
// This enables calling init explicitly in tests
initWrapper(os.Args)
}

func initWrapper(args []string) {
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)
rootCmd.PersistentFlags().StringVarP(&config.Platform, "platform", "", "", "The platform type t [kubernetes, podman]")
// Only the "platform" flag is defined at this point so it is unsafe to call rootCmd.ParseFlags(os.Args) here
parsePlatformFlagOnly(args)

var skupperCli SkupperClient
switch config.GetPlatform() {
Expand Down
15 changes: 15 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,17 @@ func TestSkupperInitFlowCollectorParseArgs(t *testing.T) {
assert.Equal(t, routerCreateOpts.FlowCollector.MemoryLimit, "3G")
assert.Equal(t, routerCreateOpts.FlowCollector.FlowRecordTtl, time.Minute*15)
}

func TestPlatformFlag(t *testing.T) {
t.Setenv("types.ENV_PLATFORM", "") // The platform is expected to be defined by the command line option
config.Platform = ""
args := []string{ "skupper", "expose", "host", "10.0.0.1", "--address", "test-service", "--port", "8080", "--platform", "podman"}
initWrapper(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"}
initWrapper(args)
assert.Equal(t, config.Platform, types.PlatformPodman)

}

0 comments on commit 906d137

Please sign in to comment.