Skip to content

Commit 5709df3

Browse files
perdasilvaPer Goncalves da Silva
authored andcommitted
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]> Upstream-repository: operator-lifecycle-manager Upstream-commit: 1bdf969e6dff317e975de21078d60dd1406235e0
1 parent 9e9736d commit 5709df3

File tree

3 files changed

+98
-4
lines changed
  • staging/operator-lifecycle-manager/pkg/controller/operators/decorators
  • vendor/github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/decorators

3 files changed

+98
-4
lines changed

staging/operator-lifecycle-manager/pkg/controller/operators/decorators/operator.go

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,32 @@ func (o *Operator) ComponentLabelKey() (string, error) {
127127
return o.componentLabelKey, nil
128128
}
129129

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

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

staging/operator-lifecycle-manager/pkg/controller/operators/decorators/operator_test.go

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

vendor/github.com/operator-framework/operator-lifecycle-manager/pkg/controller/operators/decorators/operator.go

Lines changed: 20 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)