Skip to content

Commit 7c79ec3

Browse files
authored
Refactor deprecation check to shared crdmanagement package (#4856)
For future re-use
1 parent 861bdaf commit 7c79ec3

File tree

2 files changed

+55
-41
lines changed

2 files changed

+55
-41
lines changed

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

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package crd
88
import (
99
"context"
1010
"fmt"
11-
"regexp"
1211
"strings"
1312
"time"
1413

@@ -96,7 +95,7 @@ func (c *Cleaner) Run(ctx context.Context) error {
9695

9796
for _, crd := range crds {
9897
asoCRDsSeen++
99-
newStoredVersions, deprecatedVersions := getDeprecatedStorageVersions(crd)
98+
newStoredVersions, deprecatedVersions := crdmanagement.GetDeprecatedStorageVersions(crd)
10099

101100
// 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
102101
if len(newStoredVersions) <= 0 {
@@ -263,42 +262,3 @@ func (c *Cleaner) getObjectsForMigration(ctx context.Context, crd apiextensions.
263262

264263
return list, nil
265264
}
266-
267-
var deprecatedVersionRegexp = regexp.MustCompile(`((v1alpha1api|v1beta)\d{8}(preview)?(storage)?|v1beta1)`) // handcrafted (non-ARM) resources have v1beta1 version
268-
269-
// deprecatedVersionsMap is a map of crd name to a collection of deprecated versions
270-
var deprecatedVersionsMap = map[string][]string{
271-
"trustedaccessrolebindings.containerservice.azure.com": {"v1api20230202previewstorage"},
272-
}
273-
274-
func isVersionDeprecated(crd apiextensions.CustomResourceDefinition, version string) bool {
275-
if deprecatedVersionRegexp.MatchString(version) {
276-
return true
277-
}
278-
279-
for _, deprecatedVersion := range deprecatedVersionsMap[crd.Name] {
280-
if deprecatedVersion == version {
281-
return true
282-
}
283-
}
284-
285-
return false
286-
}
287-
288-
// getDeprecatedStorageVersions returns a new list of storedVersions by removing the deprecated versions
289-
func getDeprecatedStorageVersions(crd apiextensions.CustomResourceDefinition) ([]string, []string) {
290-
storedVersions := crd.Status.StoredVersions
291-
292-
newStoredVersions := make([]string, 0, len(storedVersions))
293-
var removedVersions []string
294-
for _, version := range storedVersions {
295-
if isVersionDeprecated(crd, version) {
296-
removedVersions = append(removedVersions, version)
297-
continue
298-
}
299-
300-
newStoredVersions = append(newStoredVersions, version)
301-
}
302-
303-
return newStoredVersions, removedVersions
304-
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) Microsoft Corporation.
3+
* Licensed under the MIT license.
4+
*/
5+
6+
package crdmanagement
7+
8+
import (
9+
"regexp"
10+
11+
apiextensions "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
12+
)
13+
14+
var deprecatedVersionRegexp = regexp.MustCompile(`((v1alpha1api|v1beta)\d{8}(preview)?(storage)?|v1beta1)`) // handcrafted (non-ARM) resources have v1beta1 version
15+
16+
// deprecatedVersionsMap is a map of crd name to a collection of deprecated versions
17+
var deprecatedVersionsMap = map[string][]string{
18+
"trustedaccessrolebindings.containerservice.azure.com": {"v1api20230202previewstorage"},
19+
}
20+
21+
// IsVersionDeprecated checks if a given version of a CRD is deprecated.
22+
func IsVersionDeprecated(crd apiextensions.CustomResourceDefinition, version string) bool {
23+
if deprecatedVersionRegexp.MatchString(version) {
24+
return true
25+
}
26+
27+
for _, deprecatedVersion := range deprecatedVersionsMap[crd.Name] {
28+
if deprecatedVersion == version {
29+
return true
30+
}
31+
}
32+
33+
return false
34+
}
35+
36+
// GetDeprecatedStorageVersions returns a new list of storedVersions by removing the deprecated versions
37+
// The first return value is the list of versions that are NOT deprecated.
38+
// The second return value is the list of versions that ARE deprecated.
39+
func GetDeprecatedStorageVersions(crd apiextensions.CustomResourceDefinition) ([]string, []string) {
40+
storedVersions := crd.Status.StoredVersions
41+
42+
newStoredVersions := make([]string, 0, len(storedVersions))
43+
var removedVersions []string
44+
for _, version := range storedVersions {
45+
if IsVersionDeprecated(crd, version) {
46+
removedVersions = append(removedVersions, version)
47+
continue
48+
}
49+
50+
newStoredVersions = append(newStoredVersions, version)
51+
}
52+
53+
return newStoredVersions, removedVersions
54+
}

0 commit comments

Comments
 (0)