diff --git a/constraint/Makefile b/constraint/Makefile index ea4d54b3d..bff28a719 100644 --- a/constraint/Makefile +++ b/constraint/Makefile @@ -64,14 +64,14 @@ generate: generate-defaults --extra-dirs=k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1 CRD_SOURCE_FILE := deploy/crds.yaml -FILE_STUB := "package templates\ +FILE_STUB := "package schema\ \n\ \n// This file is generated from $(CRD_SOURCE_FILE) via \"make constraint-template-string-constant\"\ \n// DO NOT MODIFY THIS FILE DIRECTLY!\ \n\ \nconst constraintTemplateCRDYaml = \`" -YAML_CONSTANT_GOLANG_FILE := ./pkg/apis/templates/yaml_constant.go +YAML_CONSTANT_GOLANG_FILE := ./pkg/schema/yaml_constant.go constraint-template-string-constant: manifests rm -rf $(YAML_CONSTANT_GOLANG_FILE) diff --git a/constraint/pkg/apis/templates/init.go b/constraint/pkg/apis/templates/init.go deleted file mode 100644 index cccdffdd9..000000000 --- a/constraint/pkg/apis/templates/init.go +++ /dev/null @@ -1,12 +0,0 @@ -package templates - -// While multiple `init()` functions might seem like the way to handle this, the dependency between -// the two functions makes this a poor solution. Golang orders the files in a package -// lexicographically and then runs their init() functions in that order. As crd_scheme.go comes -// before scheme.go, initializeCTSchemaMap() was run before initializeSchema(). This caused a -// null-pointer exception, as the Scheme used throughout the package hadn't yet been initialized. -// Calling them in order here fixes that problem. -func init() { - initializeScheme() - initializeCTSchemaMap() -} diff --git a/constraint/pkg/apis/templates/scheme.go b/constraint/pkg/apis/templates/scheme.go deleted file mode 100644 index 913b82940..000000000 --- a/constraint/pkg/apis/templates/scheme.go +++ /dev/null @@ -1,20 +0,0 @@ -package templates - -import ( - "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" - apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - "k8s.io/apimachinery/pkg/runtime" -) - -// Scheme is the global schema used for transforming Templates between API Versions. -var Scheme *runtime.Scheme - -func initializeScheme() { - Scheme = runtime.NewScheme() - if err := apiextensionsv1.AddToScheme(Scheme); err != nil { - panic(err) - } - if err := apiextensions.AddToScheme(Scheme); err != nil { - panic(err) - } -} diff --git a/constraint/pkg/apis/templates/v1/constrainttemplate_types_test.go b/constraint/pkg/apis/templates/v1/constrainttemplate_types_test.go index 138a3bc4a..3eea0975d 100644 --- a/constraint/pkg/apis/templates/v1/constrainttemplate_types_test.go +++ b/constraint/pkg/apis/templates/v1/constrainttemplate_types_test.go @@ -21,8 +21,8 @@ import ( "github.com/google/go-cmp/cmp" "github.com/onsi/gomega" - apisTemplates "github.com/open-policy-agent/frameworks/constraint/pkg/apis/templates" "github.com/open-policy-agent/frameworks/constraint/pkg/core/templates" + "github.com/open-policy-agent/frameworks/constraint/pkg/schema" "golang.org/x/net/context" "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" @@ -155,31 +155,31 @@ func TestValidationVersionConversionAndTransformation(t *testing.T) { name: "Two deep properties, LegacySchema=true", v: &Validation{ LegacySchema: &trueBool, - OpenAPIV3Schema: apisTemplates.VersionedIncompleteSchema(), + OpenAPIV3Schema: schema.VersionedIncompleteSchema(), }, exp: &templates.Validation{ LegacySchema: &trueBool, - OpenAPIV3Schema: apisTemplates.VersionlessSchemaWithXPreserve(), + OpenAPIV3Schema: schema.VersionlessSchemaWithXPreserve(), }, }, { name: "Two deep properties, LegacySchema=false", v: &Validation{ LegacySchema: &falseBool, - OpenAPIV3Schema: apisTemplates.VersionedIncompleteSchema(), + OpenAPIV3Schema: schema.VersionedIncompleteSchema(), }, exp: &templates.Validation{ LegacySchema: &falseBool, - OpenAPIV3Schema: apisTemplates.VersionlessSchema(), + OpenAPIV3Schema: schema.VersionlessSchema(), }, }, { name: "Two deep properties, LegacySchema=nil", v: &Validation{ - OpenAPIV3Schema: apisTemplates.VersionedIncompleteSchema(), + OpenAPIV3Schema: schema.VersionedIncompleteSchema(), }, exp: &templates.Validation{ - OpenAPIV3Schema: apisTemplates.VersionlessSchema(), + OpenAPIV3Schema: schema.VersionlessSchema(), }, }, { diff --git a/constraint/pkg/apis/templates/v1/conversion.go b/constraint/pkg/apis/templates/v1/conversion.go index ac92d0b2c..7081c15e9 100644 --- a/constraint/pkg/apis/templates/v1/conversion.go +++ b/constraint/pkg/apis/templates/v1/conversion.go @@ -16,8 +16,8 @@ limitations under the License. package v1 import ( - apisTemplates "github.com/open-policy-agent/frameworks/constraint/pkg/apis/templates" coreTemplates "github.com/open-policy-agent/frameworks/constraint/pkg/core/templates" + "github.com/open-policy-agent/frameworks/constraint/pkg/schema" "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" "k8s.io/apimachinery/pkg/conversion" @@ -35,7 +35,7 @@ func Convert_v1_Validation_To_templates_Validation(in *Validation, out *coreTemp inSchemaCopy := inSchema.DeepCopy() if in.LegacySchema != nil && *in.LegacySchema { - if err := apisTemplates.AddPreserveUnknownFields(inSchemaCopy); err != nil { + if err := schema.AddPreserveUnknownFields(inSchemaCopy); err != nil { return err } } diff --git a/constraint/pkg/apis/templates/v1/defaults.go b/constraint/pkg/apis/templates/v1/defaults.go index 7a9320414..b00aa1ee3 100644 --- a/constraint/pkg/apis/templates/v1/defaults.go +++ b/constraint/pkg/apis/templates/v1/defaults.go @@ -1,13 +1,38 @@ package v1 import ( - "github.com/open-policy-agent/frameworks/constraint/pkg/apis/templates" + ctschema "github.com/open-policy-agent/frameworks/constraint/pkg/schema" + "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" + "k8s.io/apiextensions-apiserver/pkg/apiserver/schema" "k8s.io/apiextensions-apiserver/pkg/apiserver/schema/defaulting" "k8s.io/apimachinery/pkg/runtime" ) const version = "v1" +var ( + structuralSchema *schema.Structural + Scheme *runtime.Scheme +) + +func init() { + Scheme = runtime.NewScheme() + var err error + if err = apiextensionsv1.AddToScheme(Scheme); err != nil { + panic(err) + } + if err = apiextensions.AddToScheme(Scheme); err != nil { + panic(err) + } + if err = AddToScheme(Scheme); err != nil { + panic(err) + } + if structuralSchema, err = ctschema.CRDSchema(Scheme, version); err != nil { + panic(err) + } +} + func addDefaultingFuncs(scheme *runtime.Scheme) error { return RegisterDefaults(scheme) } @@ -19,7 +44,7 @@ func SetDefaults_ConstraintTemplate(obj *ConstraintTemplate) { // nolint:revive panic("Failed to convert v1 ConstraintTemplate to Unstructured") } - defaulting.Default(un, templates.ConstraintTemplateSchemas[version]) + defaulting.Default(un, structuralSchema) err = runtime.DefaultUnstructuredConverter.FromUnstructured(un, obj) if err != nil { diff --git a/constraint/pkg/apis/templates/v1/helpers.go b/constraint/pkg/apis/templates/v1/helpers.go index 2456931e8..a3c44d0d4 100644 --- a/constraint/pkg/apis/templates/v1/helpers.go +++ b/constraint/pkg/apis/templates/v1/helpers.go @@ -1,22 +1,17 @@ package v1 import ( - apisTemplates "github.com/open-policy-agent/frameworks/constraint/pkg/apis/templates" "github.com/open-policy-agent/frameworks/constraint/pkg/core/templates" ) // ToVersionless runs defaulting functions and then converts the ConstraintTemplate to the // versionless api representation. func (versioned *ConstraintTemplate) ToVersionless() (*templates.ConstraintTemplate, error) { - if err := AddToScheme(apisTemplates.Scheme); err != nil { - return nil, err - } - versionedCopy := versioned.DeepCopy() - apisTemplates.Scheme.Default(versionedCopy) + Scheme.Default(versionedCopy) versionless := &templates.ConstraintTemplate{} - if err := apisTemplates.Scheme.Convert(versionedCopy, versionless, nil); err != nil { + if err := Scheme.Convert(versionedCopy, versionless, nil); err != nil { return nil, err } diff --git a/constraint/pkg/apis/templates/v1alpha1/constrainttemplate_types_test.go b/constraint/pkg/apis/templates/v1alpha1/constrainttemplate_types_test.go index b10653425..03dd99724 100644 --- a/constraint/pkg/apis/templates/v1alpha1/constrainttemplate_types_test.go +++ b/constraint/pkg/apis/templates/v1alpha1/constrainttemplate_types_test.go @@ -21,8 +21,8 @@ import ( "github.com/google/go-cmp/cmp" "github.com/onsi/gomega" - apisTemplates "github.com/open-policy-agent/frameworks/constraint/pkg/apis/templates" "github.com/open-policy-agent/frameworks/constraint/pkg/core/templates" + "github.com/open-policy-agent/frameworks/constraint/pkg/schema" "golang.org/x/net/context" "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" @@ -150,31 +150,31 @@ func TestValidationVersionConversionAndTransformation(t *testing.T) { name: "Two deep properties, LegacySchema=true", v: &Validation{ LegacySchema: &trueBool, - OpenAPIV3Schema: apisTemplates.VersionedIncompleteSchema(), + OpenAPIV3Schema: schema.VersionedIncompleteSchema(), }, exp: &templates.Validation{ LegacySchema: &trueBool, - OpenAPIV3Schema: apisTemplates.VersionlessSchemaWithXPreserve(), + OpenAPIV3Schema: schema.VersionlessSchemaWithXPreserve(), }, }, { name: "Two deep properties, LegacySchema=false", v: &Validation{ LegacySchema: &falseBool, - OpenAPIV3Schema: apisTemplates.VersionedIncompleteSchema(), + OpenAPIV3Schema: schema.VersionedIncompleteSchema(), }, exp: &templates.Validation{ LegacySchema: &falseBool, - OpenAPIV3Schema: apisTemplates.VersionlessSchema(), + OpenAPIV3Schema: schema.VersionlessSchema(), }, }, { name: "Two deep properties, LegacySchema=nil", v: &Validation{ - OpenAPIV3Schema: apisTemplates.VersionedIncompleteSchema(), + OpenAPIV3Schema: schema.VersionedIncompleteSchema(), }, exp: &templates.Validation{ - OpenAPIV3Schema: apisTemplates.VersionlessSchema(), + OpenAPIV3Schema: schema.VersionlessSchema(), }, }, { diff --git a/constraint/pkg/apis/templates/v1alpha1/conversion.go b/constraint/pkg/apis/templates/v1alpha1/conversion.go index 065185e78..68cf02014 100644 --- a/constraint/pkg/apis/templates/v1alpha1/conversion.go +++ b/constraint/pkg/apis/templates/v1alpha1/conversion.go @@ -16,8 +16,8 @@ limitations under the License. package v1alpha1 import ( - apisTemplates "github.com/open-policy-agent/frameworks/constraint/pkg/apis/templates" coreTemplates "github.com/open-policy-agent/frameworks/constraint/pkg/core/templates" + "github.com/open-policy-agent/frameworks/constraint/pkg/schema" "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" "k8s.io/apimachinery/pkg/conversion" @@ -35,7 +35,7 @@ func Convert_v1alpha1_Validation_To_templates_Validation(in *Validation, out *co inSchemaCopy := inSchema.DeepCopy() if in.LegacySchema != nil && *in.LegacySchema { - if err := apisTemplates.AddPreserveUnknownFields(inSchemaCopy); err != nil { + if err := schema.AddPreserveUnknownFields(inSchemaCopy); err != nil { return err } } diff --git a/constraint/pkg/apis/templates/v1alpha1/defaults.go b/constraint/pkg/apis/templates/v1alpha1/defaults.go index af9390caa..a038e7b70 100644 --- a/constraint/pkg/apis/templates/v1alpha1/defaults.go +++ b/constraint/pkg/apis/templates/v1alpha1/defaults.go @@ -1,13 +1,38 @@ package v1alpha1 import ( - "github.com/open-policy-agent/frameworks/constraint/pkg/apis/templates" + ctschema "github.com/open-policy-agent/frameworks/constraint/pkg/schema" + "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" + "k8s.io/apiextensions-apiserver/pkg/apiserver/schema" "k8s.io/apiextensions-apiserver/pkg/apiserver/schema/defaulting" "k8s.io/apimachinery/pkg/runtime" ) const version = "v1alpha1" +var ( + structuralSchema *schema.Structural + Scheme *runtime.Scheme +) + +func init() { + Scheme = runtime.NewScheme() + var err error + if err = apiextensionsv1.AddToScheme(Scheme); err != nil { + panic(err) + } + if err = apiextensions.AddToScheme(Scheme); err != nil { + panic(err) + } + if err = AddToScheme(Scheme); err != nil { + panic(err) + } + if structuralSchema, err = ctschema.CRDSchema(Scheme, version); err != nil { + panic(err) + } +} + func addDefaultingFuncs(scheme *runtime.Scheme) error { return RegisterDefaults(scheme) } @@ -19,7 +44,7 @@ func SetDefaults_ConstraintTemplate(obj *ConstraintTemplate) { // nolint:revive panic("Failed to convert v1 ConstraintTemplate to Unstructured") } - defaulting.Default(un, templates.ConstraintTemplateSchemas[version]) + defaulting.Default(un, structuralSchema) err = runtime.DefaultUnstructuredConverter.FromUnstructured(un, obj) if err != nil { diff --git a/constraint/pkg/apis/templates/v1alpha1/helpers.go b/constraint/pkg/apis/templates/v1alpha1/helpers.go index 2718e7613..3e1be296e 100644 --- a/constraint/pkg/apis/templates/v1alpha1/helpers.go +++ b/constraint/pkg/apis/templates/v1alpha1/helpers.go @@ -1,22 +1,17 @@ package v1alpha1 import ( - apisTemplates "github.com/open-policy-agent/frameworks/constraint/pkg/apis/templates" "github.com/open-policy-agent/frameworks/constraint/pkg/core/templates" ) // ToVersionless runs defaulting functions and then converts the ConstraintTemplate to the // versionless api representation. func (versioned *ConstraintTemplate) ToVersionless() (*templates.ConstraintTemplate, error) { - if err := AddToScheme(apisTemplates.Scheme); err != nil { - return nil, err - } - versionedCopy := versioned.DeepCopy() - apisTemplates.Scheme.Default(versionedCopy) + Scheme.Default(versionedCopy) versionless := &templates.ConstraintTemplate{} - if err := apisTemplates.Scheme.Convert(versionedCopy, versionless, nil); err != nil { + if err := Scheme.Convert(versionedCopy, versionless, nil); err != nil { return nil, err } diff --git a/constraint/pkg/apis/templates/v1beta1/constrainttemplate_types_test.go b/constraint/pkg/apis/templates/v1beta1/constrainttemplate_types_test.go index dcf150a54..65633e94d 100644 --- a/constraint/pkg/apis/templates/v1beta1/constrainttemplate_types_test.go +++ b/constraint/pkg/apis/templates/v1beta1/constrainttemplate_types_test.go @@ -21,8 +21,8 @@ import ( "github.com/google/go-cmp/cmp" "github.com/onsi/gomega" - apisTemplates "github.com/open-policy-agent/frameworks/constraint/pkg/apis/templates" "github.com/open-policy-agent/frameworks/constraint/pkg/core/templates" + "github.com/open-policy-agent/frameworks/constraint/pkg/schema" "golang.org/x/net/context" "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" @@ -149,31 +149,31 @@ func TestValidationVersionConversionAndTransformation(t *testing.T) { name: "Two deep properties, LegacySchema=true", v: &Validation{ LegacySchema: &trueBool, - OpenAPIV3Schema: apisTemplates.VersionedIncompleteSchema(), + OpenAPIV3Schema: schema.VersionedIncompleteSchema(), }, exp: &templates.Validation{ LegacySchema: &trueBool, - OpenAPIV3Schema: apisTemplates.VersionlessSchemaWithXPreserve(), + OpenAPIV3Schema: schema.VersionlessSchemaWithXPreserve(), }, }, { name: "Two deep properties, LegacySchema=false", v: &Validation{ LegacySchema: &falseBool, - OpenAPIV3Schema: apisTemplates.VersionedIncompleteSchema(), + OpenAPIV3Schema: schema.VersionedIncompleteSchema(), }, exp: &templates.Validation{ LegacySchema: &falseBool, - OpenAPIV3Schema: apisTemplates.VersionlessSchema(), + OpenAPIV3Schema: schema.VersionlessSchema(), }, }, { name: "Two deep properties, LegacySchema=nil", v: &Validation{ - OpenAPIV3Schema: apisTemplates.VersionedIncompleteSchema(), + OpenAPIV3Schema: schema.VersionedIncompleteSchema(), }, exp: &templates.Validation{ - OpenAPIV3Schema: apisTemplates.VersionlessSchema(), + OpenAPIV3Schema: schema.VersionlessSchema(), }, }, { diff --git a/constraint/pkg/apis/templates/v1beta1/conversion.go b/constraint/pkg/apis/templates/v1beta1/conversion.go index 5f6ede47a..9bb69b0c7 100644 --- a/constraint/pkg/apis/templates/v1beta1/conversion.go +++ b/constraint/pkg/apis/templates/v1beta1/conversion.go @@ -16,8 +16,8 @@ limitations under the License. package v1beta1 import ( - apisTemplates "github.com/open-policy-agent/frameworks/constraint/pkg/apis/templates" coreTemplates "github.com/open-policy-agent/frameworks/constraint/pkg/core/templates" + "github.com/open-policy-agent/frameworks/constraint/pkg/schema" "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" "k8s.io/apimachinery/pkg/conversion" @@ -35,7 +35,7 @@ func Convert_v1beta1_Validation_To_templates_Validation(in *Validation, out *cor inSchemaCopy := inSchema.DeepCopy() if in.LegacySchema != nil && *in.LegacySchema { - if err := apisTemplates.AddPreserveUnknownFields(inSchemaCopy); err != nil { + if err := schema.AddPreserveUnknownFields(inSchemaCopy); err != nil { return err } } diff --git a/constraint/pkg/apis/templates/v1beta1/defaults.go b/constraint/pkg/apis/templates/v1beta1/defaults.go index 2a10196a2..c56d8dd29 100644 --- a/constraint/pkg/apis/templates/v1beta1/defaults.go +++ b/constraint/pkg/apis/templates/v1beta1/defaults.go @@ -1,13 +1,38 @@ package v1beta1 import ( - "github.com/open-policy-agent/frameworks/constraint/pkg/apis/templates" + ctschema "github.com/open-policy-agent/frameworks/constraint/pkg/schema" + "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" + apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" + "k8s.io/apiextensions-apiserver/pkg/apiserver/schema" "k8s.io/apiextensions-apiserver/pkg/apiserver/schema/defaulting" "k8s.io/apimachinery/pkg/runtime" ) const version = "v1beta1" +var ( + structuralSchema *schema.Structural + Scheme *runtime.Scheme +) + +func init() { + Scheme = runtime.NewScheme() + var err error + if err = apiextensionsv1.AddToScheme(Scheme); err != nil { + panic(err) + } + if err = apiextensions.AddToScheme(Scheme); err != nil { + panic(err) + } + if err = AddToScheme(Scheme); err != nil { + panic(err) + } + if structuralSchema, err = ctschema.CRDSchema(Scheme, version); err != nil { + panic(err) + } +} + func addDefaultingFuncs(scheme *runtime.Scheme) error { return RegisterDefaults(scheme) } @@ -19,7 +44,7 @@ func SetDefaults_ConstraintTemplate(obj *ConstraintTemplate) { // nolint:revive panic("Failed to convert v1 ConstraintTemplate to Unstructured") } - defaulting.Default(un, templates.ConstraintTemplateSchemas[version]) + defaulting.Default(un, structuralSchema) err = runtime.DefaultUnstructuredConverter.FromUnstructured(un, obj) if err != nil { diff --git a/constraint/pkg/apis/templates/v1beta1/helpers.go b/constraint/pkg/apis/templates/v1beta1/helpers.go index d86e461a2..6638f72fc 100644 --- a/constraint/pkg/apis/templates/v1beta1/helpers.go +++ b/constraint/pkg/apis/templates/v1beta1/helpers.go @@ -1,22 +1,17 @@ package v1beta1 import ( - apisTemplates "github.com/open-policy-agent/frameworks/constraint/pkg/apis/templates" "github.com/open-policy-agent/frameworks/constraint/pkg/core/templates" ) // ToVersionless runs defaulting functions and then converts the ConstraintTemplate to the // versionless api representation. func (versioned *ConstraintTemplate) ToVersionless() (*templates.ConstraintTemplate, error) { - if err := AddToScheme(apisTemplates.Scheme); err != nil { - return nil, err - } - versionedCopy := versioned.DeepCopy() - apisTemplates.Scheme.Default(versionedCopy) + Scheme.Default(versionedCopy) versionless := &templates.ConstraintTemplate{} - if err := apisTemplates.Scheme.Convert(versionedCopy, versionless, nil); err != nil { + if err := Scheme.Convert(versionedCopy, versionless, nil); err != nil { return nil, err } diff --git a/constraint/pkg/apis/templates/test_fakes.go b/constraint/pkg/schema/fixtures.go similarity index 99% rename from constraint/pkg/apis/templates/test_fakes.go rename to constraint/pkg/schema/fixtures.go index 9b319a074..e128123db 100644 --- a/constraint/pkg/apis/templates/test_fakes.go +++ b/constraint/pkg/schema/fixtures.go @@ -1,4 +1,4 @@ -package templates +package schema import ( "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" diff --git a/constraint/pkg/apis/templates/crd_schema.go b/constraint/pkg/schema/structural.go similarity index 50% rename from constraint/pkg/apis/templates/crd_schema.go rename to constraint/pkg/schema/structural.go index bc0016441..8034f6e8d 100644 --- a/constraint/pkg/apis/templates/crd_schema.go +++ b/constraint/pkg/schema/structural.go @@ -1,4 +1,4 @@ -package templates +package schema import ( "fmt" @@ -6,36 +6,40 @@ import ( "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" "k8s.io/apiextensions-apiserver/pkg/apiserver/schema" + "k8s.io/apimachinery/pkg/runtime" "sigs.k8s.io/yaml" ) -// ConstraintTemplateSchemas are the per-version structural schemas for -// ConstraintTemplates. -var ConstraintTemplateSchemas map[string]*schema.Structural - -func initializeCTSchemaMap() { - // Setup the CT Schema map for use in generalized defaulting functions - ConstraintTemplateSchemas = make(map[string]*schema.Structural) +var constraintTemplateCRD *apiextensionsv1.CustomResourceDefinition +func init() { // Ingest the constraint template CRD for use in defaulting functions - constraintTemplateCRD := &apiextensionsv1.CustomResourceDefinition{} + constraintTemplateCRD = &apiextensionsv1.CustomResourceDefinition{} if err := yaml.Unmarshal([]byte(constraintTemplateCRDYaml), constraintTemplateCRD); err != nil { panic(fmt.Errorf("%w: failed to unmarshal yaml into constraintTemplateCRD", err)) } +} +func CRDSchema(sch *runtime.Scheme, version string) (*schema.Structural, error) { // Fill version map with Structural types derived from ConstraintTemplate versions for _, crdVersion := range constraintTemplateCRD.Spec.Versions { + if crdVersion.Name != version { + continue + } + versionlessSchema := &apiextensions.JSONSchemaProps{} - err := Scheme.Convert(crdVersion.Schema.OpenAPIV3Schema, versionlessSchema, nil) + err := sch.Convert(crdVersion.Schema.OpenAPIV3Schema, versionlessSchema, nil) if err != nil { - panic(fmt.Errorf("%w: failed to convert JSONSchemaProps for ConstraintTemplate version %v", err, crdVersion.Name)) + return nil, fmt.Errorf("%w: failed to convert JSONSchemaProps for ConstraintTemplate version %v", err, crdVersion.Name) } structural, err := schema.NewStructural(versionlessSchema) if err != nil { - panic(fmt.Errorf("%w: failed to create Structural for ConstraintTemplate version %v", err, crdVersion.Name)) + return nil, fmt.Errorf("%w: failed to create Structural for ConstraintTemplate version %v", err, crdVersion.Name) } - ConstraintTemplateSchemas[crdVersion.Name] = structural + return structural, nil } + + return nil, fmt.Errorf("No CRD version '%q'", version) } diff --git a/constraint/pkg/apis/templates/transform.go b/constraint/pkg/schema/transform.go similarity index 99% rename from constraint/pkg/apis/templates/transform.go rename to constraint/pkg/schema/transform.go index 06768a6a5..86144c9ca 100644 --- a/constraint/pkg/apis/templates/transform.go +++ b/constraint/pkg/schema/transform.go @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package templates +package schema import ( "fmt" diff --git a/constraint/pkg/apis/templates/transform_test.go b/constraint/pkg/schema/transform_test.go similarity index 99% rename from constraint/pkg/apis/templates/transform_test.go rename to constraint/pkg/schema/transform_test.go index 25d171ee7..1c43b72ac 100644 --- a/constraint/pkg/apis/templates/transform_test.go +++ b/constraint/pkg/schema/transform_test.go @@ -13,7 +13,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package templates +package schema import ( "reflect" diff --git a/constraint/pkg/apis/templates/yaml_constant.go b/constraint/pkg/schema/yaml_constant.go similarity index 99% rename from constraint/pkg/apis/templates/yaml_constant.go rename to constraint/pkg/schema/yaml_constant.go index eefd1b4dc..6b79d3059 100644 --- a/constraint/pkg/apis/templates/yaml_constant.go +++ b/constraint/pkg/schema/yaml_constant.go @@ -1,4 +1,4 @@ -package templates +package schema // This file is generated from deploy/crds.yaml via "make constraint-template-string-constant" // DO NOT MODIFY THIS FILE DIRECTLY!