Skip to content

Commit

Permalink
Merge pull request #119 from mattfenwick/labels-diff-hacking
Browse files Browse the repository at this point in the history
handle default namespace labels in kube 1.21
  • Loading branch information
mattfenwick authored May 22, 2021
2 parents 254fc7d + bc33a54 commit ec3aee4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 9 deletions.
12 changes: 6 additions & 6 deletions pkg/connectivity/probe/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,25 @@ func (p *Pod) Host(probeMode generator.ProbeMode) string {
}
}

func (p *Pod) IsEqualToKubePod(kubePod v1.Pod) bool {
func (p *Pod) IsEqualToKubePod(kubePod v1.Pod) (string, bool) {
kubeConts := kubePod.Spec.Containers
if len(kubeConts) != len(p.Containers) {
return false
return fmt.Sprintf("have %d containers, expected %d", len(p.Containers), len(kubeConts)), false
}
for i, kubeCont := range kubeConts {
cont := p.Containers[i]
if len(kubeCont.Ports) != 1 {
return false
return fmt.Sprintf("container %d: expected 1 port, found %d", i, len(kubeCont.Ports)), false
}
if int(kubeCont.Ports[0].ContainerPort) != cont.Port {
return false
return fmt.Sprintf("container %d: expected port %d, found %d", i, cont.Port, kubeCont.Ports[0].ContainerPort), false
}
if kubeCont.Ports[0].Protocol != cont.Protocol {
return false
return fmt.Sprintf("container %d: expected protocol %s, found %s", i, cont.Protocol, kubeCont.Ports[0].Protocol), false
}
}

return true
return "", true
}

func (p *Pod) ServiceName() string {
Expand Down
20 changes: 20 additions & 0 deletions pkg/connectivity/probe/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ func NewDefaultResources(kubernetes kube.IKubernetes, namespaces []string, podNa
if err := r.getPodIPsFromKube(kubernetes); err != nil {
return nil, err
}
if err := r.getNamespaceLabelsFromKube(kubernetes); err != nil {
return nil, err
}

return r, nil
}
Expand Down Expand Up @@ -95,6 +98,23 @@ func (r *Resources) getPodIPsFromKube(kubernetes kube.IKubernetes) error {
return nil
}

func (r *Resources) getNamespaceLabelsFromKube(kubernetes kube.IKubernetes) error {
nsList, err := kubernetes.GetAllNamespaces()
if err != nil {
return err
}

for _, kubeNs := range nsList.Items {
for label, value := range kubeNs.Labels {
if ns, ok := r.Namespaces[kubeNs.Name]; ok {
ns[label] = value
}
}
}

return nil
}

func (r *Resources) GetPod(ns string, name string) (*Pod, error) {
for _, pod := range r.Pods {
if pod.Namespace == ns && pod.Name == name {
Expand Down
4 changes: 2 additions & 2 deletions pkg/connectivity/testcasestate.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ func (t *TestCaseState) verifyClusterStateHelper() error {
if actualPod.Status.PodIP != expectedPod.IP {
return errors.Errorf("for pod %s, expected ip %s (found %s)", expectedPod.PodString().String(), expectedPod.IP, actualPod.Status.PodIP)
}
if !expectedPod.IsEqualToKubePod(actualPod) {
return errors.Errorf("for pod %s, expected containers %+v (found %+v)", expectedPod.PodString().String(), expectedPod.Containers, actualPod.Spec.Containers)
if diff, ok := expectedPod.IsEqualToKubePod(actualPod); !ok {
return errors.Errorf("for pod %s, %s", expectedPod.PodString().String(), diff)
}
} else {
return errors.Errorf("missing expected pod %s", expectedPod.PodString().String())
Expand Down
27 changes: 26 additions & 1 deletion pkg/kube/ikubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package kube

import (
"fmt"
"github.com/mattfenwick/cyclonus/pkg/utils"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"math/rand"
)

Expand All @@ -13,6 +15,7 @@ type IKubernetes interface {
GetNamespace(namespace string) (*v1.Namespace, error)
SetNamespaceLabels(namespace string, labels map[string]string) (*v1.Namespace, error)
DeleteNamespace(namespace string) error
GetAllNamespaces() (*v1.NamespaceList, error)

CreateNetworkPolicy(kubePolicy *networkingv1.NetworkPolicy) (*networkingv1.NetworkPolicy, error)
GetNetworkPoliciesInNamespace(namespace string) ([]networkingv1.NetworkPolicy, error)
Expand Down Expand Up @@ -110,7 +113,17 @@ func (m *MockKubernetes) getNamespaceObject(namespace string) (*MockNamespace, e

func (m *MockKubernetes) GetNamespace(namespace string) (*v1.Namespace, error) {
if ns, ok := m.Namespaces[namespace]; ok {
return ns.NamespaceObject, nil
labels := map[string]string{}
for k, v := range ns.NamespaceObject.Labels {
labels[k] = v
}
labels["kubernetes.io/metadata.name"] = namespace
return &v1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: ns.NamespaceObject.Name,
Labels: labels,
},
}, nil
}
return nil, errors.Errorf("namespace %s not found", namespace)
}
Expand All @@ -132,6 +145,18 @@ func (m *MockKubernetes) DeleteNamespace(ns string) error {
return nil
}

func (m *MockKubernetes) GetAllNamespaces() (*v1.NamespaceList, error) {
var namespaces []v1.Namespace
for name := range m.Namespaces {
ns, err := m.GetNamespace(name)
utils.DoOrDie(err)
namespaces = append(namespaces, *ns)
}
return &v1.NamespaceList{
Items: namespaces,
}, nil
}

func (m *MockKubernetes) CreateNamespace(ns *v1.Namespace) (*v1.Namespace, error) {
if _, ok := m.Namespaces[ns.Name]; ok {
return nil, errors.Errorf("namespace %s already present", ns.Name)
Expand Down

0 comments on commit ec3aee4

Please sign in to comment.