Skip to content

Commit

Permalink
Merge pull request #381 from mortent/RemoveFlagFromErrorMsg
Browse files Browse the repository at this point in the history
Avoid referencing cmd line flags in error strings
  • Loading branch information
k8s-ci-robot authored Jul 2, 2021
2 parents d0a5567 + d727d4b commit c1a7c2d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
19 changes: 16 additions & 3 deletions pkg/manifestreader/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ func (e *UnknownTypesError) Error() string {
return fmt.Sprintf("unknown resource types: %s", strings.Join(gks, ","))
}

// NamespaceMismatchError is returned if all resources must be in a specific
// namespace, and resources are found using other namespaces.
type NamespaceMismatchError struct {
RequiredNamespace string
Namespace string
}

func (e *NamespaceMismatchError) Error() string {
return fmt.Sprintf("found namespace %q, but all resources must be in namespace %q",
e.Namespace, e.RequiredNamespace)
}

// SetNamespaces verifies that every namespaced resource has the namespace
// set, and if one does not, it will set the namespace to the provided
// defaultNamespace.
Expand Down Expand Up @@ -82,9 +94,10 @@ func SetNamespaces(mapper meta.RESTMapper, objs []*unstructured.Unstructured,
} else {
ns := obj.GetNamespace()
if enforceNamespace && ns != defaultNamespace {
return fmt.Errorf("the namespace from the provided object %q "+
"does not match the namespace %q. You must pass '--namespace=%s' to perform this operation",
ns, defaultNamespace, ns)
return &NamespaceMismatchError{
Namespace: ns,
RequiredNamespace: defaultNamespace,
}
}
}
case meta.RESTScopeRoot:
Expand Down
29 changes: 21 additions & 8 deletions pkg/manifestreader/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
apiextv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand All @@ -30,7 +31,7 @@ func TestSetNamespaces(t *testing.T) {
enforceNamespace bool

expectedNamespaces []string
expectedErrText string
expectedErr error
}{
"resources already have namespace": {
objs: []*unstructured.Unstructured{
Expand Down Expand Up @@ -86,7 +87,10 @@ func TestSetNamespaces(t *testing.T) {
},
defaultNamspace: "bar",
enforceNamespace: true,
expectedErrText: "does not match the namespace",
expectedErr: &NamespaceMismatchError{
RequiredNamespace: "bar",
Namespace: "foo",
},
},
"cluster-scoped CR with CRD": {
objs: []*unstructured.Unstructured{
Expand Down Expand Up @@ -141,7 +145,18 @@ func TestSetNamespaces(t *testing.T) {
Kind: "AnotherCustom",
}, ""),
},
expectedErrText: "unknown resource types: Custom.custom.io,AnotherCustom.custom.io",
expectedErr: &UnknownTypesError{
GroupKinds: []schema.GroupKind{
{
Group: "custom.io",
Kind: "Custom",
},
{
Group: "custom.io",
Kind: "AnotherCustom",
},
},
},
},
}

Expand All @@ -157,11 +172,9 @@ func TestSetNamespaces(t *testing.T) {

err = SetNamespaces(mapper, tc.objs, tc.defaultNamspace, tc.enforceNamespace)

if tc.expectedErrText != "" {
if err == nil {
t.Errorf("expected error %s, but not nil", tc.expectedErrText)
}
assert.Contains(t, err.Error(), tc.expectedErrText)
if tc.expectedErr != nil {
require.Error(t, err)
assert.Equal(t, tc.expectedErr, err)
return
}

Expand Down

0 comments on commit c1a7c2d

Please sign in to comment.