Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change default deploytype to avoid defaulting to invalid type #207

Merged
Merged
20 changes: 20 additions & 0 deletions .github/workflows/defaults-smoketest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Draft Defaults Smoke Test
on:
pull_request:
branches: [ main ]

jobs:
# Validate that `create` can successfully execute with default values
create-defaults-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v3
with:
go-version: 1.18.2
- name: make
run: make
- name: Validate Create Runs with Defaults
run: |
bash -c "yes || true" | ./draft
29 changes: 20 additions & 9 deletions cmd/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"os"
"strings"

"golang.org/x/exp/maps"
"gopkg.in/yaml.v3"

"github.com/manifoldco/promptui"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"golang.org/x/exp/maps"
"gopkg.in/yaml.v3"

"github.com/Azure/draft/pkg/config"
"github.com/Azure/draft/pkg/deployments"
Expand All @@ -33,6 +34,10 @@ var flagVariablesMap = make(map[string]string)
const LANGUAGE_VARIABLE = "LANGUAGE"
const TWO_SPACES = " "

// Flag defaults
const emptyDefaultFlagValue = ""
const currentDirDefaultFlagValue = "."

type createCmd struct {
appName string
lang string
Expand Down Expand Up @@ -70,11 +75,11 @@ func newCreateCmd() *cobra.Command {

f := cmd.Flags()

f.StringVarP(&cc.createConfigPath, "create-config", "c", "", "specify the path to the configuration file")
f.StringVarP(&cc.appName, "app", "a", "", "specify the name of the helm release")
f.StringVarP(&cc.lang, "language", "l", "", "specify the language used to create the Kubernetes deployment")
f.StringVarP(&cc.dest, "destination", "d", ".", "specify the path to the project directory")
f.StringVarP(&cc.deployType, "deploy-type", "", ".", "specify deployement type (eg. helm, kustomize, manifests)")
f.StringVarP(&cc.createConfigPath, "create-config", "c", emptyDefaultFlagValue, "specify the path to the configuration file")
f.StringVarP(&cc.appName, "app", "a", emptyDefaultFlagValue, "specify the name of the helm release")
f.StringVarP(&cc.lang, "language", "l", emptyDefaultFlagValue, "specify the language used to create the Kubernetes deployment")
f.StringVarP(&cc.dest, "destination", "d", currentDirDefaultFlagValue, "specify the path to the project directory")
f.StringVarP(&cc.deployType, "deploy-type", "", emptyDefaultFlagValue, "specify deployement type (eg. helm, kustomize, manifests)")
f.BoolVar(&cc.dockerfileOnly, "dockerfile-only", false, "only create Dockerfile in the project directory")
f.BoolVar(&cc.deploymentOnly, "deployment-only", false, "only create deployment files in the project directory")
f.BoolVar(&cc.skipFileDetection, "skip-file-detection", false, "skip file detection step")
Expand Down Expand Up @@ -287,7 +292,10 @@ func (cc *createCmd) createDeployment() error {

if cc.createConfig.DeployType != "" {
deployType = strings.ToLower(cc.createConfig.DeployType)
deployConfig := d.GetConfig(deployType)
deployConfig, err := d.GetConfig(deployType)
if err != nil {
return err
}
if deployConfig == nil {
return errors.New("invalid deployment type")
}
Expand All @@ -311,7 +319,10 @@ func (cc *createCmd) createDeployment() error {
deployType = cc.deployType
}

deployConfig := d.GetConfig(deployType)
deployConfig, err := d.GetConfig(deployType)
if err != nil {
return err
}
customInputs, err = prompts.RunPromptsFromConfigWithSkips(deployConfig, maps.Keys(flagVariablesMap))
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions cmd/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,11 @@ func (mcc *createCmd) mockDetectLanguage() (*config.DraftConfig, string, error)
return nil, "", ErrNoLanguageDetected
}

func TestDefaultValues(t *testing.T) {
assert.Equal(t, emptyDefaultFlagValue, "")
assert.Equal(t, currentDirDefaultFlagValue, ".")
}

func getAllDeploymentFiles(src string) (error, []string) {
deploymentFiles := []string{}
err := filepath.Walk(src,
Expand Down
16 changes: 8 additions & 8 deletions cmd/generate-workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ with draft on AKS. This command assumes the 'setup-gh' command has been run prop
}

f := cmd.Flags()
f.StringVarP(&gwCmd.workflowConfig.AksClusterName, "cluster-name", "c", "", "specify the AKS cluster name")
f.StringVarP(&gwCmd.workflowConfig.AcrName, "registry-name", "r", "", "specify the Azure container registry name")
f.StringVar(&gwCmd.workflowConfig.ContainerName, "container-name", "", "specify the container image name")
f.StringVarP(&gwCmd.workflowConfig.ResourceGroupName, "resource-group", "g", "", "specify the Azure resource group of your AKS cluster")
f.StringVarP(&gwCmd.dest, "destination", "d", ".", "specify the path to the project directory")
f.StringVarP(&gwCmd.workflowConfig.BranchName, "branch", "b", "", "specify the Github branch to automatically deploy from")
f.StringVar(&gwCmd.deployType, "deploy-type", "", "specify the type of deployment")
f.StringVarP(&gwCmd.workflowConfig.AksClusterName, "cluster-name", "c", emptyDefaultFlagValue, "specify the AKS cluster name")
f.StringVarP(&gwCmd.workflowConfig.AcrName, "registry-name", "r", emptyDefaultFlagValue, "specify the Azure container registry name")
f.StringVar(&gwCmd.workflowConfig.ContainerName, "container-name", emptyDefaultFlagValue, "specify the container image name")
f.StringVarP(&gwCmd.workflowConfig.ResourceGroupName, "resource-group", "g", emptyDefaultFlagValue, "specify the Azure resource group of your AKS cluster")
f.StringVarP(&gwCmd.dest, "destination", "d", currentDirDefaultFlagValue, "specify the path to the project directory")
f.StringVarP(&gwCmd.workflowConfig.BranchName, "branch", "b", emptyDefaultFlagValue, "specify the Github branch to automatically deploy from")
f.StringVar(&gwCmd.deployType, "deploy-type", emptyDefaultFlagValue, "specify the type of deployment")
f.StringArrayVarP(&gwCmd.flagVariables, "variable", "", []string{}, "pass additional variables")
f.StringVarP(&gwCmd.workflowConfig.BuildContextPath, "build-context-path", "x", "", "specify the docker build context path")
f.StringVarP(&gwCmd.workflowConfig.BuildContextPath, "build-context-path", "x", emptyDefaultFlagValue, "specify the docker build context path")
gwCmd.templateWriter = &writers.LocalFSWriter{}
return cmd
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/setup-gh.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ application and service principle, and will configure that application to trust
}

f := cmd.Flags()
f.StringVarP(&sc.AppName, "app", "a", "", "specify the Azure Active Directory application name")
f.StringVarP(&sc.SubscriptionID, "subscription-id", "s", "", "specify the Azure subscription ID")
f.StringVarP(&sc.ResourceGroupName, "resource-group", "r", "", "specify the Azure resource group name")
f.StringVarP(&sc.Repo, "gh-repo", "g", "", "specify the github repository link")
f.StringVarP(&sc.AppName, "app", "a", emptyDefaultFlagValue, "specify the Azure Active Directory application name")
f.StringVarP(&sc.SubscriptionID, "subscription-id", "s", emptyDefaultFlagValue, "specify the Azure subscription ID")
f.StringVarP(&sc.ResourceGroupName, "resource-group", "r", emptyDefaultFlagValue, "specify the Azure resource group name")
f.StringVarP(&sc.Repo, "gh-repo", "g", emptyDefaultFlagValue, "specify the github repository link")
sc.Provider = provider
return cmd
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/deployments/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import (
"io/fs"
"path"

log "github.com/sirupsen/logrus"
"golang.org/x/exp/maps"
"gopkg.in/yaml.v3"

log "github.com/sirupsen/logrus"

"github.com/Azure/draft/pkg/config"
"github.com/Azure/draft/pkg/embedutils"
"github.com/Azure/draft/pkg/osutil"
Expand Down Expand Up @@ -74,12 +75,12 @@ func (d *Deployments) loadConfig(lang string) (*config.DraftConfig, error) {
return &draftConfig, nil
}

func (d *Deployments) GetConfig(deployType string) *config.DraftConfig {
func (d *Deployments) GetConfig(deployType string) (*config.DraftConfig, error) {
val, ok := d.configs[deployType]
if !ok {
return nil
return nil, fmt.Errorf("deployment type: %s is not currently supported", deployType)
}
return val
return val, nil
}

func (d *Deployments) PopulateConfigs() {
Expand Down
2 changes: 2 additions & 0 deletions template/deployments/helm/draft.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,5 @@ variableDefaults:
- name: "IMAGETAG"
value: "latest"
disablePrompt: true
- name: "APPNAME"
davidgamero marked this conversation as resolved.
Show resolved Hide resolved
value: "myapp"