Skip to content

Commit

Permalink
fix: deprecated konnect flag cannot manage non-default CPs (#1036)
Browse files Browse the repository at this point in the history
  • Loading branch information
GGabriele authored Sep 27, 2023
1 parent 1ba57a5 commit 84e9bba
Show file tree
Hide file tree
Showing 12 changed files with 288 additions and 75 deletions.
57 changes: 35 additions & 22 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,36 @@ func getWorkspaceName(workspaceFlag string, targetContent *file.Content,
return targetContent.Workspace
}

func evaluateTargetRuntimeGroupOrControlPlaneName(targetContent *file.Content) error {
targetControlPlane := targetContent.Konnect.ControlPlaneName
targetRuntimeGroup := targetContent.Konnect.RuntimeGroupName
if targetControlPlane != "" && targetRuntimeGroup != "" {
return errors.New(`cannot set both runtime_group_name and control_plane_name. ` +
`Please use only control_plane_name`)
}
targetFromFile := targetControlPlane
if targetFromFile == "" {
targetFromFile = targetRuntimeGroup
}
targetFromCLI := konnectControlPlane
if targetFromCLI == "" {
targetFromCLI = konnectRuntimeGroup
}
if targetFromCLI != "" && targetFromFile != targetFromCLI {
return fmt.Errorf("warning: control plane '%v' specified via "+
"--konnect-[control-plane|runtime-group]-name flag is "+
"different from '%v' found in state file(s)",
targetFromCLI, targetFromFile)
}
if targetControlPlane != "" {
konnectControlPlane = targetControlPlane
}
if targetRuntimeGroup != "" {
konnectControlPlane = targetRuntimeGroup
}
return nil
}

func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
delay int, workspace string, enableJSONOutput bool,
) error {
Expand Down Expand Up @@ -134,30 +164,13 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
return fmt.Errorf("--workspace flag is not supported when running against Konnect")
}
if targetContent.Konnect != nil {
targetControlPlane := targetContent.Konnect.ControlPlaneName
targetRuntimeGroup := targetContent.Konnect.RuntimeGroupName
if targetControlPlane != "" && targetRuntimeGroup != "" {
return errors.New(`cannot set both runtime_group_name and control_plane_name. ` +
`Please use only control_plane_name`)
}
if konnectControlPlane != "" &&
targetControlPlane != konnectControlPlane {
return fmt.Errorf("warning: control plane '%v' specified via "+
"--konnect-control-plane-name flag is "+
"different from '%v' found in state file(s)",
konnectControlPlane, targetControlPlane)
}
if konnectRuntimeGroup != "" &&
targetRuntimeGroup != konnectRuntimeGroup {
return fmt.Errorf("warning: runtime group '%v' specified via "+
"--konnect-runtime-group-name flag is "+
"different from '%v' found in state file(s)",
konnectRuntimeGroup, targetContent.Konnect.RuntimeGroupName)
}
if konnectRuntimeGroup != "" {
konnectControlPlane = konnectRuntimeGroup
if err := evaluateTargetRuntimeGroupOrControlPlaneName(targetContent); err != nil {
return err
}
}
if konnectRuntimeGroup != "" {
konnectControlPlane = konnectRuntimeGroup
}
kongClient, err = GetKongClientForKonnectMode(ctx, &konnectConfig)
if err != nil {
return err
Expand Down
31 changes: 22 additions & 9 deletions cmd/common_konnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func GetKongClientForKonnectMode(
if err != nil {
return nil, fmt.Errorf("authenticating with Konnect: %w", err)
}
cpID, err := fetchKonnectControlPlaneID(ctx, konnectClient)
cpID, err := fetchKonnectControlPlaneID(ctx, konnectClient, konnectConfig.ControlPlaneName)
if err != nil {
return nil, err
}
Expand All @@ -80,13 +80,17 @@ func GetKongClientForKonnectMode(
}

func resetKonnectV2(ctx context.Context) error {
if konnectRuntimeGroup != "" {
konnectControlPlane = konnectRuntimeGroup
}
if konnectControlPlane == "" {
konnectControlPlane = defaultControlPlaneName
}
dumpConfig.KonnectControlPlane = konnectControlPlane
client, err := GetKongClientForKonnectMode(ctx, &konnectConfig)
if err != nil {
return err
}
if dumpConfig.KonnectControlPlane == "" {
dumpConfig.KonnectControlPlane = defaultControlPlaneName
}
currentState, err := fetchCurrentState(ctx, client, dumpConfig)
if err != nil {
return err
Expand All @@ -103,13 +107,17 @@ func resetKonnectV2(ctx context.Context) error {
}

func dumpKonnectV2(ctx context.Context) error {
if konnectRuntimeGroup != "" {
konnectControlPlane = konnectRuntimeGroup
}
if konnectControlPlane == "" {
konnectControlPlane = defaultControlPlaneName
}
dumpConfig.KonnectControlPlane = konnectControlPlane
client, err := GetKongClientForKonnectMode(ctx, &konnectConfig)
if err != nil {
return err
}
if dumpConfig.KonnectControlPlane == "" {
dumpConfig.KonnectControlPlane = defaultControlPlaneName
}
kongVersion, err := fetchKonnectKongVersion(ctx, client)
if err != nil {
return fmt.Errorf("reading Konnect Kong version: %w", err)
Expand Down Expand Up @@ -238,8 +246,10 @@ func fetchKongControlPlaneID(ctx context.Context,
return singleOutKongCP(controlPlanes)
}

func fetchKonnectControlPlaneID(ctx context.Context,
func fetchKonnectControlPlaneID(
ctx context.Context,
client *konnect.Client,
konnectControlPlaneName string,
) (string, error) {
var runtimeGroups []*konnect.RuntimeGroup
var listOpt *konnect.ListOpt
Expand All @@ -254,6 +264,9 @@ func fetchKonnectControlPlaneID(ctx context.Context,
}
listOpt = next
}
if konnectControlPlaneName != "" {
konnectControlPlane = konnectControlPlaneName
}
if konnectControlPlane == "" {
konnectControlPlane = defaultControlPlaneName
}
Expand All @@ -262,7 +275,7 @@ func fetchKonnectControlPlaneID(ctx context.Context,
return *rg.ID, nil
}
}
return "", fmt.Errorf("control planes not found: %s", konnectControlPlane)
return "", fmt.Errorf("control plane not found: %s", konnectControlPlane)
}

func singleOutKongCP(controlPlanes []konnect.ControlPlane) (string, error) {
Expand Down
47 changes: 47 additions & 0 deletions tests/integration/dump_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,3 +204,50 @@ func Test_Dump_SkipConsumers_Konnect(t *testing.T) {
})
}
}

func Test_Dump_KonnectRename(t *testing.T) {
tests := []struct {
name string
stateFile string
expectedFile string
flags []string
}{
{
name: "dump with konnect-control-plane-name",
stateFile: "testdata/sync/026-konnect-rename/konnect_test_cp.yaml",
expectedFile: "testdata/sync/026-konnect-rename/konnect_test_cp.yaml",
flags: []string{"--konnect-control-plane-name", "test"},
},
{
name: "dump with konnect-runtime-group-name",
stateFile: "testdata/sync/026-konnect-rename/konnect_test_rg.yaml",
expectedFile: "testdata/sync/026-konnect-rename/konnect_test_cp.yaml",
flags: []string{"--konnect-runtime-group-name", "test"},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Cleanup(func() {
reset(t, tc.flags...)
})
runWhenKonnect(t)
setup(t)

assert.NoError(t, sync(tc.stateFile))

var (
output string
err error
)
flags := []string{"-o", "-", "--with-id"}
flags = append(flags, tc.flags...)
output, err = dump(flags...)

assert.NoError(t, err)

expected, err := readFile(tc.expectedFile)
assert.NoError(t, err)
assert.Equal(t, expected, output)
})
}
}
Loading

0 comments on commit 84e9bba

Please sign in to comment.