-
Notifications
You must be signed in to change notification settings - Fork 418
cubemastercli: Refactor CubeVS params parsing logic #125
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| package cubebox | ||
|
|
||
| import ( | ||
| "net" | ||
|
|
||
| "github.com/tencentcloud/CubeSandbox/CubeMaster/pkg/service/sandbox/types" | ||
| "github.com/urfave/cli" | ||
| ) | ||
|
|
||
| const ( | ||
| keyAllowInternetAccess = "allow-internet-access" | ||
| keyAllowOutCIDR = "allow-out-cidr" | ||
| keyDenyOutCIDR = "deny-out-cidr" | ||
| ) | ||
|
|
||
| func parseCubeVSParams(c *cli.Context) (*types.CubeVSContext, error) { | ||
| if !c.IsSet(keyAllowInternetAccess) && | ||
| len(c.StringSlice(keyAllowOutCIDR)) == 0 && | ||
| len(c.StringSlice(keyDenyOutCIDR)) == 0 { | ||
| return nil, nil | ||
| } | ||
|
|
||
| params := &types.CubeVSContext{} | ||
| if c.IsSet(keyAllowInternetAccess) { | ||
| val := c.Bool(keyAllowInternetAccess) | ||
| params.AllowInternetAccess = &val | ||
| } | ||
| for _, v := range c.StringSlice(keyAllowOutCIDR) { | ||
| _, _, err := net.ParseCIDR(v) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| params.AllowOut = append(params.AllowOut, v) | ||
| } | ||
| for _, v := range c.StringSlice(keyDenyOutCIDR) { | ||
| _, _, err := net.ParseCIDR(v) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| params.DenyOut = append(params.DenyOut, v) | ||
| } | ||
|
|
||
| return params, nil | ||
| } | ||
|
|
||
| func mergeCubeVSParams(c *cli.Context, base *types.CubeVSContext) (*types.CubeVSContext, error) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Behavioral change: The old code used
While the current call sites (in
|
||
| if !c.IsSet(keyAllowInternetAccess) && | ||
| len(c.StringSlice(keyAllowOutCIDR)) == 0 && | ||
| len(c.StringSlice(keyDenyOutCIDR)) == 0 { | ||
| return base, nil | ||
| } | ||
|
|
||
| if base == nil { | ||
| base = &types.CubeVSContext{} | ||
| } | ||
|
|
||
| if c.IsSet(keyAllowInternetAccess) { | ||
| val := c.Bool(keyAllowInternetAccess) | ||
| base.AllowInternetAccess = &val | ||
| } | ||
| for _, v := range c.StringSlice(keyAllowOutCIDR) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Behavioral regression: CIDR deduplication removed The old code used This was intentional in the old implementation — is the removal deliberate? If so, it should be noted in the PR description. |
||
| _, _, err := net.ParseCIDR(v) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| base.AllowOut = append(base.AllowOut, v) | ||
| } | ||
| for _, v := range c.StringSlice(keyDenyOutCIDR) { | ||
| _, _, err := net.ParseCIDR(v) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| base.DenyOut = append(base.DenyOut, v) | ||
| } | ||
|
|
||
| return base, nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,7 +9,6 @@ import ( | |
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/tencentcloud/CubeSandbox/CubeMaster/pkg/service/sandbox/types" | ||
| "github.com/urfave/cli" | ||
| ) | ||
|
|
||
|
|
@@ -71,46 +70,6 @@ func TestCreateCommandParsesNodeScope(t *testing.T) { | |
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing tests for new functions Four tests were removed:
But no new tests were added for
|
||
| } | ||
|
|
||
| func TestMergeCreateFromImageCubeVSContextFlagsEqualsSyntax(t *testing.T) { | ||
| ctx := newCreateFromImageContext(t, []string{ | ||
| "--allow-internet-access=false", | ||
| "--allow-out-cidr", "172.67.0.0/16", | ||
| "--deny-out-cidr", "10.0.0.0/8", | ||
| }) | ||
|
|
||
| got, err := mergeCreateFromImageCubeVSContextFlags(ctx, nil) | ||
| if err != nil { | ||
| t.Fatalf("mergeCreateFromImageCubeVSContextFlags error=%v", err) | ||
| } | ||
| if got == nil || got.AllowInternetAccess == nil || *got.AllowInternetAccess { | ||
| t.Fatalf("AllowInternetAccess=%v, want false", got) | ||
| } | ||
| if len(got.AllowOut) != 1 || got.AllowOut[0] != "172.67.0.0/16" { | ||
| t.Fatalf("AllowOut=%v, want [172.67.0.0/16]", got.AllowOut) | ||
| } | ||
| if len(got.DenyOut) != 1 || got.DenyOut[0] != "10.0.0.0/8" { | ||
| t.Fatalf("DenyOut=%v, want [10.0.0.0/8]", got.DenyOut) | ||
| } | ||
| } | ||
|
|
||
| func TestMergeCreateFromImageCubeVSContextFlagsSupportsTrailingFalse(t *testing.T) { | ||
| ctx := newCreateFromImageContext(t, []string{ | ||
| "--allow-internet-access", "false", | ||
| "--allow-out-cidr", "172.67.0.0/16", | ||
| }) | ||
|
|
||
| got, err := mergeCreateFromImageCubeVSContextFlags(ctx, nil) | ||
| if err != nil { | ||
| t.Fatalf("mergeCreateFromImageCubeVSContextFlags error=%v", err) | ||
| } | ||
| if got == nil || got.AllowInternetAccess == nil || *got.AllowInternetAccess { | ||
| t.Fatalf("AllowInternetAccess=%v, want false", got) | ||
| } | ||
| if len(got.AllowOut) != 1 || got.AllowOut[0] != "172.67.0.0/16" { | ||
| t.Fatalf("AllowOut=%v, want [172.67.0.0/16]", got.AllowOut) | ||
| } | ||
| } | ||
|
|
||
| func TestCreateFromImageCommandParsesNodeScope(t *testing.T) { | ||
| ctx := newCreateFromImageContext(t, []string{ | ||
| "--node", "node-a", | ||
|
|
@@ -121,32 +80,6 @@ func TestCreateFromImageCommandParsesNodeScope(t *testing.T) { | |
| } | ||
| } | ||
|
|
||
| func TestMergeCreateFromImageCubeVSContextFlagsRejectsUnexpectedArgs(t *testing.T) { | ||
| ctx := newCreateFromImageContext(t, []string{ | ||
| "--allow-internet-access", "false", | ||
| "unexpected", | ||
| }) | ||
|
|
||
| _, err := mergeCreateFromImageCubeVSContextFlags(ctx, nil) | ||
| if err == nil { | ||
| t.Fatal("expected error for unexpected trailing argument") | ||
| } | ||
| } | ||
|
|
||
| func TestMergeCubeVSContextValuesPreservesExistingCIDRs(t *testing.T) { | ||
| existing := &types.CubeVSContext{ | ||
| AllowOut: []string{"192.168.0.0/16"}, | ||
| } | ||
|
|
||
| got := mergeCubeVSContextValues(existing, true, false, []string{"172.67.0.0/16"}, nil) | ||
| if got == nil || got.AllowInternetAccess == nil || *got.AllowInternetAccess { | ||
| t.Fatalf("AllowInternetAccess=%v, want false", got) | ||
| } | ||
| if len(got.AllowOut) != 2 || got.AllowOut[0] != "192.168.0.0/16" || got.AllowOut[1] != "172.67.0.0/16" { | ||
| t.Fatalf("AllowOut=%v, want merged CIDRs", got.AllowOut) | ||
| } | ||
| } | ||
|
|
||
| func TestRedoCommandParsesNodeScope(t *testing.T) { | ||
| ctx := newRedoContext(t, []string{ | ||
| "--template-id", "tpl-1", | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug:
parseCubeVSParamsalways returns a non-nil*types.CubeVSContextWhen no network flags are set, this function returns
&types.CubeVSContext{}instead ofnil. Thecreate-from-imagecommand callsparseCubeVSParams(notmergeCubeVSParams), soreq.CubeVSContextwill always be set to a non-nil empty struct even when the user provides no network flags.The old code returned
nilwhen no flags were set, which means theCubeVSContextfield would be omitted from the JSON payload (due toomitempty). With this change, it will always be serialized as"cubevs_context":{}.Suggested fix — return
nilwhen no flags are set: