Skip to content
Open
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
8 changes: 8 additions & 0 deletions modules/common/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package labels
import (
"github.com/openstack-k8s-operators/lib-common/modules/common"
"github.com/openstack-k8s-operators/lib-common/modules/common/util"
"k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -108,3 +109,10 @@ func GetLabelSelector(
MatchLabels: serviceLabels,
}
}

// EqualLabelSelectors - returns true if two labelSelectors matches, false
// otherwise
func EqualLabelSelectors(
l1, l2 metav1.LabelSelector) bool {
return equality.Semantic.DeepEqual(l1, l2)
}
17 changes: 17 additions & 0 deletions modules/common/labels/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,20 @@ func TestGetLabels(t *testing.T) {
})
}
}

// Given a map[string]string, get the corresponding labelSelectors and compare
// them via the EqualLabelSelectors utility
func TestEqualLabelSelectors(t *testing.T) {
t.Run("Compare labelSelectors", func(t *testing.T) {
g := NewWithT(t)

l0 := GetLabelSelector(map[string]string{})
l1 := GetLabelSelector(map[string]string{"app": "foo", "version": "v1", "property": "bar"})
l2 := l1
l3 := GetLabelSelector(map[string]string{"app": "api", "version": "v1"})

g.Expect(EqualLabelSelectors(l1, l0)).To(BeFalse())
g.Expect(EqualLabelSelectors(l1, l2)).To(BeTrue())
g.Expect(EqualLabelSelectors(l1, l3)).To(BeFalse())
})
}