Skip to content

Commit

Permalink
Use a specific error type when encountering unknown types
Browse files Browse the repository at this point in the history
  • Loading branch information
mortent committed May 21, 2021
1 parent dd4cd1a commit c3a312d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
9 changes: 9 additions & 0 deletions pkg/errors/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"sigs.k8s.io/cli-utils/pkg/apply/taskrunner"
"sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/cli-utils/pkg/manifestreader"
)

const (
Expand Down Expand Up @@ -49,6 +50,14 @@ Timeout after {{printf "%.0f" .err.Timeout.Seconds}} seconds waiting for {{print
{{- range .err.TimedOutResources}}
{{printf "%s/%s %s %s" .Identifier.GroupKind.Kind .Identifier.Name .Status .Message }}
{{- end}}
`

errorMsgForType[reflect.TypeOf(manifestreader.UnknownTypesError{})] = `
Unknown type(s) encountered. Every type must either be already installed in the cluster or the CRD must be among the applied manifests.
{{- range .err.GroupKinds}}
{{ printf "%s" . }}
{{- end}}
`

statusCodeForType = make(map[reflect.Type]int)
Expand Down
23 changes: 21 additions & 2 deletions pkg/manifestreader/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,30 @@ package manifestreader
import (
"encoding/json"
"fmt"
"strings"

"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/cli-utils/pkg/apply/solver"
"sigs.k8s.io/cli-utils/pkg/inventory"
"sigs.k8s.io/kustomize/kyaml/kio/filters"
"sigs.k8s.io/kustomize/kyaml/kio/kioutil"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

type UnknownTypesError struct {
GroupKinds []schema.GroupKind
}

func (e *UnknownTypesError) Error() string {
var gks []string
for _, gk := range e.GroupKinds {
gks = append(gks, gk.String())
}
return fmt.Sprintf("unknown resource types: %s", strings.Join(gks, ","))
}

// 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 All @@ -34,6 +48,7 @@ func SetNamespaces(mapper meta.RESTMapper, objs []*unstructured.Unstructured,
}
}

var unknownGKs []schema.GroupKind
for _, obj := range objs {
accessor, _ := meta.Accessor(obj)

Expand Down Expand Up @@ -91,14 +106,18 @@ func SetNamespaces(mapper meta.RESTMapper, objs []*unstructured.Unstructured,

switch scope {
case "":
return fmt.Errorf("can't find scope for resource %s %s", gk.String(), accessor.GetName())
unknownGKs = append(unknownGKs, gk)
case "Cluster":
continue
case "Namespaced":
accessor.SetNamespace(defaultNamespace)
}
}

if len(unknownGKs) > 0 {
return &UnknownTypesError{
GroupKinds: unknownGKs,
}
}
return nil
}

Expand Down
15 changes: 15 additions & 0 deletions pkg/manifestreader/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,21 @@ func TestSetNamespaces(t *testing.T) {
enforceNamespace: true,
expectedNamespaces: []string{"", "bar"},
},
"unknown types in CRs": {
objs: []*unstructured.Unstructured{
toUnstructured(schema.GroupVersionKind{
Group: "custom.io",
Version: "v1",
Kind: "Custom",
}, ""),
toUnstructured(schema.GroupVersionKind{
Group: "custom.io",
Version: "v1",
Kind: "AnotherCustom",
}, ""),
},
expectedErrText: "unknown resource types: Custom.custom.io,AnotherCustom.custom.io",
},
}

for tn, tc := range testCases {
Expand Down

0 comments on commit c3a312d

Please sign in to comment.