Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Obj null check to avoid failure on non-typed character like NULL as value for array type key/object #598

Merged
merged 4 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pkg/kapp/resources/mod_object_ref_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ func (t ObjectRefSetMod) apply(obj interface{}, path Path) error {
case part.ArrayIndex != nil:
switch {
case part.ArrayIndex.All != nil:
if obj == nil {
return nil
}
typedObj, ok := obj.([]interface{})
if !ok {
return fmt.Errorf("Unexpected non-array found: %T", obj)
Expand Down
64 changes: 64 additions & 0 deletions test/e2e/templaterules_with_non_typed_array_fields_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2022 VMware, Inc.
// SPDX-License-Identifier: Apache-2.0

package e2e

import (
"strings"
"testing"
)

func TestTemplateRulesWithNonTypedArrayFields(t *testing.T) {
env := BuildEnv(t)
logger := Logger{}
kapp := Kapp{t, env.Namespace, env.KappBinaryPath, logger}
kubectl := Kubectl{t, env.Namespace, logger}

yaml := `
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: simple-app
spec:
selector:
matchLabels:
simple-app: ""
template:
metadata:
labels:
simple-app: ""
spec:
initContainers: null
containers:
- name: simple-app
image: docker.io/dkalinin/k8s-simple-app@sha256:4c8b96d4fffdfae29258d94a22ae4ad1fe36139d47288b8960d9958d1e63a9d0
env:
- name: HELLO_MSG
value: stranger
envFrom:
---
apiVersion: v1
kind: ConfigMap
metadata:
name: simple-cm
annotations:
kapp.k14s.io/versioned: ""
data:
hello_msg: carvel
`

name := "test-templaterules-with-non-typed-array-fields"
cleanUp := func() {
kapp.Run([]string{"delete", "-a", name})
}

cleanUp()
defer cleanUp()
logger.Section("Initial deploy", func() {
kapp.RunWithOpts([]string{"deploy", "-a", name, "-f", "-"}, RunOpts{StdinReader: strings.NewReader(yaml)})

NewPresentClusterResource("deployment", "simple-app", env.Namespace, kubectl)
NewPresentClusterResource("configmap", "simple-cm-ver-1", env.Namespace, kubectl)
})
}