Skip to content

Commit

Permalink
Merge pull request #234 from haiyanmeng/crd-map
Browse files Browse the repository at this point in the history
Add support for arrays as map values into pkg/crd
  • Loading branch information
k8s-ci-robot committed Jul 10, 2019
2 parents be718bb + bb6166a commit c4d99a1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 2 deletions.
6 changes: 6 additions & 0 deletions pkg/crd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,12 @@ func mapToSchema(ctx *schemaContext, mapType *ast.MapType) *v1beta1.JSONSchemaPr
valSchema = localNamedToSchema(ctx.ForInfo(&markers.TypeInfo{}), val)
case *ast.SelectorExpr:
valSchema = namedToSchema(ctx.ForInfo(&markers.TypeInfo{}), val)
case *ast.ArrayType:
valSchema = arrayToSchema(ctx.ForInfo(&markers.TypeInfo{}), val)
if valSchema.Type == "array" && valSchema.Items.Schema.Type != "string" {
ctx.pkg.AddError(loader.ErrFromNode(fmt.Errorf("map values must be a named type, not %T", mapType.Value), mapType.Value))
return &v1beta1.JSONSchemaProps{}
}
default:
ctx.pkg.AddError(loader.ErrFromNode(fmt.Errorf("map values must be a named type, not %T", mapType.Value), mapType.Value))
return &v1beta1.JSONSchemaProps{}
Expand Down
14 changes: 12 additions & 2 deletions pkg/crd/testdata/cronjob_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,26 @@ type CronJobSpec struct {
// This is a pointer to distinguish between explicit zero and not specified.
// +optional
FailedJobsHistoryLimit *int32 `json:"failedJobsHistoryLimit,omitempty"`

// This tests byte slices are allowed as map values.
ByteSliceData map[string][]byte `json:"byteSliceData,omitempty"`

// This tests string slices are allowed as map values.
StringSliceData map[string][]string `json:"stringSliceData,omitempty"`
}

// use an explicit type marker to verify that apply-first markers generate properly

// +kubebuilder:validation:Type=string
// TotallyABool is a bool that serializes as a string.
type TotallyABool bool

func (t TotallyABool) MarshalJSON() ([]byte, error) {
if t { return []byte(`"true"`), nil } else { return []byte(`"false"`), nil}
if t {
return []byte(`"true"`), nil
} else {
return []byte(`"false"`), nil
}
}
func (t *TotallyABool) UnmarshalJSON(in []byte) error {
switch string(in) {
Expand All @@ -100,7 +111,6 @@ func (t *TotallyABool) UnmarshalJSON(in []byte) error {
}
}


// ConcurrencyPolicy describes how the job will be handled.
// Only one of the following concurrent policies may be specified.
// If none of the following policies is specified, the default one
Expand Down
13 changes: 13 additions & 0 deletions pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,12 @@ spec:
description: This tests byte slice schema generation.
format: byte
type: string
byteSliceData:
additionalProperties:
format: byte
type: string
description: This tests byte slices are allowed as map values.
type: object
canBeNull:
description: This tests that nullable works correctly
nullable: true
Expand Down Expand Up @@ -6189,6 +6195,13 @@ spec:
will be counted as failed ones.
format: int64
type: integer
stringSliceData:
additionalProperties:
items:
type: string
type: array
description: This tests string slices are allowed as map values.
type: object
successfulJobsHistoryLimit:
description: The number of successful finished jobs to retain. This
is a pointer to distinguish between explicit zero and not specified.
Expand Down

0 comments on commit c4d99a1

Please sign in to comment.