Skip to content

Commit 6d541c6

Browse files
mattia-eleuterilexfrei
authored andcommitted
fix(api): make mergeMaps always allocate, never return an input map
mergeMaps returned one of its inputs whenever the other was nil. Create and Update seed the HelmRelease labels with mergeMaps(r.releaseConfig.Labels, ...) and then write the application metadata labels into the result in place, so for an Application with no labels the writes landed on the long-lived config map shared by every request for the kind: requests cross-contaminate (application.name varies per request) and concurrent map writes crash the apiserver. Signed-off-by: mattia-eleuteri <[email protected]>
1 parent a18cbc5 commit 6d541c6

2 files changed

Lines changed: 42 additions & 6 deletions

File tree

‎pkg/registry/apps/application/rest.go‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,16 +1204,14 @@ func (r *REST) hasRequiredApplicationLabelsWithName(hr *helmv2.HelmRelease, appN
12041204
}
12051205

12061206
// mergeMaps combines two maps of labels or annotations
1207+
// mergeMaps merges b over a into a freshly allocated map. It never returns
1208+
// either input: callers mutate the result in place, and a may be a long-lived
1209+
// shared map (e.g. r.releaseConfig.Labels) that concurrent requests must not
1210+
// write to.
12071211
func mergeMaps(a, b map[string]string) map[string]string {
12081212
if a == nil && b == nil {
12091213
return nil
12101214
}
1211-
if a == nil {
1212-
return b
1213-
}
1214-
if b == nil {
1215-
return a
1216-
}
12171215
merged := make(map[string]string, len(a)+len(b))
12181216
maps.Copy(merged, a)
12191217
maps.Copy(merged, b)

‎pkg/registry/apps/application/rest_update_metadata_test.go‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,41 @@ func TestUpdate_PreservesForeignHelmReleaseMetadata(t *testing.T) {
106106
t.Errorf("expected application metadata labels to remain, got labels %v", got.Labels)
107107
}
108108
}
109+
110+
// TestCreate_DoesNotMutateSharedConfigLabels pins mergeMaps' allocation
111+
// contract. Create and Update build the HelmRelease label set starting
112+
// from r.releaseConfig.Labels, a long-lived map shared by every request
113+
// for the kind, and then write the application metadata labels into the
114+
// result in place. If mergeMaps returns one of its inputs instead of a
115+
// fresh map — which the old implementation did whenever the other input
116+
// was nil, e.g. for an Application with no labels — those in-place
117+
// writes land on the shared config map: requests cross-contaminate
118+
// (application.name varies per request) and concurrent map writes crash
119+
// the apiserver.
120+
func TestCreate_DoesNotMutateSharedConfigLabels(t *testing.T) {
121+
configLabels := map[string]string{"cozystack.io/ui": "true"}
122+
r := newTestRESTWithSchemes()
123+
r.releaseConfig.Labels = configLabels
124+
125+
app := &appsv1alpha1.Application{
126+
TypeMeta: metav1.TypeMeta{
127+
APIVersion: "apps.cozystack.io/v1alpha1",
128+
Kind: "PostgreSQL",
129+
},
130+
ObjectMeta: metav1.ObjectMeta{
131+
Name: "nolabels",
132+
Namespace: "default",
133+
// No labels: the conversion yields a nil label map, the
134+
// trigger for mergeMaps returning the config map itself.
135+
},
136+
}
137+
138+
ctx := request.WithNamespace(context.Background(), "default")
139+
if _, err := r.Create(ctx, app, nil, &metav1.CreateOptions{}); err != nil {
140+
t.Fatalf("unexpected error: %v", err)
141+
}
142+
143+
if len(configLabels) != 1 || configLabels["cozystack.io/ui"] != "true" {
144+
t.Errorf("expected shared config labels to be untouched, got %v", configLabels)
145+
}
146+
}

0 commit comments

Comments
 (0)