Skip to content

Commit

Permalink
feat: add konnect-control-plane-name flag and deprecate konnect-runti…
Browse files Browse the repository at this point in the history
…me-group-name (#1000)
  • Loading branch information
GGabriele authored Sep 25, 2023
1 parent c2a928f commit 199b398
Show file tree
Hide file tree
Showing 15 changed files with 255 additions and 88 deletions.
23 changes: 19 additions & 4 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,35 @@ 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 != "" &&
targetContent.Konnect.RuntimeGroupName != konnectRuntimeGroup {
targetRuntimeGroup != konnectRuntimeGroup {
return fmt.Errorf("warning: runtime group '%v' specified via "+
"--konnect-runtime-group flag is "+
"--konnect-runtime-group-name flag is "+
"different from '%v' found in state file(s)",
konnectRuntimeGroup, targetContent.Konnect.RuntimeGroupName)
}
konnectRuntimeGroup = targetContent.Konnect.RuntimeGroupName
if konnectRuntimeGroup != "" {
konnectControlPlane = konnectRuntimeGroup
}
}
kongClient, err = GetKongClientForKonnectMode(ctx, &konnectConfig)
if err != nil {
return err
}
dumpConfig.KonnectRuntimeGroup = konnectRuntimeGroup
dumpConfig.KonnectControlPlane = konnectControlPlane
}

rootClient, err := utils.GetKongClient(rootConfig)
Expand Down
38 changes: 22 additions & 16 deletions cmd/common_konnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (
"golang.org/x/sync/errgroup"
)

const defaultRuntimeGroupName = "default"
const defaultControlPlaneName = "default"

func authenticate(
ctx context.Context, client *konnect.Client, konnectConfig utils.KonnectConfig,
Expand All @@ -41,6 +41,10 @@ func GetKongClientForKonnectMode(
)
}

if konnectConfig.Address == "" {
konnectConfig.Address = defaultKonnectURL
}

