Skip to content

Commit

Permalink
Integrating Deployment templates into new generic handler (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
bfoley13 authored Oct 9, 2024
1 parent fe4784c commit 073e184
Show file tree
Hide file tree
Showing 45 changed files with 522 additions and 706 deletions.
26 changes: 14 additions & 12 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ import (

"gopkg.in/yaml.v3"

"github.com/Azure/draft/pkg/handlers"
"github.com/Azure/draft/pkg/reporeader"
"github.com/Azure/draft/pkg/reporeader/readers"
"github.com/manifoldco/promptui"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/Azure/draft/pkg/config"
"github.com/Azure/draft/pkg/deployments"
dryrunpkg "github.com/Azure/draft/pkg/dryrun"
"github.com/Azure/draft/pkg/filematches"
"github.com/Azure/draft/pkg/languages"
Expand Down Expand Up @@ -304,21 +304,20 @@ func (cc *createCmd) generateDockerfile(langConfig *config.DraftConfig, lowerLan

func (cc *createCmd) createDeployment() error {
log.Info("--- Deployment File Creation ---")
d := deployments.CreateDeploymentsFromEmbedFS(template.Deployments, cc.dest)
var deployType string
var deployConfig *config.DraftConfig
var deployTemplate *handlers.Template
var err error

if cc.createConfig.DeployType != "" {
deployType = strings.ToLower(cc.createConfig.DeployType)
deployConfig, err = d.GetConfig(deployType)
deployTemplate, err = handlers.GetTemplate(fmt.Sprintf("deployment-%s", deployType), "", cc.dest, cc.templateWriter)
if err != nil {
return err
}
if deployConfig == nil {
if deployTemplate == nil || deployTemplate.Config == nil {
return errors.New("invalid deployment type")
}
err = validateConfigInputsToPrompts(deployConfig, cc.createConfig.DeployVariables)
err = validateConfigInputsToPrompts(deployTemplate.Config, cc.createConfig.DeployVariables)
if err != nil {
return err
}
Expand All @@ -337,28 +336,31 @@ func (cc *createCmd) createDeployment() error {
deployType = cc.deployType
}

deployConfig, err = d.GetConfig(deployType)
deployTemplate, err = handlers.GetTemplate(fmt.Sprintf("deployment-%s", deployType), "", cc.dest, cc.templateWriter)
if err != nil {
return err
}

deployConfig.VariableMapToDraftConfig(flagVariablesMap)
if deployTemplate == nil || deployTemplate.Config == nil {
return errors.New("invalid deployment type")
}

err = prompts.RunPromptsFromConfigWithSkips(deployConfig)
deployTemplate.Config.VariableMapToDraftConfig(flagVariablesMap)

err = prompts.RunPromptsFromConfigWithSkips(deployTemplate.Config)
if err != nil {
return err
}
}

if cc.templateVariableRecorder != nil {
for _, variable := range deployConfig.Variables {
for _, variable := range deployTemplate.Config.Variables {
cc.templateVariableRecorder.Record(variable.Name, variable.Value)
}
}

log.Infof("--> Creating %s Kubernetes resources...\n", deployType)

return d.CopyDeploymentFiles(deployType, deployConfig, cc.templateWriter)
return deployTemplate.Generate()
}

func (cc *createCmd) createFiles(detectedLang *config.DraftConfig, lowerLang string) error {
Expand Down
12 changes: 8 additions & 4 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"

"github.com/Azure/draft/pkg/deployments"
"github.com/Azure/draft/pkg/languages"
"github.com/Azure/draft/template"
)
Expand All @@ -18,6 +17,12 @@ const (
JSON Format = "json"
)

var supportedDeploymentTypes = [...]string{
"helm",
"kustomize",
"manifests",
}

type infoCmd struct {
format string
info *draftInfo
Expand All @@ -32,7 +37,7 @@ type draftConfigInfo struct {

type draftInfo struct {
SupportedLanguages []draftConfigInfo `json:"supportedLanguages"`
SupportedDeploymentTypes []string `json:"supportedDeploymentTypes"`
SupportedDeploymentTypes [3]string `json:"supportedDeploymentTypes"`
}

func newInfoCmd() *cobra.Command {
Expand All @@ -57,7 +62,6 @@ func newInfoCmd() *cobra.Command {
func (ic *infoCmd) run() error {
log.Debugf("getting supported languages")
l := languages.CreateLanguagesFromEmbedFS(template.Dockerfiles, "")
d := deployments.CreateDeploymentsFromEmbedFS(template.Deployments, "")

languagesInfo := make([]draftConfigInfo, 0)
for _, lang := range l.Names() {
Expand All @@ -72,7 +76,7 @@ func (ic *infoCmd) run() error {

ic.info = &draftInfo{
SupportedLanguages: languagesInfo,
SupportedDeploymentTypes: d.DeployTypes(),
SupportedDeploymentTypes: supportedDeploymentTypes,
}

infoText, err := json.MarshalIndent(ic.info, "", " ")
Expand Down
104 changes: 27 additions & 77 deletions example/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,10 @@ package example
import (
"fmt"

"github.com/Azure/draft/pkg/config"
"github.com/Azure/draft/pkg/deployments"
"github.com/Azure/draft/pkg/templatewriter"
"github.com/Azure/draft/pkg/handlers"
"github.com/Azure/draft/pkg/templatewriter/writers"
"github.com/Azure/draft/template"
)

// WriteDeploymentFiles generates Deployment Files using Draft, writing to a Draft TemplateWriter. See the corresponding draft.yaml file in templates/deployments/[deployType] for the template inputs.
func WriteDeploymentFiles(w templatewriter.TemplateWriter, deploymentOutputPath string, deployConfig *config.DraftConfig, deploymentType string) error {
d := deployments.CreateDeploymentsFromEmbedFS(template.Deployments, deploymentOutputPath)

err := d.CopyDeploymentFiles(deploymentType, deployConfig, w)
if err != nil {
return fmt.Errorf("failed to generate manifest: %e", err)
}
return nil
}

// WriteDeploymentFilesExample shows how to set up a fileWriter and generate a fileMap using WriteDeploymentFiles
func WriteDeploymentFilesExample() error {
// Create a file map
Expand All @@ -32,76 +18,40 @@ func WriteDeploymentFilesExample() error {
}

// Select the deployment type to generate the files for (must correspond to a directory in the template/deployments directory)
deploymentType := "manifests"
deploymentTemplateType := "deployment-manifests"

// Create a DraftConfig of inputs to the template (must correspond to the inputs in the template/deployments/<deploymentType>/draft.yaml files)
deployConfig := &config.DraftConfig{
Variables: []*config.BuilderVar{
{
Name: "PORT",
Default: config.BuilderVarDefault{
Value: "80",
},
Description: "the port exposed in the application",
Value: "8080",
},
{
Name: "APPNAME",
Description: "the name of the application",
Value: "example-app",
},
{
Name: "SERVICEPORT",
Default: config.BuilderVarDefault{
ReferenceVar: "PORT",
},
Description: "the port the service uses to make the application accessible from outside the cluster",
Value: "8080",
},
{
Name: "NAMESPACE",
Default: config.BuilderVarDefault{
Value: "default",
},
Description: "the name of the image to use in the deployment",
Value: "example-namespace",
},
{
Name: "IMAGENAME",
Default: config.BuilderVarDefault{
IsPromptDisabled: true,
Value: "the name of the image to use in the deployment",
},
Description: "the name of the image to use in the deployment",
Value: "example-image",
},
{
Name: "IMAGETAG",
Default: config.BuilderVarDefault{
IsPromptDisabled: true,
Value: "latest",
},
Description: "the tag of the image to use in the deployment",
Value: "latest",
},
{
Name: "GENERATORLABEL",
Default: config.BuilderVarDefault{
IsPromptDisabled: true,
Value: "draft",
},
Description: "the label to use to identify the deployment as generated by draft",
},
},
templateVars := map[string]string{
"PORT": "8080",
"APPNAME": "example-app",
"SERVICEPORT": "8080",
"NAMESPACE": "example-namespace",
"IMAGENAME": "example-image",
"IMAGETAG": "latest",
"GENERATORLABEL": "draft",
}

// Set the output path for the deployment files
outputPath := "./"

// Write the deployment files
err := WriteDeploymentFiles(&w, outputPath, deployConfig, deploymentType)
// Get the deployment template
d, err := handlers.GetTemplate(deploymentTemplateType, "", outputPath, &w)
if err != nil {
return err
return fmt.Errorf("failed to get template: %e", err)
}
if d == nil {
return fmt.Errorf("template is nil")
}

// Set the variable values within the template
for k, v := range templateVars {
d.Config.SetVariable(k, v)
}

// Generate the deployment files
err = d.Generate()
if err != nil {
return fmt.Errorf("failed to generate manifest: %e", err)
}

// Read written files from the file map
Expand Down
Loading

0 comments on commit 073e184

Please sign in to comment.