Skip to content

Commit

Permalink
Update OLM to use UID for OG Labels
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
awgreene committed May 1, 2020
1 parent 50f95fe commit badc835
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions pkg/operators/v1/operatorgroup_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ const (

OperatorGroupKind = "OperatorGroup"

OperatorGroupLabelPrefix = "olm.operatorgroup/"
OperatorGroupLabelTemplate = OperatorGroupLabelPrefix + "%s.%s"
OperatorGroupLabelPrefix = "olm.operatorgroup.uid/"
OperatorGroupLabelTemplate = OperatorGroupLabelPrefix + "%s"
)

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

// GetLabel returns a label that is applied to Namespaces to signify that the
// namespace is a part of the OperatorGroup using selectors.
func (o *OperatorGroup) GetLabel() string {
key, _ := o.OGLabelKeyAndValue()
return key
}

// OGLabelKeyAndValue returns a key and value that should be applied to namespaces listed in the OperatorGroup.
func (o *OperatorGroup) OGLabelKeyAndValue() (string, string) {
return fmt.Sprintf(OperatorGroupLabelTemplate, o.GetNamespace(), o.GetName()), ""
// If the UID is not set an error is returned.
func (o *OperatorGroup) OGLabelKeyAndValue() (string, string, error) {
if string(o.GetUID()) == "" {
return "", "", fmt.Errorf("Missing UID")
}
return fmt.Sprintf(OperatorGroupLabelTemplate, o.GetUID()), "", nil
}

// NamespaceLabelSelector provides a selector that can be used to filter namespaces that belong to the OperatorGroup.
func (o *OperatorGroup) NamespaceLabelSelector() *metav1.LabelSelector {
func (o *OperatorGroup) NamespaceLabelSelector() (*metav1.LabelSelector, error) {
if len(o.Spec.TargetNamespaces) == 0 {
// If no target namespaces are set, check if a selector exists.
if o.Spec.Selector != nil {
return o.Spec.Selector
return o.Spec.Selector, nil
}
// No selector exists, return nil which should be used to select EVERYTHING.
return nil
return nil, nil
}
// Return a label that should be present on all namespaces defined in the OperatorGroup.Spec.TargetNamespaces field.
ogKey, ogValue := o.OGLabelKeyAndValue()
ogKey, ogValue, err := o.OGLabelKeyAndValue()
if err != nil {
return nil, err
}

return &metav1.LabelSelector{
MatchLabels: map[string]string{
ogKey: ogValue,
},
}
}, nil
}

// IsOperatorGroupLabel returns true if the label is an OperatorGroup label.
Expand Down

0 comments on commit badc835

Please sign in to comment.