Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
}

dumpConfig.SkipHashForBasicAuth = determineSkipHashForBasicAuth(*targetContent, dumpConfig)
dumpConfig.DiagnosticPolicy = diagnosticPolicy

var kongClient *kong.Client
mode := getMode(targetContent)
Expand Down Expand Up @@ -446,8 +447,9 @@ func syncMain(ctx context.Context, filenames []string, dry bool, parallelism,
}
// read the target state
rawState, err := file.Get(ctx, targetContent, file.RenderConfig{
CurrentState: currentState,
KongVersion: parsedKongVersion,
CurrentState: currentState,
KongVersion: parsedKongVersion,
DiagnosticPolicy: diagnosticPolicy,
}, dumpConfig, kongClient)
if err != nil {
return err
Expand Down
6 changes: 4 additions & 2 deletions cmd/common_konnect.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,8 @@ func syncKonnect(ctx context.Context,
}

targetKongState, targetKonnectState, err := file.GetForKonnect(ctx, targetContent, file.RenderConfig{
CurrentState: currentState,
CurrentState: currentState,
DiagnosticPolicy: diagnosticPolicy,
}, kongClient)
if err != nil {
return err
Expand Down Expand Up @@ -437,7 +438,8 @@ func getKonnectState(ctx context.Context,
var err error
// get export of Kong resources
kongState, err = dump.Get(ctx, kongClient, dump.Config{
SkipConsumers: skipConsumers,
SkipConsumers: skipConsumers,
DiagnosticPolicy: diagnosticPolicy,
})
if err != nil {
return fmt.Errorf("reading configuration from Kong: %w", err)
Expand Down
11 changes: 9 additions & 2 deletions cmd/file_convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ func executeConvert(_ *cobra.Command, _ []string) error {
file.Format(strings.ToUpper(convertCmdStateFormat)),
sourceFormat,
destinationFormat,
envVarsMode)
envVarsMode,
diagnosticPolicy)
if err != nil {
return fmt.Errorf("converting file: %w", err)
}
Expand All @@ -80,7 +81,8 @@ func executeConvert(_ *cobra.Command, _ []string) error {
file.Format(strings.ToUpper(convertCmdStateFormat)),
sourceFormat,
destinationFormat,
envVarsMode)
envVarsMode,
diagnosticPolicy)
if err != nil {
return fmt.Errorf("converting '%s' file: %w", filename, err)
}
Expand Down Expand Up @@ -146,6 +148,10 @@ can be converted into a 'kong-gateway-3.x' configuration file.`,
return err
}

if err := preRunDiagnosticPolicyFlags(); err != nil {
return err
}

return nil
},
}
Expand Down Expand Up @@ -181,6 +187,7 @@ can be converted into a 'kong-gateway-3.x' configuration file.`,
"yaml", "output file format: json or yaml.")
convertCmd.Flags().BoolVar(&convertCmdNoExpandEnvVars, "no-expand-env-vars",
false, `do not expand ${{ env "DECK_VAR_NAME" }} placeholders in the output.`)
addDiagnosticSeverityFlags(convertCmd.Flags())

return convertCmd
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/file_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ func executeFileRenderCmd(_ *cobra.Command, _ []string) error {
file.Format(strings.ToUpper(fileRenderCmdStateFormat)),
convert.FormatDistributed,
convert.FormatKongGateway3x,
envVarsMode)
envVarsMode,
diagnosticPolicy)
}

func newFileRenderCmd() *cobra.Command {
Expand Down Expand Up @@ -58,6 +59,9 @@ combined JSON file:
if len(fileRenderCmdKongStateFile) == 0 {
fileRenderCmdKongStateFile = []string{"-"} // default to stdin
}
if err := preRunDiagnosticPolicyFlags(); err != nil {
return err
}
return preRunSilenceEventsFlag()
},
}
Expand All @@ -70,6 +74,7 @@ combined JSON file:
renderCmd.Flags().BoolVar(&fileRenderCmdPopulateEnvVars, "populate-env-vars", false,
"Populate 'DECK_' environment variables in the output file. The default behavior\n"+
"is to mock environment variable values.")
addDiagnosticSeverityFlags(renderCmd.Flags())

