forked from kptdev/porch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutableevaluator.go
More file actions
100 lines (86 loc) · 3.09 KB
/
Copy pathexecutableevaluator.go
File metadata and controls
100 lines (86 loc) · 3.09 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// Copyright 2022, 2026 The kpt Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package internal
import (
"bytes"
"context"
"fmt"
"os/exec"
kptfilev1 "github.com/kptdev/kpt/api/kptfile/v1"
"github.com/kptdev/kpt/pkg/fn"
"github.com/kptdev/porch/controllers/functionconfigs/reconciler"
pb "github.com/kptdev/porch/func/evaluator"
regclientref "github.com/regclient/regclient/types/ref"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/klog/v2"
)
type ExecutableEvaluatorOptions struct {
FunctionCacheDir string // Path to cached functions
}
type executableEvaluator struct {
// Fast-path function cache
FunctionConfigStore *reconciler.FunctionConfigStore
}
var _ Evaluator = &executableEvaluator{}
func NewExecutableEvaluator(FunctionConfigStore *reconciler.FunctionConfigStore) (Evaluator, error) {
return &executableEvaluator{
FunctionConfigStore: FunctionConfigStore,
}, nil
}
func (e *executableEvaluator) EvaluateFunction(ctx context.Context, req *pb.EvaluateFunctionRequest) (*pb.EvaluateFunctionResponse, error) {
var selectedBinary string
if req.Tag != "" {
ref, err := regclientref.New(req.Image)
if err != nil {
return nil, fmt.Errorf("failed to parse image %q as reference: %w", req.Image, err)
}
ref.Tag = ""
ref.Digest = ""
req.Image = ref.CommonName()
binary, exists := e.FunctionConfigStore.GetBinaryFromCacheByConstraint(req.Image, req.Tag)
if !exists {
return nil, &fn.NotFoundError{
Function: kptfilev1.Function{Image: req.Image},
}
}
selectedBinary = binary
} else {
klog.V(2).Infof("Image tag is empty, using the image with explicit tag: %q", req.Image)
binary, exists := e.FunctionConfigStore.GetBinaryFromCache(req.Image)
if !exists {
return nil, &fn.NotFoundError{
Function: kptfilev1.Function{Image: req.Image},
}
}
selectedBinary = binary
}
klog.Infof("Evaluating %q in executable mode", req.Image)
var stdout, stderr bytes.Buffer
cmd := exec.CommandContext(ctx, selectedBinary) // #nosec G204 -- variables controlled internally
cmd.Stdin = bytes.NewReader(req.ResourceList)
cmd.Stdout = &stdout
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
klog.V(4).Infof("Resource List: %s", req.ResourceList)
return nil, status.Errorf(codes.Internal, "Failed to execute function %q: %s (%s)", req.Image, err, stderr.String())
}
outbytes := stdout.Bytes()
klog.Infof("Evaluated %q: stdout %d bytes, stderr:\n%s", req.Image, len(outbytes), stderr.String())
// TODO: include stderr in the output?
return &pb.EvaluateFunctionResponse{
ResourceList: outbytes,
Log: stderr.Bytes(),
}, nil
}