Skip to content

Commit

Permalink
Support remote resources for kustomize edit add
Browse files Browse the repository at this point in the history
  • Loading branch information
kzwang committed Jul 24, 2020
1 parent 51f9a84 commit 2ee4eec
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
3 changes: 2 additions & 1 deletion kustomize/internal/commands/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/ifc"
"sigs.k8s.io/kustomize/api/konfig"
"sigs.k8s.io/kustomize/api/loader"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/util"
)
Expand Down Expand Up @@ -98,7 +99,7 @@ func runCreate(opts createFlags, fSys filesys.FileSystem, uf ifc.KunstructuredFa
var resources []string
var err error
if opts.resources != "" {
resources, err = util.GlobPatterns(fSys, strings.Split(opts.resources, ","))
resources, err = util.GlobPatternsWithLoader(fSys, loader.NewFileLoaderAtCwd(fSys), strings.Split(opts.resources, ","))
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion kustomize/internal/commands/edit/add/addresource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/loader"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/util"
)
Expand Down Expand Up @@ -48,7 +49,7 @@ func (o *addResourceOptions) Validate(args []string) error {

// RunAddResource runs addResource command (do real work).
func (o *addResourceOptions) RunAddResource(fSys filesys.FileSystem) error {
resources, err := util.GlobPatterns(fSys, o.resourceFilePaths)
resources, err := util.GlobPatternsWithLoader(fSys, loader.NewFileLoaderAtCwd(fSys), o.resourceFilePaths)
if err != nil {
return err
}
Expand Down
27 changes: 27 additions & 0 deletions kustomize/internal/commands/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strings"

"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/ifc"
)

// GlobPatterns accepts a slice of glob strings and returns the set of
Expand All @@ -29,6 +30,32 @@ func GlobPatterns(fSys filesys.FileSystem, patterns []string) ([]string, error)
return result, nil
}

// GlobPatterns accepts a slice of glob strings and returns the set of
// matching file paths. If files are not found, will try load from remote.
func GlobPatternsWithLoader(fSys filesys.FileSystem, ldr ifc.Loader, patterns []string) ([]string, error) {
var result []string
for _, pattern := range patterns {
files, err := fSys.Glob(pattern)
if err != nil {
return nil, err
}
if len(files) == 0 {
loader, err := ldr.New(pattern)
if err != nil {
log.Printf("%s has no match", pattern)
} else {
result = append(result, pattern)
if loader != nil {
loader.Cleanup()
}
}
continue
}
result = append(result, files...)
}
return result, nil
}

// ConvertToMap converts a slice of strings in the form of
// `key:value` into a map.
func ConvertToMap(input string, kind string) (map[string]string, error) {
Expand Down
60 changes: 60 additions & 0 deletions kustomize/internal/commands/util/util_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package util

import (
"fmt"
"reflect"
"testing"

"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/ifc"
)

func TestConvertToMap(t *testing.T) {
Expand Down Expand Up @@ -35,3 +39,59 @@ func TestConvertToMapError(t *testing.T) {
t.Errorf("incorrect error: %v", err.Error())
}
}

func TestGlobPatternsWithLoaderRemoteFile(t *testing.T) {
fSys := filesys.MakeFsInMemory()
fSys.Create("test.yml")
httpPath := "https://example.com/example.yaml"
ldr := fakeLoader{
path: httpPath,
}

// test load remote file
resources, err := GlobPatternsWithLoader(fSys, ldr, []string{httpPath})
if err != nil {
t.Fatalf("unexpected load error: %v", err)
}
if len(resources) != 1 || resources[0] != httpPath {
t.Fatalf("incorrect resources: %v", resources)
}

// test load local and remote file
resources, err = GlobPatternsWithLoader(fSys, ldr, []string{httpPath, "/test.yml"})
if err != nil {
t.Fatalf("unexpected load error: %v", err)
}
if len(resources) != 2 || resources[0] != httpPath || resources[1] != "/test.yml" {
t.Fatalf("incorrect resources: %v", resources)
}

// test load invalid file
resources, err = GlobPatternsWithLoader(fSys, ldr, []string{"http://invalid"})
if err != nil {
t.Fatalf("unexpected load error: %v", err)
}
if len(resources) > 0 {
t.Fatalf("incorrect resources: %v", resources)
}
}

type fakeLoader struct {
path string
}

func (l fakeLoader) Root() string {
return ""
}
func (l fakeLoader) New(newRoot string) (ifc.Loader, error) {
if newRoot == l.path {
return nil, nil
}
return nil, fmt.Errorf("%s not exist", newRoot)
}
func (l fakeLoader) Load(location string) ([]byte, error) {
return nil, nil
}
func (l fakeLoader) Cleanup() error {
return nil
}

0 comments on commit 2ee4eec

Please sign in to comment.