Add support to get files referred in the generators and tranformers

fields
This commit is contained in:
Haiyan Meng
2020-01-09 13:45:39 -08:00
parent 983ac2be31
commit 3519cc56a1
4 changed files with 133 additions and 35 deletions

View File

@@ -5,12 +5,12 @@ import (
"flag" "flag"
"fmt" "fmt"
"log" "log"
"path/filepath"
"sort" "sort"
"time" "time"
"sigs.k8s.io/kustomize/api/internal/crawl/doc"
"sigs.k8s.io/kustomize/api/internal/crawl/index" "sigs.k8s.io/kustomize/api/internal/crawl/index"
"sigs.k8s.io/kustomize/api/konfig"
) )
// iterateArr adds each item in arr into countMap. // iterateArr adds each item in arr into countMap.
@@ -25,17 +25,6 @@ func iterateArr(arr []string, countMap map[string]int) {
} }
// isKustomizationFile determines whether a file path is a kustomization file
func isKustomizationFile(path string) bool {
basename := filepath.Base(path)
for _, name := range konfig.RecognizedKustomizationFileNames() {
if basename == name {
return true
}
}
return false
}
// SortMapKeyByValue takes a map as its input, sorts its keys according to their values // SortMapKeyByValue takes a map as its input, sorts its keys according to their values
// in the map, and outputs the sorted keys as a slice. // in the map, and outputs the sorted keys as a slice.
func SortMapKeyByValue(m map[string]int) []string { func SortMapKeyByValue(m map[string]int) []string {
@@ -44,7 +33,7 @@ func SortMapKeyByValue(m map[string]int) []string {
keys = append(keys, key) keys = append(keys, key)
} }
// sort keys according to their values in the map m // sort keys according to their values in the map m
sort.Slice(keys, func(i, j int) bool {return m[keys[i]] > m[keys[j]]}) sort.Slice(keys, func(i, j int) bool { return m[keys[i]] > m[keys[j]] })
return keys return keys
} }
@@ -101,7 +90,7 @@ If you only want to list the 10 most popular features, set the flag to 10.`)
iterateArr(hit.Document.Kinds, kindsMap) iterateArr(hit.Document.Kinds, kindsMap)
iterateArr(hit.Document.Identifiers, identifiersMap) iterateArr(hit.Document.Identifiers, identifiersMap)
if isKustomizationFile(hit.Document.FilePath) { if doc.IsKustomizationFile(hit.Document.FilePath) {
kustomizationFilecount++ kustomizationFilecount++
iterateArr(hit.Document.Identifiers, kustomizeIdentifiersMap) iterateArr(hit.Document.Identifiers, kustomizeIdentifiersMap)
} }

View File

@@ -43,7 +43,12 @@ type CrawledDocument interface {
ID() string ID() string
GetDocument() *doc.Document GetDocument() *doc.Document
// Get all the Documents directly referred in a Document. // Get all the Documents directly referred in a Document.
GetResources() ([]*doc.Document, error) // For a Document representing a non-kustomization file, an empty slice will be returned.
// For a Document representing a kustomization file:
// the `includeResources` parameter determines whether the documents referred in the `resources` field are returned or not;
// the `includeTransformers` parameter determines whether the documents referred in the `transformers` field are returned or not;
// the `includeGenerators` parameter determines whether the documents referred in the `generators` field are returned or not.
GetResources(includeResources, includeTransformers, includeGenerators bool) ([]*doc.Document, error)
WasCached() bool WasCached() bool
} }
@@ -95,7 +100,7 @@ func addBranches(cdoc CrawledDocument, match Crawler, indx IndexFunc,
return return
} }
deps, err := cdoc.GetResources() deps, err := cdoc.GetResources(true, false, false)
if err != nil { if err != nil {
logger.Println(err) logger.Println(err)
return return

View File

@@ -3,6 +3,7 @@ package doc
import ( import (
"fmt" "fmt"
"log" "log"
"path/filepath"
"sort" "sort"
"strings" "strings"
@@ -51,15 +52,21 @@ func (doc *KustomizationDocument) String() string {
doc.IsSame, doc.Kinds, len(doc.Identifiers), len(doc.Values)) doc.IsSame, doc.Kinds, len(doc.Identifiers), len(doc.Values))
} }
// IsKustomizationFile determines whether a file path is a kustomization file
func IsKustomizationFile(path string) bool {
basename := filepath.Base(path)
for _, name := range konfig.RecognizedKustomizationFileNames() {
if basename == name {
return true
}
}
return false
}
// Implements the CrawlerDocument interface. // Implements the CrawlerDocument interface.
func (doc *KustomizationDocument) GetResources() ([]*Document, error) { func (doc *KustomizationDocument) GetResources(
isResource := true includeResources, includeTransformers, includeGenerators bool) ([]*Document, error) {
for _, suffix := range konfig.RecognizedKustomizationFileNames() { if !IsKustomizationFile(doc.FilePath) {
if strings.HasSuffix(doc.FilePath, "/"+suffix) {
isResource = false
}
}
if isResource {
return []*Document{}, nil return []*Document{}, nil
} }
@@ -77,20 +84,42 @@ func (doc *KustomizationDocument) GetResources() ([]*Document, error) {
} }
k.FixKustomizationPostUnmarshalling() k.FixKustomizationPostUnmarshalling()
res := make([]*Document, 0, len(k.Resources)) res := make([]*Document, 0)
for _, r := range k.Resources {
if includeResources {
resourceDocs := doc.CollectDocuments(k.Resources)
res = append(res, resourceDocs...)
}
if includeGenerators {
generatorDocs := doc.CollectDocuments(k.Generators)
res = append(res, generatorDocs...)
}
if includeTransformers {
transformerDocs := doc.CollectDocuments(k.Transformers)
res = append(res, transformerDocs...)
}
return res, nil
}
// CollectDocuments construct a Document for each path in paths, and return
// a slice of Document pointers.
func (doc *KustomizationDocument) CollectDocuments(paths []string) []*Document {
docs := make([]*Document, 0, len(paths))
for _, r := range paths {
if strings.TrimSpace(r) == "" { if strings.TrimSpace(r) == "" {
continue continue
} }
next, err := doc.Document.FromRelativePath(r) next, err := doc.Document.FromRelativePath(r)
if err != nil { if err != nil {
log.Printf("GetResources error: %v\n", err) log.Printf("CollectDocuments error: %v\n", err)
continue continue
} }
res = append(res, &next) docs = append(docs, &next)
} }
return docs
return res, nil
} }
func (doc *KustomizationDocument) readBytes() ([]map[string]interface{}, error) { func (doc *KustomizationDocument) readBytes() ([]map[string]interface{}, error) {

View File

@@ -189,11 +189,13 @@ metadata:
} }
} }
func TestGetResources(t *testing.T) { type TestStructForGetResources struct {
tests := []struct {
doc KustomizationDocument doc KustomizationDocument
resources []*Document resources []*Document
}{ }
func TestGetResources(t *testing.T) {
tests := []TestStructForGetResources{
{ {
doc: KustomizationDocument{ doc: KustomizationDocument{
Document: Document{ Document: Document{
@@ -248,9 +250,12 @@ resources:
resources: []*Document{}, resources: []*Document{},
}, },
} }
runTest(t, tests, true, false, false)
}
func runTest(t *testing.T, tests []TestStructForGetResources, includeResources, includeTransformers, includeGenerators bool) {
for _, test := range tests { for _, test := range tests {
res, err := test.doc.GetResources() res, err := test.doc.GetResources(includeResources, includeTransformers, includeGenerators)
if err != nil { if err != nil {
t.Errorf("Unexpected error: %v\n", err) t.Errorf("Unexpected error: %v\n", err)
continue continue
@@ -284,3 +289,73 @@ resources:
} }
} }
} }
func TestGetResourcesAndGenerators(t *testing.T) {
tests := []TestStructForGetResources{
{
doc: KustomizationDocument{
Document: Document{
RepositoryURL: "sigs.k8s.io/kustomize",
FilePath: "some/path/to/kdir/kustomization.yaml",
DocumentData: `
resources:
- file.yaml
generators:
- gen.yaml
transformers:
- tr.yaml
`},
},
resources: []*Document{
{
RepositoryURL: "sigs.k8s.io/kustomize",
FilePath: "some/path/to/kdir/gen.yaml",
},
{
RepositoryURL: "sigs.k8s.io/kustomize",
FilePath: "some/path/to/kdir/file.yaml",
},
},
},
}
runTest(t, tests, true, false, true)
}
func TestGetResourcesAndGeneratorsAndTransformers(t *testing.T) {
tests := []TestStructForGetResources{
{
doc: KustomizationDocument{
Document: Document{
RepositoryURL: "sigs.k8s.io/kustomize",
FilePath: "some/path/to/kdir/kustomization.yaml",
DocumentData: `
resources:
- file.yaml
generators:
- gen.yaml
transformers:
- tr.yaml
`},
},
resources: []*Document{
{
RepositoryURL: "sigs.k8s.io/kustomize",
FilePath: "some/path/to/kdir/tr.yaml",
},
{
RepositoryURL: "sigs.k8s.io/kustomize",
FilePath: "some/path/to/kdir/gen.yaml",
},
{
RepositoryURL: "sigs.k8s.io/kustomize",
FilePath: "some/path/to/kdir/file.yaml",
},
},
},
}
runTest(t, tests, true, true, true)
}