Skip to content

Commit 9cfe0af

Browse files
Maria NtallaAntoine Pelisse
authored andcommitted
Add support for x-kubernetes-map-type
This adds support for x-kubernetes-map-type, which controls how to merge maps in server-side apply.
1 parent 7c268b3 commit 9cfe0af

File tree

4 files changed

+62
-6
lines changed

4 files changed

+62
-6
lines changed

pkg/crd/markers/topology.go

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,22 @@ import (
2323
"sigs.k8s.io/controller-tools/pkg/markers"
2424
)
2525

26-
// ToplogyMarkers list topology markers (i.e. markers that specify if a
27-
// list is an associative-list or a set, or if a map is atomic or not).
26+
// TopologyMarkers specify topology markers (i.e. markers that describe if a
27+
// list behaves as an associative-list or a set, if a map is atomic or not).
2828
var TopologyMarkers = []*definitionWithHelp{
2929
must(markers.MakeDefinition("listMapKey", markers.DescribesField, ListMapKey(""))).
3030
WithHelp(ListMapKey("").Help()),
3131
must(markers.MakeDefinition("listType", markers.DescribesField, ListType(""))).
3232
WithHelp(ListType("").Help()),
33+
must(markers.MakeDefinition("mapType", markers.DescribesField, MapType(""))).
34+
WithHelp(MapType("").Help()),
3335
}
3436

3537
func init() {
3638
AllDefinitions = append(AllDefinitions, TopologyMarkers...)
3739
}
3840

39-
// +controllertools:marker:generateHelp:category="CRD topology"
41+
// +controllertools:marker:generateHelp:category="CRD processing"
4042

4143
// ListType specifies the type of data-structure that the list
4244
// represents (map, set, atomic).
@@ -54,7 +56,7 @@ func init() {
5456
// are typically manipulated together by the same actor.
5557
type ListType string
5658

57-
// +controllertools:marker:generateHelp:category="CRD topology"
59+
// +controllertools:marker:generateHelp:category="CRD processing"
5860

5961
// ListMapKey specifies the keys to map listTypes.
6062
//
@@ -63,6 +65,22 @@ type ListType string
6365
// should be scalar types.
6466
type ListMapKey string
6567

68+
// +controllertools:marker:generateHelp:category="CRD processing"
69+
70+
// MapType specifies the level of atomicity of the map;
71+
// i.e. whether each item in the map is independent of the others,
72+
// or all fields are treated as a single unit.
73+
//
74+
// Possible values:
75+
//
76+
// - "granular": items in the map are independent of each other,
77+
// and can be manipulated by different actors.
78+
// This is the default behavior.
79+
//
80+
// - "atomic": all fields are treated as one unit.
81+
// Any changes have to replace the entire map.
82+
type MapType string
83+
6684
func (l ListType) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
6785
if schema.Type != "array" {
6886
return fmt.Errorf("must apply listType to an array")
@@ -87,3 +105,18 @@ func (l ListMapKey) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
87105
schema.XListMapKeys = append(schema.XListMapKeys, string(l))
88106
return nil
89107
}
108+
109+
func (m MapType) ApplyToSchema(schema *apiext.JSONSchemaProps) error {
110+
if schema.Type != "object" {
111+
return fmt.Errorf("must apply mapType to an object")
112+
}
113+
114+
if m != "atomic" && m != "granular" {
115+
return fmt.Errorf(`MapType must be either "granular" or "atomic"`)
116+
}
117+
118+
p := string(m)
119+
schema.XMapType = &p
120+
121+
return nil
122+
}

pkg/crd/markers/zz_generated.markerhelp.go

Lines changed: 13 additions & 2 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
@@ -132,6 +132,10 @@ type CronJobSpec struct {
132132
// +listMapKey=name
133133
// +listMapKey=secondary
134134
AssociativeList []AssociativeType `json:"associativeList"`
135+
136+
// A map that allows different actors to manage different fields
137+
// +mapType=granular
138+
MapOfInfo map[string][]byte `json:"mapOfInfo"`
135139
}
136140

137141
type NestedObject struct {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ 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
128135
jobTemplate:
129136
description: Specifies the job that will be created when executing
130137
a CronJob.
@@ -5054,6 +5061,7 @@ spec:
50545061
- defaultedString
50555062
- embeddedResource
50565063
- jobTemplate
5064+
- mapOfInfo
50575065
- patternObject
50585066
- schedule
50595067
- twoOfAKindPart0

0 commit comments

Comments
 (0)