/
namespace.go
48 lines (41 loc) · 1.26 KB
/
namespace.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package graph
import (
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/types"
)
// buildReferencedNamespaces returns a map of all the Namespace resources from the current clusterNamespaces with
// a label that matches any of the Gateway Listener's label selector.
func buildReferencedNamespaces(
clusterNamespaces map[types.NamespacedName]*v1.Namespace,
gw *Gateway,
) map[types.NamespacedName]*v1.Namespace {
referencedNamespaces := make(map[types.NamespacedName]*v1.Namespace)
for name, ns := range clusterNamespaces {
if isNamespaceReferenced(ns, gw) {
referencedNamespaces[name] = ns
}
}
if len(referencedNamespaces) == 0 {
return nil
}
return referencedNamespaces
}
// isNamespaceReferenced returns true if a given Namespace resource has a label
// that matches any of the Gateway Listener's label selector.
func isNamespaceReferenced(ns *v1.Namespace, gw *Gateway) bool {
if gw == nil || ns == nil {
return false
}
nsLabels := labels.Set(ns.GetLabels())
for _, listener := range gw.Listeners {
if listener.AllowedRouteLabelSelector == nil {
// Can have listeners with AllowedRouteLabelSelector not set.
continue
}
if listener.AllowedRouteLabelSelector.Matches(nsLabels) {
return true
}
}
return false
}