return renderCmd
}
4 changes: 4 additions & 0 deletions cmd/gateway_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ func newApplyCmd() *cobra.Command {
if len(applyCmdKongStateFile) == 0 {
applyCmdKongStateFile = []string{"-"}
}
if err := preRunDiagnosticPolicyFlags(); err != nil {
return err
}
return preRunSilenceEventsFlag()
},
}
Expand All @@ -52,6 +55,7 @@ func newApplyCmd() *cobra.Command {
"This flag is only valid with Konnect.")
applyCmd.Flags().BoolVar(&syncJSONOutput, "json-output",
false, "generate command execution report in a JSON format")
addDiagnosticSeverityFlags(applyCmd.Flags())
addSilenceEventsFlag(applyCmd.Flags())

return applyCmd
Expand Down
7 changes: 7 additions & 0 deletions cmd/gateway_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ func newDiffCmd(deprecated bool) *cobra.Command {
if len(diffCmdKongStateFile) == 0 {
diffCmdKongStateFile = []string{"-"}
}
if err := preRunDiagnosticPolicyFlags(); err != nil {
return err
}
return preRunSilenceEventsFlag()
}

Expand All @@ -52,6 +55,9 @@ func newDiffCmd(deprecated bool) *cobra.Command {
return fmt.Errorf("a state file with Kong's configuration " +
"must be specified using `-s`/`--state` flag")
}
if err := preRunDiagnosticPolicyFlags(); err != nil {
return err
}
return preRunSilenceEventsFlag()
}
}
Expand Down Expand Up @@ -113,6 +119,7 @@ that will be created, updated, or deleted.
"This flag is not valid with Konnect.")
diffCmd.Flags().BoolVar(&syncCmdAssumeYes, "yes",
false, "assume `yes` to prompts and run non-interactively.")
addDiagnosticSeverityFlags(diffCmd.Flags())
addSilenceEventsFlag(diffCmd.Flags())
return diffCmd
}
7 changes: 7 additions & 0 deletions cmd/gateway_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ func newSyncCmd(deprecated bool) *cobra.Command {
if len(syncCmdKongStateFile) == 0 {
syncCmdKongStateFile = []string{"-"}
}
if err := preRunDiagnosticPolicyFlags(); err != nil {
return err
}
return preRunSilenceEventsFlag()
}

Expand All @@ -52,6 +55,9 @@ func newSyncCmd(deprecated bool) *cobra.Command {
return fmt.Errorf("a state file with Kong's configuration " +
"must be specified using `-s`/`--state` flag")
}
if err := preRunDiagnosticPolicyFlags(); err != nil {
return err
}
return preRunSilenceEventsFlag()
}
}
Expand Down Expand Up @@ -113,6 +119,7 @@ to get Kong's state in sync with the input state.`,
false, "assume `yes` to prompts and run non-interactively.")
syncCmd.Flags().BoolVar(&syncJSONOutput, "json-output",
false, "generate command execution report in a JSON format")
addDiagnosticSeverityFlags(syncCmd.Flags())
addSilenceEventsFlag(syncCmd.Flags())
return syncCmd
}
12 changes: 10 additions & 2 deletions cmd/gateway_validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,9 @@ func executeValidate(cmd *cobra.Command, _ []string) error {
}

rawState, err := file.Get(ctx, targetContent, file.RenderConfig{
CurrentState: dummyEmptyState,
}, dump.Config{}, kongClient)
CurrentState: dummyEmptyState,
DiagnosticPolicy: diagnosticPolicy,
}, dump.Config{DiagnosticPolicy: diagnosticPolicy}, kongClient)
if err != nil {
return err
}
Expand Down Expand Up @@ -221,6 +222,9 @@ this command unless --online flag is used.
return fmt.Errorf("a state file with Kong's configuration " +
"must be specified using `-s`/`--state` flag")
}
if err := preRunDiagnosticPolicyFlags(); err != nil {
return err
}
return preRunSilenceEventsFlag()
}
} else {
Expand Down Expand Up @@ -250,6 +254,9 @@ this command unless --online flag is used.
)
}
}
if err := preRunDiagnosticPolicyFlags(); err != nil {
return err
}
return preRunSilenceEventsFlag()
}

Expand Down Expand Up @@ -297,6 +304,7 @@ this command unless --online flag is used.
10, "Maximum number of concurrent requests to Kong.")
validateCmd.Flags().BoolVar(&validateKonnectCompatibility, "konnect-compatibility",
false, "validate that the state file(s) are ready to be deployed to Konnect")
addDiagnosticSeverityFlags(validateCmd.Flags())

