Skip to content

Commit 5fdd751

Browse files
Merge pull request openshift#1459 from JoelSpeed/schema-check-test
Add openshift/crd-schema-checker to verification steps
2 parents cd0541b + 03a4513 commit 5fdd751

File tree

909 files changed

+156656
-3290
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

909 files changed

+156656
-3290
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ verify-scripts:
4949
bash -x hack/verify-integration-tests.sh
5050
bash -x hack/verify-group-versions.sh
5151
bash -x hack/verify-prerelease-lifecycle-gen.sh
52+
bash -x hack/verify-crd-schema-checker.sh
5253

5354
.PHONY: verify
5455
verify: verify-scripts verify-codegen-crds

example/.codegen.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ deepcopy:
44
disabled: false
55
headerFilePath: ../hack/empty.txt
66
outputFileBaseName: zz_generated.deepcopy
7+
schemacheck:
8+
disabled: false
9+
enabledValidators: []
10+
disabledValidators: []
711
schemapatch:
812
disabled: false
913
requiredFeatureSets:

hack/verify-crd-schema-checker.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash
2+
3+
source "$(dirname "${BASH_SOURCE}")/lib/init.sh"
4+
5+
# Use PULL_BASE_REF for CI, otherwise use master unless overriden.
6+
COMPARISON_BASE=${COMPARISON_BASE:-${PULL_BASE_SHA:-"master"}}
7+
8+
GENERATOR=schemacheck EXTRA_ARGS=--schemacheck:comparison-base=${COMPARISON_BASE} ${SCRIPT_ROOT}/hack/update-codegen.sh

openapi/generated_openapi/zz_generated.openapi.go

Lines changed: 98 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

openapi/openapi.json

Lines changed: 98 additions & 28 deletions
Large diffs are not rendered by default.

tools/codegen/cmd/root.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ var rootCmd = &cobra.Command{
4545
return fmt.Errorf("could not build generation context: %w", err)
4646
}
4747

48-
if err := executeGenerators(genCtx, allGenerators()...); err != nil {
48+
generators := allGenerators()
49+
if verify {
50+
generators = append(generators, allVerifiers()...)
51+
}
52+
53+
if err := executeGenerators(genCtx, generators...); err != nil {
4954
return fmt.Errorf("could not run generators: %w", err)
5055
}
5156

@@ -130,6 +135,14 @@ func allGenerators() []generation.Generator {
130135
}
131136
}
132137

138+
// allVerifiers returns an ordered list of verifiers to run when
139+
// the root command is executed with the --verify flag.
140+
func allVerifiers() []generation.Generator {
141+
return []generation.Generator{
142+
newSchemaCheckGenerator(),
143+
}
144+
}
145+
133146
// allMultiGroupGenerators returns an ordered list of multi-group
134147
// generators to run when the root command is executed.
135148
func allMultiGroupGenerators() []generation.MultiGroupGenerator {

tools/codegen/cmd/schemacheck.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/openshift/api/tools/codegen/pkg/generation"
8+
"github.com/openshift/api/tools/codegen/pkg/schemacheck"
9+
"github.com/openshift/crd-schema-checker/pkg/cmd/options"
10+
"github.com/spf13/cobra"
11+
)
12+
13+
var (
14+
defaultComparisonConfig = options.NewComparatorOptions()
15+
comparisonBase = "master"
16+
)
17+
18+
// schemacheckCmd represents the schemacheck command
19+
var schemacheckCmd = &cobra.Command{
20+
Use: "schemacheck",
21+
Short: "schemacheck validates CRD API schemas based on the best practices",
22+
RunE: func(cmd *cobra.Command, args []string) error {
23+
genCtx, err := generation.NewContext(generation.Options{
24+
BaseDir: baseDir,
25+
APIGroupVersions: apiGroupVersions,
26+
})
27+
if err != nil {
28+
return fmt.Errorf("could not build generation context: %w", err)
29+
}
30+
31+
gen := newSchemaCheckGenerator()
32+
33+
return executeGenerators(genCtx, gen)
34+
},
35+
}
36+
37+
func init() {
38+
rootCmd.AddCommand(schemacheckCmd)
39+
40+
knownComparators := strings.Join(defaultComparisonConfig.KnownComparators, ", ")
41+
rootCmd.PersistentFlags().StringSliceVar(&defaultComparisonConfig.DisabledComparators, "schemacheck:disabled-validators", defaultComparisonConfig.DisabledComparators, fmt.Sprintf("list of comparators that must be disabled. Available comparators: %s", knownComparators))
42+
rootCmd.PersistentFlags().StringSliceVar(&defaultComparisonConfig.EnabledComparators, "schemacheck:enabled-validators", defaultComparisonConfig.EnabledComparators, fmt.Sprintf("list of comparators that must be enabled. Available comparators: %s", knownComparators))
43+
rootCmd.PersistentFlags().StringVar(&comparisonBase, "schemacheck:comparison-base", comparisonBase, "base branch/commit to compare against")
44+
}
45+
46+
// newSchemaCheckGenerator builds a new schemacheck generator.
47+
func newSchemaCheckGenerator() generation.Generator {
48+
return schemacheck.NewGenerator(schemacheck.Options{
49+
EnabledComparators: defaultComparisonConfig.EnabledComparators,
50+
DisabledComparators: defaultComparisonConfig.DisabledComparators,
51+
ComparisonBase: comparisonBase,
52+
})
53+
}

tools/codegen/pkg/generation/types.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ type Config struct {
1515
// When omitted, the default configuration will be used.
1616
OpenAPI *OpenAPIConfig `json:"openapi,omitempty"`
1717

18+
// SchemaCheck represents the configuration for the schemacheck generator.
19+
// When omitted, the default configuration will be used.
20+
// When provided, any equivalent flag provided values are ignored.
21+
SchemaCheck *SchemaCheckConfig `json:"schemacheck,omitempty"`
22+
1823
// SchemaPatch represents the configuration for the schemapatch generator.
1924
// When omitted, the default configuration will be used.
2025
// When provided, any equivalent flag provided values are ignored.
@@ -57,6 +62,21 @@ type OpenAPIConfig struct {
5762
Disabled bool `json:"disabled,omitempty"`
5863
}
5964

65+
// SchemaCheckConfig is the configuration for the schemacheck generator.
66+
type SchemaCheckConfig struct {
67+
// Disabled determines whether the schemacheck generator should be run or not.
68+
// This generator is enabled by default so this field defaults to false.
69+
Disabled bool `json:"disabled,omitempty"`
70+
71+
// EnabledValidators is a list of the validators that should be enabled.
72+
// If this is empty, the default validators are enabled.
73+
EnabledValidators []string `json:"enabledValidators,omitempty"`
74+
75+
// DisabledValidators is a list of the validators that should be disabled.
76+
// If this is empty, no default validators are disabled.
77+
DisabledValidators []string `json:"disabledValidators,omitempty"`
78+
}
79+
6080
// SchemaPatchConfig is the configuration for the schemapatch generator.
6181
type SchemaPatchConfig struct {
6282
// Disabled determines whether the schemapatch generator should be run or not.

0 commit comments

Comments
 (0)