Skip to content

Commit 2b15238

Browse files
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 ce1f563 commit 2b15238

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
@@ -1129,16 +1129,14 @@ func (r *REST) hasRequiredApplicationLabelsWithName(hr *helmv2.HelmRelease, appN
11291129
}
11301130

11311131
// mergeMaps combines two maps of labels or annotations
1132+
// mergeMaps merges b over a into a freshly allocated map. It never returns
1133+
// either input: callers mutate the result in place, and a may be a long-lived
1134+
// shared map (e.g. r.releaseConfig.Labels) that concurrent requests must not
1135+
// write to.
11321136
func mergeMaps(a, b map[string]string) map[string]string {
11331137
if a == nil && b == nil {
11341138
return nil
11351139
}
1136-
if a == nil {
1137-
return b
1138-
}
1139-
if b == nil {
1140-
return a
1141-
}
11421140
merged := make(map[string]string, len(a)+len(b))
11431141
for k, v := range a {
11441142
merged[k] = v

‎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)