Skip to content

Commit

Permalink
On cluster build: support Build Envs (#1174)
Browse files Browse the repository at this point in the history
* On cluster build: support Build Envs

Signed-off-by: Zbynek Roubalik <zroubalik@gmail.com>

* add test

Signed-off-by: Zbynek Roubalik <zroubalik@gmail.com>

Signed-off-by: Zbynek Roubalik <zroubalik@gmail.com>
  • Loading branch information
zroubalik authored Aug 19, 2022
1 parent 62b7232 commit bfdbdbe
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 15 deletions.
44 changes: 30 additions & 14 deletions function_envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,36 @@ func (e Env) String() string {
return ""
}

// KeyValuePair returns a string representation of the Env field in form NAME=VALUE
// if NAME is not defined for an Env, empty string is returned
func (e Env) KeyValuePair() string {
keyValue := ""
if e.Name != nil {
value := ""
if e.Value != nil {
value = *e.Value
}

keyValue = fmt.Sprintf("%s=%s", *e.Name, value)
}

return keyValue
}

// ValidateEnvs checks that input Envs are correct and contain all necessary fields.
// Returns array of error messages, empty if no errors are found
//
// Allowed settings:
// - name: EXAMPLE1 # ENV directly from a value
// value: value1
// - name: EXAMPLE2 # ENV from the local ENV var
// value: {{ env:MY_ENV }}
// - name: EXAMPLE3
// value: {{ secret:secretName:key }} # ENV from a key in secret
// - value: {{ secret:secretName }} # all key-pair values from secret are set as ENV
// - name: EXAMPLE4
// value: {{ configMap:configMapName:key }} # ENV from a key in configMap
// - value: {{ configMap:configMapName }} # all key-pair values from configMap are set as ENV
// - name: EXAMPLE1 # ENV directly from a value
// value: value1
// - name: EXAMPLE2 # ENV from the local ENV var
// value: {{ env:MY_ENV }}
// - name: EXAMPLE3
// value: {{ secret:secretName:key }} # ENV from a key in secret
// - value: {{ secret:secretName }} # all key-pair values from secret are set as ENV
// - name: EXAMPLE4
// value: {{ configMap:configMapName:key }} # ENV from a key in configMap
// - value: {{ configMap:configMapName }} # all key-pair values from configMap are set as ENV
func ValidateEnvs(envs []Env) (errors []string) {
for i, env := range envs {
if env.Name == nil && env.Value == nil {
Expand Down Expand Up @@ -94,10 +110,10 @@ func ValidateEnvs(envs []Env) (errors []string) {
// Returns array of error messages, empty if no errors are found
//
// Allowed settings:
// - name: EXAMPLE1 # ENV directly from a value
// value: value1
// - name: EXAMPLE2 # ENV from the local ENV var
// value: {{ env:MY_ENV }}
// - name: EXAMPLE1 # ENV directly from a value
// value: value1
// - name: EXAMPLE2 # ENV from the local ENV var
// value: {{ env:MY_ENV }}
func ValidateBuildEnvs(envs []Env) (errors []string) {
for i, env := range envs {
if env.Name == nil && env.Value == nil {
Expand Down
50 changes: 49 additions & 1 deletion function_envs_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

package function

import "testing"
import (
"fmt"
"testing"
)

func Test_validateBuildEnvs(t *testing.T) {

Expand Down Expand Up @@ -609,3 +612,48 @@ func Test_validateEnvs(t *testing.T) {
}

}

func Test_KeyValuePair(t *testing.T) {
name := "name"
value := "value"

tests := []struct {
name string
env Env
want string
}{
{
"name & value",
Env{

Name: &name,
Value: &value,
},
fmt.Sprintf("%s=%s", name, value),
},
{
"name only",
Env{

Name: &name,
},
fmt.Sprintf("%s=", name),
},
{
"value only",
Env{

Value: &value,
},
"",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.env.KeyValuePair(); got != tt.want {
t.Errorf("KeyValuePair() for env = %v\n got %q errors but want %q", tt.env, got, tt.want)
}
})
}
}
21 changes: 21 additions & 0 deletions pipelines/tekton/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ func generatePipeline(f fn.Function, labels map[string]string) *pplnv1beta1.Pipe
Name: "builderImage",
Description: "Buildpacks builder image to be used",
},
{
Name: "buildEnvs",
Description: "Environment variables to set during build time",
Type: "array",
},
}

workspaces := []pplnv1beta1.PipelineWorkspaceDeclaration{
Expand Down Expand Up @@ -95,6 +100,18 @@ func generatePipelineRun(f fn.Function, labels map[string]string) *pplnv1beta1.P
contextDir = *f.Git.ContextDir
}

buildEnvs := &pplnv1beta1.ArrayOrString{
Type: pplnv1beta1.ParamTypeArray,
ArrayVal: []string{},
}
if len(f.BuildEnvs) > 0 {
var envs []string
for _, e := range f.BuildEnvs {
envs = append(envs, e.KeyValuePair())
}
buildEnvs = pplnv1beta1.NewArrayOrString(envs[0], envs[1:]...)
}

return &pplnv1beta1.PipelineRun{
ObjectMeta: v1.ObjectMeta{
GenerateName: fmt.Sprintf("%s-run-", getPipelineName(f)),
Expand Down Expand Up @@ -127,6 +144,10 @@ func generatePipelineRun(f fn.Function, labels map[string]string) *pplnv1beta1.P
Name: "builderImage",
Value: *pplnv1beta1.NewArrayOrString(getBuilderImage(f)),
},
{
Name: "buildEnvs",
Value: *buildEnvs,
},
},

Workspaces: []pplnv1beta1.WorkspaceBinding{
Expand Down
4 changes: 4 additions & 0 deletions pipelines/tekton/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func taskBuild(runAfter string) pplnv1beta1.PipelineTask {
{Name: "APP_IMAGE", Value: *pplnv1beta1.NewArrayOrString("$(params.imageName)")},
{Name: "SOURCE_SUBPATH", Value: *pplnv1beta1.NewArrayOrString("$(params.contextDir)")},
{Name: "BUILDER_IMAGE", Value: *pplnv1beta1.NewArrayOrString("$(params.builderImage)")},
{Name: "ENV_VARS", Value: pplnv1beta1.ArrayOrString{
Type: pplnv1beta1.ParamTypeArray,
ArrayVal: []string{"$(params.buildEnvs[*])"},
}},
},
}
}
Expand Down

0 comments on commit bfdbdbe

Please sign in to comment.