Skip to content

Commit

Permalink
feat(kong2tf): PROTOTYPE kong2tf with import blocks and lifecycle-ign…
Browse files Browse the repository at this point in the history
…ore blocks, plus appropriate switches; patches "route ID is read only field" bug
  • Loading branch information
tysoekong committed Aug 20, 2024
1 parent 621202d commit 283b09a
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 65 deletions.
17 changes: 14 additions & 3 deletions cmd/file_kong2tf.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
)

var (
cmdKong2TfInputFilename string
cmdKong2TfOutputFilename string
cmdKong2TfInputFilename string
cmdKong2TfOutputFilename string
cmdKong2TfGenerateImportsForControlPlaneID string
cmdKong2TfIgnoreCredentialChanges bool
)

// Executes the CLI command "kong2Tf"
Expand All @@ -37,7 +39,12 @@ func executeKong2Tf(cmd *cobra.Command, _ []string) error {
logbasics.Info("Successfully read input file '%s'", cmdKong2TfInputFilename)

logbasics.Info("Converting Kong configuration to Terraform")
result, err = kong2tf.Convert(inputContent)

var generateImportsForControlPlaneID *string
if cmdKong2TfGenerateImportsForControlPlaneID != "" {
generateImportsForControlPlaneID = &cmdKong2TfGenerateImportsForControlPlaneID
}
result, err = kong2tf.Convert(inputContent, generateImportsForControlPlaneID, cmdKong2TfIgnoreCredentialChanges)
if err != nil {
log.Printf("Error converting Kong configuration to Terraform; %v", err)
return fmt.Errorf("failed converting Kong configuration to Terraform; %w", err)
Expand Down Expand Up @@ -78,6 +85,10 @@ into Terraform resources.`,
"decK file to process. Use - to read from stdin.")
kong2TfCmd.Flags().StringVarP(&cmdKong2TfOutputFilename, "output-file", "o", "-",
"Output file to write. Use - to write to stdout.")
kong2TfCmd.Flags().StringVarP(&cmdKong2TfGenerateImportsForControlPlaneID, "generate-imports-for-control-plane-id", "g", "",
"decK file to process. Use - to read from stdin.")
kong2TfCmd.Flags().BoolVar(&cmdKong2TfIgnoreCredentialChanges, "ignore-credential-changes", false,
"Enable flag to add a 'lifecycle' block to each consumer credential, that ignores any changes from local to remote state.")

return kong2TfCmd
}
39 changes: 20 additions & 19 deletions kong2tf/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ import (
)

type ITerraformBuilder interface {
buildServices(*file.Content)
buildRoutes(*file.Content)
buildGlobalPlugins(*file.Content)
buildConsumers(*file.Content)
buildConsumerGroups(*file.Content)
buildUpstreams(*file.Content)
buildCACertificates(*file.Content)
buildCertificates(*file.Content)
buildVaults(*file.Content)
buildServices(*file.Content, *string, bool)
buildRoutes(*file.Content, *string, bool)
buildGlobalPlugins(*file.Content, *string, bool)
buildConsumers(*file.Content, *string, bool)
buildConsumerGroups(*file.Content, *string, bool)
buildUpstreams(*file.Content, *string, bool)
buildCACertificates(*file.Content, *string, bool)
buildCertificates(*file.Content, *string, bool)
buildVaults(*file.Content, *string, bool)
getContent() string
}

Expand All @@ -31,15 +31,16 @@ func newDirector(builder ITerraformBuilder) *Director {
}
}

func (d *Director) builTerraformResources(content *file.Content) string {
d.builder.buildGlobalPlugins(content)
d.builder.buildServices(content)
d.builder.buildUpstreams(content)
d.builder.buildRoutes(content)
d.builder.buildConsumers(content)
d.builder.buildConsumerGroups(content)
d.builder.buildCACertificates(content)
d.builder.buildCertificates(content)
d.builder.buildVaults(content)
func (d *Director) builTerraformResources(content *file.Content, generateImportsForControlPlaneID *string, ignoreCredentialChanges bool) string {

d.builder.buildGlobalPlugins(content, generateImportsForControlPlaneID, ignoreCredentialChanges)
d.builder.buildServices(content, generateImportsForControlPlaneID, ignoreCredentialChanges)
d.builder.buildUpstreams(content, generateImportsForControlPlaneID, ignoreCredentialChanges)
d.builder.buildRoutes(content, generateImportsForControlPlaneID, ignoreCredentialChanges)
d.builder.buildConsumers(content, generateImportsForControlPlaneID, ignoreCredentialChanges)
d.builder.buildConsumerGroups(content, generateImportsForControlPlaneID, ignoreCredentialChanges)
d.builder.buildCACertificates(content, generateImportsForControlPlaneID, ignoreCredentialChanges)
d.builder.buildCertificates(content, generateImportsForControlPlaneID, ignoreCredentialChanges)
d.builder.buildVaults(content, generateImportsForControlPlaneID, ignoreCredentialChanges)
return d.builder.getContent()
}
31 changes: 21 additions & 10 deletions kong2tf/builder_default_terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,20 @@ type DefaultTerraformBuider struct {
content string
}

type TemplateObjectWrapper struct {
Content interface{}
GenerateImportsForControlPlaneID *string
IgnoreCredentialChanges bool
}

func newDefaultTerraformBuilder() *DefaultTerraformBuider {
return &DefaultTerraformBuider{}
}

//go:embed templates/service.go.tmpl
var terraformServiceTemplate string

func (b *DefaultTerraformBuider) buildServices(content *file.Content) {
func (b *DefaultTerraformBuider) buildServices(content *file.Content, generateImportsForControlPlaneID *string, ignoreCredentialChanges bool) {
tmpl, err := template.New(terraformServiceTemplate).Funcs(funcs).Parse(terraformServiceTemplate)
if err != nil {
log.Fatal(err, "Failed to parse template")
Expand All @@ -67,7 +73,7 @@ func (b *DefaultTerraformBuider) buildServices(content *file.Content) {
//go:embed templates/route.go.tmpl
var terraformRouteTemplate string

func (b *DefaultTerraformBuider) buildRoutes(content *file.Content) {
func (b *DefaultTerraformBuider) buildRoutes(content *file.Content, generateImportsForControlPlaneID *string, ignoreCredentialChanges bool) {
logbasics.Info("Starting to build routes")
logbasics.Info("Template content before parsing", "template", terraformRouteTemplate)

Expand All @@ -90,7 +96,7 @@ func (b *DefaultTerraformBuider) buildRoutes(content *file.Content) {
//go:embed templates/global_plugin.go.tmpl
var terraformGlobalPluginTemplate string

func (b *DefaultTerraformBuider) buildGlobalPlugins(content *file.Content) {
func (b *DefaultTerraformBuider) buildGlobalPlugins(content *file.Content, generateImportsForControlPlaneID *string, ignoreCredentialChanges bool) {
logbasics.Info("Starting to build global plugins")
logbasics.Info("Template content before parsing", "template", terraformGlobalPluginTemplate)

Expand All @@ -113,7 +119,7 @@ func (b *DefaultTerraformBuider) buildGlobalPlugins(content *file.Content) {
//go:embed templates/consumer.go.tmpl
var terraformConsumerTemplate string

func (b *DefaultTerraformBuider) buildConsumers(content *file.Content) {
func (b *DefaultTerraformBuider) buildConsumers(content *file.Content, generateImportsForControlPlaneID *string, ignoreCredentialChanges bool) {
logbasics.Info("Starting to build consumers")
logbasics.Info("Template content before parsing", "template", terraformConsumerTemplate)

Expand All @@ -123,9 +129,14 @@ func (b *DefaultTerraformBuider) buildConsumers(content *file.Content) {
}

for _, consumer := range content.Consumers {
wrapper := TemplateObjectWrapper{
Content: consumer,
GenerateImportsForControlPlaneID: generateImportsForControlPlaneID,
IgnoreCredentialChanges: ignoreCredentialChanges,
}
var buffer bytes.Buffer

err = tmpl.Execute(&buffer, consumer)
err = tmpl.Execute(&buffer, wrapper)
if err != nil {
log.Fatal(err)
}
Expand All @@ -136,7 +147,7 @@ func (b *DefaultTerraformBuider) buildConsumers(content *file.Content) {
//go:embed templates/consumer_group.go.tmpl
var terraformConsumerGroupTemplate string

func (b *DefaultTerraformBuider) buildConsumerGroups(content *file.Content) {
func (b *DefaultTerraformBuider) buildConsumerGroups(content *file.Content, generateImportsForControlPlaneID *string, ignoreCredentialChanges bool) {
logbasics.Info("Starting to build consumer groups")
logbasics.Info("Template content before parsing", "template", terraformConsumerGroupTemplate)

Expand All @@ -159,7 +170,7 @@ func (b *DefaultTerraformBuider) buildConsumerGroups(content *file.Content) {
//go:embed templates/upstream.go.tmpl
var terraformUpstreamTemplate string

func (b *DefaultTerraformBuider) buildUpstreams(content *file.Content) {
func (b *DefaultTerraformBuider) buildUpstreams(content *file.Content, generateImportsForControlPlaneID *string, ignoreCredentialChanges bool) {
tmpl, err := template.New(terraformUpstreamTemplate).Funcs(funcs).Parse(terraformUpstreamTemplate)
if err != nil {
log.Fatal(err)
Expand All @@ -179,7 +190,7 @@ func (b *DefaultTerraformBuider) buildUpstreams(content *file.Content) {
//go:embed templates/ca_certificate.go.tmpl
var terraformCACertificateTemplate string

func (b *DefaultTerraformBuider) buildCACertificates(content *file.Content) {
func (b *DefaultTerraformBuider) buildCACertificates(content *file.Content, generateImportsForControlPlaneID *string, ignoreCredentialChanges bool) {
tmpl, err := template.New(terraformCACertificateTemplate).Funcs(funcs).Parse(terraformCACertificateTemplate)
if err != nil {
log.Fatal(err)
Expand All @@ -199,7 +210,7 @@ func (b *DefaultTerraformBuider) buildCACertificates(content *file.Content) {
//go:embed templates/certificate.go.tmpl
var terraformCertificateTemplate string

func (b *DefaultTerraformBuider) buildCertificates(content *file.Content) {
func (b *DefaultTerraformBuider) buildCertificates(content *file.Content, generateImportsForControlPlaneID *string, ignoreCredentialChanges bool) {
tmpl, err := template.New(terraformCertificateTemplate).Funcs(funcs).Parse(terraformCertificateTemplate)
if err != nil {
log.Fatal(err)
Expand All @@ -219,7 +230,7 @@ func (b *DefaultTerraformBuider) buildCertificates(content *file.Content) {
//go:embed templates/vault.go.tmpl
var terraformVaultTemplate string

func (b *DefaultTerraformBuider) buildVaults(content *file.Content) {
func (b *DefaultTerraformBuider) buildVaults(content *file.Content, generateImportsForControlPlaneID *string, ignoreCredentialChanges bool) {
tmpl, err := template.New(terraformVaultTemplate).Funcs(funcs).Parse(terraformVaultTemplate)
if err != nil {
log.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions kong2tf/kong2tf.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ type TfConfig struct {
ControlPlaneID string
}

func Convert(inputContent *file.Content) (string, error) {
func Convert(inputContent *file.Content, generateImportsForControlPlaneID *string, ignoreCredentialChanges bool) (string, error) {
builder := getTerraformBuilder()
director := newDirector(builder)
return director.builTerraformResources(inputContent), nil
return director.builTerraformResources(inputContent, generateImportsForControlPlaneID, ignoreCredentialChanges), nil
}
2 changes: 1 addition & 1 deletion kong2tf/kong2tf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func Test_convertKongGatewayToTerraform(t *testing.T) {
assert.Fail(t, err.Error())
}

output, err := Convert(inputContent)
output, err := Convert(inputContent, nil, false)

if err == nil {
compareFileContent(t, tt.outputFilename, []byte(output))
Expand Down
Loading

0 comments on commit 283b09a

Please sign in to comment.