Skip to content

Commit

Permalink
Add stack file template configuration to build
Browse files Browse the repository at this point in the history
Adding functionality which will pull templates defined in the
function YAML file. Additionaly added overwrite flag to the
build command in order to enable the command to overwrite
templates. Also removed the filtering funcitonality from template
pull stack command and renamed the method which will be used to
pull the configuration

Signed-off-by: Martin Dekov <[email protected]>
  • Loading branch information
martindekov committed Nov 12, 2019
1 parent 83081ef commit 6533820
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 82 deletions.
8 changes: 8 additions & 0 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func init() {
buildCmd.Flags().StringVar(&handler, "handler", "", "Directory with handler for function, e.g. handler.js")
buildCmd.Flags().StringVar(&functionName, "name", "", "Name of the deployed function")
buildCmd.Flags().StringVar(&language, "lang", "", "Programming language template")
buildCmd.Flags().BoolVar(&overwrite, "overwrite", false, "Overwrite existing templates?")

// Setup flags that are used only by this command (variables defined above)
buildCmd.Flags().BoolVar(&nocache, "no-cache", false, "Do not use Docker's build cache")
Expand Down Expand Up @@ -156,6 +157,13 @@ func runBuild(cmd *cobra.Command, args []string) error {
return fmt.Errorf("could not pull templates for OpenFaaS: %v", pullErr)
}

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

if len(services.Functions) > 0 {

build(&services, parallel, shrinkwrap)
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 {
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

0 comments on commit 6533820

Please sign in to comment.