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 4f6fb12
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
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, "")
}

0 comments on commit 4f6fb12

Please sign in to comment.