Skip to content

Commit

Permalink
Add --tag digest flag to faas-cli generate
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Ellis (OpenFaaS Ltd) <[email protected]>
  • Loading branch information
alexellis committed Jun 28, 2023
1 parent 3af5f4d commit cfbfce6
Show file tree
Hide file tree
Showing 7 changed files with 87 additions and 19 deletions.
15 changes: 15 additions & 0 deletions builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
48 changes: 31 additions & 17 deletions commands/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)")
Expand All @@ -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
}

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
}
Expand All @@ -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)
Expand All @@ -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)

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down
19 changes: 18 additions & 1 deletion commands/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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 {
Expand Down
1 change: 0 additions & 1 deletion commands/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions t1.yml
Original file line number Diff line number Diff line change
@@ -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

3 changes: 3 additions & 0 deletions t1/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module handler/function

go 1.19
10 changes: 10 additions & 0 deletions t1/handler.go
Original file line number Diff line number Diff line change
@@ -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))
}

0 comments on commit cfbfce6

Please sign in to comment.