@@ -19,6 +19,7 @@ import (
19
19
"context"
20
20
"encoding/json"
21
21
"fmt"
22
+ "sync"
22
23
23
24
appsv1 "k8s.io/api/apps/v1"
24
25
kuberuntime "k8s.io/apimachinery/pkg/runtime"
@@ -38,7 +39,8 @@ const (
38
39
defaultRevisionHistoryLimit = 10
39
40
)
40
41
41
- var encoderMap = map [schema.GroupVersion ]kuberuntime.Encoder {}
42
+ // encoderMap is a map of `GroupVersion` to `kuberuntime.Encoder`.
43
+ var encoderMap sync.Map
42
44
43
45
// GetCurrentAndUpdate returns the current and update ControllerRevisions. It also
44
46
// returns a collision count that records the number of name collisions set saw when creating
@@ -135,7 +137,8 @@ func newRevision(obj client.Object, component string, labels map[string]string,
135
137
// getPatch returns a merge patch that can be applied to restore a CR to a previous version.
136
138
// The current state that we save is just the spec.
137
139
func getPatch (obj client.Object , gvk schema.GroupVersionKind ) ([]byte , error ) {
138
- encoder , ok := encoderMap [gvk .GroupVersion ()]
140
+ var encoder kuberuntime.Encoder
141
+ val , ok := encoderMap .Load (gvk .GroupVersion ())
139
142
if ! ok {
140
143
encoder = scheme .Codecs .EncoderForVersion (
141
144
serializerjson .NewSerializerWithOptions (
@@ -149,7 +152,9 @@ func getPatch(obj client.Object, gvk schema.GroupVersionKind) ([]byte, error) {
149
152
}),
150
153
gvk .GroupVersion (),
151
154
)
152
- encoderMap [gvk .GroupVersion ()] = encoder
155
+ encoderMap .Store (gvk .GroupVersion (), encoder )
156
+ } else {
157
+ encoder = val .(kuberuntime.Encoder )
153
158
}
154
159
155
160
buf := bytes.Buffer {}
0 commit comments