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

Add stack file template configuration to build #726

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 22 additions & 12 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@ import (

// Flags that are to be added to commands.
var (
nocache bool
squash bool
parallel int
shrinkwrap bool
buildArgs []string
buildArgMap map[string]string
buildOptions []string
tagFormat schema.BuildFormat
buildLabels []string
buildLabelMap map[string]string
envsubst bool
quietBuild bool
nocache bool
squash bool
parallel int
shrinkwrap bool
buildArgs []string
buildArgMap map[string]string
buildOptions []string
tagFormat schema.BuildFormat
buildLabels []string
buildLabelMap map[string]string
envsubst bool
quietBuild bool
disableStackPull bool
)

func init() {
Expand All @@ -56,6 +57,8 @@ func init() {

buildCmd.Flags().BoolVar(&quietBuild, "quiet", false, "Perform a quiet build, without showing output from Docker")

buildCmd.Flags().BoolVar(&disableStackPull, "disable-stack-pull", false, "Disables the template configuration in the stack.yml")

// Set bash-completion.
_ = buildCmd.Flags().SetAnnotation("handler", cobra.BashCompSubdirsInDir, []string{})

Expand Down Expand Up @@ -181,6 +184,13 @@ func runBuild(cmd *cobra.Command, args []string) error {
return nil
}

if len(services.StackConfiguration.TemplateConfigs) != 0 && !disableStackPull {
err := pullStackTemplates(services.StackConfiguration.TemplateConfigs, cmd)
if err != nil {
return fmt.Errorf("could not pull templates from function yaml file: %s", err.Error())
}
}

errors := build(&services, parallel, shrinkwrap, quietBuild)
if len(errors) > 0 {
errorSummary := "Errors received during build:\n"
Expand Down
19 changes: 2 additions & 17 deletions commands/template_pull_stack.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ var (
func init() {
templatePullStackCmd.Flags().BoolVar(&overwrite, "overwrite", false, "Overwrite existing templates?")
templatePullStackCmd.Flags().BoolVar(&pullDebug, "debug", false, "Enable debug output")
templatePullStackCmd.PersistentFlags().StringVarP(&customRepoName, "repo", "r", "", "The custom name of the template repo")

templatePullCmd.AddCommand(templatePullStackCmd)
}
Expand All @@ -41,10 +40,7 @@ func runTemplatePullStack(cmd *cobra.Command, args []string) error {
if err != nil {
return err
}
if len(customRepoName) > 0 {
return pullSpecificTemplate(templatesConfig, customRepoName, cmd)
}
return pullAllTemplates(templatesConfig, cmd)
return pullStackTemplates(templatesConfig, cmd)
}

func loadTemplateConfig() ([]stack.TemplateSource, error) {
Expand Down Expand Up @@ -72,7 +68,7 @@ func readStackConfig() (stack.Configuration, error) {
return configField, nil
}

func pullAllTemplates(templateInfo []stack.TemplateSource, cmd *cobra.Command) error {
func pullStackTemplates(templateInfo []stack.TemplateSource, cmd *cobra.Command) error {
for _, val := range templateInfo {
fmt.Printf("Pulling template: %s from configuration file: %s\n", val.Name, yamlFile)
if len(val.Source) == 0 {
Expand All @@ -98,14 +94,3 @@ func findTemplate(templateInfo []stack.TemplateSource, customName string) (speci
}
return nil
}

func pullSpecificTemplate(templateInfo []stack.TemplateSource, customName string, cmd *cobra.Command) error {
martindekov marked this conversation as resolved.
Show resolved Hide resolved
desiredTemplate := findTemplate(templateInfo, customName)
if desiredTemplate == nil {
return fmt.Errorf("Unable to find template repo with name: `%s`", customName)
}
if len(desiredTemplate.Source) == 0 {
return runTemplateStorePull(cmd, []string{desiredTemplate.Name})
}
return pullTemplate(desiredTemplate.Source)
}
63 changes: 1 addition & 62 deletions commands/template_pull_stack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,68 +98,7 @@ func Test_pullAllTemplates(t *testing.T) {
}
for _, test := range tests {
t.Run(test.title, func(t *testing.T) {
actualError := pullAllTemplates(test.existingTemplates, templatePullStackCmd)
if actualError != nil && test.expectedError == false {
t.Errorf("Unexpected error: %s", actualError.Error())
}
})
}
}

func Test_pullSpecificTemplate(t *testing.T) {
tests := []struct {
title string
desiredTemplate string
existingTemplates []stack.TemplateSource
expectedError bool
}{
{
title: "Pull custom named template",
desiredTemplate: "my_powershell",
existingTemplates: []stack.TemplateSource{
{Name: "my_powershell", Source: "https://github.com/openfaas-incubator/powershell-http-template"},
{Name: "my_rust", Source: "https://github.com/openfaas-incubator/openfaas-rust-template"},
},
expectedError: false,
},
{
title: "Pull missing template",
desiredTemplate: "my_perl",
existingTemplates: []stack.TemplateSource{
{Name: "my_powershell", Source: "https://github.com/openfaas-incubator/powershell-http-template"},
{Name: "my_rust", Source: "https://github.com/openfaas-incubator/openfaas-rust-template"},
},
expectedError: true,
},
{
title: "Pull custom template from store",
desiredTemplate: "perl-alpine",
existingTemplates: []stack.TemplateSource{
{Name: "perl-alpine"},
{Name: "my_rust", Source: "https://github.com/openfaas-incubator/openfaas-rust-template"},
},
expectedError: false,
},
{
title: "Pull specific template with invalid URL",
desiredTemplate: "my_powershell",
existingTemplates: []stack.TemplateSource{
{Name: "my_powershell", Source: "invalidURL"},
},
expectedError: true,
},
{
title: "Pull template missing from store",
desiredTemplate: "my_powershell",
existingTemplates: []stack.TemplateSource{
{Name: "my_powershell"},
},
expectedError: true,
},
}
for _, test := range tests {
t.Run(test.title, func(t *testing.T) {
actualError := pullSpecificTemplate(test.existingTemplates, test.desiredTemplate, templatePullStackCmd)
actualError := pullStackTemplates(test.existingTemplates, templatePullStackCmd)
if actualError != nil && test.expectedError == false {
t.Errorf("Unexpected error: %s", actualError.Error())
}
Expand Down
7 changes: 4 additions & 3 deletions stack/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,10 @@ type EnvironmentFile struct {

// Services root level YAML file to define FaaS function-set
type Services struct {
Version string `yaml:"version,omitempty"`
Functions map[string]Function `yaml:"functions,omitempty"`
Provider Provider `yaml:"provider,omitempty"`
Version string `yaml:"version,omitempty"`
Functions map[string]Function `yaml:"functions,omitempty"`
Provider Provider `yaml:"provider,omitempty"`
StackConfiguration StackConfiguration `yaml:"configuration,omitempty"`
}

// LanguageTemplate read from template.yml within root of a language template folder
Expand Down