Skip to content

Commit 631a753

Browse files
Extract container runtime & fn error resolver (#1877)
* move fn to separate resolver * extract container runtime
1 parent 10d32a3 commit 631a753

File tree

5 files changed

+52
-28
lines changed

5 files changed

+52
-28
lines changed

internal/cmdrender/pipeline_function.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ import (
2323
"os"
2424
"path/filepath"
2525

26-
"github.com/GoogleContainerTools/kpt/internal/cmdrender/runtime"
2726
"github.com/GoogleContainerTools/kpt/internal/errors"
27+
"github.com/GoogleContainerTools/kpt/internal/fnruntime"
2828
"github.com/GoogleContainerTools/kpt/internal/types"
2929
kptfilev1alpha2 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1alpha2"
3030
"sigs.k8s.io/kustomize/kyaml/fn/runtime/runtimeutil"
@@ -40,13 +40,13 @@ func newFnRunner(ctx context.Context, f *kptfilev1alpha2.Function, pkgPath types
4040
return nil, err
4141
}
4242

43-
cfn := &runtime.ContainerFn{
43+
cfn := &fnruntime.ContainerFn{
4444
Path: pkgPath,
4545
Image: f.Image,
4646
Ctx: ctx,
4747
}
4848

49-
cfnw := &runtime.ContainerFnWrapper{
49+
cfnw := &fnruntime.ContainerFnWrapper{
5050
Fn: cfn,
5151
}
5252

internal/errors/resolver/fn.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package resolver
16+
17+
import (
18+
goerrors "errors"
19+
20+
"github.com/GoogleContainerTools/kpt/internal/errors"
21+
)
22+
23+
//nolint:gochecknoinits
24+
func init() {
25+
AddErrorResolver(&fnExecErrorResolver{})
26+
}
27+
28+
// gitExecErrorResolver is an implementation of the ErrorResolver interface
29+
// that can produce error messages for errors of the FnExecError type.
30+
type fnExecErrorResolver struct{}
31+
32+
func (*fnExecErrorResolver) Resolve(err error) (ResolvedResult, bool) {
33+
kioErr := errors.UnwrapKioError(err)
34+
35+
var fnErr *errors.FnExecError
36+
if !goerrors.As(kioErr, &fnErr) {
37+
return ResolvedResult{}, false
38+
}
39+
// TODO: write complete details to a file
40+
41+
return ResolvedResult{
42+
Message: fnErr.String(),
43+
ExitCode: 1,
44+
}, true
45+
}

internal/errors/resolver/git.go

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ import (
1919
"fmt"
2020
"strings"
2121

22-
"github.com/GoogleContainerTools/kpt/internal/errors"
2322
"github.com/GoogleContainerTools/kpt/internal/gitutil"
2423
)
2524

2625
//nolint:gochecknoinits
2726
func init() {
2827
AddErrorResolver(&gitExecErrorResolver{})
29-
AddErrorResolver(&fnExecErrorResolver{})
3028
}
3129

3230
const (
@@ -111,22 +109,3 @@ func (*gitExecErrorResolver) Resolve(err error) (ResolvedResult, bool) {
111109
ExitCode: 1,
112110
}, true
113111
}
114-
115-
// gitExecErrorResolver is an implementation of the ErrorResolver interface
116-
// that can produce error messages for errors of the FnExecError type.
117-
type fnExecErrorResolver struct{}
118-
119-
func (*fnExecErrorResolver) Resolve(err error) (ResolvedResult, bool) {
120-
kioErr := errors.UnwrapKioError(err)
121-
122-
var fnErr *errors.FnExecError
123-
if !goerrors.As(kioErr, &fnErr) {
124-
return ResolvedResult{}, false
125-
}
126-
// TODO: write complete details to a file
127-
128-
return ResolvedResult{
129-
Message: fnErr.String(),
130-
ExitCode: 1,
131-
}, true
132-
}

internal/cmdrender/runtime/container.go renamed to internal/fnruntime/container.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package runtime
15+
package fnruntime
1616

1717
import (
1818
"bytes"

internal/cmdrender/runtime/container_test.go renamed to internal/fnruntime/container_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@
1414
// See the License for the specific language governing permissions and
1515
// limitations under the License.
1616

17-
package runtime_test
17+
package fnruntime_test
1818

1919
import (
2020
"bytes"
2121
"context"
2222
"testing"
2323

24-
"github.com/GoogleContainerTools/kpt/internal/cmdrender/runtime"
24+
"github.com/GoogleContainerTools/kpt/internal/fnruntime"
2525
"github.com/GoogleContainerTools/kpt/internal/printer"
2626
"github.com/stretchr/testify/assert"
2727
)
@@ -50,7 +50,7 @@ func TestContainerFn(t *testing.T) {
5050
ctx := context.Background()
5151
t.Run(tt.name, func(t *testing.T) {
5252
outBuff, errBuff := &bytes.Buffer{}, &bytes.Buffer{}
53-
instance := runtime.ContainerFn{
53+
instance := fnruntime.ContainerFn{
5454
Ctx: printer.WithContext(ctx, printer.New(outBuff, errBuff)),
5555
Image: tt.image,
5656
}

0 commit comments

Comments
 (0)