Drop useless or duplicative code.

This commit is contained in:
Jeffrey Regan
2018-07-24 10:54:18 -07:00
parent 8d420ec3f7
commit a78aa22399
8 changed files with 50 additions and 90 deletions

View File

@@ -70,7 +70,7 @@ func (o *buildOptions) Validate(args []string) error {
// RunBuild runs build command. // RunBuild runs build command.
func (o *buildOptions) RunBuild(out io.Writer, fSys fs.FileSystem) error { 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) absPath, err := filepath.Abs(o.kustomizationPath)
if err != nil { if err != nil {

View File

@@ -68,7 +68,7 @@ func (o *diffOptions) Validate(args []string) error {
// RunDiff gets the differences between Application.MakeCustomizedResMap() and Application.MakeUncustomizedResMap(). // RunDiff gets the differences between Application.MakeCustomizedResMap() and Application.MakeUncustomizedResMap().
func (o *diffOptions) RunDiff(out, errOut io.Writer, fSys fs.FileSystem) error { 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) absPath, err := filepath.Abs(o.kustomizationPath)
if err != nil { if err != nil {

View File

@@ -137,7 +137,7 @@ func (f *ConfigMapFactory) MakeConfigMap2(
all = append(all, pairs...) all = append(all, pairs...)
for _, kv := range all { for _, kv := range all {
err = addKeyFromLiteralToConfigMap(cm, kv.key, kv.value) err = AddKv(cm, kv.key, kv.value)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -166,7 +166,7 @@ func (f *ConfigMapFactory) handleConfigMapFromLiteralSources(
if err != nil { if err != nil {
return err return err
} }
err = addKeyFromLiteralToConfigMap(configMap, k, v) err = AddKv(configMap, k, v)
if err != nil { if err != nil {
return err return err
} }
@@ -251,7 +251,7 @@ func (f *ConfigMapFactory) handleConfigMapFromEnvFileSource(
return fmt.Errorf("env config file %s cannot be a directory", args.EnvSource) return fmt.Errorf("env config file %s cannot be a directory", args.EnvSource)
} }
return addFromEnvFile(args.EnvSource, func(key, value string) error { 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 { if err != nil {
return err 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, // AddKv 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. // Error if key invalid, or already exists.
func addKeyFromLiteralToConfigMap(configMap *v1.ConfigMap, keyName, data string) error { func AddKv(configMap *v1.ConfigMap, keyName, data string) error {
// Note, the rules for ConfigMap keys are the exact same as the ones for SecretKeys. // Note, the rules for ConfigMap keys are the exact same as the ones for SecretKeys.
if errs := validation.IsConfigMapKey(keyName); len(errs) != 0 { 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, ";")) return fmt.Errorf("%q is not a valid key name for a ConfigMap: %s", keyName, strings.Join(errs, ";"))

View File

@@ -21,6 +21,7 @@ import (
"testing" "testing"
"github.com/kubernetes-sigs/kustomize/pkg/fs" "github.com/kubernetes-sigs/kustomize/pkg/fs"
"github.com/kubernetes-sigs/kustomize/pkg/loader"
"github.com/kubernetes-sigs/kustomize/pkg/types" "github.com/kubernetes-sigs/kustomize/pkg/types"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/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 // 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 { for _, tc := range testCases {
cm, err := f.MakeConfigMap1(&tc.input) cm, err := f.MakeConfigMap1(&tc.input)
if err != nil { if err != nil {

View File

@@ -34,9 +34,7 @@ type FakeLoader struct {
func NewFakeLoader(initialDir string) FakeLoader { func NewFakeLoader(initialDir string) FakeLoader {
// Create fake filesystem and inject it into initial Loader. // Create fake filesystem and inject it into initial Loader.
fakefs := fs.MakeFakeFS() fakefs := fs.MakeFakeFS()
var schemes []loader.SchemeLoader rootLoader := loader.NewLoader(loader.NewFileLoader(fakefs))
schemes = append(schemes, loader.NewFileLoader(fakefs))
rootLoader := loader.Init(schemes)
ldr, _ := rootLoader.New(initialDir) ldr, _ := rootLoader.New(initialDir)
return FakeLoader{fs: fakefs, delegate: ldr} return FakeLoader{fs: fakefs, delegate: ldr}
} }

View File

@@ -26,18 +26,19 @@ import (
const currentDir = "." const currentDir = "."
// Internal implementation of SchemeLoader interface. // FileLoader loads files from a file system.
type fileLoader struct { type FileLoader struct {
fs fs.FileSystem fs fs.FileSystem
} }
// NewFileLoader returns a SchemeLoader to handle a file system. // NewFileLoader returns a new FileLoader.
func NewFileLoader(fs fs.FileSystem) SchemeLoader { func NewFileLoader(fs fs.FileSystem) *FileLoader {
return &fileLoader{fs: fs} return &FileLoader{fs: fs}
} }
// Is the location calculated with the root and location params a full file path. // IsAbsPath return true if the location calculated with the root
func (l *fileLoader) IsScheme(root string, location string) bool { // and location params a full file path.
func (l *FileLoader) IsAbsPath(root string, location string) bool {
fullFilePath, err := l.FullLocation(root, location) fullFilePath, err := l.FullLocation(root, location)
if err != nil { if err != nil {
return false return false
@@ -45,11 +46,12 @@ func (l *fileLoader) IsScheme(root string, location string) bool {
return filepath.IsAbs(fullFilePath) 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 // 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, // 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. // but not both. Special case for ".": Expands to current working directory.
// Example: "/home/seans/project", "subdir/bar" -> "/home/seans/project/subdir/bar". // 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 // First, validate the parameters
if len(root) == 0 && len(location) == 0 { if len(root) == 0 && len(location) == 0 {
return "", fmt.Errorf("unable to calculate full location: root and location empty") 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. // Load returns the bytes from reading a file at fullFilePath.
// Implements the Loader interface. // Implements the Loader interface.
func (l *fileLoader) Load(fullFilePath string) ([]byte, error) { func (l *FileLoader) Load(p string) ([]byte, error) {
// Validate path to load from is a full file path. return l.fs.ReadFile(p)
if !filepath.IsAbs(fullFilePath) {
return nil, fmt.Errorf("attempting to load file without full file path: %s\n", fullFilePath)
}
return l.fs.ReadFile(fullFilePath)
} }
// GlobLoad returns the map from path to bytes from reading a glob path. // GlobLoad returns the map from path to bytes from reading a glob path.
// Implements the Loader interface. // Implements the Loader interface.
func (l *fileLoader) GlobLoad(fullFilePath string) (map[string][]byte, error) { func (l *FileLoader) GlobLoad(p string) (map[string][]byte, error) {
// Validate path to load from is a full file path. return l.fs.ReadFiles(p)
if !filepath.IsAbs(fullFilePath) {
return nil, fmt.Errorf("Attempting to load file without full file path: %s\n", fullFilePath)
}
return l.fs.ReadFiles(fullFilePath)
} }

View File

@@ -19,10 +19,9 @@ package loader
import "fmt" import "fmt"
// Loader interface exposes methods to read bytes in a scheme-agnostic manner. // Loader interface exposes methods to read bytes.
// The Loader encapsulating a root location to calculate where to read from.
type Loader interface { 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 Root() string
// New returns Loader located at newRoot. // New returns Loader located at newRoot.
New(newRoot string) (Loader, error) New(newRoot string) (Loader, error)
@@ -32,33 +31,20 @@ type Loader interface {
GlobLoad(location string) (map[string][]byte, error) GlobLoad(location string) (map[string][]byte, error)
} }
// Private implmentation of Loader interface. // Private implementation of Loader interface.
type loaderImpl struct { type loaderImpl struct {
root string root string
schemes []SchemeLoader fLoader *FileLoader
}
// 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)
} }
const emptyRoot = "" const emptyRoot = ""
// Init initializes the first loader with the supported schemes. // NewLoader initializes the first loader with the supported fLoader.
// Example schemes: fileLoader, httpLoader, gitLoader. func NewLoader(fl *FileLoader) Loader {
func Init(schemes []SchemeLoader) Loader { return &loaderImpl{root: emptyRoot, fLoader: fl}
return &loaderImpl{root: emptyRoot, schemes: schemes}
} }
// Root returns the scheme-specific root location for this Loader. // Root returns the root location for this Loader.
func (l *loaderImpl) Root() string { func (l *loaderImpl) Root() string {
return l.root return l.root
} }
@@ -69,53 +55,35 @@ func (l *loaderImpl) Root() string {
// Example: "/home/seans/project" or "/home/seans/project/" // Example: "/home/seans/project" or "/home/seans/project/"
// NOT "/home/seans/project/file.yaml". // NOT "/home/seans/project/file.yaml".
func (l *loaderImpl) New(newRoot string) (Loader, error) { 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 { if err != nil {
return nil, err return nil, err
} }
root, err := scheme.FullLocation(l.root, newRoot) return &loaderImpl{root: root, fLoader: l.fLoader}, nil
if err != nil {
return nil, err
}
return &loaderImpl{root: root, schemes: l.schemes}, 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 // "location" can be an absolute path, or if relative, full location is
// calculated from the Root(). // calculated from the Root().
func (l *loaderImpl) Load(location string) ([]byte, error) { func (l *loaderImpl) Load(location string) ([]byte, error) {
scheme, err := l.getSchemeLoader(location) fullLocation, err := l.fLoader.FullLocation(l.root, location)
if err != nil { if err != nil {
fmt.Printf("Trouble in fulllocation: %v\n", err)
return nil, err return nil, err
} }
fullLocation, err := scheme.FullLocation(l.root, location) return l.fLoader.Load(fullLocation)
if err != nil {
return nil, err
}
return scheme.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 // "location" can be an absolute path, or if relative, full location is
// calculated from the Root(). // calculated from the Root().
func (l *loaderImpl) GlobLoad(location string) (map[string][]byte, error) { 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 { if err != nil {
return nil, err return nil, err
} }
fullLocation, err := scheme.FullLocation(l.root, location) return l.fLoader.GlobLoad(fullLocation)
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)
} }

View File

@@ -25,10 +25,7 @@ import (
) )
func initializeRootLoader(fakefs fs.FileSystem) Loader { func initializeRootLoader(fakefs fs.FileSystem) Loader {
var schemes []SchemeLoader return NewLoader(NewFileLoader(fakefs))
schemes = append(schemes, NewFileLoader(fakefs))
rootLoader := Init(schemes)
return rootLoader
} }
func TestLoader_Root(t *testing.T) { func TestLoader_Root(t *testing.T) {
@@ -46,7 +43,7 @@ func TestLoader_Root(t *testing.T) {
} }
_, err = rootLoader.New("https://google.com/project") _, err = rootLoader.New("https://google.com/project")
if err == nil { if err == nil {
t.Fatalf("Expected error for unknown scheme not returned") t.Fatalf("Expected error")
} }
// Test with trailing slash in directory. // Test with trailing slash in directory.