// authenticate with konnect
var err error
var konnectClient *konnect.Client
Expand All @@ -54,14 +58,16 @@ func GetKongClientForKonnectMode(
if err != nil {
return nil, fmt.Errorf("authenticating with Konnect: %w", err)
}
kongRGID, err := fetchKongRuntimeGroupID(ctx, konnectClient)
cpID, err := fetchKonnectControlPlaneID(ctx, konnectClient)
if err != nil {
return nil, err
}

// set the kong runtime group ID in the client
konnectClient.SetRuntimeGroupID(kongRGID)
konnectAddress = konnectConfig.Address + "/konnect-api/api/runtime_groups/" + kongRGID
// set the kong control plane ID in the client
konnectClient.SetRuntimeGroupID(cpID)
konnectAddress = konnectConfig.Address + "/konnect-api/api/runtime_groups/" + cpID
// TODO: replace the above with the following once the Konnect API is updated
// konnectAddress = konnectConfig.Address + "/v2/control-planes/" + cpID

// initialize kong client
return utils.GetKongClient(utils.KongClientConfig{
Expand All @@ -78,8 +84,8 @@ func resetKonnectV2(ctx context.Context) error {
if err != nil {
return err
}
if dumpConfig.KonnectRuntimeGroup == "" {
dumpConfig.KonnectRuntimeGroup = defaultRuntimeGroupName
if dumpConfig.KonnectControlPlane == "" {
dumpConfig.KonnectControlPlane = defaultControlPlaneName
}
currentState, err := fetchCurrentState(ctx, client, dumpConfig)
if err != nil {
Expand All @@ -101,8 +107,8 @@ func dumpKonnectV2(ctx context.Context) error {
if err != nil {
return err
}
if dumpConfig.KonnectRuntimeGroup == "" {
dumpConfig.KonnectRuntimeGroup = defaultRuntimeGroupName
if dumpConfig.KonnectControlPlane == "" {
dumpConfig.KonnectControlPlane = defaultControlPlaneName
}
kongVersion, err := fetchKonnectKongVersion(ctx, client)
if err != nil {
Expand All @@ -121,7 +127,7 @@ func dumpKonnectV2(ctx context.Context) error {
Filename: dumpCmdKongStateFile,
FileFormat: file.Format(strings.ToUpper(dumpCmdStateFormat)),
WithID: dumpWithID,
RuntimeGroupName: konnectRuntimeGroup,
ControlPlaneName: konnectControlPlane,
KongVersion: kongVersion,
})
}
Expand Down Expand Up @@ -232,31 +238,31 @@ func fetchKongControlPlaneID(ctx context.Context,
return singleOutKongCP(controlPlanes)
}

func fetchKongRuntimeGroupID(ctx context.Context,
func fetchKonnectControlPlaneID(ctx context.Context,
client *konnect.Client,
) (string, error) {
var runtimeGroups []*konnect.RuntimeGroup
var listOpt *konnect.ListOpt
for {
currentRuntimeGroups, next, err := client.RuntimeGroups.List(ctx, listOpt)
if err != nil {
return "", fmt.Errorf("fetching runtime groups: %w", err)
return "", fmt.Errorf("fetching control planes: %w", err)
}
runtimeGroups = append(runtimeGroups, currentRuntimeGroups...)
if next == nil {
break
}
listOpt = next
}
if konnectRuntimeGroup == "" {
konnectRuntimeGroup = defaultRuntimeGroupName
if konnectControlPlane == "" {
konnectControlPlane = defaultControlPlaneName
}
for _, rg := range runtimeGroups {
if *rg.Name == konnectRuntimeGroup {
if *rg.Name == konnectControlPlane {
return *rg.ID, nil
}
}
return "", fmt.Errorf("runtime groups not found: %s", konnectRuntimeGroup)
return "", fmt.Errorf("control planes not found: %s", konnectControlPlane)
}

func singleOutKongCP(controlPlanes []konnect.ControlPlane) (string, error) {
Expand Down
11 changes: 11 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ var (
disableAnalytics bool

konnectRuntimeGroup string
konnectControlPlane string
)

// NewRootCmd represents the base command when called without any subcommands
Expand Down Expand Up @@ -201,9 +202,18 @@ It can be used to export, import, or sync entities to Kong.`,

rootCmd.PersistentFlags().String("konnect-runtime-group-name", "",
"Konnect Runtime group name.")
rootCmd.PersistentFlags().MarkDeprecated(
"konnect-runtime-group-name", "use --konnect-control-plane-name instead")
viper.BindPFlag("konnect-runtime-group-name",
rootCmd.PersistentFlags().Lookup("konnect-runtime-group-name"))

rootCmd.PersistentFlags().String("konnect-control-plane-name", "",
"Konnect Control Plane name.")
viper.BindPFlag("konnect-control-plane-name",
rootCmd.PersistentFlags().Lookup("konnect-control-plane-name"))

rootCmd.MarkFlagsMutuallyExclusive("konnect-runtime-group-name", "konnect-control-plane-name")

rootCmd.AddCommand(newSyncCmd())
rootCmd.AddCommand(newVersionCmd())
rootCmd.AddCommand(newValidateCmd())
Expand Down Expand Up @@ -383,6 +393,7 @@ func initKonnectConfig() error {
konnectConfig.Debug = (viper.GetInt("verbose") >= 1)
konnectConfig.Address = viper.GetString("konnect-addr")
konnectConfig.Headers = extendHeaders(viper.GetStringSlice("headers"))
konnectControlPlane = viper.GetString("konnect-control-plane-name")
konnectRuntimeGroup = viper.GetString("konnect-runtime-group-name")
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type Config struct {
// tags.
SelectorTags []string

// KonnectRuntimeGroup
KonnectRuntimeGroup string
// KonnectControlPlane
KonnectControlPlane string

// IsConsumerGroupScopedPluginSupported
IsConsumerGroupScopedPluginSupported bool
Expand Down
3 changes: 3 additions & 0 deletions file/kong_json_schema.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion file/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func Get(ctx context.Context, fileContent *Content, opt RenderConfig, dumpConfig
builder.client = wsClient
builder.ctx = ctx
builder.skipCACerts = dumpConfig.SkipCACerts
builder.isKonnect = dumpConfig.KonnectRuntimeGroup != ""
builder.isKonnect = dumpConfig.KonnectControlPlane != ""

if len(dumpConfig.SelectorTags) > 0 {
builder.selectTags = dumpConfig.SelectorTags
Expand Down
1 change: 1 addition & 0 deletions file/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,6 +605,7 @@ type Info struct {
// +k8s:deepcopy-gen=true
type Konnect struct {
RuntimeGroupName string `json:"runtime_group_name,omitempty" yaml:"runtime_group_name,omitempty"`
ControlPlaneName string `json:"control_plane_name,omitempty" yaml:"control_plane_name,omitempty"`
}

// Kong represents Kong implementation of a Service in Konnect.
Expand Down
6 changes: 3 additions & 3 deletions file/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type WriteConfig struct {
Filename string
FileFormat Format
WithID bool
RuntimeGroupName string
ControlPlaneName string
KongVersion string
}

Expand Down Expand Up @@ -55,9 +55,9 @@ func KongStateToContent(kongState *state.KongState, config WriteConfig) (*Conten
return nil, fmt.Errorf("get format version: %w", err)
}
file.FormatVersion = formatVersion
if config.RuntimeGroupName != "" {
if config.ControlPlaneName != "" {
file.Konnect = &Konnect{
RuntimeGroupName: config.RuntimeGroupName,
ControlPlaneName: config.ControlPlaneName,
}
}

Expand Down
4 changes: 4 additions & 0 deletions konnect/runtime_group_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ func (s *RuntimeGroupService) List(ctx context.Context,
opt *ListOpt,
) ([]*RuntimeGroup, *ListOpt, error) {
data, next, err := s.client.list(ctx, "/konnect-api/api/runtime_groups", opt)
// TODO: replace the above with the following once the Konnect API is updated.
//
// Note: pagination logic will need to be fixed too.
// data, next, err := s.client.list(ctx, "/v2/control-planes", opt)
if err != nil {
return nil, nil, err
}
Expand Down
Loading

0 comments on commit 199b398

Please sign in to comment.