Skip to content

Commit 7911b2c

Browse files
[refactor]: Internalize loader api
This PR intends to move the loader api to internal. Only the necessary methods which are needed for the api have been put into `pkg/loader.go`. Signed-off-by: Varsha Prasad Narsing <[email protected]>
1 parent e3b9afc commit 7911b2c

31 files changed

+109
-97
lines changed

api/internal/accumulator/loadconfigfromcrds_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99

1010
"github.com/stretchr/testify/require"
1111
. "sigs.k8s.io/kustomize/api/internal/accumulator"
12+
"sigs.k8s.io/kustomize/api/internal/loader"
1213
"sigs.k8s.io/kustomize/api/internal/plugins/builtinconfig"
13-
"sigs.k8s.io/kustomize/api/loader"
1414
"sigs.k8s.io/kustomize/api/types"
1515
"sigs.k8s.io/kustomize/kyaml/filesys"
1616
"sigs.k8s.io/kustomize/kyaml/resid"

api/internal/generators/configmap_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/stretchr/testify/assert"
1111
. "sigs.k8s.io/kustomize/api/internal/generators"
1212
"sigs.k8s.io/kustomize/api/kv"
13-
"sigs.k8s.io/kustomize/api/loader"
13+
"sigs.k8s.io/kustomize/api/pkg/loader"
1414
valtest_test "sigs.k8s.io/kustomize/api/testutils/valtest"
1515
"sigs.k8s.io/kustomize/api/types"
1616
"sigs.k8s.io/kustomize/kyaml/filesys"

api/internal/generators/secret_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"github.com/stretchr/testify/assert"
1111
. "sigs.k8s.io/kustomize/api/internal/generators"
1212
"sigs.k8s.io/kustomize/api/kv"
13-
"sigs.k8s.io/kustomize/api/loader"
13+
"sigs.k8s.io/kustomize/api/pkg/loader"
1414
valtest_test "sigs.k8s.io/kustomize/api/testutils/valtest"
1515
"sigs.k8s.io/kustomize/api/types"
1616
"sigs.k8s.io/kustomize/kyaml/filesys"
File renamed without changes.

api/loader/fileloader.go renamed to api/internal/loader/fileloader.go

Lines changed: 43 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func IsRemoteFile(path string) bool {
2525
return err == nil && (u.Scheme == "http" || u.Scheme == "https")
2626
}
2727

