Skip to content

Commit 89af2c2

Browse files
committed
Update OLM to use UID for OG Labels
Problem: OLM applies an "OperatorGroup Label" to namespaces which makes it easy to select namespaces that are included in an OperatorGroup. Currently, OLM applies a label with a blank value whose key is equal to "olm.operatorgroup/<OperatorGroup Namespace>.<OperatorGroup Name>". Kubernetes limits the lengths of label values and keys to 63 characters. This limit can easily be overcome when the OperatorGroup has a long name or when it is deployed in a namespace with a long name. Solution: Update OLM to use "olm.operatorgroup.uid/<OperatorGroup UID>" as the key for OperatorGroup labels. The length of this label will always be 58 characters as UIDs are 36 characters long.
1 parent 50f95fe commit 89af2c2

File tree

1 file changed

+17
-16
lines changed

1 file changed

+17
-16
lines changed

pkg/operators/v1/operatorgroup_types.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ const (
1717

1818
OperatorGroupKind = "OperatorGroup"
1919

20-
OperatorGroupLabelPrefix = "olm.operatorgroup/"
21-
OperatorGroupLabelTemplate = OperatorGroupLabelPrefix + "%s.%s"
20+
OperatorGroupLabelPrefix = "olm.operatorgroup.uid/"
21+
OperatorGroupLabelTemplate = OperatorGroupLabelPrefix + "%s"
2222
)
2323

2424
// OperatorGroupSpec is the spec for an OperatorGroup resource.
@@ -108,35 +108,36 @@ func (o *OperatorGroup) HasServiceAccountSynced() bool {
108108
return false
109109
}
110110

111-
// GetLabel returns a label that is applied to Namespaces to signify that the
112-
// namespace is a part of the OperatorGroup using selectors.
113-
func (o *OperatorGroup) GetLabel() string {
114-
key, _ := o.OGLabelKeyAndValue()
115-
return key
116-
}
117-
118111
// OGLabelKeyAndValue returns a key and value that should be applied to namespaces listed in the OperatorGroup.
119-
func (o *OperatorGroup) OGLabelKeyAndValue() (string, string) {
120-
return fmt.Sprintf(OperatorGroupLabelTemplate, o.GetNamespace(), o.GetName()), ""
112+
// If the UID is not set an error is returned.
113+
func (o *OperatorGroup) OGLabelKeyAndValue() (string, string, error) {
114+
if string(o.UID) == "" {
115+
return "", "", fmt.Errorf("Missing UID")
116+
}
117+
return fmt.Sprintf(OperatorGroupLabelTemplate, string(o.UID)), "", nil
121118
}
122119

123120
// NamespaceLabelSelector provides a selector that can be used to filter namespaces that belong to the OperatorGroup.
124-
func (o *OperatorGroup) NamespaceLabelSelector() *metav1.LabelSelector {
121+
func (o *OperatorGroup) NamespaceLabelSelector() (*metav1.LabelSelector, error) {
125122
if len(o.Spec.TargetNamespaces) == 0 {
126123
// If no target namespaces are set, check if a selector exists.
127124
if o.Spec.Selector != nil {
128-
return o.Spec.Selector
125+
return o.Spec.Selector, nil
129126
}
130127
// No selector exists, return nil which should be used to select EVERYTHING.
131-
return nil
128+
return nil, nil
132129
}
133130
// Return a label that should be present on all namespaces defined in the OperatorGroup.Spec.TargetNamespaces field.
134-
ogKey, ogValue := o.OGLabelKeyAndValue()
131+
ogKey, ogValue, err := o.OGLabelKeyAndValue()
132+
if err != nil {
133+
return nil, err
134+
}
135+
135136
return &metav1.LabelSelector{
136137
MatchLabels: map[string]string{
137138
ogKey: ogValue,
138139
},
139-
}
140+
}, nil
140141
}
141142

142143
// IsOperatorGroupLabel returns true if the label is an OperatorGroup label.

0 commit comments

Comments
 (0)