Skip to content

Commit

Permalink
proper error message for fn eval/render when there are non-KRM resour…
Browse files Browse the repository at this point in the history
…ces (#2198)
  • Loading branch information
natasha41575 committed Jun 14, 2021
1 parent 000c9b3 commit 0e00495
Show file tree
Hide file tree
Showing 18 changed files with 214 additions and 1 deletion.
22 changes: 22 additions & 0 deletions e2e/testdata/fn-eval/non-krm-resource/.expected/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# 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.

testType: eval
image: gcr.io/kpt-fn/set-namespace:v0.1.3
args:
namespace: staging
exitCode: 1
stdErr: |
[FAIL] "gcr.io/kpt-fn/set-namespace:v0.1.3"
error: input resource list must contain only KRM resources: nonkrm.yaml: resource must have `apiVersion`
1 change: 1 addition & 0 deletions e2e/testdata/fn-eval/non-krm-resource/.krmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.expected
14 changes: 14 additions & 0 deletions e2e/testdata/fn-eval/non-krm-resource/nonkrm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 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.
Non-krm-resource: true
26 changes: 26 additions & 0 deletions e2e/testdata/fn-eval/non-krm-resource/resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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.
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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.

exitCode: 1
stdErr: "Error: input resource list must contain only KRM resources: non-krm.yaml: resource must have `apiVersion`"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.expected
4 changes: 4 additions & 0 deletions e2e/testdata/fn-render/non-krm-resource-no-pipeline/Kptfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
apiVersion: kpt.dev/v1alpha2
kind: Kptfile
metadata:
name: app
14 changes: 14 additions & 0 deletions e2e/testdata/fn-render/non-krm-resource-no-pipeline/non-krm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 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.
Non-krm-resource: true
26 changes: 26 additions & 0 deletions e2e/testdata/fn-render/non-krm-resource-no-pipeline/resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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.
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
16 changes: 16 additions & 0 deletions e2e/testdata/fn-render/non-krm-resource/.expected/config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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.

exitCode: 1
stdErr: "Error: input resource list must contain only KRM resources: non-krm.yaml: resource must have `apiVersion`"
1 change: 1 addition & 0 deletions e2e/testdata/fn-render/non-krm-resource/.krmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.expected
12 changes: 12 additions & 0 deletions e2e/testdata/fn-render/non-krm-resource/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:v0.1.3
configMap:
namespace: staging
- image: gcr.io/kpt-fn/set-labels:v0.1.4
configMap:
tier: backend
14 changes: 14 additions & 0 deletions e2e/testdata/fn-render/non-krm-resource/non-krm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# 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.
Non-krm-resource: true
26 changes: 26 additions & 0 deletions e2e/testdata/fn-render/non-krm-resource/resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# 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.
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
3 changes: 3 additions & 0 deletions internal/cmdrender/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,9 @@ func (pn *pkgNode) runPipeline(ctx context.Context, hctx *hydrationContext, inpu
}

if pl.IsEmpty() {
if err := kptfilev1alpha2.AreKRM(input); err != nil {
return nil, fmt.Errorf("input resource list must contain only KRM resources: %s", err.Error())
}
return input, nil
}

Expand Down
8 changes: 7 additions & 1 deletion internal/fnruntime/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,18 @@ func (fr *FunctionRunner) Filter(input []*yaml.RNode) (output []*yaml.RNode, err
// do executes the kpt function and returns the modified resources.
// fnResult is updated with the function results returned by the kpt function.
func (fr *FunctionRunner) do(input []*yaml.RNode) (output []*yaml.RNode, err error) {
fnResult := fr.fnResult
if krmErr := kptfilev1alpha2.AreKRM(input); krmErr != nil {
return output, fmt.Errorf("input resource list must contain only KRM resources: %s", krmErr.Error())
}

fnResult := fr.fnResult
output, err = fr.filter.Filter(input)
if pathErr := enforcePathInvariants(output); pathErr != nil {
return output, pathErr
}
if krmErr := kptfilev1alpha2.AreKRM(output); krmErr != nil {
return output, fmt.Errorf("output resource list must contain only KRM resources: %s", krmErr.Error())
}

// parse the results irrespective of the success/failure of fn exec
resultErr := parseStructuredResult(fr.filter.Results, fnResult)
Expand Down
11 changes: 11 additions & 0 deletions pkg/api/kptfile/v1alpha2/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/GoogleContainerTools/kpt/internal/types"
"sigs.k8s.io/kustomize/kyaml/kio"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

Expand Down Expand Up @@ -183,6 +184,16 @@ func IsKRM(n *yaml.RNode) error {
return nil
}

func AreKRM(nodes []*yaml.RNode) error {
for i := range nodes {
if err := IsKRM(nodes[i]); err != nil {
path, _, _ := kioutil.GetFileAnnotations(nodes[i])
return fmt.Errorf("%s: %s", path, err.Error())
}
}
return nil
}

// ValidateError is the error returned when validation fails.
type ValidateError struct {
// Field is the field that causes error
Expand Down

0 comments on commit 0e00495

Please sign in to comment.