Skip to content

Commit 1bdf969

Browse files
perdasilvaPer Goncalves da Silva
and
Per Goncalves da Silva
authored
Fix label key truncation for subscription annotations (#2731)
Signed-off-by: Per Goncalves da Silva <[email protected]> Co-authored-by: Per Goncalves da Silva <[email protected]>
1 parent bf256ba commit 1bdf969

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

pkg/controller/operators/decorators/operator.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,32 @@ func (o *Operator) ComponentLabelKey() (string, error) {
128128
return o.componentLabelKey, nil
129129
}
130130

131-
if o.GetName() == "" {
131+
name := o.GetName()
132+
if name == "" {
132133
return "", fmt.Errorf(componentLabelKeyError, "empty name field")
133134
}
134135

135-
name := o.GetName()
136136
if len(name) > 63 {
137137
// Truncate
138138
name = name[0:63]
139+
140+
// Remove trailing illegal characters
141+
idx := len(name) - 1
142+
for ; idx >= 0; idx-- {
143+
lastChar := name[idx]
144+
if lastChar != '.' && lastChar != '_' && lastChar != '-' {
145+
break
146+
}
147+
}
148+
149+
// Just being defensive. This is unlikely to happen since the operator name should
150+
// be compatible kubernetes naming constraints
151+
if idx < 0 {
152+
return "", fmt.Errorf(componentLabelKeyError, "unsupported name field")
153+
}
154+
155+
// Update Label
156+
name = name[0 : idx+1]
139157
}
140158
o.componentLabelKey = ComponentLabelKeyPrefix + name
141159

pkg/controller/operators/decorators/operator_test.go

+58
Original file line numberDiff line numberDiff line change
@@ -231,3 +231,61 @@ func TestAddComponents(t *testing.T) {
231231
})
232232
}
233233
}
234+
235+
func TestComponentLabelKey(t *testing.T) {
236+
tests := []struct {
237+
description string
238+
componentLabelKey string
239+
name string
240+
expectedLabel string
241+
returnsError bool
242+
}{
243+
{
244+
description: "when componentLabelKey is set then return it",
245+
componentLabelKey: "my-component-label",
246+
name: "my-operator",
247+
expectedLabel: "my-component-label",
248+
returnsError: false,
249+
},
250+
{
251+
description: "when componentLabelKey is not set and operator name is less than 63 characters then do not truncate",
252+
componentLabelKey: "",
253+
name: "my-operator",
254+
expectedLabel: ComponentLabelKeyPrefix + "my-operator",
255+
returnsError: false,
256+
},
257+
{
258+
description: "when componentLabelKey is not set and operator name is more than 63 characters truncate",
259+
name: "this-is-my-operator-its-the-coolest-you-got-a-problem-with-that-come-at-me-bro",
260+
expectedLabel: ComponentLabelKeyPrefix + "this-is-my-operator-its-the-coolest-you-got-a-problem-with-that",
261+
returnsError: false,
262+
},
263+
{
264+
description: "when componentLabelKey is not set and operator name is more than 63 characters truncate and drop trailing illegal characters",
265+
name: "this-is-my-operator-its-the-coolest-you-got-a-problem-with----...---___...---",
266+
expectedLabel: ComponentLabelKeyPrefix + "this-is-my-operator-its-the-coolest-you-got-a-problem-with",
267+
returnsError: false,
268+
},
269+
{
270+
description: "when componentLabelKey is not set and operator name is more than 63 characters and is made up of illegal characters then return error",
271+
name: "----...---___...-------...---___...-------...---___...-------...---___...---",
272+
expectedLabel: "",
273+
returnsError: true,
274+
},
275+
}
276+
277+
for _, tt := range tests {
278+
operator := &Operator{
279+
Operator: &operatorsv1.Operator{
280+
ObjectMeta: metav1.ObjectMeta{
281+
Name: tt.name,
282+
},
283+
},
284+
componentLabelKey: tt.componentLabelKey,
285+
}
286+
287+
actualLabel, actualErr := operator.ComponentLabelKey()
288+
require.Equal(t, tt.returnsError, actualErr != nil, actualErr)
289+
require.Equal(t, tt.expectedLabel, actualLabel)
290+
}
291+
}

0 commit comments

Comments
 (0)