-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathenv.go
More file actions
85 lines (78 loc) · 2.65 KB
/
env.go
File metadata and controls
85 lines (78 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"encoding/json"
"fmt"
"os"
"regexp"
"github.com/crossplane-contrib/function-shell/input/v1alpha1"
"github.com/crossplane/crossplane-runtime/v2/pkg/errors"
"github.com/crossplane/crossplane-runtime/v2/pkg/fieldpath"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
fnv1 "github.com/crossplane/function-sdk-go/proto/v1"
"github.com/crossplane/function-sdk-go/request"
"github.com/crossplane/function-sdk-go/resource"
)
func addShellEnvVarsFromRef(envVarsRef v1alpha1.ShellEnvVarsRef, shellEnvVars map[string]string) (map[string]string, error) {
var envVarsData map[string]string
envVars := os.Getenv(envVarsRef.Name)
if err := json.Unmarshal([]byte(envVars), &envVarsData); err != nil {
return shellEnvVars, err
}
for _, key := range envVarsRef.Keys {
shellEnvVars[key] = envVarsData[key]
}
return shellEnvVars, nil
}
func fromFieldRef(req *fnv1.RunFunctionRequest, fieldRef v1alpha1.FieldRef) (string, error) {
if fieldRef.Path == "" {
return "", errors.New("path must be set")
}
// Check for context key presence and capture context key and path
contextRegex := regexp.MustCompile(`^context\[(.+?)].(.+)$`)
if match := contextRegex.FindStringSubmatch(fieldRef.Path); match != nil {
if v, ok := request.GetContextKey(req, match[1]); ok {
context := &unstructured.Unstructured{}
if err := resource.AsObject(v.GetStructValue(), context); err != nil {
return "", errors.Wrapf(err, "cannot convert context to %s", v)
}
value, err := fieldpath.Pave(context.Object).GetValue(match[2])
if err != nil {
switch fieldRef.Policy {
case v1alpha1.FieldRefPolicyOptional:
return fieldRef.DefaultValue, nil
case v1alpha1.FieldRefPolicyRequired:
fallthrough
default:
return "", errors.Wrap(err, "cannot get context value")
}
}
return fmt.Sprintf("%v", value), nil
}
} else {
oxr, err := request.GetObservedCompositeResource(req)
if err != nil {
return "", errors.Wrapf(err, "cannot get observed composite resource from %T", req)
}
value, err := oxr.Resource.GetValue(fieldRef.Path)
if err != nil {
switch fieldRef.Policy {
case v1alpha1.FieldRefPolicyOptional:
return fieldRef.DefaultValue, nil
case v1alpha1.FieldRefPolicyRequired:
fallthrough
default:
return "", errors.Wrap(err, "cannot get observed composite value")
}
}
return fmt.Sprintf("%v", value), nil
}
return fieldRef.DefaultValue, nil
}
// a valueRef behaves like a fieldRef with a Required Policy.
func fromValueRef(req *fnv1.RunFunctionRequest, path string) (string, error) {
return fromFieldRef(
req, v1alpha1.FieldRef{
Path: path,
Policy: v1alpha1.FieldRefPolicyRequired,
})
}