Skip to content

Commit

Permalink
Extract container runtime & fn error resolver (#1877)
Browse files Browse the repository at this point in the history
* move fn to separate resolver

* extract container runtime
  • Loading branch information
Shell32-Natsu committed May 3, 2021
1 parent 10d32a3 commit 631a753
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 28 deletions.
6 changes: 3 additions & 3 deletions internal/cmdrender/pipeline_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"os"
"path/filepath"

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

cfn := &runtime.ContainerFn{
cfn := &fnruntime.ContainerFn{
Path: pkgPath,
Image: f.Image,
Ctx: ctx,
}

cfnw := &runtime.ContainerFnWrapper{
cfnw := &fnruntime.ContainerFnWrapper{
Fn: cfn,
}

Expand Down
45 changes: 45 additions & 0 deletions internal/errors/resolver/fn.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright 2021 Google LLC
//
// 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 resolver

import (
goerrors "errors"

"github.com/GoogleContainerTools/kpt/internal/errors"
)

//nolint:gochecknoinits
func init() {
AddErrorResolver(&fnExecErrorResolver{})
}

// gitExecErrorResolver is an implementation of the ErrorResolver interface
// that can produce error messages for errors of the FnExecError type.
type fnExecErrorResolver struct{}

func (*fnExecErrorResolver) Resolve(err error) (ResolvedResult, bool) {
kioErr := errors.UnwrapKioError(err)

var fnErr *errors.FnExecError
if !goerrors.As(kioErr, &fnErr) {
return ResolvedResult{}, false
}
// TODO: write complete details to a file

return ResolvedResult{
Message: fnErr.String(),
ExitCode: 1,
}, true
}
21 changes: 0 additions & 21 deletions internal/errors/resolver/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,12 @@ import (
"fmt"
"strings"

"github.com/GoogleContainerTools/kpt/internal/errors"
"github.com/GoogleContainerTools/kpt/internal/gitutil"
)

//nolint:gochecknoinits
func init() {
AddErrorResolver(&gitExecErrorResolver{})
AddErrorResolver(&fnExecErrorResolver{})
}

const (
Expand Down Expand Up @@ -111,22 +109,3 @@ func (*gitExecErrorResolver) Resolve(err error) (ResolvedResult, bool) {
ExitCode: 1,
}, true
}

// gitExecErrorResolver is an implementation of the ErrorResolver interface
// that can produce error messages for errors of the FnExecError type.
type fnExecErrorResolver struct{}

func (*fnExecErrorResolver) Resolve(err error) (ResolvedResult, bool) {
kioErr := errors.UnwrapKioError(err)

var fnErr *errors.FnExecError
if !goerrors.As(kioErr, &fnErr) {
return ResolvedResult{}, false
}
// TODO: write complete details to a file

return ResolvedResult{
Message: fnErr.String(),
ExitCode: 1,
}, true
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package runtime
package fnruntime

import (
"bytes"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package runtime_test
package fnruntime_test

import (
"bytes"
"context"
"testing"

"github.com/GoogleContainerTools/kpt/internal/cmdrender/runtime"
"github.com/GoogleContainerTools/kpt/internal/fnruntime"
"github.com/GoogleContainerTools/kpt/internal/printer"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -50,7 +50,7 @@ func TestContainerFn(t *testing.T) {
ctx := context.Background()
t.Run(tt.name, func(t *testing.T) {
outBuff, errBuff := &bytes.Buffer{}, &bytes.Buffer{}
instance := runtime.ContainerFn{
instance := fnruntime.ContainerFn{
Ctx: printer.WithContext(ctx, printer.New(outBuff, errBuff)),
Image: tt.image,
}
Expand Down

0 comments on commit 631a753

Please sign in to comment.