Skip to content

Commit

Permalink
Improve error messages on filtering for Kubernetes-type files. (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickdappollonio authored Sep 5, 2023
1 parent 12415e0 commit 758a6e0
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
33 changes: 32 additions & 1 deletion slice/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package slice

import "fmt"
import (
"fmt"
"strings"
)

type strictModeSkipErr struct {
fieldName string
Expand All @@ -20,3 +23,31 @@ type skipErr struct {
func (e *skipErr) Error() string {
return fmt.Sprintf("resource %s %q is configured to be skipped", e.kind, e.name)
}

const nonK8sHelper = `the file has no Kubernetes metadata: it is most likely a non-Kubernetes YAML file, you can skip it with --skip-non-k8s`

type cantFindFieldErr struct {
fieldName string
fileCount int
meta kubeObjectMeta
}

func (e *cantFindFieldErr) Error() string {
var sb strings.Builder

sb.WriteString(fmt.Sprintf(
"unable to find Kubernetes %q field in file %d",
e.fieldName, e.fileCount,
))

if e.meta.empty() {
sb.WriteString(": " + nonK8sHelper)
} else {
sb.WriteString(fmt.Sprintf(
": processed details: kind %q, name %q, apiVersion %q",
e.meta.Kind, e.meta.Name, e.meta.APIVersion,
))
}

return sb.String()
}
1 change: 1 addition & 0 deletions slice/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
func TestErrorsInterface(t *testing.T) {
require.Implementsf(t, (*error)(nil), &strictModeSkipErr{}, "strictModeSkipErr should implement error")
require.Implementsf(t, (*error)(nil), &skipErr{}, "skipErr should implement error")
require.Implementsf(t, (*error)(nil), &cantFindFieldErr{}, "cantFindFieldErr should implement error")
}

func requireErrorIf(t *testing.T, wantErr bool, err error) {
Expand Down
4 changes: 4 additions & 0 deletions slice/kube.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ type kubeObjectMeta struct {
Namespace string
}

func (k kubeObjectMeta) empty() bool {
return k.APIVersion == "" && k.Kind == "" && k.Name == "" && k.Namespace == ""
}

// from: https://github.com/helm/helm/blob/v3.11.1/pkg/releaseutil/kind_sorter.go#LL31-L67C2
var helmInstallOrder = []string{
"Namespace",
Expand Down
4 changes: 2 additions & 2 deletions slice/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ func (s *Split) parseYAMLManifest(contents []byte) (yamlFile, error) {

// Check if we have a Kubernetes kind and we're requesting inclusion or exclusion
if k8smeta.Kind == "" && !s.opts.AllowEmptyKinds && (hasIncluded || hasExcluded) {
return yamlFile{}, fmt.Errorf("unable to find Kubernetes \"kind\" field in file number %d", s.fileCount)
return yamlFile{}, &cantFindFieldErr{fieldName: "kind", fileCount: s.fileCount, meta: k8smeta}
}

// Check if we have a Kubernetes name and we're requesting inclusion or exclusion
if k8smeta.Name == "" && !s.opts.AllowEmptyNames && (hasIncluded || hasExcluded) {
return yamlFile{}, fmt.Errorf("unable to find Kubernetes \"metadata.name\" field in file number %d", s.fileCount)
return yamlFile{}, &cantFindFieldErr{fieldName: "metadata.name", fileCount: s.fileCount, meta: k8smeta}
}

// We need to check if the file should be skipped
Expand Down

0 comments on commit 758a6e0

Please sign in to comment.