Skip to content

Commit

Permalink
Add fprocess to generated CRDs and fix indentation
Browse files Browse the repository at this point in the history
Indentation changed to four spaces when upgrading to the v3
YAML paser, this reverts to 2 spaces.

The fprocess variable in the store was missing from generated
function YAML files with faas-cli generate --from-store - this
has now been fixed and an env will be emitted when there is
no existing conflicting definition in the store's manifest.

Tested before/after - env now can be deployed, previously it
couldn't be.

Signed-off-by: Alex Ellis (OpenFaaS Ltd) <alexellis2@gmail.com>
  • Loading branch information
alexellis committed Mar 21, 2023
1 parent 2fd2848 commit c337049
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 93 deletions.
21 changes: 18 additions & 3 deletions commands/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package commands

import (
"bytes"
"fmt"
"os"
"sort"
Expand Down Expand Up @@ -131,6 +132,16 @@ func runGenerate(cmd *cobra.Command, args []string) error {

allAnnotations := util.MergeMap(item.Annotations, annotations)

if len(item.Fprocess) > 0 {
if item.Environment == nil {
item.Environment = make(map[string]string)
}

if _, ok := item.Environment["fprocess"]; !ok {
item.Environment["fprocess"] = item.Fprocess
}
}

services.Functions[item.Name] = stack.Function{
Name: item.Name,
Image: item.Images[desiredArch],
Expand Down Expand Up @@ -223,11 +234,15 @@ func generateCRDYAML(services stack.Services, format schema.BuildFormat, apiVers
Spec: spec,
}

//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
}

objectString := buff.String()

objectsString += "---\n" + string(objectString)
}
}
Expand Down
174 changes: 87 additions & 87 deletions commands/generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,23 @@ var generateTestcases = []struct {
Name: "Specified Namespace and API Version",
Input: `
provider:
name: openfaas
gateway: http://127.0.0.1:8080
network: "func_functions"
name: openfaas
gateway: http://127.0.0.1:8080
network: "func_functions"
functions:
url-ping:
lang: python
handler: ./sample/url-ping
image: alexellis/faas-url-ping:0.2`,
lang: python
handler: ./sample/url-ping
image: alexellis/faas-url-ping:0.2`,
Output: []string{`---
apiVersion: openfaas.com/v1
kind: Function
metadata:
name: url-ping
namespace: openfaas-fn
name: url-ping
namespace: openfaas-fn
spec:
name: url-ping
image: alexellis/faas-url-ping:0.2
name: url-ping
image: alexellis/faas-url-ping:0.2
`},
Format: schema.DefaultFormat,
APIVersion: "openfaas.com/v1",
Expand All @@ -54,27 +54,27 @@ spec:
Name: "Annotation present",
Input: `
provider:
name: openfaas
gateway: http://127.0.0.1:8080
name: openfaas
gateway: http://127.0.0.1:8080
functions:
url-ping:
lang: python
handler: ./sample/url-ping
image: alexellis/faas-url-ping:0.2
annotations:
com.scale.zero: 1
lang: python
handler: ./sample/url-ping
image: alexellis/faas-url-ping:0.2
annotations:
com.scale.zero: 1
`,
Output: []string{`---
apiVersion: openfaas.com/v1
kind: Function
metadata:
name: url-ping
namespace: openfaas-fn
name: url-ping
namespace: openfaas-fn
spec:
name: url-ping
image: alexellis/faas-url-ping:0.2
annotations:
com.scale.zero: "1"
name: url-ping
image: alexellis/faas-url-ping:0.2
annotations:
com.scale.zero: "1"
`},
Format: schema.DefaultFormat,
APIVersion: "openfaas.com/v1",
Expand All @@ -86,22 +86,22 @@ spec:
Name: "Blank namespace",
Input: `
provider:
name: openfaas
gateway: http://127.0.0.1:8080
network: "func_functions"
name: openfaas
gateway: http://127.0.0.1:8080
network: "func_functions"
functions:
url-ping:
lang: python
handler: ./sample/url-ping
image: alexellis/faas-url-ping:0.2`,
lang: python
handler: ./sample/url-ping
image: alexellis/faas-url-ping:0.2`,
Output: []string{`---
apiVersion: openfaas.com/v1
kind: Function
metadata:
name: url-ping
name: url-ping
spec:
name: url-ping
image: alexellis/faas-url-ping:0.2
name: url-ping
image: alexellis/faas-url-ping:0.2
`},
Format: schema.DefaultFormat,
APIVersion: "openfaas.com/v1",
Expand All @@ -113,23 +113,23 @@ spec:
Name: "BranchAndSHA Image format",
Input: `
provider:
name: openfaas
gateway: http://127.0.0.1:8080
network: "func_functions"
name: openfaas
gateway: http://127.0.0.1:8080
network: "func_functions"
functions:
url-ping:
lang: python
handler: ./sample/url-ping
image: alexellis/faas-url-ping:0.2`,
lang: python
handler: ./sample/url-ping
image: alexellis/faas-url-ping:0.2`,
Output: []string{`---
apiVersion: openfaas.com/v1
kind: Function
metadata:
name: url-ping
namespace: openfaas-function
name: url-ping
namespace: openfaas-function
spec:
name: url-ping
image: alexellis/faas-url-ping:0.2-master-6bgf36qd
name: url-ping
image: alexellis/faas-url-ping:0.2-master-6bgf36qd
`},
Format: schema.BranchAndSHAFormat,
APIVersion: "openfaas.com/v1",
Expand All @@ -141,60 +141,60 @@ spec:
Name: "Multiple functions",
Input: `
provider:
name: openfaas
gateway: http://127.0.0.1:8080
network: "func_functions"
name: openfaas
gateway: http://127.0.0.1:8080
network: "func_functions"
functions:
url-ping:
lang: python
handler: ./sample/url-ping
image: alexellis/faas-url-ping:0.2
lang: python
handler: ./sample/url-ping
image: alexellis/faas-url-ping:0.2
astronaut-finder:
lang: python3
handler: ./astronaut-finder
image: astronaut-finder
environment:
write_debug: true`,
lang: python3
handler: ./astronaut-finder
image: astronaut-finder
environment:
write_debug: true`,
Output: []string{`---
apiVersion: openfaas.com/v2alpha2
kind: Function
metadata:
name: url-ping
namespace: openfaas-fn
name: url-ping
namespace: openfaas-fn
spec:
name: url-ping
image: alexellis/faas-url-ping:0.2
name: url-ping
image: alexellis/faas-url-ping:0.2
---
apiVersion: openfaas.com/v2alpha2
kind: Function
metadata:
name: astronaut-finder
namespace: openfaas-fn
name: astronaut-finder
namespace: openfaas-fn
spec:
name: astronaut-finder
image: astronaut-finder:latest
environment:
write_debug: "true"
name: astronaut-finder
image: astronaut-finder:latest
environment:
write_debug: "true"
`, `---
apiVersion: openfaas.com/v2alpha2
kind: Function
metadata:
name: astronaut-finder
namespace: openfaas-fn
name: astronaut-finder
namespace: openfaas-fn
spec:
name: astronaut-finder
image: astronaut-finder:latest
environment:
write_debug: "true"
name: astronaut-finder
image: astronaut-finder:latest
environment:
write_debug: "true"
---
apiVersion: openfaas.com/v2alpha2
kind: Function
metadata:
name: url-ping
namespace: openfaas-fn
name: url-ping
namespace: openfaas-fn
spec:
name: url-ping
image: alexellis/faas-url-ping:0.2
name: url-ping
image: alexellis/faas-url-ping:0.2
`},
Format: schema.DefaultFormat,
APIVersion: "openfaas.com/v2alpha2",
Expand All @@ -206,24 +206,24 @@ spec:
Name: "Read-only root filesystem",
Input: `
provider:
name: openfaas
gateway: http://127.0.0.1:8080
name: openfaas
gateway: http://127.0.0.1:8080
functions:
url-ping:
lang: python
handler: ./sample/url-ping
image: alexellis/faas-url-ping:0.2
readonly_root_filesystem: true`,
lang: python
handler: ./sample/url-ping
image: alexellis/faas-url-ping:0.2
readonly_root_filesystem: true`,
Output: []string{`---
apiVersion: openfaas.com/v1
kind: Function
metadata:
name: url-ping
namespace: openfaas-fn
name: url-ping
namespace: openfaas-fn
spec:
name: url-ping
image: alexellis/faas-url-ping:0.2
readOnlyRootFilesystem: true
name: url-ping
image: alexellis/faas-url-ping:0.2
readOnlyRootFilesystem: true
`},
Format: schema.DefaultFormat,
APIVersion: "openfaas.com/v1",
Expand Down Expand Up @@ -328,7 +328,7 @@ var generateOrderedTestcases = []struct {
provider:
name: openfaas
gateway: http://127.0.0.1:8080
network: "func_functions"
network: "func_functions"
functions:
fn1:
lang: python3
Expand Down Expand Up @@ -385,7 +385,7 @@ functions:
provider:
name: openfaas
gateway: http://127.0.0.1:8080
network: "func_functions"
network: "func_functions"
functions:
fn3:
lang: python3
Expand Down Expand Up @@ -442,7 +442,7 @@ functions:
provider:
name: openfaas
gateway: http://127.0.0.1:8080
network: "func_functions"
network: "func_functions"
functions:
fn3:
lang: python3
Expand Down
9 changes: 6 additions & 3 deletions config/config_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package config

import (
"bytes"
"encoding/base64"

"fmt"
Expand Down Expand Up @@ -153,12 +154,14 @@ func (configFile *ConfigFile) save() error {
}
defer file.Close()

data, err := yaml.Marshal(configFile)
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(&configFile); err != nil {
return err
}

_, err = file.Write(data)
_, err = file.Write(buff.Bytes())
return err
}

Expand Down

0 comments on commit c337049

Please sign in to comment.