28-
// fileLoader is a kustomization's interface to files.
28+
// FileLoader is a kustomization's interface to files.
2929
//
3030
// The directory in which a kustomization file sits
3131
// is referred to below as the kustomization's _root_.
@@ -38,49 +38,48 @@ func IsRemoteFile(path string) bool {
3838
//
3939
// * supplemental data paths
4040
//
41-
// `Load` is used to visit these paths.
41+
// `Load` is used to visit these paths.
4242
//
43-
// These paths refer to resources, patches,
44-
// data for ConfigMaps and Secrets, etc.
43+
// These paths refer to resources, patches,
44+
// data for ConfigMaps and Secrets, etc.
4545
//
46-
// The loadRestrictor may disallow certain paths
47-
// or classes of paths.
46+
// The loadRestrictor may disallow certain paths
47+
// or classes of paths.
4848
//
4949
// * bases (other kustomizations)
5050
//
51-
// `New` is used to load bases.
51+
// `New` is used to load bases.
5252
//
53-
// A base can be either a remote git repo URL, or
54-
// a directory specified relative to the current
55-
// root. In the former case, the repo is locally
56-
// cloned, and the new loader is rooted on a path
57-
// in that clone.
53+
// A base can be either a remote git repo URL, or
54+
// a directory specified relative to the current
55+
// root. In the former case, the repo is locally
56+
// cloned, and the new loader is rooted on a path
57+
// in that clone.
5858
//
59-
// As loaders create new loaders, a root history
60-
// is established, and used to disallow:
59+
// As loaders create new loaders, a root history
60+
// is established, and used to disallow:
6161
//
62-
// - A base that is a repository that, in turn,
63-
// specifies a base repository seen previously
64-
// in the loading stack (a cycle).
62+
// - A base that is a repository that, in turn,
63+
// specifies a base repository seen previously
64+
// in the loading stack (a cycle).
6565
//
66-
// - An overlay depending on a base positioned at
67-
// or above it. I.e. '../foo' is OK, but '.',
68-
// '..', '../..', etc. are disallowed. Allowing
69-
// such a base has no advantages and encourages
70-
// cycles, particularly if some future change
71-
// were to introduce globbing to file
72-
// specifications in the kustomization file.
66+
// - An overlay depending on a base positioned at
67+
// or above it. I.e. '../foo' is OK, but '.',
68+
// '..', '../..', etc. are disallowed. Allowing
69+
// such a base has no advantages and encourages
70+
// cycles, particularly if some future change
71+
// were to introduce globbing to file
72+
// specifications in the kustomization file.
7373
//
7474
// These restrictions assure that kustomizations
7575
// are self-contained and relocatable, and impose
7676
// some safety when relying on remote kustomizations,
7777
// e.g. a remotely loaded ConfigMap generator specified
7878
// to read from /etc/passwd will fail.
79-
//
80-
type fileLoader struct {
79+
type FileLoader struct {
8180
// Loader that spawned this loader.
8281
// Used to avoid cycles.
83-
referrer *fileLoader
82+
referrer *FileLoader
8483

8584
// An absolute, cleaned path to a directory.
8685
// The Load function will read non-absolute
@@ -107,23 +106,9 @@ type fileLoader struct {
107106
cleaner func() error
108107
}
109108

110-
// NewFileLoaderAtCwd returns a loader that loads from PWD.
111-
// A convenience for kustomize edit commands.
112-
func NewFileLoaderAtCwd(fSys filesys.FileSystem) *fileLoader {
113-
return newLoaderOrDie(
114-
RestrictionRootOnly, fSys, filesys.SelfDir)
115-
}
116-
117-
// NewFileLoaderAtRoot returns a loader that loads from "/".
118-
// A convenience for tests.
119-
func NewFileLoaderAtRoot(fSys filesys.FileSystem) *fileLoader {
120-
return newLoaderOrDie(
121-
RestrictionRootOnly, fSys, filesys.Separator)
122-
}
123-
124109
// Repo returns the absolute path to the repo that contains Root if this fileLoader was created from a url
125110
// or the empty string otherwise.
126-
func (fl *fileLoader) Repo() string {
111+
func (fl *FileLoader) Repo() string {
127112
if fl.repoSpec != nil {
128113
return fl.repoSpec.Dir.String()
129114
}
@@ -132,13 +117,13 @@ func (fl *fileLoader) Repo() string {
132117

133118
// Root returns the absolute path that is prepended to any
134119
// relative paths used in Load.
135-
func (fl *fileLoader) Root() string {
120+
func (fl *FileLoader) Root() string {
136121
return fl.root.String()
137122
}
138123

139-
func newLoaderOrDie(
124+
func NewLoaderOrDie(
140125
lr LoadRestrictorFunc,
141-
fSys filesys.FileSystem, path string) *fileLoader {
126+
fSys filesys.FileSystem, path string) *FileLoader {
142127
root, err := filesys.ConfirmDir(fSys, path)
143128
if err != nil {
144129
log.Fatalf("unable to make loader at '%s'; %v", path, err)
@@ -147,12 +132,12 @@ func newLoaderOrDie(
147132
lr, root, fSys, nil, git.ClonerUsingGitExec)
148133
}
149134

150-
// newLoaderAtConfirmedDir returns a new fileLoader with given root.
135+
// newLoaderAtConfirmedDir returns a new FileLoader with given root.
151136
func newLoaderAtConfirmedDir(
152137
lr LoadRestrictorFunc,
153138
root filesys.ConfirmedDir, fSys filesys.FileSystem,
154-
referrer *fileLoader, cloner git.Cloner) *fileLoader {
155-
return &fileLoader{
139+
referrer *FileLoader, cloner git.Cloner) *FileLoader {
140+
return &FileLoader{
156141
loadRestrictor: lr,
157142
root: root,
158143
referrer: referrer,
@@ -164,7 +149,7 @@ func newLoaderAtConfirmedDir(
164149

165150
// New returns a new Loader, rooted relative to current loader,
166151
// or rooted in a temp directory holding a git repo clone.
167-
func (fl *fileLoader) New(path string) (ifc.Loader, error) {
152+
func (fl *FileLoader) New(path string) (ifc.Loader, error) {
168153
if path == "" {
169154
return nil, errors.Errorf("new root cannot be empty")
170155
}
@@ -200,7 +185,7 @@ func (fl *fileLoader) New(path string) (ifc.Loader, error) {
200185
// directory holding a cloned git repo.
201186
func newLoaderAtGitClone(
202187
repoSpec *git.RepoSpec, fSys filesys.FileSystem,
203-
referrer *fileLoader, cloner git.Cloner) (ifc.Loader, error) {
188+
referrer *FileLoader, cloner git.Cloner) (ifc.Loader, error) {
204189
cleaner := repoSpec.Cleaner(fSys)
205190
err := cloner(repoSpec)
206191
if err != nil {
@@ -229,7 +214,7 @@ func newLoaderAtGitClone(
229214
return nil, fmt.Errorf("%q refers to directory outside of repo %q", repoSpec.AbsPath(),
230215
repoSpec.CloneDir())
231216
}
232-
return &fileLoader{
217+
return &FileLoader{
233218
// Clones never allowed to escape root.
234219
loadRestrictor: RestrictionRootOnly,
235220
root: root,
@@ -241,7 +226,7 @@ func newLoaderAtGitClone(
241226
}, nil
242227
}
243228

244-
func (fl *fileLoader) errIfGitContainmentViolation(
229+
func (fl *FileLoader) errIfGitContainmentViolation(
245230
base filesys.ConfirmedDir) error {
246231
containingRepo := fl.containingRepo()
247232
if containingRepo == nil {
@@ -259,7 +244,7 @@ func (fl *fileLoader) errIfGitContainmentViolation(
259244

260245
// Looks back through referrers for a git repo, returning nil
261246
// if none found.
262-
func (fl *fileLoader) containingRepo() *git.RepoSpec {
247+
func (fl *FileLoader) containingRepo() *git.RepoSpec {
263248
if fl.repoSpec != nil {
264249
return fl.repoSpec
265250
}
@@ -271,7 +256,7 @@ func (fl *fileLoader) containingRepo() *git.RepoSpec {
271256

272257
// errIfArgEqualOrHigher tests whether the argument,
273258
// is equal to or above the root of any ancestor.
274-
func (fl *fileLoader) errIfArgEqualOrHigher(
259+
func (fl *FileLoader) errIfArgEqualOrHigher(
275260
candidateRoot filesys.ConfirmedDir) error {
276261
if fl.root.HasPrefix(candidateRoot) {
277262
return fmt.Errorf(
@@ -288,7 +273,7 @@ func (fl *fileLoader) errIfArgEqualOrHigher(
288273
// I.e. Allow a distinction between git URI with
289274
// path foo and tag bar and a git URI with the same
290275
// path but a different tag?
291-
func (fl *fileLoader) errIfRepoCycle(newRepoSpec *git.RepoSpec) error {
276+
func (fl *FileLoader) errIfRepoCycle(newRepoSpec *git.RepoSpec) error {
292277
// TODO(monopole): Use parsed data instead of Raw().
293278
if fl.repoSpec != nil &&
294279
strings.HasPrefix(fl.repoSpec.Raw(), newRepoSpec.Raw()) {
@@ -305,7 +290,7 @@ func (fl *fileLoader) errIfRepoCycle(newRepoSpec *git.RepoSpec) error {
305290
// Load returns the content of file at the given path,
306291
// else an error. Relative paths are taken relative
307292
// to the root.
308-
func (fl *fileLoader) Load(path string) ([]byte, error) {
293+
func (fl *FileLoader) Load(path string) ([]byte, error) {
309294
if IsRemoteFile(path) {
310295
return fl.httpClientGetContent(path)
311296
}
@@ -319,7 +304,7 @@ func (fl *fileLoader) Load(path string) ([]byte, error) {
319304
return fl.fSys.ReadFile(path)
320305
}
321306

322-
func (fl *fileLoader) httpClientGetContent(path string) ([]byte, error) {
307+
func (fl *FileLoader) httpClientGetContent(path string) ([]byte, error) {
323308
var hc *http.Client
324309
if fl.http != nil {
325310
hc = fl.http
@@ -344,6 +329,6 @@ func (fl *fileLoader) httpClientGetContent(path string) ([]byte, error) {
344329
}
345330

346331
// Cleanup runs the cleaner.
347-
func (fl *fileLoader) Cleanup() error {
332+
func (fl *FileLoader) Cleanup() error {
348333
return fl.cleaner()
349334
}

api/loader/fileloader_test.go renamed to api/internal/loader/fileloader_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,9 @@ func MakeFakeFs(td []testData) filesys.FileSystem {
9393
return fSys
9494
}
9595

96-
func makeLoader() *fileLoader {
97-
return NewFileLoaderAtRoot(MakeFakeFs(testCases))
96+
func makeLoader() *FileLoader {
97+
return NewLoaderOrDie(
98+
RestrictionRootOnly, MakeFakeFs(testCases), filesys.Separator)
9899
}
99100

100101
func TestLoaderLoad(t *testing.T) {
@@ -226,7 +227,7 @@ func TestLoaderLocalScheme(t *testing.T) {
226227
dir.Join(filepath.Join(parts...)),
227228
[]byte(content),
228229
))
229-
actualContent, err := newLoaderOrDie(RestrictionRootOnly,
230+
actualContent, err := NewLoaderOrDie(RestrictionRootOnly,
230231
fSys,
231232
dir.String(),
232233
).Load(strings.Join(parts, "//"))
@@ -240,7 +241,7 @@ func TestLoaderLocalScheme(t *testing.T) {
240241
"root",
241242
}
242243
require.NoError(t, fSys.MkdirAll(dir.Join(filepath.Join(parts...))))
243-
ldr, err := newLoaderOrDie(RestrictionRootOnly,
244+
ldr, err := NewLoaderOrDie(RestrictionRootOnly,
244245
fSys,
245246
dir.String(),
246247
).New(strings.Join(parts, "//"))
@@ -322,7 +323,7 @@ func TestRestrictionRootOnlyInRealLoader(t *testing.T) {
322323

323324
var l ifc.Loader
324325

325-
l = newLoaderOrDie(RestrictionRootOnly, fSys, dir)
326+
l = NewLoaderOrDie(RestrictionRootOnly, fSys, dir)
326327

327328
l = doSanityChecksAndDropIntoBase(t, l)
328329

@@ -343,7 +344,7 @@ func TestRestrictionNoneInRealLoader(t *testing.T) {
343344

344345
var l ifc.Loader
345346

346-
l = newLoaderOrDie(RestrictionNone, fSys, dir)
347+
l = NewLoaderOrDie(RestrictionNone, fSys, dir)
347348

348349
l = doSanityChecksAndDropIntoBase(t, l)
349350

@@ -442,7 +443,7 @@ func TestLoaderDisallowsLocalBaseFromRemoteOverlay(t *testing.T) {
442443

443444
// Establish that a local overlay can navigate
444445
// to the local bases.
445-
l1 = newLoaderOrDie(
446+
l1 = NewLoaderOrDie(
446447
RestrictionRootOnly, fSys, cloneRoot+"/foo/overlay")
447448
require.Equal(cloneRoot+"/foo/overlay", l1.Root())
448449

@@ -600,7 +601,8 @@ func TestLoaderHTTP(t *testing.T) {
600601
},
601602
}
602603

603-
l1 := NewFileLoaderAtRoot(MakeFakeFs(testCasesFile))
604+
l1 := NewLoaderOrDie(
605+
RestrictionRootOnly, MakeFakeFs(testCasesFile), filesys.Separator)
604606
require.Equal("/", l1.Root())
605607

606608
for _, x := range testCasesFile {
File renamed without changes.

api/internal/localizer/localizer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111

1212
"sigs.k8s.io/kustomize/api/ifc"
1313
"sigs.k8s.io/kustomize/api/internal/generators"
14+
"sigs.k8s.io/kustomize/api/internal/loader"
1415
"sigs.k8s.io/kustomize/api/internal/target"
15-
"sigs.k8s.io/kustomize/api/loader"
1616
"sigs.k8s.io/kustomize/api/provider"
1717
"sigs.k8s.io/kustomize/api/resmap"
1818
"sigs.k8s.io/kustomize/api/types"

0 commit comments

Comments
 (0)