Skip to content

Commit 6694c6b

Browse files
Maria NtallaAntoine Pelisse
authored andcommitted
Add support for structType marker
It maps to x-kubernetes-map-type in the openapi schema, similar to the mapType marker.
1 parent 9cfe0af commit 6694c6b

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

pkg/crd/markers/topology.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ var TopologyMarkers = []*definitionWithHelp{
3232
WithHelp(ListType("").Help()),
3333
must(markers.MakeDefinition("mapType", markers.DescribesField, MapType(""))).
3434
WithHelp(MapType("").Help()),
35+
must(markers.MakeDefinition("structType", markers.DescribesField, StructType(""))).
36+
WithHelp(StructType("").Help()),
3537
}
3638

3739
func init() {
@@ -81,6 +83,22 @@ type ListMapKey string
8183
// Any changes have to replace the entire map.
8284
type MapType string
8385

86+
// +controllertools:marker:generateHelp:category="CRD processing"
87+
88+
// StructType specifies the level of atomicity of the struct;
89+
// i.e. whether each field in the struct is independent of the others,
90+
// or all fields are treated as a single unit.
91+
//
92+
// Possible values:
93+
//
94+
// - "granular": fields in the struct are independent of each other,
95+
// and can be manipulated by different actors.
96+
// This is the default behavior.
97+
//
98+
// - "atomic": all fields are treated as one unit.
99+
// Any changes have to replace the entire struct.
100+
type StructType string
101+
84102
func (l ListType) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
85103
if schema.Type != "array" {
86104
return fmt.Errorf("must apply listType to an array")
@@ -120,3 +138,18 @@ func (m MapType) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
120138

121139
return nil
122140
}
141+
142+
func (s StructType) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
143+
if schema.Type != "object" && schema.Type != "" {
144+
return fmt.Errorf("must apply structType to an object; either explicitly set or defaulted through an empty schema type")
145+
}
146+
147+
if s != "atomic" && s != "granular" {
148+
return fmt.Errorf(`StructType must be either "granular" or "atomic"`)
149+
}
150+
151+
p := string(s)
152+
schema.XMapType = &p
153+
154+
return nil
155+
}

pkg/crd/markers/zz_generated.markerhelp.go

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/crd/testdata/cronjob_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ type CronJobSpec struct {
136136
// A map that allows different actors to manage different fields
137137
// +mapType=granular
138138
MapOfInfo map[string][]byte `json:"mapOfInfo"`
139+
140+
// A struct that can only be entirely replaced
141+
// +structType=atomic
142+
StructWithSeveralFields NestedObject `json:"structWithSeveralFields"`
139143
}
140144

141145
type NestedObject struct {

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,6 @@ spec:
125125
a pointer to distinguish between explicit zero and not specified.
126126
format: int32
127127
type: integer
128-
mapOfInfo:
129-
description: A map that allows different actors to manage different fields
130-
type: object
131-
x-kubernetes-map-type: granular
132-
additionalProperties:
133-
format: byte
134-
type: string
135128
jobTemplate:
136129
description: Specifies the job that will be created when executing
137130
a CronJob.
@@ -4993,6 +4986,13 @@ spec:
49934986
- template
49944987
type: object
49954988
type: object
4989+
mapOfInfo:
4990+
description: A map that allows different actors to manage different fields
4991+
type: object
4992+
x-kubernetes-map-type: granular
4993+
additionalProperties:
4994+
format: byte
4995+
type: string
49964996
noReallySuspend:
49974997
description: This flag is like suspend, but for when you really mean
49984998
it. It helps test the +kubebuilder:validation:Type marker.
@@ -5017,6 +5017,18 @@ spec:
50175017
type: array
50185018
description: This tests string slices are allowed as map values.
50195019
type: object
5020+
structWithSeveralFields:
5021+
description: A struct that can only be entirely replaced
5022+
type: object
5023+
x-kubernetes-map-type: atomic
5024+
properties:
5025+
bar:
5026+
type: boolean
5027+
foo:
5028+
type: string
5029+
required:
5030+
- bar
5031+
- foo
50205032
successfulJobsHistoryLimit:
50215033
description: The number of successful finished jobs to retain. This
50225034
is a pointer to distinguish between explicit zero and not specified.
@@ -5064,6 +5076,7 @@ spec:
50645076
- mapOfInfo
50655077
- patternObject
50665078
- schedule
5079+
- structWithSeveralFields
50675080
- twoOfAKindPart0
50685081
- twoOfAKindPart1
50695082
- unprunedEmbeddedResource

0 commit comments

Comments
 (0)