Skip to content

Commit

Permalink
feat(apply): add deck gateway apply command
Browse files Browse the repository at this point in the history
The `deck gateway apply` command allows you to apply partial
configuration to a running Gateway instance.

To do this, it runs a `sync` with the NoDeletes flag enabled.
This means that only new and existing resources are updated.
Existing resources that do not exist in the declarative
configuration file are left untouched.
  • Loading branch information
mheap committed Dec 2, 2024
1 parent 5f161d8 commit fa24c39
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 7 deletions.
7 changes: 4 additions & 3 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func RemoveConsumerPlugins(targetContentPlugins []file.FPlugin) []file.FPlugin {
}

func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
delay int, workspace string, enableJSONOutput bool,
delay int, workspace string, enableJSONOutput bool, noDeletes bool,
) error {
// read target file
if enableJSONOutput {
Expand Down Expand Up @@ -373,7 +373,7 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
}

totalOps, err := performDiff(
ctx, currentState, targetState, dry, parallelism, delay, kongClient, mode == modeKonnect, enableJSONOutput)
ctx, currentState, targetState, dry, parallelism, delay, kongClient, mode == modeKonnect, enableJSONOutput, noDeletes)
if err != nil {
if enableJSONOutput {
var errs reconcilerUtils.ErrArray
Expand Down Expand Up @@ -502,7 +502,7 @@ func fetchCurrentState(ctx context.Context, client *kong.Client, dumpConfig dump

func performDiff(ctx context.Context, currentState, targetState *state.KongState,
dry bool, parallelism int, delay int, client *kong.Client, isKonnect bool,
enableJSONOutput bool,
enableJSONOutput bool, noDeletes bool,
) (int, error) {
s, err := diff.NewSyncer(diff.SyncerOpts{
CurrentState: currentState,
Expand All @@ -511,6 +511,7 @@ func performDiff(ctx context.Context, currentState, targetState *state.KongState
StageDelaySec: delay,
NoMaskValues: noMaskValues,
IsKonnect: isKonnect,
NoDeletes: noDeletes,
})
if err != nil {
return 0, err
Expand Down
2 changes: 1 addition & 1 deletion cmd/common_konnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func resetKonnectV2(ctx context.Context) error {
if err != nil {
return err
}
_, err = performDiff(ctx, currentState, targetState, false, 10, 0, client, true, resetJSONOutput)
_, err = performDiff(ctx, currentState, targetState, false, 10, 0, client, true, resetJSONOutput, false)
if err != nil {
return err
}
Expand Down
55 changes: 55 additions & 0 deletions cmd/gateway_apply.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cmd

import (
"github.com/spf13/cobra"
)

var (
applyCmdParallelism int
applyCmdDBUpdateDelay int
applyWorkspace string
applyJSONOutput bool
)

var applyCmdKongStateFile []string

func executeApply(cmd *cobra.Command, _ []string) error {
return syncMain(cmd.Context(), applyCmdKongStateFile, false,
applyCmdParallelism, applyCmdDBUpdateDelay, applyWorkspace, applyJSONOutput, true)
}

func newApplyCmd() *cobra.Command {
short := "Apply configuration to Kong without deleting existing entities"
execute := executeApply

applyCmd := &cobra.Command{
Use: "apply [flags] [kong-state-files...]",
Short: short,
Long: `The apply command allows you to apply partial Kong configuration files without deleting existing entities.`,
Args: cobra.MinimumNArgs(0),
RunE: execute,
PreRunE: func(_ *cobra.Command, args []string) error {
applyCmdKongStateFile = args
if len(applyCmdKongStateFile) == 0 {
applyCmdKongStateFile = []string{"-"}
}
return preRunSilenceEventsFlag()
},
}

applyCmd.Flags().StringVarP(&applyWorkspace, "workspace", "w", "",
"Apply configuration to a specific workspace "+
"(Kong Enterprise only).\n"+
"This takes precedence over _workspace fields in state files.")
applyCmd.Flags().IntVar(&applyCmdParallelism, "parallelism",
10, "Maximum number of concurrent operations.")
applyCmd.Flags().IntVar(&applyCmdDBUpdateDelay, "db-update-propagation-delay",
0, "artificial delay (in seconds) that is injected between insert operations \n"+
"for related entities (usually for Cassandra deployments).\n"+
"See `db_update_propagation` in kong.conf.")
applyCmd.Flags().BoolVar(&syncJSONOutput, "json-output",
false, "generate command execution report in a JSON format")
addSilenceEventsFlag(applyCmd.Flags())

return applyCmd
}
2 changes: 1 addition & 1 deletion cmd/gateway_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var (

func executeDiff(cmd *cobra.Command, _ []string) error {
return syncMain(cmd.Context(), diffCmdKongStateFile, true,
diffCmdParallelism, 0, diffWorkspace, diffJSONOutput)
diffCmdParallelism, 0, diffWorkspace, diffJSONOutput, false)
}

// newDiffCmd represents the diff command
Expand Down
2 changes: 1 addition & 1 deletion cmd/gateway_reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func executeReset(cmd *cobra.Command, _ []string) error {
if err != nil {
return err
}
_, err = performDiff(ctx, currentState, targetState, false, 10, 0, wsClient, false, resetJSONOutput)
_, err = performDiff(ctx, currentState, targetState, false, 10, 0, wsClient, false, resetJSONOutput, false)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/gateway_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var syncCmdKongStateFile []string

func executeSync(cmd *cobra.Command, _ []string) error {
return syncMain(cmd.Context(), syncCmdKongStateFile, false,
syncCmdParallelism, syncCmdDBUpdateDelay, syncWorkspace, syncJSONOutput)
syncCmdParallelism, syncCmdDBUpdateDelay, syncWorkspace, syncJSONOutput, false)
}

// newSyncCmd represents the sync command
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ It can be used to export, import, or sync entities to Kong.`,
gatewayCmd.AddCommand(newPingCmd(false))
gatewayCmd.AddCommand(newDumpCmd(false))
gatewayCmd.AddCommand(newDiffCmd(false))
gatewayCmd.AddCommand(newApplyCmd())
}
{
fileCmd := newFileSubCmd()
Expand Down

0 comments on commit fa24c39

Please sign in to comment.