Skip to content

Commit 60cc92b

Browse files
committed
Fix tests and PR feedback
1 parent 0b5ea88 commit 60cc92b

File tree

3 files changed

+135
-12
lines changed

3 files changed

+135
-12
lines changed

v2/cmd/asoctl/internal/crd/cleaner.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,8 @@ func (c *Cleaner) Run(ctx context.Context) error {
9797
asoCRDsSeen++
9898
newStoredVersions, deprecatedVersions := crdmanagement.GetDeprecatedStorageVersions(crd)
9999

100-
// If there is no new version found other than the matched version, we short circuit here, as there is no updated version found in the CRDs
101-
if len(newStoredVersions) <= 0 {
102-
// TODO: test?
100+
// If there is no new version found other than alpha/beta, it means there wasn't a newer version installed/reconciled yet and we short circuit here, as there is no updated version found in the CRDs
101+
if len(newStoredVersions) == 1 && (strings.HasPrefix(newStoredVersions[0], "v1alpha") || strings.HasPrefix(newStoredVersions[0], "v1beta")) {
103102
return eris.Errorf("it doesn't look like your version of ASO is one that supports deprecating CRD %q, versions %q. Have you upgraded ASO yet?", crd.Name, deprecatedVersions)
104103
}
105104

v2/internal/crdmanagement/deprecation.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1313
)
1414

15-
var versionRegexp = regexp.MustCompile(`^v(1api)?(\d{8})(preview)?(storage)?$`)
15+
var versionRegexp = regexp.MustCompile(`^v(1api|1alpha1api|1beta)?(\d{8}|\d)(preview)?(storage)?$`)
1616

1717
// GetDeprecatedStorageVersions returns a new list of storedVersions by removing the deprecated versions
1818
// The first return value is the list of versions that are NOT deprecated.
@@ -79,6 +79,26 @@ func CompareVersions(a string, b string) int {
7979
return 1
8080
}
8181

82+
// Prefer everything over v1alpha1apiYYYYMMDD
83+
hasV1Alpha1A := matchA[1] == "1alpha1api"
84+
hasV1Alpha1B := matchB[1] == "1alpha1api"
85+
86+
if hasV1Alpha1A && !hasV1Alpha1B {
87+
return -1
88+
} else if !hasV1Alpha1A && hasV1Alpha1B {
89+
return 1
90+
}
91+
92+
// Prefer everything over v1betaYYYYMMDD
93+
hasV1BetaA := matchA[1] == "1beta"
94+
hasV1BetaB := matchB[1] == "1beta"
95+
96+
if hasV1BetaA && !hasV1BetaB {
97+
return -1
98+
} else if !hasV1BetaA && hasV1BetaB {
99+
return 1
100+
}
101+
82102
// Prefer newer date over older date
83103
dateA := matchA[2]
84104
dateB := matchB[2]
@@ -90,12 +110,12 @@ func CompareVersions(a string, b string) int {
90110
}
91111

92112
// Prefer vYYYYMMDD over v1apiYYYYMMDD
93-
hasV1apiA := matchA[1] == "1api"
94-
hasV1apiB := matchB[1] == "1api"
113+
hasV1ApiA := matchA[1] == "1api"
114+
hasV1ApiB := matchB[1] == "1api"
95115

96-
if hasV1apiA && !hasV1apiB {
116+
if hasV1ApiA && !hasV1ApiB {
97117
return -1
98-
} else if !hasV1apiA && hasV1apiB {
118+
} else if !hasV1ApiA && hasV1ApiB {
99119
return 1
100120
}
101121

v2/internal/crdmanagement/deprecation_test.go

Lines changed: 108 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,19 +287,19 @@ func Test_CompareVersions(t *testing.T) {
287287
{
288288
name: "One invalid pattern (a < b)",
289289
a: "v1api20240101",
290-
b: "v1beta1",
290+
b: "v1betazzz1",
291291
expected: -1,
292292
},
293293
{
294294
name: "One invalid pattern (a > b)",
295-
a: "v1beta1",
295+
a: "v1betazzz1",
296296
b: "v1api20240101",
297297
expected: 1,
298298
},
299299
{
300300
name: "Equal invalid patterns",
301-
a: "v1beta1",
302-
b: "v1beta1",
301+
a: "v1betazzz1",
302+
b: "v1betazzz1",
303303
expected: 0,
304304
},
305305
{
@@ -332,6 +332,110 @@ func Test_CompareVersions(t *testing.T) {
332332
b: "v1api20240101storage",
333333
expected: -1,
334334
},
335+
// v1alpha1api tests
336+
{
337+
name: "v1alpha1api vs v1api same date (a < b)",
338+
a: "v1alpha1api20240101",
339+
b: "v1api20240101",
340+
expected: -1,
341+
},
342+
{
343+
name: "v1alpha1api vs v1api same date (a > b)",
344+
a: "v1api20240101",
345+
b: "v1alpha1api20240101",
346+
expected: 1,
347+
},
348+
{
349+
name: "v1alpha1api vs v same date (a < b)",
350+
a: "v1alpha1api20240101",
351+
b: "v20240101",
352+
expected: -1,
353+
},
354+
{
355+
name: "v1alpha1api vs v same date (a > b)",
356+
a: "v20240101",
357+
b: "v1alpha1api20240101",
358+
expected: 1,
359+
},
360+
{
361+
name: "v1alpha1api newer date vs v1api older date (a < b)",
362+
a: "v1alpha1api20250101",
363+
b: "v1api20240101",
364+
expected: -1,
365+
},
366+
{
367+
name: "v1alpha1api newer date vs v older date (a < b)",
368+
a: "v1alpha1api20250101",
369+
b: "v20240101",
370+
expected: -1,
371+
},
372+
// v1beta tests
373+
{
374+
name: "v1beta vs v1api same date (a < b)",
375+
a: "v1beta20240101",
376+
b: "v1api20240101",
377+
expected: -1,
378+
},
379+
{
380+
name: "v1beta vs v1api same date (a > b)",
381+
a: "v1api20240101",
382+
b: "v1beta20240101",
383+
expected: 1,
384+
},
385+
{
386+
name: "v1beta vs v same date (a < b)",
387+
a: "v1beta20240101",
388+
b: "v20240101",
389+
expected: -1,
390+
},
391+
{
392+
name: "v1beta vs v same date (a > b)",
393+
a: "v20240101",
394+
b: "v1beta20240101",
395+
expected: 1,
396+
},
397+
{
398+
name: "v1beta newer date vs v1api older date (a < b)",
399+
a: "v1beta20250101",
400+
b: "v1api20240101",
401+
expected: -1,
402+
},
403+
{
404+
name: "v1beta newer date vs v older date (a < b)",
405+
a: "v1beta20250101",
406+
b: "v20240101",
407+
expected: -1,
408+
},
409+
{
410+
name: "v1beta vs v1alpha1api same date (a > b)",
411+
a: "v1beta20240101",
412+
b: "v1alpha1api20240101",
413+
expected: 1,
414+
},
415+
{
416+
name: "v1beta vs v1alpha1api same date (a < b)",
417+
a: "v1alpha1api20240101",
418+
b: "v1beta20240101",
419+
expected: -1,
420+
},
421+
{
422+
name: "v1beta with preview vs non-preview v1api (a < b)",
423+
a: "v1beta20240101preview",
424+
b: "v1api20240101",
425+
expected: -1,
426+
},
427+
{
428+
name: "v1beta with storage vs v1api with storage same date (a < b)",
429+
a: "v1beta20240101storage",
430+
b: "v1api20240101storage",
431+
expected: -1,
432+
},
433+
{
434+
name: "v1beta vs v1 (a < b)",
435+
a: "v1beta1",
436+
b: "v1",
437+
expected: -1,
438+
},
335439
}
336440

337441
for _, c := range cases {

0 commit comments

Comments
 (0)