Skip to content

Commit 77e1872

Browse files
authored
Merge pull request #199 from monopole/moarDeletion
Drop the notion of the SchemeLoader
2 parents 12d1771 + a78aa22 commit 77e1872

File tree

8 files changed

+49
-89
lines changed

8 files changed

+49
-89
lines changed

pkg/commands/build.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (o *buildOptions) Validate(args []string) error {
7070

7171
// RunBuild runs build command.
7272
func (o *buildOptions) RunBuild(out io.Writer, fSys fs.FileSystem) error {
73-
l := loader.Init([]loader.SchemeLoader{loader.NewFileLoader(fSys)})
73+
l := loader.NewLoader(loader.NewFileLoader(fSys))
7474

7575
absPath, err := filepath.Abs(o.kustomizationPath)
7676
if err != nil {

pkg/commands/diff.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func (o *diffOptions) Validate(args []string) error {
6868
// RunDiff gets the differences between Application.MakeCustomizedResMap() and Application.MakeUncustomizedResMap().
6969
func (o *diffOptions) RunDiff(out, errOut io.Writer, fSys fs.FileSystem) error {
7070

71-
l := loader.Init([]loader.SchemeLoader{loader.NewFileLoader(fSys)})
71+
l := loader.NewLoader(loader.NewFileLoader(fSys))
7272

7373
absPath, err := filepath.Abs(o.kustomizationPath)
7474
if err != nil {

pkg/configmapandsecret/configmapfactory.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ func (f *ConfigMapFactory) MakeConfigMap2(
137137
all = append(all, pairs...)
138138

139139
for _, kv := range all {
140-
err = addKeyFromLiteralToConfigMap(cm, kv.key, kv.value)
140+
err = AddKv(cm, kv.key, kv.value)
141141
if err != nil {
142142
return nil, err
143143
}
@@ -166,7 +166,7 @@ func (f *ConfigMapFactory) handleConfigMapFromLiteralSources(
166166
if err != nil {
167167
return err
168168
}
169-
err = addKeyFromLiteralToConfigMap(configMap, k, v)
169+
err = AddKv(configMap, k, v)
170170
if err != nil {
171171
return err
172172
}
@@ -251,7 +251,7 @@ func (f *ConfigMapFactory) handleConfigMapFromEnvFileSource(
251251
return fmt.Errorf("env config file %s cannot be a directory", args.EnvSource)
252252
}
253253
return addFromEnvFile(args.EnvSource, func(key, value string) error {
254-
return addKeyFromLiteralToConfigMap(configMap, key, value)
254+
return AddKv(configMap, key, value)
255255
})
256256
}
257257

@@ -262,12 +262,12 @@ func addKeyFromFileToConfigMap(configMap *v1.ConfigMap, keyName, filePath string
262262
if err != nil {
263263
return err
264264
}
265-
return addKeyFromLiteralToConfigMap(configMap, keyName, string(data))
265+
return AddKv(configMap, keyName, string(data))
266266
}
267267

268-
// addKeyFromLiteralToConfigMap adds the given key and data to the given config map,
269-
// returning an error if the key is not valid or if the key already exists.
270-
func addKeyFromLiteralToConfigMap(configMap *v1.ConfigMap, keyName, data string) error {
268+
// AddKv adds the given key and data to the given config map.
269+
// Error if key invalid, or already exists.
270+
func AddKv(configMap *v1.ConfigMap, keyName, data string) error {
271271
// Note, the rules for ConfigMap keys are the exact same as the ones for SecretKeys.
272272
if errs := validation.IsConfigMapKey(keyName); len(errs) != 0 {
273273
return fmt.Errorf("%q is not a valid key name for a ConfigMap: %s", keyName, strings.Join(errs, ";"))

pkg/configmapandsecret/configmapfactory_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"testing"
2222

2323
"github.com/kubernetes-sigs/kustomize/pkg/fs"
24+
"github.com/kubernetes-sigs/kustomize/pkg/loader"
2425
"github.com/kubernetes-sigs/kustomize/pkg/types"
2526
corev1 "k8s.io/api/core/v1"
2627
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -134,7 +135,9 @@ func TestConstructConfigMap(t *testing.T) {
134135
}
135136

136137
// TODO: all tests should use a FakeFs
137-
f := NewConfigMapFactory(fs.MakeRealFS(), nil)
138+
fSys := fs.MakeRealFS()
139+
f := NewConfigMapFactory(fSys,
140+
loader.NewLoader(loader.NewFileLoader(fSys)))
138141
for _, tc := range testCases {
139142
cm, err := f.MakeConfigMap1(&tc.input)
140143
if err != nil {

pkg/internal/loadertest/fakeloader.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,7 @@ type FakeLoader struct {
3434
func NewFakeLoader(initialDir string) FakeLoader {
3535
// Create fake filesystem and inject it into initial Loader.
3636
fakefs := fs.MakeFakeFS()
37-
var schemes []loader.SchemeLoader
38-
schemes = append(schemes, loader.NewFileLoader(fakefs))
39-
rootLoader := loader.Init(schemes)
37+
rootLoader := loader.NewLoader(loader.NewFileLoader(fakefs))
4038
ldr, _ := rootLoader.New(initialDir)
4139
return FakeLoader{fs: fakefs, delegate: ldr}
4240
}

pkg/loader/fileloader.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,32 @@ import (
2626

2727
const currentDir = "."
2828

29-
// Internal implementation of SchemeLoader interface.
30-
type fileLoader struct {
29+
// FileLoader loads files from a file system.
30+
type FileLoader struct {
3131
fs fs.FileSystem
3232
}
3333

34-
// NewFileLoader returns a SchemeLoader to handle a file system.
35-
func NewFileLoader(fs fs.FileSystem) SchemeLoader {
36-
return &fileLoader{fs: fs}
34+
// NewFileLoader returns a new FileLoader.
35+
func NewFileLoader(fs fs.FileSystem) *FileLoader {
36+
return &FileLoader{fs: fs}
3737
}
3838

39-
// Is the location calculated with the root and location params a full file path.
40-
func (l *fileLoader) IsScheme(root string, location string) bool {
39+
// IsAbsPath return true if the location calculated with the root
40+
// and location params a full file path.
41+
func (l *FileLoader) IsAbsPath(root string, location string) bool {
4142
fullFilePath, err := l.FullLocation(root, location)
4243
if err != nil {
4344
return false
4445
}
4546
return filepath.IsAbs(fullFilePath)
4647
}
4748

49+
// FullLocation returns some notion of a full path.
4850
// If location is a full file path, then ignore root. If location is relative, then
4951
// join the root path with the location path. Either root or location can be empty,
5052
// but not both. Special case for ".": Expands to current working directory.
5153
// Example: "/home/seans/project", "subdir/bar" -> "/home/seans/project/subdir/bar".
52-
func (l *fileLoader) FullLocation(root string, location string) (string, error) {
54+
func (l *FileLoader) FullLocation(root string, location string) (string, error) {
5355
// First, validate the parameters
5456
if len(root) == 0 && len(location) == 0 {
5557
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)
7274

7375
// Load returns the bytes from reading a file at fullFilePath.
7476
// Implements the Loader interface.
75-
func (l *fileLoader) Load(fullFilePath string) ([]byte, error) {
76-
// Validate path to load from is a full file path.
77-
if !filepath.IsAbs(fullFilePath) {
78-
return nil, fmt.Errorf("attempting to load file without full file path: %s\n", fullFilePath)
79-
}
80-
return l.fs.ReadFile(fullFilePath)
77+
func (l *FileLoader) Load(p string) ([]byte, error) {
78+
return l.fs.ReadFile(p)
8179
}
8280

8381
// GlobLoad returns the map from path to bytes from reading a glob path.
8482
// Implements the Loader interface.
85-
func (l *fileLoader) GlobLoad(fullFilePath string) (map[string][]byte, error) {
86-
// Validate path to load from is a full file path.
87-
if !filepath.IsAbs(fullFilePath) {
88-
return nil, fmt.Errorf("Attempting to load file without full file path: %s\n", fullFilePath)
89-
}
90-
return l.fs.ReadFiles(fullFilePath)
83+
func (l *FileLoader) GlobLoad(p string) (map[string][]byte, error) {
84+
return l.fs.ReadFiles(p)
9185
}

pkg/loader/loader.go

Lines changed: 19 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,9 @@ package loader
1919

2020
import "fmt"
2121

22-
// Loader interface exposes methods to read bytes in a scheme-agnostic manner.
23-
// The Loader encapsulating a root location to calculate where to read from.
22+
// Loader interface exposes methods to read bytes.
2423
type Loader interface {
25-
// Root returns the scheme-specific string representing the root location for this Loader.
24+
// Root returns the root location for this Loader.
2625
Root() string
2726
// New returns Loader located at newRoot.
2827
New(newRoot string) (Loader, error)
@@ -32,33 +31,20 @@ type Loader interface {
3231
GlobLoad(location string) (map[string][]byte, error)
3332
}
3433

35-
// Private implmentation of Loader interface.
34+
// Private implementation of Loader interface.
3635
type loaderImpl struct {
3736
root string
38-
schemes []SchemeLoader
39-
}
40-
41-
// SchemeLoader is the interface for different types of loaders (e.g. fileLoader, httpLoader, etc.)
42-
type SchemeLoader interface {
43-
// Does this location correspond to this scheme.
44-
IsScheme(root string, location string) bool
45-
// Combines the root and path into a full location string.
46-
FullLocation(root string, path string) (string, error)
47-
// Load bytes at scheme-specific location or an error.
48-
Load(location string) ([]byte, error)
49-
// GlobLoad returns the bytes read from a glob path or an error.
50-
GlobLoad(location string) (map[string][]byte, error)
37+
fLoader *FileLoader
5138
}
5239

5340
const emptyRoot = ""
5441

55-
// Init initializes the first loader with the supported schemes.
56-
// Example schemes: fileLoader, httpLoader, gitLoader.
57-
func Init(schemes []SchemeLoader) Loader {
58-
return &loaderImpl{root: emptyRoot, schemes: schemes}
42+
// NewLoader initializes the first loader with the supported fLoader.
43+
func NewLoader(fl *FileLoader) Loader {
44+
return &loaderImpl{root: emptyRoot, fLoader: fl}
5945
}
6046

61-
// Root returns the scheme-specific root location for this Loader.
47+
// Root returns the root location for this Loader.
6248
func (l *loaderImpl) Root() string {
6349
return l.root
6450
}
@@ -69,53 +55,35 @@ func (l *loaderImpl) Root() string {
6955
// Example: "/home/seans/project" or "/home/seans/project/"
7056
// NOT "/home/seans/project/file.yaml".
7157
func (l *loaderImpl) New(newRoot string) (Loader, error) {
72-
scheme, err := l.getSchemeLoader(newRoot)
73-
if err != nil {
74-
return nil, err
58+
if !l.fLoader.IsAbsPath(l.root, newRoot) {
59+
return nil, fmt.Errorf("Not abs path: l.root='%s', loc='%s'\n", l.root, newRoot)
7560
}
76-
root, err := scheme.FullLocation(l.root, newRoot)
61+
root, err := l.fLoader.FullLocation(l.root, newRoot)
7762
if err != nil {
7863
return nil, err
7964
}
80-
return &loaderImpl{root: root, schemes: l.schemes}, nil
65+
return &loaderImpl{root: root, fLoader: l.fLoader}, nil
8166
}
8267

83-
// Load returns all the bytes read from scheme-specific location or an error.
68+
// Load returns all the bytes read from location or an error.
8469
// "location" can be an absolute path, or if relative, full location is
8570
// calculated from the Root().
8671
func (l *loaderImpl) Load(location string) ([]byte, error) {
87-
scheme, err := l.getSchemeLoader(location)
88-
if err != nil {
89-
return nil, err
90-
}
91-
fullLocation, err := scheme.FullLocation(l.root, location)
72+
fullLocation, err := l.fLoader.FullLocation(l.root, location)
9273
if err != nil {
74+
fmt.Printf("Trouble in fulllocation: %v\n", err)
9375
return nil, err
9476
}
95-
return scheme.Load(fullLocation)
77+
return l.fLoader.Load(fullLocation)
9678
}
9779

98-
// GlobLoad returns a map from path to bytes read from scheme-specific location or an error.
80+
// GlobLoad returns a map from path to bytes read from the location or an error.
9981
// "location" can be an absolute path, or if relative, full location is
10082
// calculated from the Root().
10183
func (l *loaderImpl) GlobLoad(location string) (map[string][]byte, error) {
102-
scheme, err := l.getSchemeLoader(location)
103-
if err != nil {
104-
return nil, err
105-
}
106-
fullLocation, err := scheme.FullLocation(l.root, location)
84+
fullLocation, err := l.fLoader.FullLocation(l.root, location)
10785
if err != nil {
10886
return nil, err
10987
}
110-
return scheme.GlobLoad(fullLocation)
111-
}
112-
113-
// Helper function to parse scheme from location parameter.
114-
func (l *loaderImpl) getSchemeLoader(location string) (SchemeLoader, error) {
115-
for _, scheme := range l.schemes {
116-
if scheme.IsScheme(l.root, location) {
117-
return scheme, nil
118-
}
119-
}
120-
return nil, fmt.Errorf("Unknown Scheme: %s, %s\n", l.root, location)
88+
return l.fLoader.GlobLoad(fullLocation)
12189
}

pkg/loader/loader_test.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@ import (
2525
)
2626

2727
func initializeRootLoader(fakefs fs.FileSystem) Loader {
28-
var schemes []SchemeLoader
29-
schemes = append(schemes, NewFileLoader(fakefs))
30-
rootLoader := Init(schemes)
31-
return rootLoader
28+
return NewLoader(NewFileLoader(fakefs))
3229
}
3330

3431
func TestLoader_Root(t *testing.T) {
@@ -46,7 +43,7 @@ func TestLoader_Root(t *testing.T) {
4643
}
4744
_, err = rootLoader.New("https://google.com/project")
4845
if err == nil {
49-
t.Fatalf("Expected error for unknown scheme not returned")
46+
t.Fatalf("Expected error")
5047
}
5148

5249
// Test with trailing slash in directory.

0 commit comments

Comments
 (0)