Skip to content

Commit bb6166a

Browse files
committed
Add support for byte slices and string slices as map values into pkg/crd
This makes it possible for a CRD to have map fields whose map values are byte slices or string slices. However, non-byte and non-string slices (e.g., `map[string][]int`) are still not allowed to be used as map values in CRDs. Signed-off-by: Haiyan Meng <[email protected]>
1 parent cf8f8c9 commit bb6166a

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

pkg/crd/schema.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,12 @@ func mapToSchema(ctx *schemaContext, mapType *ast.MapType) *v1beta1.JSONSchemaPr
291291
valSchema = localNamedToSchema(ctx.ForInfo(&markers.TypeInfo{}), val)
292292
case *ast.SelectorExpr:
293293
valSchema = namedToSchema(ctx.ForInfo(&markers.TypeInfo{}), val)
294+
case *ast.ArrayType:
295+
valSchema = arrayToSchema(ctx.ForInfo(&markers.TypeInfo{}), val)
296+
if valSchema.Type == "array" && valSchema.Items.Schema.Type != "string" {
297+
ctx.pkg.AddError(loader.ErrFromNode(fmt.Errorf("map values must be a named type, not %T", mapType.Value), mapType.Value))
298+
return &v1beta1.JSONSchemaProps{}
299+
}
294300
default:
295301
ctx.pkg.AddError(loader.ErrFromNode(fmt.Errorf("map values must be a named type, not %T", mapType.Value), mapType.Value))
296302
return &v1beta1.JSONSchemaProps{}

pkg/crd/testdata/cronjob_types.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,26 @@ type CronJobSpec struct {
7878
// This is a pointer to distinguish between explicit zero and not specified.
7979
// +optional
8080
FailedJobsHistoryLimit *int32 `json:"failedJobsHistoryLimit,omitempty"`
81+
82+
// This tests byte slices are allowed as map values.
83+
ByteSliceData map[string][]byte `json:"byteSliceData,omitempty"`
84+
85+
// This tests string slices are allowed as map values.
86+
StringSliceData map[string][]string `json:"stringSliceData,omitempty"`
8187
}
8288

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

8591
// +kubebuilder:validation:Type=string
8692
// TotallyABool is a bool that serializes as a string.
8793
type TotallyABool bool
94+
8895
func (t TotallyABool) MarshalJSON() ([]byte, error) {
89-
if t { return []byte(`"true"`), nil } else { return []byte(`"false"`), nil}
96+
if t {
97+
return []byte(`"true"`), nil
98+
} else {
99+
return []byte(`"false"`), nil
100+
}
90101
}
91102
func (t *TotallyABool) UnmarshalJSON(in []byte) error {
92103
switch string(in) {
@@ -100,7 +111,6 @@ func (t *TotallyABool) UnmarshalJSON(in []byte) error {
100111
}
101112
}
102113

103-
104114
// ConcurrencyPolicy describes how the job will be handled.
105115
// Only one of the following concurrent policies may be specified.
106116
// If none of the following policies is specified, the default one

pkg/crd/testdata/testdata.kubebuilder.io_cronjobs.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,12 @@ spec:
427427
description: This tests byte slice schema generation.
428428
format: byte
429429
type: string
430+
byteSliceData:
431+
additionalProperties:
432+
format: byte
433+
type: string
434+
description: This tests byte slices are allowed as map values.
435+
type: object
430436
canBeNull:
431437
description: This tests that nullable works correctly
432438
nullable: true
@@ -6189,6 +6195,13 @@ spec:
61896195
will be counted as failed ones.
61906196
format: int64
61916197
type: integer
6198+
stringSliceData:
6199+
additionalProperties:
6200+
items:
6201+
type: string
6202+
type: array
6203+
description: This tests string slices are allowed as map values.
6204+
type: object
61926205
successfulJobsHistoryLimit:
61936206
description: The number of successful finished jobs to retain. This
61946207
is a pointer to distinguish between explicit zero and not specified.

0 commit comments

Comments
 (0)