-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add crds.yaml-based defaulting functions to pkg/apis/templates/... (#129
) Each ConstrainTemplate version (v1alpha1, v1beta1, v1) has a default value for the LegacySchema field. true for v1alpha1, v1beta1, and false for v1. These defaults are defined in the CRD yaml. For the ConstraintTemplate golang types to have the same functionality to have the same defaulting functionality. We are required to write custom defaulting functions for each defaulted field. While we could do this, we could easily see how this practice could become unsustainable. A forgotten defaulting function would yield unexpected bugs. To remedy this, this PR adds defaulting functions that default based on the content of deploy/crds.yaml, i.e. the ConstraintTemplate CRD generated by controller-gen. This provides us with a single defaulting source of truth: the constrainttemplate_types.go files, which contain annotations that define a field's default values. Fixes #128 Signed-off-by: juliankatz <juliankatz@google.com>
- Loading branch information
1 parent
d4a673c
commit 2924b2c
Showing
45 changed files
with
1,214 additions
and
270 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package templates | ||
|
||
import ( | ||
"fmt" | ||
|
||
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
"k8s.io/apiextensions-apiserver/pkg/apiserver/schema" | ||
"sigs.k8s.io/yaml" | ||
) | ||
|
||
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) | ||
|
||
// Ingest the constraint template CRD for use in defaulting functions | ||
constraintTemplateCRD := &apiextensionsv1.CustomResourceDefinition{} | ||
if err := yaml.Unmarshal([]byte(constraintTemplateCRDYaml), constraintTemplateCRD); err != nil { | ||
panic(fmt.Errorf("%w: failed to unmarshal yaml into constraintTemplateCRD", err)) | ||
} | ||
|
||
// Fill version map with Structural types derived from ConstraintTemplate versions | ||
for _, crdVersion := range constraintTemplateCRD.Spec.Versions { | ||
versionlessSchema := &apiextensions.JSONSchemaProps{} | ||
err := Scheme.Convert(crdVersion.Schema.OpenAPIV3Schema, versionlessSchema, nil) | ||
if err != nil { | ||
panic(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)) | ||
} | ||
|
||
ConstraintTemplateSchemas[crdVersion.Name] = structural | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package templates | ||
|
||
import ( | ||
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
) | ||
|
||
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package templates | ||
|
||
import ( | ||
"k8s.io/apiextensions-apiserver/pkg/apis/apiextensions" | ||
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" | ||
) | ||
|
||
func VersionedIncompleteSchema() *apiextensionsv1.JSONSchemaProps { | ||
return &apiextensionsv1.JSONSchemaProps{ | ||
Properties: map[string]apiextensionsv1.JSONSchemaProps{ | ||
"message": { | ||
Type: "string", | ||
}, | ||
"labels": { | ||
Type: "array", | ||
Items: &apiextensionsv1.JSONSchemaPropsOrArray{ | ||
Schema: &apiextensionsv1.JSONSchemaProps{ | ||
Type: "object", | ||
Properties: map[string]apiextensionsv1.JSONSchemaProps{ | ||
"key": {Type: "string"}, | ||
"allowedRegex": {Type: "string"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func VersionlessSchemaWithXPreserve() *apiextensions.JSONSchemaProps { | ||
trueBool := true | ||
return &apiextensions.JSONSchemaProps{ | ||
XPreserveUnknownFields: &trueBool, | ||
Properties: map[string]apiextensions.JSONSchemaProps{ | ||
"message": { | ||
Type: "string", | ||
}, | ||
"labels": { | ||
Type: "array", | ||
Items: &apiextensions.JSONSchemaPropsOrArray{ | ||
Schema: &apiextensions.JSONSchemaProps{ | ||
Type: "object", | ||
XPreserveUnknownFields: &trueBool, | ||
Properties: map[string]apiextensions.JSONSchemaProps{ | ||
"key": {Type: "string"}, | ||
"allowedRegex": {Type: "string"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func VersionlessSchema() *apiextensions.JSONSchemaProps { | ||
return &apiextensions.JSONSchemaProps{ | ||
Properties: map[string]apiextensions.JSONSchemaProps{ | ||
"message": { | ||
Type: "string", | ||
}, | ||
"labels": { | ||
Type: "array", | ||
Items: &apiextensions.JSONSchemaPropsOrArray{ | ||
Schema: &apiextensions.JSONSchemaProps{ | ||
Type: "object", | ||
Properties: map[string]apiextensions.JSONSchemaProps{ | ||
"key": {Type: "string"}, | ||
"allowedRegex": {Type: "string"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.