Skip to content

Commit

Permalink
Output based on KIC v3.x vs v2.x
Browse files Browse the repository at this point in the history
Remove status and timestamp

Linter and simplify

Remove unused builders

Simplify output generation

deps update
  • Loading branch information
battlebyte committed Feb 14, 2024
1 parent f419701 commit 9e8a5bb
Show file tree
Hide file tree
Showing 16 changed files with 4,184 additions and 1,996 deletions.
119 changes: 53 additions & 66 deletions cmd/file_kong2kic.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,10 @@ import (
var (
cmdKong2KicInputFilename string
cmdKong2KicOutputFilename string
cmdKong2KicAPI string
cmdKong2KicOutputFormat string
cmdKong2KicManifestStyle string
CmdKong2KicClassName string
CmdKong2KicV1beta1 bool
cmdKong2KicClassName string
cmdKong2KicIngress bool
cmdKong2KicKICv2 bool
)

// Executes the CLI command "kong2kic"
Expand All @@ -28,14 +27,10 @@ func executeKong2Kic(cmd *cobra.Command, _ []string) error {
outputContent *file.Content
err error
outputFileFormat file.Format
yamlOrJSON string
)

if CmdKong2KicV1beta1 {
kong2kic.GatewayAPIVersion = "gateway.networking.k8s.io/v1beta1"
} else {
kong2kic.GatewayAPIVersion = "gateway.networking.k8s.io/v1"
}
kong2kic.ClassName = CmdKong2KicClassName
kong2kic.ClassName = cmdKong2KicClassName
verbosity, _ := cmd.Flags().GetInt("verbose")
logbasics.Initialize(log.LstdFlags, verbosity)

Expand All @@ -44,13 +39,13 @@ func executeKong2Kic(cmd *cobra.Command, _ []string) error {
return fmt.Errorf("failed reading input file '%s'; %w", cmdKong2KicInputFilename, err)
}

outputFileFormat, err = validateInput(cmdKong2KicOutputFormat, cmdKong2KicAPI, cmdKong2KicManifestStyle)
outputFileFormat, yamlOrJSON, err = validateInput(cmdKong2KicOutputFormat)
if err != nil {
return err
}

outputContent = inputContent.DeepCopy()
err = kong2kic.WriteContentToFile(outputContent, cmdKong2KicOutputFilename, outputFileFormat)
err = kong2kic.WriteContentToFile(outputContent, cmdKong2KicOutputFilename, outputFileFormat, yamlOrJSON)

Check failure on line 49 in cmd/file_kong2kic.go

View workflow job for this annotation

GitHub Actions / test

File is not `gofumpt`-ed (gofumpt)
if err != nil {
return fmt.Errorf("failed converting Kong to Ingress '%s'; %w", cmdKong2KicInputFilename, err)
Expand All @@ -59,51 +54,48 @@ func executeKong2Kic(cmd *cobra.Command, _ []string) error {
return nil
}

func validateInput(cmdKong2KicOutputFormat, cmdKong2KicAPI, cmdKong2KicManifestStyle string) (
outputFileFormat file.Format, err error,
func validateInput(cmdKong2KicOutputFormat string) (
outputFileFormat file.Format,
yamlOrJSON string,
err error,
) {
const (
JSON = "JSON"
YAML = "YAML"
INGRESS = "INGRESS"
GATEWAY = "GATEWAY"
CRD = "CRD"
ANNOTATION = "ANNOTATION"
)

if strings.ToUpper(cmdKong2KicOutputFormat) == JSON &&
strings.ToUpper(cmdKong2KicAPI) == INGRESS &&
strings.ToUpper(cmdKong2KicManifestStyle) == CRD {
outputFileFormat = kong2kic.KICJSONCrdIngressAPI
} else if strings.ToUpper(cmdKong2KicOutputFormat) == JSON &&
strings.ToUpper(cmdKong2KicAPI) == INGRESS &&
strings.ToUpper(cmdKong2KicManifestStyle) == ANNOTATION {
outputFileFormat = kong2kic.KICJSONAnnotationIngressAPI
} else if strings.ToUpper(cmdKong2KicOutputFormat) == YAML &&
strings.ToUpper(cmdKong2KicAPI) == INGRESS &&
strings.ToUpper(cmdKong2KicManifestStyle) == CRD {
outputFileFormat = kong2kic.KICYAMLCrdIngressAPI
} else if strings.ToUpper(cmdKong2KicOutputFormat) == YAML &&
strings.ToUpper(cmdKong2KicAPI) == INGRESS &&
strings.ToUpper(cmdKong2KicManifestStyle) == ANNOTATION {
outputFileFormat = kong2kic.KICYAMLAnnotationIngressAPI
} else if strings.ToUpper(cmdKong2KicOutputFormat) == JSON &&
strings.ToUpper(cmdKong2KicAPI) == GATEWAY {
outputFileFormat = kong2kic.KICJSONGatewayAPI
} else if strings.ToUpper(cmdKong2KicOutputFormat) == YAML &&
strings.ToUpper(cmdKong2KicAPI) == GATEWAY {
outputFileFormat = kong2kic.KICYAMLGatewayAPI
if strings.ToUpper(cmdKong2KicOutputFormat) == file.YAML && !cmdKong2KicKICv2 && !cmdKong2KicIngress {
// default to KICv 3.x and Gateway API YAML
outputFileFormat = kong2kic.KICV3GATEWAY
yamlOrJSON = file.YAML
} else if strings.ToUpper(cmdKong2KicOutputFormat) == file.JSON && !cmdKong2KicKICv2 && !cmdKong2KicIngress {
// KICv 3.x and Gateway API JSON
outputFileFormat = kong2kic.KICV3GATEWAY
yamlOrJSON = file.JSON
} else if strings.ToUpper(cmdKong2KicOutputFormat) == file.YAML && !cmdKong2KicKICv2 && cmdKong2KicIngress {
// KICv 3.x and Ingress API YAML
outputFileFormat = kong2kic.KICV3INGRESS
yamlOrJSON = file.YAML
} else if strings.ToUpper(cmdKong2KicOutputFormat) == file.JSON && !cmdKong2KicKICv2 && cmdKong2KicIngress {
// KICv 3.x and Ingress API JSON
outputFileFormat = kong2kic.KICV3INGRESS
yamlOrJSON = file.JSON
} else if strings.ToUpper(cmdKong2KicOutputFormat) == file.YAML && cmdKong2KicKICv2 && !cmdKong2KicIngress {
// KICv 2.x and Gateway API YAML
outputFileFormat = kong2kic.KICV2GATEWAY
yamlOrJSON = file.YAML
} else if strings.ToUpper(cmdKong2KicOutputFormat) == file.JSON && cmdKong2KicKICv2 && !cmdKong2KicIngress {
// KICv 2.x and Gateway API JSON
outputFileFormat = kong2kic.KICV2GATEWAY
yamlOrJSON = file.JSON
} else if strings.ToUpper(cmdKong2KicOutputFormat) == file.YAML && cmdKong2KicKICv2 && cmdKong2KicIngress {
// KICv 2.x and Ingress API YAML
outputFileFormat = kong2kic.KICV2INGRESS
yamlOrJSON = file.YAML
} else if strings.ToUpper(cmdKong2KicOutputFormat) == file.JSON && cmdKong2KicKICv2 && cmdKong2KicIngress {
// KICv 2.x and Ingress API JSON
outputFileFormat = kong2kic.KICV2INGRESS
yamlOrJSON = file.JSON
} else {
err = fmt.Errorf("invalid combination of output format and manifest style. Valid combinations are:\n" +
"Ingress API with annotation style in YAML format: --format yaml --api ingress --style annotation\n" +
"Ingress API with annotation style in JSON format: --format json --api ingress --style annotation\n" +
"Ingress API with CRD style in YAML format: --format yaml --api ingress --style crd\n" +
"Ingress API with CRD style in JSON format: --format json --api ingress --style crd\n" +
"Gateway API in YAML format: --format yaml --api gateway\n" +
"Gateway API in JSON format: --format json --api gateway")
err = fmt.Errorf("invalid combination parameters. Please use --help for more information")
}

return outputFileFormat, err
return outputFileFormat, yamlOrJSON, err
}

//
Expand All @@ -118,9 +110,9 @@ func newKong2KicCmd() *cobra.Command {
Short: "Convert Kong configuration files to Kong Ingress Controller (KIC) manifests",
Long: `Convert Kong configuration files to Kong Ingress Controller (KIC) manifests.
Manifests can be generated using the Ingress API or the Gateway API. Ingress API manifests
can be generated using annotations in Ingress and Service objects (recommended) or
using KongIngress objects. Output in YAML or JSON format. Only HTTP/HTTPS routes are supported.`,
The kong2kic subcommand transforms Kong’s configuration files, written in the deck format,
into Kubernetes manifests suitable for the Kong Ingress Controller. By default kong2kic generates
manifests for KIC v3.x using the Kubernetes Gateway API. Only HTTP/HTTPS routes are supported.`,
RunE: executeKong2Kic,
Args: cobra.NoArgs,
}
Expand All @@ -131,18 +123,13 @@ using KongIngress objects. Output in YAML or JSON format. Only HTTP/HTTPS routes
"Output file to write. Use - to write to stdout.")
kong2KicCmd.Flags().StringVarP(&cmdKong2KicOutputFormat, "format", "f", "yaml",
"output file format: json or yaml.")
kong2KicCmd.Flags().StringVar(&CmdKong2KicClassName, "class-name", "kong",
kong2KicCmd.Flags().StringVar(&cmdKong2KicClassName, "class-name", "kong",
`Value to use for "kubernetes.io/ingress.class" ObjectMeta.Annotations and for
"parentRefs.name" in the case of HTTPRoute.`)
kong2KicCmd.Flags().StringVarP(&cmdKong2KicAPI, "api", "a", "ingress",
`Use Ingress API manifests or Gateway API manifests: ingress or gateway`)
kong2KicCmd.Flags().BoolVar(&CmdKong2KicV1beta1, "v1beta1", false,
`Only for Gateway API, setting this flag will use "apiVersion: gateway.networking.k8s.io/v1beta1"
in Gateway API manifests. Otherwise, "apiVersion: gateway.konghq.com/v1" is used.
KIC versions earlier than 3.0 only support v1beta1.`)
kong2KicCmd.Flags().StringVar(&cmdKong2KicManifestStyle, "style", "annotation",
`Only for Ingress API, generate manifests with annotations in Service objects
and Ingress objects, or use only KongIngress objects without annotations: annotation or crd.`)
kong2KicCmd.Flags().BoolVar(&cmdKong2KicIngress, "ingress", false,
`Use Kubernetes Ingress API manifests instead of Gateway API manifests.`)
kong2KicCmd.Flags().BoolVar(&cmdKong2KicKICv2, "kicv2", false,
`Generate manifests compatible with KIC v2.x.`)

return kong2KicCmd
}
24 changes: 17 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,9 +312,11 @@ func initConfig() {
}

rootConfig.Address = viper.GetString("kong-addr")
rootConfig.TLSServerName = viper.GetString("tls-server-name")
rootConfig.TLSSkipVerify = viper.GetBool("tls-skip-verify")
rootConfig.TLSCACert = caCertContent

tlsServerName := viper.GetString("tls-server-name")
tlsSkipVerify := viper.GetBool("tls-skip-verify")
tlsCACert := caCertContent

rootConfig.Headers = extendHeaders(viper.GetStringSlice("headers"))
rootConfig.SkipWorkspaceCrud = viper.GetBool("skip-workspace-crud")
rootConfig.Debug = (viper.GetInt("verbose") >= 1)
Expand All @@ -334,7 +336,7 @@ func initConfig() {
clientCertContent = strings.TrimRight(clientCertContent, "\n")
}
}
rootConfig.TLSClientCert = clientCertContent
tlsClientCert := clientCertContent

clientKeyContent := viper.GetString("tls-client-key")

Expand All @@ -350,15 +352,23 @@ func initConfig() {
clientKeyContent = strings.TrimRight(clientKeyContent, "\n")
}
}
rootConfig.TLSClientKey = clientKeyContent
tlsClientKey := clientKeyContent

if (rootConfig.TLSClientKey == "" && rootConfig.TLSClientCert != "") ||
(rootConfig.TLSClientKey != "" && rootConfig.TLSClientCert == "") {
if (tlsClientKey == "" && tlsClientCert != "") ||
(tlsClientKey != "" && tlsClientCert == "") {
fmt.Printf("tls-client-cert and tls-client-key / tls-client-cert-file and tls-client-key-file " +
"must be used in conjunction but only one was provided")
os.Exit(1)
}

rootConfig.TLSConfig = utils.TLSConfig{
ServerName: tlsServerName,
SkipVerify: tlsSkipVerify,
CACert: tlsCACert,
ClientCert: tlsClientCert,
ClientKey: tlsClientKey,
}

// cookie-jar support
rootConfig.CookieJarPath = viper.GetString("kong-cookie-jar-path")

Expand Down
58 changes: 31 additions & 27 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,43 +1,46 @@
module github.com/kong/deck

go 1.21.1
go 1.21.6

replace github.com/yudai/gojsondiff v1.0.0 => github.com/Kong/gojsondiff v1.3.0

require (
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
github.com/blang/semver/v4 v4.0.0
github.com/daveshanley/vacuum v0.5.0
github.com/daveshanley/vacuum v0.8.7
github.com/fatih/color v1.15.0
github.com/google/go-cmp v0.6.0
github.com/kong/go-apiops v0.1.27
github.com/kong/go-database-reconciler v1.3.0
github.com/kong/go-kong v0.50.0
github.com/kong/kubernetes-ingress-controller/v2 v2.12.3
github.com/kong/go-apiops v0.1.30
github.com/kong/go-database-reconciler v1.7.0
github.com/kong/go-kong v0.51.1-0.20240125175037-0c077f5b9ac7
github.com/mitchellh/go-homedir v1.1.0
github.com/spf13/cobra v1.8.0
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.18.2
github.com/stretchr/testify v1.8.4
golang.org/x/sync v0.6.0
k8s.io/api v0.28.3
k8s.io/apiextensions-apiserver v0.28.3
k8s.io/apimachinery v0.28.3
k8s.io/api v0.29.1
k8s.io/apiextensions-apiserver v0.29.1
k8s.io/apimachinery v0.29.1
k8s.io/code-generator v0.29.1
sigs.k8s.io/gateway-api v1.0.0
sigs.k8s.io/yaml v1.4.0
)

require (
github.com/bahlo/generic-list-go v0.2.0 // indirect
github.com/buger/jsonparser v1.1.1 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.5.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/go-memdb v1.3.4 // indirect
github.com/hashicorp/go-retryablehttp v0.7.5 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
github.com/pb33f/doctor v0.0.4 // indirect
github.com/shirou/gopsutil/v3 v3.24.1 // indirect
github.com/ssgelm/cookiejarparser v1.0.1 // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Expand All @@ -54,7 +57,7 @@ require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/dop251/goja v0.0.0-20231027120936-b396bb4c349d // indirect
github.com/dop251/goja_nodejs v0.0.0-20231022114343-5c1f9037c9ab // indirect
github.com/dop251/goja_nodejs v0.0.0-20231122114759-e84d9a924c5c // indirect
github.com/dprotaso/go-yit v0.0.0-20220510233725-9ba8df137936 // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
Expand All @@ -63,9 +66,9 @@ require (
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/jsonreference v0.20.4 // indirect
github.com/go-openapi/swag v0.22.7 // indirect
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.3 // indirect
Expand All @@ -84,6 +87,7 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kong/go-slugify v1.0.0 // indirect
github.com/kong/kubernetes-ingress-controller/v3 v3.1.0
github.com/kong/semver/v4 v4.0.1 // indirect
github.com/lithammer/fuzzysearch v1.1.8 // indirect
github.com/lufia/plan9stats v0.0.0-20230326075908-cb1d2100619a // indirect
Expand All @@ -97,12 +101,12 @@ require (
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/mozillazg/go-unidecode v0.2.0 // indirect
github.com/pb33f/libopenapi v0.13.11 // indirect
github.com/pb33f/libopenapi-validator v0.0.28 // indirect
github.com/pb33f/libopenapi v0.15.5 // indirect
github.com/pb33f/libopenapi-validator v0.0.42 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/power-devops/perfstat v0.0.0-20221212215047-62379fc7944b // indirect
github.com/pterm/pterm v0.12.70 // indirect
github.com/pterm/pterm v0.12.77 // indirect
github.com/rivo/uniseg v0.4.4 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
Expand All @@ -124,22 +128,22 @@ require (
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa // indirect
golang.org/x/exp v0.0.0-20240119083558-1b970713d09a // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.16.0 // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/term v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.16.1 // indirect
google.golang.org/protobuf v1.31.0 // indirect
golang.org/x/tools v0.17.0 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/gengo v0.0.0-20230829151522-9cce18d56c01 // indirect
k8s.io/klog/v2 v2.110.1 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/controller-runtime v0.16.3 // indirect
k8s.io/kube-openapi v0.0.0-20240103051144-eec4567ac022 // indirect
k8s.io/utils v0.0.0-20240102154912-e7106e64919e // indirect
sigs.k8s.io/controller-runtime v0.17.0 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 // indirect
)
Loading

0 comments on commit 9e8a5bb

Please sign in to comment.