Skip to content

Commit b28f2c4

Browse files
committed
pull labels from namespace objects, handling default ns labels in kube 1.21
1 parent 77fcd53 commit b28f2c4

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

pkg/connectivity/probe/resources.go

+20
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ func NewDefaultResources(kubernetes kube.IKubernetes, namespaces []string, podNa
3939
if err := r.getPodIPsFromKube(kubernetes); err != nil {
4040
return nil, err
4141
}
42+
if err := r.getNamespaceLabelsFromKube(kubernetes); err != nil {
43+
return nil, err
44+
}
4245

4346
return r, nil
4447
}
@@ -95,6 +98,23 @@ func (r *Resources) getPodIPsFromKube(kubernetes kube.IKubernetes) error {
9598
return nil
9699
}
97100

101+
func (r *Resources) getNamespaceLabelsFromKube(kubernetes kube.IKubernetes) error {
102+
nsList, err := kubernetes.GetAllNamespaces()
103+
if err != nil {
104+
return err
105+
}
106+
107+
for _, kubeNs := range nsList.Items {
108+
for label, value := range kubeNs.Labels {
109+
if ns, ok := r.Namespaces[kubeNs.Name]; ok {
110+
ns[label] = value
111+
}
112+
}
113+
}
114+
115+
return nil
116+
}
117+
98118
func (r *Resources) GetPod(ns string, name string) (*Pod, error) {
99119
for _, pod := range r.Pods {
100120
if pod.Namespace == ns && pod.Name == name {

pkg/kube/ikubernetes.go

+11
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ type IKubernetes interface {
1414
GetNamespace(namespace string) (*v1.Namespace, error)
1515
SetNamespaceLabels(namespace string, labels map[string]string) (*v1.Namespace, error)
1616
DeleteNamespace(namespace string) error
17+
GetAllNamespaces() (*v1.NamespaceList, error)
1718

1819
CreateNetworkPolicy(kubePolicy *networkingv1.NetworkPolicy) (*networkingv1.NetworkPolicy, error)
1920
GetNetworkPoliciesInNamespace(namespace string) ([]networkingv1.NetworkPolicy, error)
@@ -144,6 +145,16 @@ func (m *MockKubernetes) DeleteNamespace(ns string) error {
144145
return nil
145146
}
146147

148+
func (m *MockKubernetes) GetAllNamespaces() (*v1.NamespaceList, error) {
149+
var namespaces []v1.Namespace
150+
for _, ns := range m.Namespaces {
151+
namespaces = append(namespaces, *ns.NamespaceObject)
152+
}
153+
return &v1.NamespaceList{
154+
Items: namespaces,
155+
}, nil
156+
}
157+
147158
func (m *MockKubernetes) CreateNamespace(ns *v1.Namespace) (*v1.Namespace, error) {
148159
if _, ok := m.Namespaces[ns.Name]; ok {
149160
return nil, errors.Errorf("namespace %s already present", ns.Name)

0 commit comments

Comments
 (0)