Skip to content

Commit

Permalink
Add +optional/+required markers at package and struct level for CRD v…
Browse files Browse the repository at this point in the history
…alidation

Using +kubebuilder:validation:Optional/Required at the package level sets the default value for fields in that package to that value, while using the opposite marker on fields in that package sets them as an exception
  • Loading branch information
damemi committed Jun 5, 2019
1 parent 2afad9e commit af492d2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
2 changes: 2 additions & 0 deletions pkg/crd/markers/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ func init() {
AllDefinitions = append(AllDefinitions,
markers.Must(markers.MakeDefinition("groupName", markers.DescribesPackage, "")),
markers.Must(markers.MakeDefinition("versionName", markers.DescribesPackage, "")),
markers.Must(markers.MakeDefinition("kubebuilder:validation:Optional", markers.DescribesPackage, struct{}{})),
markers.Must(markers.MakeDefinition("kubebuilder:validation:Required", markers.DescribesPackage, struct{}{})),
)
}
4 changes: 4 additions & 0 deletions pkg/crd/markers/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ var ValidationMarkers = mustMakeAllWithPrefix("kubebuilder:validation", markers.
Format(""),
Type(""),
Nullable(false),
Required(struct{}{}),
Optional(struct{}{}),
)

func init() {
Expand Down Expand Up @@ -88,6 +90,8 @@ type Enum []interface{}
type Format string
type Type string
type Nullable bool
type Required struct{}
type Optional struct{}

func (m Maximum) ApplyToSchema(schema *v1beta1.JSONSchemaProps) error {
if schema.Type != "integer" {
Expand Down
10 changes: 9 additions & 1 deletion pkg/crd/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,15 @@ func (p *Parser) NeedSchemaFor(typ TypeIdent) {
p.Schemata[typ] = apiext.JSONSchemaProps{}

schemaCtx := newSchemaContext(typ.Package, p)
schema := infoToSchema(schemaCtx.ForInfo(info))
ctxForInfo := schemaCtx.ForInfo(info)

pkgMarkers, err := markers.PackageMarkers(p.Collector, typ.Package)
if err != nil {
typ.Package.AddError(err)
}
ctxForInfo.PackageMarkers = pkgMarkers

schema := infoToSchema(ctxForInfo)

p.Schemata[typ] = *schema

Expand Down
24 changes: 22 additions & 2 deletions pkg/crd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type schemaContext struct {
info *markers.TypeInfo

schemaRequester schemaRequester
PackageMarkers markers.MarkerValues
}

// newSchemaContext constructs a new schemaContext for the given package and schema requester.
Expand Down Expand Up @@ -342,8 +343,27 @@ func structToSchema(ctx *schemaContext, structType *ast.StructType) *v1beta1.JSO
fieldName := jsonOpts[0]
inline = inline || fieldName == "" // anonymous fields are inline fields in YAML/JSON

if !inline && !omitEmpty {
props.Required = append(props.Required, fieldName)
// if no default required mode is set, default to required
defaultMode := "required"
if ctx.PackageMarkers.Get("kubebuilder:validation:Optional") != nil {
defaultMode = "optional"
}

switch defaultMode {
// if this package isn't set to optional default...
case "required":
// ...everything that's not inline, omitempty, or explicitly optional is required
// TODO(damemi): Actually add support for mythical `+optional` marker
if !inline && !omitEmpty && field.Markers.Get("kubebuilder:validation:Optional") == nil && field.Markers.Get("optional") == nil {
props.Required = append(props.Required, fieldName)
}

// if this package isn't set to required default...
case "optional":
// ...everything that isn't explicitly required is optional
if field.Markers.Get("kubebuilder:validation:Required") != nil {
props.Required = append(props.Required, fieldName)
}
}

propSchema := typeToSchema(ctx.ForInfo(&markers.TypeInfo{}), field.RawField.Type)
Expand Down

0 comments on commit af492d2

Please sign in to comment.