diff --git a/builder/build.go b/builder/build.go index 14b41075..c6064ad4 100644 --- a/builder/build.go +++ b/builder/build.go @@ -162,6 +162,21 @@ func BuildImage(image string, handler string, functionName string, language stri return nil } +type FunctionMetadataSource interface { + Get(tagType schema.BuildFormat, contextPath string) (branch, version string, err error) +} + +type FunctionMetadataSourceLive struct { +} + +func (FunctionMetadataSourceLive) Get(tagType schema.BuildFormat, contextPath string) (branch, version string, err error) { + return GetImageTagValues(tagType, contextPath) +} + +func NewFunctionMetadataSourceLive() FunctionMetadataSource { + return FunctionMetadataSourceLive{} +} + // GetImageTagValues returns the image tag format and component information determined via GIT func GetImageTagValues(tagType schema.BuildFormat, contextPath string) (branch, version string, err error) { switch tagType { diff --git a/commands/generate.go b/commands/generate.go index a53de468..848f3aef 100644 --- a/commands/generate.go +++ b/commands/generate.go @@ -9,9 +9,9 @@ import ( "os" "sort" + "github.com/openfaas/faas-cli/builder" v2 "github.com/openfaas/faas-cli/schema/store/v2" - "github.com/openfaas/faas-cli/builder" "github.com/openfaas/faas-cli/proxy" "github.com/openfaas/faas-cli/schema" knativev1 "github.com/openfaas/faas-cli/schema/knative/v1" @@ -44,7 +44,7 @@ func init() { generateCmd.Flags().StringVar(&api, "api", defaultAPIVersion, "CRD API version e.g openfaas.com/v1, serving.knative.dev/v1") generateCmd.Flags().StringVarP(&crdFunctionNamespace, "namespace", "n", "openfaas-fn", "Kubernetes namespace for functions") - generateCmd.Flags().Var(&tagFormat, "tag", "Override latest tag on function Docker image, accepts 'latest', 'sha', 'branch', 'describe'") + generateCmd.Flags().Var(&tagFormat, "tag", "Override latest tag on function Docker image, accepts 'digest', 'latest', 'sha', 'branch', 'describe'") generateCmd.Flags().BoolVar(&envsubst, "envsubst", true, "Substitute environment variables in stack.yml file") generateCmd.Flags().StringVar(&desiredArch, "arch", "x86_64", "Desired image arch. (Default x86_64)") generateCmd.Flags().StringArrayVar(&annotationArgs, "annotation", []string{}, "Any annotations you want to add (to store functions only)") @@ -67,8 +67,9 @@ faas-cli generate --api=openfaas.com/v1 -f stack.yml --tag branch -n openfaas-fn func preRunGenerate(cmd *cobra.Command, args []string) error { if len(api) == 0 { - return fmt.Errorf("You must supply api version with the --api flag") + return fmt.Errorf("you must supply the API version with the --api flag") } + return nil } @@ -101,7 +102,11 @@ func runGenerate(cmd *cobra.Command, args []string) error { return fmt.Errorf("error parsing annotations: %v", annotationErr) } - if len(fromStore) > 0 { + if fromStore != "" { + if tagFormat == schema.DigestFormat { + return fmt.Errorf("digest tag format is not supported for store functions") + } + services = stack.Services{ Provider: stack.Provider{ Name: "openfaas", @@ -167,12 +172,8 @@ Use "--yaml" to pass a file or "--from-store" to generate using function store.` os.Exit(1) } - branch, version, err := builder.GetImageTagValues(tagFormat, handler) - if err != nil { - return err - } - - objectsString, err := generateCRDYAML(services, tagFormat, api, crdFunctionNamespace, branch, version) + objectsString, err := generateCRDYAML(services, tagFormat, api, crdFunctionNamespace, + builder.NewFunctionMetadataSourceLive()) if err != nil { return err } @@ -184,14 +185,14 @@ Use "--yaml" to pass a file or "--from-store" to generate using function store.` } // generateCRDYAML generates CRD YAML for functions -func generateCRDYAML(services stack.Services, format schema.BuildFormat, apiVersion, namespace, branch, version string) (string, error) { +func generateCRDYAML(services stack.Services, format schema.BuildFormat, apiVersion, namespace string, metadataSource builder.FunctionMetadataSource) (string, error) { var objectsString string if len(services.Functions) > 0 { if apiVersion == knativev1.APIVersionLatest { - return generateknativev1ServingServiceCRDYAML(services, format, api, crdFunctionNamespace, branch, version) + return generateknativev1ServingServiceCRDYAML(services, format, api, crdFunctionNamespace) } orderedNames := generateFunctionOrder(services.Functions) @@ -211,6 +212,11 @@ func generateCRDYAML(services stack.Services, format schema.BuildFormat, apiVers return "", envErr } + branch, version, err := metadataSource.Get(tagFormat, handler) + if err != nil { + return "", err + } + metadata := schema.Metadata{Name: name, Namespace: namespace} imageName := schema.BuildImageName(format, function.Image, version, branch) @@ -250,7 +256,7 @@ func generateCRDYAML(services stack.Services, format schema.BuildFormat, apiVers return objectsString, nil } -func generateknativev1ServingServiceCRDYAML(services stack.Services, format schema.BuildFormat, apiVersion, namespace, branch, version string) (string, error) { +func generateknativev1ServingServiceCRDYAML(services stack.Services, format schema.BuildFormat, apiVersion, namespace string) (string, error) { crds := []knativev1.ServingServiceCRD{} orderedNames := generateFunctionOrder(services.Functions) @@ -278,6 +284,11 @@ func generateknativev1ServingServiceCRDYAML(services stack.Services, format sche annotations = *function.Annotations } + branch, version, err := builder.GetImageTagValues(tagFormat, function.Handler) + if err != nil { + return "", err + } + imageName := schema.BuildImageName(format, function.Image, version, branch) crd := knativev1.ServingServiceCRD{ @@ -328,12 +339,15 @@ func generateknativev1ServingServiceCRDYAML(services stack.Services, format sche var objectsString string for _, crd := range crds { - //Marshal the object definition to yaml - objectString, err := yaml.Marshal(crd) - if err != nil { + + var buff bytes.Buffer + yamlEncoder := yaml.NewEncoder(&buff) + yamlEncoder.SetIndent(2) // this is what you're looking for + if err := yamlEncoder.Encode(&crd); err != nil { return "", err } - objectsString += "---\n" + string(objectString) + + objectsString += "---\n" + string(buff.Bytes()) } return objectsString, nil diff --git a/commands/generate_test.go b/commands/generate_test.go index 3947bb4d..f26e28d4 100644 --- a/commands/generate_test.go +++ b/commands/generate_test.go @@ -247,7 +247,8 @@ func Test_generateCRDYAML(t *testing.T) { } services := *parsedServices - generatedYAML, err := generateCRDYAML(services, testcase.Format, testcase.APIVersion, testcase.Namespace, testcase.Branch, testcase.Version) + generatedYAML, err := generateCRDYAML(services, testcase.Format, testcase.APIVersion, testcase.Namespace, + NewFunctionMetadataSourceStub(testcase.Branch, testcase.Version)) if err != nil { t.Fatalf("%s failed: error while generating CRD YAML", testcase.Name) } @@ -258,6 +259,22 @@ func Test_generateCRDYAML(t *testing.T) { } } +type FunctionMetadataSourceStub struct { + version string + branch string +} + +func NewFunctionMetadataSourceStub(branch, version string) FunctionMetadataSourceStub { + return FunctionMetadataSourceStub{ + version: version, + branch: branch, + } +} + +func (f FunctionMetadataSourceStub) Get(tagType schema.BuildFormat, contextPath string) (branch, version string, err error) { + return f.branch, f.version, nil +} + func stringInSlice(a string, list []string) bool { for _, b := range list { if b == a { diff --git a/commands/push.go b/commands/push.go index 08878bae..cf6e7eca 100644 --- a/commands/push.go +++ b/commands/push.go @@ -105,7 +105,6 @@ func pushStack(services *stack.Services, queueDepth int, tagFormat schema.BuildF } imageName := schema.BuildImageName(tagFormat, function.Image, sha, branch) - fmt.Println("Push 0", tagFormat, imageName) fmt.Printf(aec.YellowF.Apply("[%d] > Pushing %s [%s]\n"), index, function.Name, imageName) if len(function.Image) == 0 { diff --git a/t1.yml b/t1.yml new file mode 100644 index 00000000..3c8a8ddc --- /dev/null +++ b/t1.yml @@ -0,0 +1,10 @@ +version: 1.0 +provider: + name: openfaas + gateway: http://127.0.0.1:8080 +functions: + t1: + lang: go + handler: ./t1 + image: ttl.sh/t1:latest + diff --git a/t1/go.mod b/t1/go.mod new file mode 100644 index 00000000..22137492 --- /dev/null +++ b/t1/go.mod @@ -0,0 +1,3 @@ +module handler/function + +go 1.19 diff --git a/t1/handler.go b/t1/handler.go new file mode 100644 index 00000000..407f9ab4 --- /dev/null +++ b/t1/handler.go @@ -0,0 +1,10 @@ +package function + +import ( + "fmt" +) + +// Handle a serverless request +func Handle(req []byte) string { + return fmt.Sprintf("Ciao, Go. You said: %s", string(req)) +}