-
Notifications
You must be signed in to change notification settings - Fork 5
/
env.go
60 lines (53 loc) · 1.95 KB
/
env.go
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
package main
import (
"encoding/json"
"fmt"
"os"
"regexp"
"github.com/crossplane-contrib/function-shell/input/v1alpha1"
"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/fieldpath"
fnv1beta1 "github.com/crossplane/function-sdk-go/proto/v1beta1"
"github.com/crossplane/function-sdk-go/request"
"github.com/crossplane/function-sdk-go/resource"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)
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 fromValueRef(req *fnv1beta1.RunFunctionRequest, path string) (string, error) {
// Check for context key presence and capture context key and path
contextRegex := regexp.MustCompile(`^context\[(.+?)].(.+)$`)
if match := contextRegex.FindStringSubmatch(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 {
return "", errors.Wrapf(err, "cannot get context value at %s", match[2])
}
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(path)
if err != nil {
return "", errors.Wrapf(err, "cannot get observed composite value at %s", path)
}
return fmt.Sprintf("%v", value), nil
}
return "", nil
}