Skip to content

Commit

Permalink
Merge pull request #31 from awgreene/og-labels
Browse files Browse the repository at this point in the history
Update OLM to use UID for OG Labels
  • Loading branch information
awgreene authored May 1, 2020
2 parents ecc33ea + badc835 commit 5b1f691
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 5b1f691

Please sign in to comment.