Skip to content

Commit

Permalink
simplify checkObjectNamespaceLabels (envoyproxy#2480)
Browse files Browse the repository at this point in the history
* simplify and optimize checkObjectNamespaceLabels

Signed-off-by: yeedove <yeedove@gmail.com>

* move ContainsAllLabels and add ut.

Signed-off-by: yeedove <yeedove@gmail.com>

---------

Signed-off-by: yeedove <yeedove@gmail.com>
Co-authored-by: zirain <zirain2009@gmail.com>
  • Loading branch information
deszhou and zirain authored Jan 24, 2024
1 parent 914b48c commit c86681c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 23 deletions.
25 changes: 2 additions & 23 deletions internal/provider/kubernetes/predicates.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,36 +74,15 @@ func (r *gatewayAPIReconciler) checkObjectNamespaceLabels(obj matav1.Object) (bo
if err := r.client.Get(
context.Background(),
client.ObjectKey{
Namespace: "", // Namespace object should have empty Namespace
Namespace: "", // Namespace object should have an empty Namespace
Name: nsString,
},
ns,
); err != nil {
return false, err
}
return containAll(ns.Labels, r.namespaceLabels), nil
}

func containAll(labels map[string]string, labelsToCheck []string) bool {
if len(labels) < len(labelsToCheck) {
return false
}
for _, l := range labelsToCheck {
if !contains(labels, l) {
return false
}
}
return true
}

func contains(m map[string]string, i string) bool {
for k := range m {
if k == i {
return true
}
}

return false
return utils.ContainsAllLabels(ns.Labels, r.namespaceLabels), nil
}

// validateGatewayForReconcile returns true if the provided object is a Gateway
Expand Down
15 changes: 15 additions & 0 deletions internal/provider/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,18 @@ func HashString(str string) string {
h.Write([]byte(str))
return strings.ToLower(fmt.Sprintf("%x", h.Sum(nil)))
}

// ContainsAllLabels checks if all specified labels are present in the given map.
// It returns true if all labels are found, otherwise false.
// The function assumes that the map contains at least as many labels as specified in labelsToCheck.
func ContainsAllLabels(labels map[string]string, labelsToCheck []string) bool {
if len(labels) < len(labelsToCheck) {
return false
}
for _, label := range labelsToCheck {
if _, ok := labels[label]; !ok {
return false
}
}
return true
}
24 changes: 24 additions & 0 deletions internal/provider/utils/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,27 @@ func TestGetHashedName(t *testing.T) {
})
}
}

func TestContainsAllLabels(t *testing.T) {
type args struct {
labels map[string]string
labelsToCheck []string
}
tests := []struct {
name string
args args
want bool
}{
{"test all labels present", args{map[string]string{"label1": "foo", "label2": "bar"}, []string{"label1", "label2"}}, true},
{"test some labels missing", args{map[string]string{"label1": "foo", "label2": "bar"}, []string{"label1", "label3"}}, false},
{"test empty map", args{map[string]string{}, []string{"label1", "label2"}}, false},
{"test empty labelsToCheck", args{map[string]string{"label1": "foo", "label2": "bar"}, []string{}}, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ContainsAllLabels(tt.args.labels, tt.args.labelsToCheck); got != tt.want {
t.Errorf("ContainsAllLabels() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit c86681c

Please sign in to comment.