Skip to content

Commit 167481c

Browse files
committed
fixup! Update CRD generator to support experimental CRDs
1 parent 30aeef7 commit 167481c

File tree

6 files changed

+1796
-7
lines changed

6 files changed

+1796
-7
lines changed

hack/tools/crd-generator/main.go

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func main() {
5757
func runGenerator(args ...string) {
5858
outputDir := "config/crd"
5959
ctVer := ""
60+
crdRoot := "github.com/operator-framework/operator-controller/api/v1"
6061
if len(args) >= 1 {
6162
// Get the output directory
6263
outputDir = args[0]
@@ -65,10 +66,15 @@ func runGenerator(args ...string) {
6566
// get the controller-tools version
6667
ctVer = args[1]
6768
}
69+
if len(args) >= 3 {
70+
crdRoot = args[2]
71+
}
72+
73+
log.Printf("crdRoot: %s", crdRoot)
6874

6975
roots, err := loader.LoadRoots(
7076
"k8s.io/apimachinery/pkg/runtime/schema", // Needed to parse generated register functions.
71-
"github.com/operator-framework/operator-controller/api/v1",
77+
crdRoot,
7278
)
7379
if err != nil {
7480
log.Fatalf("failed to load package roots: %s", err)
@@ -90,6 +96,7 @@ func runGenerator(args ...string) {
9096

9197
crd.AddKnownTypes(parser)
9298
for _, r := range roots {
99+
log.Printf("Looking at package %v", r)
93100
parser.NeedPackage(r)
94101
}
95102

@@ -137,6 +144,7 @@ func runGenerator(args ...string) {
137144

138145
conv, err := crd.AsVersion(*channelCrd, apiextensionsv1.SchemeGroupVersion)
139146
if err != nil {
147+
log.Printf("CRD: %v", *channelCrd)
140148
log.Fatalf("failed to convert CRD: %s", err)
141149
}
142150

@@ -166,6 +174,8 @@ func runGenerator(args ...string) {
166174
out = append(breakLine, out...)
167175
}
168176

177+
log.Printf("writing %v bytes", len(out))
178+
169179
fileName := fmt.Sprintf("%s/%s/%s_%s.yaml", outputDir, channel, crdRaw.Spec.Group, crdRaw.Spec.Names.Plural)
170180
err = os.WriteFile(fileName, out, 0o600)
171181
if err != nil {
@@ -206,6 +216,8 @@ func opconTweaks(channel string, name string, jsonProps apiextensionsv1.JSONSche
206216
numExpressions := strings.Count(jsonProps.Description, validationPrefix)
207217
numValid := 0
208218
if numExpressions > 0 {
219+
log.Printf("found validations")
220+
209221
enumRe := regexp.MustCompile(validationPrefix + "Enum=([A-Za-z;]*)>")
210222
enumMatches := enumRe.FindAllStringSubmatch(jsonProps.Description, 64)
211223
for _, enumMatch := range enumMatches {
@@ -220,7 +232,7 @@ func opconTweaks(channel string, name string, jsonProps apiextensionsv1.JSONSche
220232
}
221233
}
222234

223-
celRe := regexp.MustCompile(validationPrefix + "XValidation:message=\"([^\"]*)\",rule=\"([^\"]*)\">")
235+
celRe := regexp.MustCompile(validationPrefix + "XValidation:rule=\"([^\"]*)\",message=\"([^\"]*)\">")
224236
celMatches := celRe.FindAllStringSubmatch(jsonProps.Description, 64)
225237
for _, celMatch := range celMatches {
226238
if len(celMatch) != 3 {
@@ -262,6 +274,7 @@ func formatDescription(description string, channel string, name string) string {
262274
log.Fatalf("Invalid <opcon:experimental:description> tag for %s", name)
263275
}
264276
description = re.ReplaceAllString(description, "\n\n")
277+
log.Printf("found experimental:description")
265278
} else {
266279
description = strings.ReplaceAll(description, startTag, "")
267280
description = strings.ReplaceAll(description, endTag, "")
@@ -279,6 +292,7 @@ func formatDescription(description string, channel string, name string) string {
279292
log.Fatalf("Invalid <opcon:util:excludeFromCRD> tag for %s", name)
280293
}
281294
description = re.ReplaceAllString(description, "\n\n\n")
295+
log.Printf("found excludeFromCRD")
282296
}
283297

284298
opconRe := regexp.MustCompile(`<opcon:.*>`)

hack/tools/crd-generator/main_test.go

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,64 @@ import (
1111
)
1212

1313
func TestRunGenerator(t *testing.T) {
14+
here, err := os.Getwd()
15+
require.NoError(t, err)
1416
// Get to repo root
15-
err := os.Chdir("../../..")
17+
err = os.Chdir("../../..")
1618
require.NoError(t, err)
19+
defer os.Chdir(here)
1720
dir, err := os.MkdirTemp("", "crd-generate-*")
1821
require.NoError(t, err)
1922
defer os.RemoveAll(dir)
2023
require.NoError(t, os.Mkdir(filepath.Join(dir, "standard"), 0o700))
2124
require.NoError(t, os.Mkdir(filepath.Join(dir, "experimental"), 0o700))
2225
runGenerator(dir, "v0.17.3")
2326

24-
f1 := filepath.Join(dir, "/standard/olm.operatorframework.io_clusterextensions.yaml")
27+
f1 := filepath.Join(dir, "standard/olm.operatorframework.io_clusterextensions.yaml")
2528
f2 := "config/base/operator-controller/crd/standard/olm.operatorframework.io_clusterextensions.yaml"
2629
fmt.Printf("comparing: %s to %s\n", f1, f2)
2730
compareFiles(t, f1, f2)
2831

29-
f1 = filepath.Join(dir, "/standard/olm.operatorframework.io_clustercatalogs.yaml")
32+
f1 = filepath.Join(dir, "standard/olm.operatorframework.io_clustercatalogs.yaml")
3033
f2 = "config/base/catalogd/crd/standard/olm.operatorframework.io_clustercatalogs.yaml"
3134
fmt.Printf("comparing: %s to %s\n", f1, f2)
3235
compareFiles(t, f1, f2)
3336

34-
f1 = filepath.Join(dir, "/experimental/olm.operatorframework.io_clusterextensions.yaml")
37+
f1 = filepath.Join(dir, "experimental/olm.operatorframework.io_clusterextensions.yaml")
3538
f2 = "config/base/operator-controller/crd/experimental/olm.operatorframework.io_clusterextensions.yaml"
3639
fmt.Printf("comparing: %s to %s\n", f1, f2)
3740
compareFiles(t, f1, f2)
3841

39-
f1 = filepath.Join(dir, "/experimental/olm.operatorframework.io_clustercatalogs.yaml")
42+
f1 = filepath.Join(dir, "experimental/olm.operatorframework.io_clustercatalogs.yaml")
4043
f2 = "config/base/catalogd/crd/experimental/olm.operatorframework.io_clustercatalogs.yaml"
4144
fmt.Printf("comparing: %s to %s\n", f1, f2)
4245
compareFiles(t, f1, f2)
4346
}
4447

48+
func TestTags(t *testing.T) {
49+
here, err := os.Getwd()
50+
require.NoError(t, err)
51+
err = os.Chdir("testdata")
52+
defer os.Chdir(here)
53+
require.NoError(t, err)
54+
dir, err := os.MkdirTemp("", "crd-generate-*")
55+
require.NoError(t, err)
56+
defer os.RemoveAll(dir)
57+
require.NoError(t, os.Mkdir(filepath.Join(dir, "standard"), 0o700))
58+
require.NoError(t, os.Mkdir(filepath.Join(dir, "experimental"), 0o700))
59+
runGenerator(dir, "v0.17.3", "github.com/operator-framework/operator-controller/hack/tools/crd-generator/testdata/api/v1")
60+
61+
f1 := filepath.Join(dir, "standard/olm.operatorframework.io_clusterextensions.yaml")
62+
f2 := "output/standard/olm.operatorframework.io_clusterextensions.yaml"
63+
fmt.Printf("comparing: %s to %s\n", f1, f2)
64+
compareFiles(t, f1, f2)
65+
66+
f1 = filepath.Join(dir, "experimental/olm.operatorframework.io_clusterextensions.yaml")
67+
f2 = "output/experimental/olm.operatorframework.io_clusterextensions.yaml"
68+
fmt.Printf("comparing: %s to %s\n", f1, f2)
69+
compareFiles(t, f1, f2)
70+
}
71+
4572
func compareFiles(t *testing.T, file1, file2 string) {
4673
f1, err := os.Open(file1)
4774
require.NoError(t, err)

0 commit comments

Comments
 (0)