validateCmd.MarkFlagsMutuallyExclusive("konnect-compatibility", "workspace")
validateCmd.MarkFlagsMutuallyExclusive("konnect-compatibility", "rbac-resources-only")
Expand Down
4 changes: 4 additions & 0 deletions cmd/konnect_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ func newKonnectDiffCmd() *cobra.Command {
konnectDiffCmdParallelism)
},
PreRunE: func(_ *cobra.Command, _ []string) error {
if err := preRunDiagnosticPolicyFlags(); err != nil {
return err
}
return preRunSilenceEventsFlag()
},
}
Expand All @@ -45,6 +48,7 @@ func newKonnectDiffCmd() *cobra.Command {
false, "return exit code 2 if there is a diff present,\n"+
"exit code 0 if no diff is found,\n"+
"and exit code 1 if an error occurs.")
addDiagnosticSeverityFlags(konnectDiffCmd.Flags())
addSilenceEventsFlag(konnectDiffCmd.Flags())
return konnectDiffCmd
}
4 changes: 4 additions & 0 deletions cmd/konnect_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ to get Konnect's state in sync with the input state.` + konnectAlphaState,
konnectDiffCmdParallelism)
},
PreRunE: func(_ *cobra.Command, _ []string) error {
if err := preRunDiagnosticPolicyFlags(); err != nil {
return err
}
return preRunSilenceEventsFlag()
},
}
Expand All @@ -33,6 +36,7 @@ to get Konnect's state in sync with the input state.` + konnectAlphaState,
100, "Maximum number of concurrent operations.")
konnectSyncCmd.Flags().BoolVar(&noMaskValues, "no-mask-deck-env-vars-value",
false, "do not mask DECK_ environment variable values at diff output.")
addDiagnosticSeverityFlags(konnectSyncCmd.Flags())
addSilenceEventsFlag(konnectSyncCmd.Flags())
return konnectSyncCmd
}
51 changes: 51 additions & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"errors"
"fmt"
"slices"
"strings"

"github.com/fatih/color"
"github.com/kong/go-database-reconciler/pkg/cprint"
"github.com/kong/go-database-reconciler/pkg/diff"
reconcilerUtils "github.com/kong/go-database-reconciler/pkg/utils"
"github.com/spf13/pflag"
)

