Skip to content

Commit 933c39c

Browse files
authored
Merge pull request crossplane#6205 from cychiang/6057-fix-duplicate-results-due-to-cache-issue
fix(cli): Resolve duplicate validate results due to multiple version of dependencies in cache
2 parents f714904 + 89bbd89 commit 933c39c

File tree

4 files changed

+34
-14
lines changed

4 files changed

+34
-14
lines changed

cmd/crank/beta/validate/cache.go

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type Cache interface {
3232
Store(schemas [][]byte, path string) error
3333
Flush() error
3434
Init() error
35-
Load() ([]*unstructured.Unstructured, error)
35+
Load(image string) ([]*unstructured.Unstructured, error)
3636
Exists(image string) (string, error)
3737
}
3838

@@ -86,26 +86,24 @@ func (c *LocalCache) Flush() error {
8686
return c.fs.RemoveAll(c.cacheDir)
8787
}
8888

89-
// Load loads the schemas from the cache directory.
90-
func (c *LocalCache) Load() ([]*unstructured.Unstructured, error) {
91-
loader, err := NewLoader(c.cacheDir)
89+
// Load loads schemas from the cache directory.
90+
// image should be a validate image name with the format: <registry>/<image>:<tag>.
91+
func (c *LocalCache) Load(image string) ([]*unstructured.Unstructured, error) {
92+
cacheImagePath := c.getCachePath(image)
93+
loader, err := NewLoader(cacheImagePath)
9294
if err != nil {
93-
return nil, errors.Wrapf(err, "cannot create loader from the path %s", c.cacheDir)
95+
return nil, errors.Wrapf(err, "cannot create loader from %s", cacheImagePath)
9496
}
95-
9697
schemas, err := loader.Load()
9798
if err != nil {
98-
return nil, errors.Wrapf(err, "cannot load schemas from the path %s", c.cacheDir)
99+
return nil, errors.Wrapf(err, "cannot load schemas from %s", cacheImagePath)
99100
}
100-
101101
return schemas, nil
102102
}
103103

104104
// Exists checks if the cache contains the image and returns the path if it doesn't exist.
105105
func (c *LocalCache) Exists(image string) (string, error) {
106-
fName := strings.ReplaceAll(image, ":", "@")
107-
path := filepath.Join(c.cacheDir, fName)
108-
106+
path := c.getCachePath(image)
109107
_, err := os.Stat(path)
110108
if err != nil && os.IsNotExist(err) {
111109
return path, nil
@@ -115,3 +113,9 @@ func (c *LocalCache) Exists(image string) (string, error) {
115113

116114
return "", nil
117115
}
116+
117+
// getCachePath transforms an image name to a validate folder path that store schemas.
118+
func (c *LocalCache) getCachePath(image string) string {
119+
cacheImagePath := strings.ReplaceAll(image, ":", "@")
120+
return filepath.Join(c.cacheDir, cacheImagePath)
121+
}

cmd/crank/beta/validate/cache_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@ func TestLocalCacheInit(t *testing.T) {
149149
func TestLocalCacheLoad(t *testing.T) {
150150
type args struct {
151151
cacheDir string
152+
image string
152153
}
153154
type want struct {
154155
schemas []*unstructured.Unstructured
@@ -161,7 +162,10 @@ func TestLocalCacheLoad(t *testing.T) {
161162
}{
162163
"Load": {
163164
reason: "Load should load the schemas from the cache",
164-
args: args{cacheDir: "./testdata/crds"},
165+
args: args{
166+
cacheDir: "./testdata/crds",
167+
image: "xpkg.upbound.io/provider-dummy:v1.0.0",
168+
},
165169
want: want{
166170
schemas: []*unstructured.Unstructured{
167171
{
@@ -193,7 +197,7 @@ func TestLocalCacheLoad(t *testing.T) {
193197
cacheDir: tc.args.cacheDir,
194198
}
195199

196-
got, err := c.Load()
200+
got, err := c.Load(tc.args.image)
197201
if diff := cmp.Diff(tc.want.schemas, got); diff != "" {
198202
t.Errorf("%s\nLoad(...): -want, +got:\n%s", tc.reason, diff)
199203
}

cmd/crank/beta/validate/manager.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ func (m *Manager) CacheAndLoad(cleanCache bool) error {
199199
return errors.Wrapf(err, "cannot cache package dependencies")
200200
}
201201

202-
schemas, err := m.cache.Load()
202+
schemas, err := m.loadDependencies()
203203
if err != nil {
204204
return errors.Wrapf(err, "cannot load cache")
205205
}
@@ -312,3 +312,15 @@ func (m *Manager) cacheDependencies() error {
312312

313313
return nil
314314
}
315+
316+
func (m *Manager) loadDependencies() ([]*unstructured.Unstructured, error) {
317+
schemas := make([]*unstructured.Unstructured, 0)
318+
for dep := range m.deps {
319+
cachedSchema, err := m.cache.Load(dep)
320+
if err != nil {
321+
return nil, errors.Wrapf(err, "cannot load cache for %s", dep)
322+
}
323+
schemas = append(schemas, cachedSchema...)
324+
}
325+
return schemas, nil
326+
}
File renamed without changes.

0 commit comments

Comments
 (0)