mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-21 22:41:42 +00:00
Support remote resources for kustomize edit add
This commit is contained in:
@@ -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"
|
||||
)
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
)
|
||||
@@ -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
|
||||
}
|
||||
|
||||
@@ -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
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user