Skip to content

Commit

Permalink
Clean up file reader
Browse files Browse the repository at this point in the history
  • Loading branch information
jgustie committed Mar 2, 2021
1 parent b077439 commit 9e1789c
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions internal/readers/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,14 @@ func (r *FileReader) Read() ([]*yaml.RNode, error) {
}

// Silently ignore errors if we cannot get any valid resources of this
if nodes, err := kio.FromBytes(data); err == nil {
for _, n := range nodes {
m, err := n.GetMeta()
if err != nil {
continue
}

// Kind is required
if m.Kind == "" {
continue
}

// Name may be required
if m.Name == "" &&
!strings.HasSuffix(m.Kind, "List") &&
m.APIVersion != konjurev1beta2.APIVersion {
continue
}
nodes, err := kio.FromBytes(data)
if err != nil {
return nil
}

// Only keep things that appear to be Kube resources
for _, n := range nodes {
if keepNode(n) {
result = append(result, n)
}
}
Expand All @@ -108,6 +97,28 @@ func (r *FileReader) Read() ([]*yaml.RNode, error) {
return result, nil
}

// keepNode tests the supplied node to see if it should be included in the result.
func keepNode(node *yaml.RNode) bool {
m, err := node.GetMeta()
if err != nil {
return false
}

// Kind is required
if m.Kind == "" {
return false
}

// Name may be required
if m.Name == "" &&
!strings.HasSuffix(m.Kind, "List") &&
m.APIVersion != konjurev1beta2.APIVersion {
return false
}

return true
}

// isKustomizeRoot tests to see if the specified directory can be used a Kustomize root.
func isKustomizeRoot(path string) bool {
f, err := os.Open(path)
Expand Down

0 comments on commit 9e1789c

Please sign in to comment.