mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
Drop useless or duplicative code.
This commit is contained in:
@@ -70,7 +70,7 @@ func (o *buildOptions) Validate(args []string) error {
|
||||
|
||||
// RunBuild runs build command.
|
||||
func (o *buildOptions) RunBuild(out io.Writer, fSys fs.FileSystem) error {
|
||||
l := loader.Init([]loader.SchemeLoader{loader.NewFileLoader(fSys)})
|
||||
l := loader.NewLoader(loader.NewFileLoader(fSys))
|
||||
|
||||
absPath, err := filepath.Abs(o.kustomizationPath)
|
||||
if err != nil {
|
||||
|
||||
@@ -68,7 +68,7 @@ func (o *diffOptions) Validate(args []string) error {
|
||||
// RunDiff gets the differences between Application.MakeCustomizedResMap() and Application.MakeUncustomizedResMap().
|
||||
func (o *diffOptions) RunDiff(out, errOut io.Writer, fSys fs.FileSystem) error {
|
||||
|
||||
l := loader.Init([]loader.SchemeLoader{loader.NewFileLoader(fSys)})
|
||||
l := loader.NewLoader(loader.NewFileLoader(fSys))
|
||||
|
||||
absPath, err := filepath.Abs(o.kustomizationPath)
|
||||
if err != nil {
|
||||
|
||||
@@ -137,7 +137,7 @@ func (f *ConfigMapFactory) MakeConfigMap2(
|
||||
all = append(all, pairs...)
|
||||
|
||||
for _, kv := range all {
|
||||
err = addKeyFromLiteralToConfigMap(cm, kv.key, kv.value)
|
||||
err = AddKv(cm, kv.key, kv.value)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -166,7 +166,7 @@ func (f *ConfigMapFactory) handleConfigMapFromLiteralSources(
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = addKeyFromLiteralToConfigMap(configMap, k, v)
|
||||
err = AddKv(configMap, k, v)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -251,7 +251,7 @@ func (f *ConfigMapFactory) handleConfigMapFromEnvFileSource(
|
||||
return fmt.Errorf("env config file %s cannot be a directory", args.EnvSource)
|
||||
}
|
||||
return addFromEnvFile(args.EnvSource, func(key, value string) error {
|
||||
return addKeyFromLiteralToConfigMap(configMap, key, value)
|
||||
return AddKv(configMap, key, value)
|
||||
})
|
||||
}
|
||||
|
||||
@@ -262,12 +262,12 @@ func addKeyFromFileToConfigMap(configMap *v1.ConfigMap, keyName, filePath string
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return addKeyFromLiteralToConfigMap(configMap, keyName, string(data))
|
||||
return AddKv(configMap, keyName, string(data))
|
||||
}
|
||||
|
||||
// addKeyFromLiteralToConfigMap adds the given key and data to the given config map,
|
||||
// returning an error if the key is not valid or if the key already exists.
|
||||
func addKeyFromLiteralToConfigMap(configMap *v1.ConfigMap, keyName, data string) error {
|
||||
// AddKv adds the given key and data to the given config map.
|
||||
// Error if key invalid, or already exists.
|
||||
func AddKv(configMap *v1.ConfigMap, keyName, data string) error {
|
||||
// Note, the rules for ConfigMap keys are the exact same as the ones for SecretKeys.
|
||||
if errs := validation.IsConfigMapKey(keyName); len(errs) != 0 {
|
||||
return fmt.Errorf("%q is not a valid key name for a ConfigMap: %s", keyName, strings.Join(errs, ";"))
|
||||
|
||||
@@ -21,6 +21,7 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/kubernetes-sigs/kustomize/pkg/fs"
|
||||
"github.com/kubernetes-sigs/kustomize/pkg/loader"
|
||||
"github.com/kubernetes-sigs/kustomize/pkg/types"
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
@@ -134,7 +135,9 @@ func TestConstructConfigMap(t *testing.T) {
|
||||
}
|
||||
|
||||
// TODO: all tests should use a FakeFs
|
||||
f := NewConfigMapFactory(fs.MakeRealFS(), nil)
|
||||
fSys := fs.MakeRealFS()
|
||||
f := NewConfigMapFactory(fSys,
|
||||
loader.NewLoader(loader.NewFileLoader(fSys)))
|
||||
for _, tc := range testCases {
|
||||
cm, err := f.MakeConfigMap1(&tc.input)
|
||||
if err != nil {
|
||||
|
||||
@@ -34,9 +34,7 @@ type FakeLoader struct {
|
||||
func NewFakeLoader(initialDir string) FakeLoader {
|
||||
// Create fake filesystem and inject it into initial Loader.
|
||||
fakefs := fs.MakeFakeFS()
|
||||
var schemes []loader.SchemeLoader
|
||||
schemes = append(schemes, loader.NewFileLoader(fakefs))
|
||||
rootLoader := loader.Init(schemes)
|
||||
rootLoader := loader.NewLoader(loader.NewFileLoader(fakefs))
|
||||
ldr, _ := rootLoader.New(initialDir)
|
||||
return FakeLoader{fs: fakefs, delegate: ldr}
|
||||
}
|
||||
|
||||
@@ -26,18 +26,19 @@ import (
|
||||
|
||||
const currentDir = "."
|
||||
|
||||
// Internal implementation of SchemeLoader interface.
|
||||
type fileLoader struct {
|
||||
// FileLoader loads files from a file system.
|
||||
type FileLoader struct {
|
||||
fs fs.FileSystem
|
||||
}
|
||||
|
||||
// NewFileLoader returns a SchemeLoader to handle a file system.
|
||||
func NewFileLoader(fs fs.FileSystem) SchemeLoader {
|
||||
return &fileLoader{fs: fs}
|
||||
// NewFileLoader returns a new FileLoader.
|
||||
func NewFileLoader(fs fs.FileSystem) *FileLoader {
|
||||
return &FileLoader{fs: fs}
|
||||
}
|
||||
|
||||
// Is the location calculated with the root and location params a full file path.
|
||||
func (l *fileLoader) IsScheme(root string, location string) bool {
|
||||
// IsAbsPath return true if the location calculated with the root
|
||||
// and location params a full file path.
|
||||
func (l *FileLoader) IsAbsPath(root string, location string) bool {
|
||||
fullFilePath, err := l.FullLocation(root, location)
|
||||
if err != nil {
|
||||
return false
|
||||
@@ -45,11 +46,12 @@ func (l *fileLoader) IsScheme(root string, location string) bool {
|
||||
return filepath.IsAbs(fullFilePath)
|
||||
}
|
||||
|
||||
// FullLocation returns some notion of a full path.
|
||||
// If location is a full file path, then ignore root. If location is relative, then
|
||||
// join the root path with the location path. Either root or location can be empty,
|
||||
// but not both. Special case for ".": Expands to current working directory.
|
||||
// Example: "/home/seans/project", "subdir/bar" -> "/home/seans/project/subdir/bar".
|
||||
func (l *fileLoader) FullLocation(root string, location string) (string, error) {
|
||||
func (l *FileLoader) FullLocation(root string, location string) (string, error) {
|
||||
// First, validate the parameters
|
||||
if len(root) == 0 && len(location) == 0 {
|
||||
return "", fmt.Errorf("unable to calculate full location: root and location empty")
|
||||
@@ -72,20 +74,12 @@ func (l *fileLoader) FullLocation(root string, location string) (string, error)
|
||||
|
||||
// Load returns the bytes from reading a file at fullFilePath.
|
||||
// Implements the Loader interface.
|
||||
func (l *fileLoader) Load(fullFilePath string) ([]byte, error) {
|
||||
// Validate path to load from is a full file path.
|
||||
if !filepath.IsAbs(fullFilePath) {
|
||||
return nil, fmt.Errorf("attempting to load file without full file path: %s\n", fullFilePath)
|
||||
}
|
||||
return l.fs.ReadFile(fullFilePath)
|
||||
func (l *FileLoader) Load(p string) ([]byte, error) {
|
||||
return l.fs.ReadFile(p)
|
||||
}
|
||||
|
||||
// GlobLoad returns the map from path to bytes from reading a glob path.
|
||||
// Implements the Loader interface.
|
||||
func (l *fileLoader) GlobLoad(fullFilePath string) (map[string][]byte, error) {
|
||||
// Validate path to load from is a full file path.
|
||||
if !filepath.IsAbs(fullFilePath) {
|
||||
return nil, fmt.Errorf("Attempting to load file without full file path: %s\n", fullFilePath)
|
||||
}
|
||||
return l.fs.ReadFiles(fullFilePath)
|
||||
func (l *FileLoader) GlobLoad(p string) (map[string][]byte, error) {
|
||||
return l.fs.ReadFiles(p)
|
||||
}
|
||||
|
||||
@@ -19,10 +19,9 @@ package loader
|
||||
|
||||
import "fmt"
|
||||
|
||||
// Loader interface exposes methods to read bytes in a scheme-agnostic manner.
|
||||
// The Loader encapsulating a root location to calculate where to read from.
|
||||
// Loader interface exposes methods to read bytes.
|
||||
type Loader interface {
|
||||
// Root returns the scheme-specific string representing the root location for this Loader.
|
||||
// Root returns the root location for this Loader.
|
||||
Root() string
|
||||
// New returns Loader located at newRoot.
|
||||
New(newRoot string) (Loader, error)
|
||||
@@ -32,33 +31,20 @@ type Loader interface {
|
||||
GlobLoad(location string) (map[string][]byte, error)
|
||||
}
|
||||
|
||||
// Private implmentation of Loader interface.
|
||||
// Private implementation of Loader interface.
|
||||
type loaderImpl struct {
|
||||
root string
|
||||
schemes []SchemeLoader
|
||||
}
|
||||
|
||||
// SchemeLoader is the interface for different types of loaders (e.g. fileLoader, httpLoader, etc.)
|
||||
type SchemeLoader interface {
|
||||
// Does this location correspond to this scheme.
|
||||
IsScheme(root string, location string) bool
|
||||
// Combines the root and path into a full location string.
|
||||
FullLocation(root string, path string) (string, error)
|
||||
// Load bytes at scheme-specific location or an error.
|
||||
Load(location string) ([]byte, error)
|
||||
// GlobLoad returns the bytes read from a glob path or an error.
|
||||
GlobLoad(location string) (map[string][]byte, error)
|
||||
fLoader *FileLoader
|
||||
}
|
||||
|
||||
const emptyRoot = ""
|
||||
|
||||
// Init initializes the first loader with the supported schemes.
|
||||
// Example schemes: fileLoader, httpLoader, gitLoader.
|
||||
func Init(schemes []SchemeLoader) Loader {
|
||||
return &loaderImpl{root: emptyRoot, schemes: schemes}
|
||||
// NewLoader initializes the first loader with the supported fLoader.
|
||||
func NewLoader(fl *FileLoader) Loader {
|
||||
return &loaderImpl{root: emptyRoot, fLoader: fl}
|
||||
}
|
||||
|
||||
// Root returns the scheme-specific root location for this Loader.
|
||||
// Root returns the root location for this Loader.
|
||||
func (l *loaderImpl) Root() string {
|
||||
return l.root
|
||||
}
|
||||
@@ -69,53 +55,35 @@ func (l *loaderImpl) Root() string {
|
||||
// Example: "/home/seans/project" or "/home/seans/project/"
|
||||
// NOT "/home/seans/project/file.yaml".
|
||||
func (l *loaderImpl) New(newRoot string) (Loader, error) {
|
||||
scheme, err := l.getSchemeLoader(newRoot)
|
||||
if !l.fLoader.IsAbsPath(l.root, newRoot) {
|
||||
return nil, fmt.Errorf("Not abs path: l.root='%s', loc='%s'\n", l.root, newRoot)
|
||||
}
|
||||
root, err := l.fLoader.FullLocation(l.root, newRoot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
root, err := scheme.FullLocation(l.root, newRoot)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &loaderImpl{root: root, schemes: l.schemes}, nil
|
||||
return &loaderImpl{root: root, fLoader: l.fLoader}, nil
|
||||
}
|
||||
|
||||
// Load returns all the bytes read from scheme-specific location or an error.
|
||||
// Load returns all the bytes read from location or an error.
|
||||
// "location" can be an absolute path, or if relative, full location is
|
||||
// calculated from the Root().
|
||||
func (l *loaderImpl) Load(location string) ([]byte, error) {
|
||||
scheme, err := l.getSchemeLoader(location)
|
||||
fullLocation, err := l.fLoader.FullLocation(l.root, location)
|
||||
if err != nil {
|
||||
fmt.Printf("Trouble in fulllocation: %v\n", err)
|
||||
return nil, err
|
||||
}
|
||||
fullLocation, err := scheme.FullLocation(l.root, location)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return scheme.Load(fullLocation)
|
||||
return l.fLoader.Load(fullLocation)
|
||||
}
|
||||
|
||||
// GlobLoad returns a map from path to bytes read from scheme-specific location or an error.
|
||||
// GlobLoad returns a map from path to bytes read from the location or an error.
|
||||
// "location" can be an absolute path, or if relative, full location is
|
||||
// calculated from the Root().
|
||||
func (l *loaderImpl) GlobLoad(location string) (map[string][]byte, error) {
|
||||
scheme, err := l.getSchemeLoader(location)
|
||||
fullLocation, err := l.fLoader.FullLocation(l.root, location)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
fullLocation, err := scheme.FullLocation(l.root, location)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return scheme.GlobLoad(fullLocation)
|
||||
}
|
||||
|
||||
// Helper function to parse scheme from location parameter.
|
||||
func (l *loaderImpl) getSchemeLoader(location string) (SchemeLoader, error) {
|
||||
for _, scheme := range l.schemes {
|
||||
if scheme.IsScheme(l.root, location) {
|
||||
return scheme, nil
|
||||
}
|
||||
}
|
||||
return nil, fmt.Errorf("Unknown Scheme: %s, %s\n", l.root, location)
|
||||
return l.fLoader.GlobLoad(fullLocation)
|
||||
}
|
||||
|
||||
@@ -25,10 +25,7 @@ import (
|
||||
)
|
||||
|
||||
func initializeRootLoader(fakefs fs.FileSystem) Loader {
|
||||
var schemes []SchemeLoader
|
||||
schemes = append(schemes, NewFileLoader(fakefs))
|
||||
rootLoader := Init(schemes)
|
||||
return rootLoader
|
||||
return NewLoader(NewFileLoader(fakefs))
|
||||
}
|
||||
|
||||
func TestLoader_Root(t *testing.T) {
|
||||
@@ -46,7 +43,7 @@ func TestLoader_Root(t *testing.T) {
|
||||
}
|
||||
_, err = rootLoader.New("https://google.com/project")
|
||||
if err == nil {
|
||||
t.Fatalf("Expected error for unknown scheme not returned")
|
||||
t.Fatalf("Expected error")
|
||||
}
|
||||
|
||||
// Test with trailing slash in directory.
|
||||
|
||||
Reference in New Issue
Block a user