Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,14 +127,32 @@ func (o *Operator) ComponentLabelKey() (string, error) {
return o.componentLabelKey, nil
}

if o.GetName() == "" {
name := o.GetName()
if name == "" {
return "", fmt.Errorf(componentLabelKeyError, "empty name field")
}

name := o.GetName()
if len(name) > 63 {
// Truncate
name = name[0:63]

// Remove trailing illegal characters
idx := len(name) - 1
for ; idx >= 0; idx-- {
lastChar := name[idx]
if lastChar != '.' && lastChar != '_' && lastChar != '-' {
break
}
}

// Just being defensive. This is unlikely to happen since the operator name should
// be compatible kubernetes naming constraints
if idx < 0 {
return "", fmt.Errorf(componentLabelKeyError, "unsupported name field")
}

// Update Label
name = name[0 : idx+1]
Comment on lines +138 to +155
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why no one seems to:

if name = strings.TrimRight(name[0:63], "._-"); len(name) <= 0 {
    return ....
}

Should the illegal characters be checked outside the len(name) check? Is it possible to set a name with an illegal trailing character? Could that happen?

Copy link
Contributor

@awgreene awgreene Feb 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why no one seems to:

if name = strings.TrimRight(name[0:63], "._-"); len(name) <= 0 {
return ....
}

I actually prefer your suggestion, but this is a backport of a fix and we attempt to avoid altering the code. We can always create a PR on master to do this though.

Is it possible to set a name with an illegal trailing character? Could that happen?

The kube api server will reject the create request and OLM will constantly try to create it even with the error, which is why we're backporting this.

}
o.componentLabelKey = ComponentLabelKeyPrefix + name

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,61 @@ func TestAddComponents(t *testing.T) {
})
}
}

func TestComponentLabelKey(t *testing.T) {
tests := []struct {
description string
componentLabelKey string
name string
expectedLabel string
returnsError bool
}{
{
description: "when componentLabelKey is set then return it",
componentLabelKey: "my-component-label",
name: "my-operator",
expectedLabel: "my-component-label",
returnsError: false,
},
{
description: "when componentLabelKey is not set and operator name is less than 63 characters then do not truncate",
componentLabelKey: "",
name: "my-operator",
expectedLabel: ComponentLabelKeyPrefix + "my-operator",
returnsError: false,
},
{
description: "when componentLabelKey is not set and operator name is more than 63 characters truncate",
name: "this-is-my-operator-its-the-coolest-you-got-a-problem-with-that-come-at-me-bro",
expectedLabel: ComponentLabelKeyPrefix + "this-is-my-operator-its-the-coolest-you-got-a-problem-with-that",
returnsError: false,
},
{
description: "when componentLabelKey is not set and operator name is more than 63 characters truncate and drop trailing illegal characters",
name: "this-is-my-operator-its-the-coolest-you-got-a-problem-with----...---___...---",
expectedLabel: ComponentLabelKeyPrefix + "this-is-my-operator-its-the-coolest-you-got-a-problem-with",
returnsError: false,
},
{
description: "when componentLabelKey is not set and operator name is more than 63 characters and is made up of illegal characters then return error",
name: "----...---___...-------...---___...-------...---___...-------...---___...---",
expectedLabel: "",
returnsError: true,
},
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't have the case for a small label key with illegal characters...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We'll need to fix this in master.


for _, tt := range tests {
operator := &Operator{
Operator: &operatorsv1.Operator{
ObjectMeta: metav1.ObjectMeta{
Name: tt.name,
},
},
componentLabelKey: tt.componentLabelKey,
}

actualLabel, actualErr := operator.ComponentLabelKey()
require.Equal(t, tt.returnsError, actualErr != nil, actualErr)
require.Equal(t, tt.expectedLabel, actualLabel)
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.