Skip to content

Commit

Permalink
improve kpt fn render error messages (kptdev#1955)
Browse files Browse the repository at this point in the history
  • Loading branch information
natasha41575 authored and frankfarzan committed Jun 3, 2021
1 parent 8d5d2a7 commit 45d1803
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ exitCode: 1
image: gcr.io/kpt-fn/dne # non-existing image
args:
namespace: staging
stdErr: 'failed to check function image existence: function image "gcr.io/kpt-fn/dne" doesn''t exist'
stdErr: "error: function image \"gcr.io/kpt-fn/dne\" doesn't exist"
stdOut: |
[RUNNING] "gcr.io/kpt-fn/dne"
[FAIL] "gcr.io/kpt-fn/dne"
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,4 @@
# limitations under the License.

exitCode: 1
stdErr: "missing Resource metadata"
stdOut: |
[RUNNING] "gcr.io/kpt-fn/set-namespace:unstable"
[PASS] "gcr.io/kpt-fn/set-namespace:unstable"
stdErr: 'Error: invalid pipeline: function "gcr.io/kpt-fn/set-label:unstable": functionConfig must be a valid KRM resource with `apiVersion` and `kind` fields'
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

exitCode: 1
stdErr: 'failed to check function image existence: function image "gcr.io/kpt-fn/set-label1" doesn''t exist:'
stdErr: "Error: function image \"gcr.io/kpt-fn/set-label1\" doesn't exist"
stdOut: |
[RUNNING] "gcr.io/kpt-fn/set-namespace:unstable"
[PASS] "gcr.io/kpt-fn/set-namespace:unstable"
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
diff --git a/db/resources.yaml b/db/resources.yaml
index 10cd18b..a9dd224 100644
--- a/db/resources.yaml
+++ b/db/resources.yaml
@@ -11,17 +11,29 @@
# 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.
-
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
+ namespace: staging
+ labels:
+ tier: backend
spec:
replicas: 3
+ selector:
+ matchLabels:
+ tier: backend
+ template:
+ metadata:
+ labels:
+ tier: backend
---
apiVersion: custom.io/v1
kind: Custom
metadata:
name: custom
+ namespace: staging
+ labels:
+ tier: backend
spec:
image: nginx:1.2.3
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.expected
12 changes: 12 additions & 0 deletions e2e/testdata/fn-render/no-pipeline-in-subpackage/Kptfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: kpt.dev/v1alpha2
kind: Kptfile
metadata:
name: app
pipeline:
mutators:
- image: gcr.io/kpt-fn/set-namespace:unstable
configMap:
namespace: staging
- image: gcr.io/kpt-fn/set-label:unstable
configMap:
tier: backend
4 changes: 4 additions & 0 deletions e2e/testdata/fn-render/no-pipeline-in-subpackage/db/Kptfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: kpt.dev/v1alpha2
kind: Kptfile
metadata:
name: app
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,16 @@
# See the License for the specific language governing permissions and
# limitations under the License.

exitCode: 0
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
---
apiVersion: custom.io/v1
kind: Custom
metadata:
name: custom
spec:
image: nginx:1.2.3
4 changes: 2 additions & 2 deletions internal/fnruntime/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (f *ContainerFn) Run(reader io.Reader, writer io.Writer) error {
// output
err := f.prepareImage()
if err != nil {
return fmt.Errorf("failed to check function image existence: %w", err)
return err
}

errSink := bytes.Buffer{}
Expand Down Expand Up @@ -164,7 +164,7 @@ func (f *ContainerFn) prepareImage() error {
defer cancel()
cmd = exec.CommandContext(ctx, dockerBin, args...)
if _, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("function image %q doesn't exist: %w", f.Image, err)
return fmt.Errorf("function image %q doesn't exist", f.Image)
}
return nil
}
11 changes: 6 additions & 5 deletions pkg/api/kptfile/v1alpha2/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"regexp"
"strings"

"github.com/GoogleContainerTools/kpt/internal/errors"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

Expand All @@ -28,9 +27,8 @@ const (
)

func (kf *KptFile) Validate() error {
const op errors.Op = "v1alpha2.kptfile.validate"
if err := kf.Pipeline.validate(); err != nil {
return errors.E(op, fmt.Errorf("pipeline is not valid: %w", err))
return fmt.Errorf("invalid pipeline: %w", err)
}
// TODO: validate other fields
return nil
Expand All @@ -40,7 +38,6 @@ func (kf *KptFile) Validate() error {
// 'mutators' and 'validators' share same schema and
// they are valid if all functions in them are ALL valid.
func (p *Pipeline) validate() error {
const op errors.Op = "v1alpha2.pipeline.validate"
if p == nil {
return nil
}
Expand All @@ -51,7 +48,7 @@ func (p *Pipeline) validate() error {
f := fns[i]
err := f.validate()
if err != nil {
return errors.E(op, fmt.Errorf("function %q is invalid: %w", f.Image, err))
return fmt.Errorf("function %q: %w", f.Image, err)
}
}
return nil
Expand All @@ -74,6 +71,10 @@ func (f *Function) validate() error {
configFields = append(configFields, "configMap")
}
if !IsNodeZero(&f.Config) {
config := yaml.NewRNode(&f.Config)
if _, err := config.GetMeta(); err != nil {
return fmt.Errorf("functionConfig must be a valid KRM resource with `apiVersion` and `kind` fields")
}
configFields = append(configFields, "config")
}
if len(configFields) > 1 {
Expand Down

0 comments on commit 45d1803

Please sign in to comment.