/
fileloader.go
224 lines (206 loc) · 6.5 KB
/
fileloader.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
Copyright 2018 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package loader
import (
"fmt"
"log"
"path/filepath"
"strings"
"sigs.k8s.io/kustomize/pkg/fs"
"sigs.k8s.io/kustomize/pkg/ifc"
)
// TODO: 2018/Nov/20 remove this before next release.
// Leave only the true path. Retaining only for
// quick revert.
const enforceRelocatable = true
// fileLoader loads files, returning an array of bytes.
// It also enforces two kustomization requirements:
//
// 1) relocatable
//
// A kustomization and the resources, bases,
// patches, etc. that it depends on should be
// relocatable, so all path specifications
// must be relative, not absolute. The paths
// are taken relative to the location of the
// kusttomization file.
//
// 2) acyclic
//
// There should be no cycles in overlay to base
// relationships, including no cycles between
// git repositories.
//
// The loader has a notion of a current working directory
// (CWD), called 'root', that is independent of the CWD
// of the process. When `Load` is called with a file path
// argument, the load is done relative to this root,
// not relative to the process CWD.
//
// The loader offers a `New` method returning a new loader
// with a new root. The new root can be one of two things,
// a remote git repo URL, or a directory specified relative
// to the current root. In the former case, the remote
// repository is locally cloned, and the new loader is
// rooted on a path in that clone.
//
// Crucially, a root history is used to so that New fails
// if its argument either matches or is a parent of the
// current or any previously used root.
//
// This disallows:
//
// * A base that is a git repository that, in turn,
// specifies a base repository seen previously
// in the loading process (a cycle).
//
// * An overlay depending on a base positioned at or
// above it. I.e. '../foo' is OK, but '.', '..',
// '../..', etc. are disallowed. Allowing such a
// base has no advantages and encourage cycles,
// particularly if some future change were to
// introduce globbing to file specifications in
// the kustomization file.
//
type fileLoader struct {
// Previously visited directories, tracked to
// avoid cycles. The last entry is the current root.
roots []string
// File system utilities.
fSys fs.FileSystem
// Used to clone repositories.
cloner gitCloner
// Used to clean up, as needed.
cleaner func() error
}
// NewFileLoaderAtCwd returns a loader that loads from ".".
func NewFileLoaderAtCwd(fSys fs.FileSystem) *fileLoader {
return newLoaderOrDie(fSys, ".")
}
// NewFileLoaderAtRoot returns a loader that loads from "/".
func NewFileLoaderAtRoot(fSys fs.FileSystem) *fileLoader {
return newLoaderOrDie(fSys, "/")
}
// Root returns the absolute path that is prepended to any
// relative paths used in Load.
func (l *fileLoader) Root() string {
return l.roots[len(l.roots)-1]
}
func newLoaderOrDie(fSys fs.FileSystem, path string) *fileLoader {
l, err := newFileLoaderAt(
path, fSys, []string{}, simpleGitCloner)
if err != nil {
log.Fatalf("unable to make loader at '%s'; %v", path, err)
}
return l
}
// newFileLoaderAt returns a new fileLoader with given root.
func newFileLoaderAt(
root string, fSys fs.FileSystem,
roots []string, cloner gitCloner) (*fileLoader, error) {
if root == "" {
return nil, fmt.Errorf(
"loader root cannot be empty")
}
root, err := filepath.Abs(root)
if err != nil {
return nil, fmt.Errorf(
"absolute path error in '%s' : %v", root, err)
}
if !fSys.IsDir(root) {
return nil, fmt.Errorf("absolute root dir '%s' does not exist", root)
}
return &fileLoader{
roots: append(roots, root),
fSys: fSys,
cloner: cloner,
cleaner: func() error { return nil },
}, nil
}
// New returns a new Loader, rooted relative to current loader,
// or rooted in a temp directory holding a git repo clone.
func (l *fileLoader) New(root string) (ifc.Loader, error) {
if root == "" {
return nil, fmt.Errorf("new root cannot be empty")
}
if isRepoUrl(root) {
if err := l.seenBefore(root); err != nil {
return nil, err
}
return newGitLoader(root, l.fSys, l.roots, l.cloner)
}
if enforceRelocatable && filepath.IsAbs(root) {
return nil, fmt.Errorf("new root '%s' cannot be absolute", root)
}
// Get absolute path to squeeze out "..", ".", etc.
// to facilitate the seenBefore test.
absRoot, err := filepath.Abs(filepath.Join(l.Root(), root))
if err != nil {
return nil, fmt.Errorf(
"problem joining '%s' to '%s': %v", l.Root(), root, err)
}
if err := l.seenBefore(absRoot); err != nil {
return nil, err
}
return newFileLoaderAt(absRoot, l.fSys, l.roots, l.cloner)
}
// newGitLoader returns a new Loader pinned to a temporary
// directory holding a cloned git repo.
func newGitLoader(
root string, fSys fs.FileSystem,
roots []string, cloner gitCloner) (ifc.Loader, error) {
tmpDirForRepo, pathInRepo, err := cloner(root)
if err != nil {
return nil, err
}
trueRoot := filepath.Join(tmpDirForRepo, pathInRepo)
if !fSys.IsDir(trueRoot) {
return nil, fmt.Errorf(
"something wrong cloning '%s'; unable to find '%s'",
root, trueRoot)
}
return &fileLoader{
roots: append(roots, root, trueRoot),
fSys: fSys,
cloner: cloner,
cleaner: func() error { return fSys.RemoveAll(tmpDirForRepo) },
}, nil
}
// seenBefore tests whether the current or any previously
// visited root begins with the given path.
func (l *fileLoader) seenBefore(path string) error {
for _, r := range l.roots {
if strings.HasPrefix(r, path) {
return fmt.Errorf(
"cycle detected: new root '%s' contains previous root '%s'",
path, r)
}
}
return nil
}
// Load returns content of file at the given relative path.
func (l *fileLoader) Load(path string) ([]byte, error) {
if filepath.IsAbs(path) {
if enforceRelocatable {
return nil, fmt.Errorf(
"must use relative path; '%s' is absolute", path)
}
} else {
path = filepath.Join(l.Root(), path)
}
return l.fSys.ReadFile(path)
}
// Cleanup runs the cleaner.
func (l *fileLoader) Cleanup() error {
return l.cleaner()
}