Expand All @@ -23,6 +25,12 @@ func printStats(stats diff.Stats) {

var silenceEvents bool

var (
diagnosticAlwaysError string
diagnosticAlwaysWarning string
diagnosticPolicy reconcilerUtils.DiagnosticPolicy
)

func preRunSilenceEventsFlag() error {
if silenceEvents {
cprint.DisableOutput = true
Expand All @@ -35,6 +43,49 @@ func addSilenceEventsFlag(set *pflag.FlagSet) {
"disable printing events to stdout")
}

func preRunDiagnosticPolicyFlags() error {
policy, err := buildDiagnosticPolicy(diagnosticAlwaysError, diagnosticAlwaysWarning)
if err != nil {
return err
}
diagnosticPolicy = policy
return nil
}

func addDiagnosticSeverityFlags(set *pflag.FlagSet) {
set.StringVarP(&diagnosticAlwaysError, "warnings-as-errors", "E", "",
"Treat the given comma-separated diagnostic codes as errors.")
set.StringVarP(&diagnosticAlwaysWarning, "errors-as-warnings", "W", "",
"Treat the given comma-separated diagnostic codes as warnings.")
}

func buildDiagnosticPolicy(alwaysErrorValue, alwaysWarningValue string) (reconcilerUtils.DiagnosticPolicy, error) {
alwaysErrorCodes, err := parseDiagnosticCodesFlag("-E/--warnings-as-errors", alwaysErrorValue)
if err != nil {
return reconcilerUtils.DiagnosticPolicy{}, err
}

alwaysWarningCodes, err := parseDiagnosticCodesFlag("-W/--warnings-as-warnings", alwaysWarningValue)
if err != nil {
return reconcilerUtils.DiagnosticPolicy{}, err
}

return reconcilerUtils.NewDiagnosticPolicy(alwaysErrorCodes, alwaysWarningCodes), nil
}

func parseDiagnosticCodesFlag(flagName, value string) ([]reconcilerUtils.DiagnosticCode, error) {
if strings.TrimSpace(value) == "" {
return nil, nil
}

codes, err := reconcilerUtils.ParseDiagnosticCodes(value)
if err != nil {
return nil, fmt.Errorf("invalid value for %s: %w. Valid diagnostic codes: %v",
flagName, err, reconcilerUtils.ValidDiagnosticCodes())
}
return codes, nil
}

func validateInputFlag(flagName string, flagValue string, allowedValues []string, errorMessage string) error {
if slices.Contains(allowedValues, flagValue) {
return nil
Expand Down
11 changes: 7 additions & 4 deletions convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func Convert(
from Format,
to Format,
envVarsMode file.RenderEnvVarsMode,
diagnosticPolicy utils.DiagnosticPolicy,
) error {
var outputContent *file.Content

Expand Down Expand Up @@ -113,7 +114,7 @@ func Convert(
case from == FormatDistributed && to == FormatKongGateway,
from == FormatDistributed && to == FormatKongGateway2x,
from == FormatDistributed && to == FormatKongGateway3x:
outputContent, err = convertDistributedToKong(inputContent, outputFilename, outputFormat, to)
outputContent, err = convertDistributedToKong(inputContent, outputFilename, outputFormat, to, diagnosticPolicy)
if err != nil {
return err
}
Expand Down Expand Up @@ -315,6 +316,7 @@ func convertDistributedToKong(
outputFilename string,
format file.Format,
kongFormat Format,
diagnosticPolicy utils.DiagnosticPolicy,
) (*file.Content, error) {
var version semver.Version

Expand All @@ -328,9 +330,10 @@ func convertDistributedToKong(

s, _ := state.NewKongState()
rawState, err := file.Get(context.Background(), targetContent, file.RenderConfig{
CurrentState: s,
KongVersion: version,
}, dump.Config{}, nil)
CurrentState: s,
KongVersion: version,
DiagnosticPolicy: diagnosticPolicy,
}, dump.Config{DiagnosticPolicy: diagnosticPolicy}, nil)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,7 +370,7 @@ func Test_Convert(t *testing.T) {
envVarsMode = file.EnvVarsExpand
}
err := Convert(inputFiles, tt.args.outputFilename, file.YAML, tt.args.fromFormat,
tt.args.toFormat, envVarsMode)
tt.args.toFormat, envVarsMode, utils.DiagnosticPolicy{})
if (err != nil) != tt.wantErr {
t.Errorf("Convert() error = %v, wantErr %v", err, tt.wantErr)
}
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ require (
github.com/fatih/color v1.19.0
github.com/google/go-cmp v0.7.0
github.com/kong/go-apiops v0.4.1
github.com/kong/go-database-reconciler v1.37.0
github.com/kong/go-database-reconciler v1.38.0
github.com/kong/go-kong v0.75.1
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.10.2
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,8 @@ github.com/kong/go-apiops v0.4.1 h1:jVE7O2wSwrmV/meppKRxst9ny+aASJlwj7zma+9pbzc=
github.com/kong/go-apiops v0.4.1/go.mod h1:Xt99d90LallLVwYJAGaufiNbBdsK0KKboe7gR4Ryths=
github.com/kong/go-database-reconciler v1.37.0 h1:I2eYzthvdMCBrL6Ky4UTgeNQoPDTSf4xLZxZdylzngQ=
github.com/kong/go-database-reconciler v1.37.0/go.mod h1:4YhUoBTMO5smJ0BuWaSfxknbhjrKx9Cg4hvUMR2EW8M=
github.com/kong/go-database-reconciler v1.38.0 h1:KecRJLDRMrH/r/BTORQ17x4coZBOv8vKXTP/K9rlVzg=
github.com/kong/go-database-reconciler v1.38.0/go.mod h1:4YhUoBTMO5smJ0BuWaSfxknbhjrKx9Cg4hvUMR2EW8M=
github.com/kong/go-kong v0.75.1 h1:2W4bZQUvXYGsZ7PvbZKkWnjrZmEiKueSY1yf6xv2jwY=
github.com/kong/go-kong v0.75.1/go.mod h1:Wx5aTcMjyUnIF94M5NYFWb/EnuEkqB5STrWvybFSYYQ=
github.com/kong/go-slugify v1.0.0 h1:vCFAyf2sdoSlBtLcrmDWUFn0ohlpKiKvQfXZkO5vSKY=
Expand Down
Loading
Loading