-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: Add new command for Kong to Terraform conversion * Granular testing * fix plugin route reference * feat(kong2tf): PROTOTYPE kong2tf with import blocks and lifecycle-ignore blocks, plus appropriate switches; patches "route ID is read only field" bug * Replace templated kong2tf with recursive implementation * fix: linting issues --------- Co-authored-by: battlebyte <[email protected]> Co-authored-by: Jack Tysoe <[email protected]> Co-authored-by: Prashansa Kulshrestha <[email protected]>
- Loading branch information
1 parent
fb795ad
commit a084b3c
Showing
51 changed files
with
2,920 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
|
||
"github.com/kong/deck/kong2tf" | ||
"github.com/kong/go-apiops/filebasics" | ||
"github.com/kong/go-apiops/logbasics" | ||
"github.com/kong/go-database-reconciler/pkg/file" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
cmdKong2TfInputFilename string | ||
cmdKong2TfOutputFilename string | ||
cmdKong2TfGenerateImportsForControlPlaneID string | ||
cmdKong2TfIgnoreCredentialChanges bool | ||
) | ||
|
||
// Executes the CLI command "kong2Tf" | ||
func executeKong2Tf(cmd *cobra.Command, _ []string) error { | ||
_ = sendAnalytics("file-kong2Tf", "", modeLocal) | ||
var ( | ||
result string | ||
err error | ||
) | ||
|
||
verbosity, _ := cmd.Flags().GetInt("verbose") | ||
logbasics.Initialize(log.LstdFlags, verbosity) | ||
|
||
logbasics.Info("Starting execution of executeKong2Tf") | ||
|
||
inputContent, err := file.GetContentFromFiles([]string{cmdKong2TfInputFilename}, false) | ||
if err != nil { | ||
log.Printf("Error reading input file '%s'; %v", cmdKong2TfInputFilename, err) | ||
return fmt.Errorf("failed reading input file '%s'; %w", cmdKong2TfInputFilename, err) | ||
} | ||
logbasics.Info("Successfully read input file '%s'", cmdKong2TfInputFilename) | ||
|
||
logbasics.Info("Converting Kong configuration to Terraform") | ||
|
||
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) | ||
} | ||
logbasics.Info("Successfully converted Kong configuration to Terraform") | ||
|
||
logbasics.Info("Writing output to file '%s'", cmdKong2TfOutputFilename) | ||
err = filebasics.WriteFile(cmdKong2TfOutputFilename, []byte(result)) | ||
if err != nil { | ||
log.Printf("Error writing output to file '%s'; %v", cmdKong2TfOutputFilename, err) | ||
return err | ||
} | ||
logbasics.Info("Successfully wrote output to file '%s'", cmdKong2TfOutputFilename) | ||
|
||
logbasics.Info("Finished execution of executeKong2Tf") | ||
return nil | ||
} | ||
|
||
// | ||
// | ||
// Define the CLI data for the kong2Tf command | ||
// | ||
// | ||
|
||
func newKong2TfCmd() *cobra.Command { | ||
kong2TfCmd := &cobra.Command{ | ||
Use: "kong2tf", | ||
Short: "Convert Kong configuration files to Terraform resources", | ||
Long: `Convert Kong configuration files to Terraform resources. | ||
The kong2tf subcommand transforms Kong Gateway entities in deck format, | ||
into Terraform resources.`, | ||
RunE: executeKong2Tf, | ||
Args: cobra.NoArgs, | ||
} | ||
|
||
kong2TfCmd.Flags().StringVarP(&cmdKong2TfInputFilename, "state", "s", "-", | ||
"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", "", "Generate terraform import statements for the control plane ID.") | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package kong2tf | ||
|
||
import ( | ||
"github.com/kong/go-database-reconciler/pkg/file" | ||
) | ||
|
||
type ITerraformBuilder interface { | ||
buildControlPlaneVar(*string) | ||
buildServices(*file.Content, *string) | ||
buildRoutes(*file.Content, *string) | ||
buildGlobalPlugins(*file.Content, *string) | ||
buildConsumers(*file.Content, *string, bool) | ||
buildConsumerGroups(*file.Content, *string) | ||
buildUpstreams(*file.Content, *string) | ||
buildCACertificates(*file.Content, *string) | ||
buildCertificates(*file.Content, *string) | ||
buildVaults(*file.Content, *string) | ||
getContent() string | ||
} | ||
|
||
func getTerraformBuilder() ITerraformBuilder { | ||
return newDefaultTerraformBuilder() | ||
} | ||
|
||
type Director struct { | ||
builder ITerraformBuilder | ||
} | ||
|
||
func newDirector(builder ITerraformBuilder) *Director { | ||
return &Director{ | ||
builder: builder, | ||
} | ||
} | ||
|
||
func (d *Director) builTerraformResources( | ||
content *file.Content, | ||
generateImportsForControlPlaneID *string, | ||
ignoreCredentialChanges bool, | ||
) string { | ||
d.builder.buildControlPlaneVar(generateImportsForControlPlaneID) | ||
d.builder.buildGlobalPlugins(content, generateImportsForControlPlaneID) | ||
d.builder.buildServices(content, generateImportsForControlPlaneID) | ||
d.builder.buildUpstreams(content, generateImportsForControlPlaneID) | ||
d.builder.buildRoutes(content, generateImportsForControlPlaneID) | ||
d.builder.buildConsumers(content, generateImportsForControlPlaneID, ignoreCredentialChanges) | ||
d.builder.buildConsumerGroups(content, generateImportsForControlPlaneID) | ||
d.builder.buildCACertificates(content, generateImportsForControlPlaneID) | ||
d.builder.buildCertificates(content, generateImportsForControlPlaneID) | ||
d.builder.buildVaults(content, generateImportsForControlPlaneID) | ||
return d.builder.getContent() | ||
} |
Oops, something went wrong.