Skip to content

Commit

Permalink
conditionally add --show-managed-fields in the tests to have consiste…
Browse files Browse the repository at this point in the history
…nt test expectations
  • Loading branch information
Dmitriy Kalinin committed Aug 20, 2021
1 parent a0f7786 commit 65d3d12
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion test/e2e/cluster_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,36 @@ package e2e
import (
"fmt"
"strings"
"sync"
"testing"

ctlres "github.com/k14s/kapp/pkg/kapp/resources"
)

var (
hasShowManagedFieldsFlag bool
determineShowManagedFieldsFlag sync.Once
)

type ClusterResource struct {
res ctlres.Resource
}

func NewPresentClusterResource(kind, name, ns string, kubectl Kubectl) ClusterResource {
out, _ := kubectl.RunWithOpts([]string{"get", kind, name, "-n", ns, "-o", "yaml"}, RunOpts{})
// Since -oyaml output is different between different kubectl versions
// due to inclusion/exclusion of managed fields, lets try to
// always include it via a flag. Older versions did not have it.
determineShowManagedFieldsFlag.Do(func() {
_, err := kubectl.RunWithOpts([]string{"get", "node", "--show-managed-fields"}, RunOpts{AllowError: true})
hasShowManagedFieldsFlag = (err == nil)
})

args := []string{"get", kind, name, "-n", ns, "-o", "yaml"}
if hasShowManagedFieldsFlag {
args = append(args, "--show-managed-fields")
}

out, _ := kubectl.RunWithOpts(args, RunOpts{})
return ClusterResource{ctlres.MustNewResourceFromBytes([]byte(out))}
}

Expand Down

0 comments on commit 65d3d12

Please sign in to comment.