-
Notifications
You must be signed in to change notification settings - Fork 5
/
fn.go
143 lines (121 loc) · 4.26 KB
/
fn.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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"bytes"
"context"
"strings"
"github.com/crossplane-contrib/function-shell/input/v1alpha1"
"github.com/crossplane/crossplane-runtime/pkg/errors"
"github.com/crossplane/crossplane-runtime/pkg/logging"
fnv1beta1 "github.com/crossplane/function-sdk-go/proto/v1beta1"
"github.com/crossplane/function-sdk-go/request"
"github.com/crossplane/function-sdk-go/response"
"github.com/keegancsmith/shell"
)
// Function returns whatever response you ask it to.
type Function struct {
fnv1beta1.UnimplementedFunctionRunnerServiceServer
log logging.Logger
}
// RunFunction runs the Function.
func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequest) (*fnv1beta1.RunFunctionResponse, error) {
f.log.Info("Running function", "tag", req.GetMeta().GetTag())
rsp := response.To(req, response.DefaultTTL)
in := &v1alpha1.Parameters{}
if err := request.GetInput(req, in); err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot get Function from input"))
return rsp, nil
}
oxr, err := request.GetObservedCompositeResource(req)
if err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot get observed composite resource from %T", req))
return rsp, nil
}
// Our input is an opaque object nested in a Composition. Let's validate it
if err := ValidateParameters(in, oxr); err != nil {
response.Fatal(rsp, errors.Wrap(err, "invalid Function input"))
return rsp, nil
}
log := f.log.WithValues(
"oxr-version", oxr.Resource.GetAPIVersion(),
"oxr-kind", oxr.Resource.GetKind(),
"oxr-name", oxr.Resource.GetName(),
)
dxr, err := request.GetDesiredCompositeResource(req)
if err != nil {
response.Fatal(rsp, errors.Wrap(err, "cannot get desired composite resource"))
return rsp, nil
}
dxr.Resource.SetAPIVersion(oxr.Resource.GetAPIVersion())
dxr.Resource.SetKind(oxr.Resource.GetKind())
stdoutField := in.StdoutField
if len(in.StdoutField) == 0 {
stdoutField = "status.atFunction.shell.stdout"
}
stderrField := in.StderrField
if len(in.StderrField) == 0 {
stderrField = "status.atFunction.shell.stderr"
}
shellCmd := ""
if len(in.ShellCommand) == 0 && len(in.ShellCommandField) == 0 {
log.Info("no shell command in in.ShellCommand nor in.ShellCommandField")
return rsp, nil
}
if len(in.ShellCommand) > 0 {
shellCmd = in.ShellCommand
}
// Prefer shell cmd from field over direct function input
if len(in.ShellCommandField) > 0 {
shellCmd = in.ShellCommandField
}
var shellEnvVars = make(map[string]string)
for _, envVar := range in.ShellEnvVars {
if envVar.ValueRef != "" {
envValue, err := fromValueRef(req, envVar.ValueRef)
if err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot process contents of valueRef %s", envVar.ValueRef))
return rsp, nil
}
shellEnvVars[envVar.Key] = envValue
} else {
shellEnvVars[envVar.Key] = envVar.Value
}
}
if len(in.ShellEnvVarsRef.Keys) > 0 {
shellEnvVars, err = addShellEnvVarsFromRef(in.ShellEnvVarsRef, shellEnvVars)
if err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot process contents of shellEnvVarsRef %s", in.ShellEnvVarsRef.Name))
return rsp, nil
}
}
var exportCmds string
//exportCmds = "export PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin;"
for k, v := range shellEnvVars {
exportCmds = exportCmds + "export " + k + "=\"" + v + "\";"
}
log.Info(shellCmd)
var stdout, stderr bytes.Buffer
cmd := shell.Commandf(exportCmds + shellCmd)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
err = cmd.Run()
if err != nil {
response.Fatal(rsp, errors.Wrapf(err, "shellCmd %s for %s failed", shellCmd, oxr.Resource.GetKind()))
return rsp, nil
}
out := strings.TrimSpace(stdout.String())
err = dxr.Resource.SetValue(stdoutField, out)
if err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot set field %s to %s for %s", stdoutField, out, oxr.Resource.GetKind()))
return rsp, nil
}
err = dxr.Resource.SetValue(stderrField, strings.TrimSpace(stderr.String()))
if err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot set field %s to %s for %s", stderrField, out, oxr.Resource.GetKind()))
return rsp, nil
}
if err := response.SetDesiredCompositeResource(rsp, dxr); err != nil {
response.Fatal(rsp, errors.Wrapf(err, "cannot set desired composite resources from %T", req))
return rsp, nil
}
return rsp, nil
}