Skip to content

Commit

Permalink
k8s: refactoring to reduce code duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
lebauce committed Sep 28, 2018
1 parent d879420 commit 92acf54
Show file tree
Hide file tree
Showing 40 changed files with 1,119 additions and 2,603 deletions.
9 changes: 3 additions & 6 deletions analyzer/probes.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,16 @@ func NewTopologyProbeBundleFromConfig(g *graph.Graph) (*probe.ProbeBundle, error
continue
}

var err error
switch t {
case "k8s":
var err error
probes[t], err = k8s.NewProbe(g)
if err != nil {
if probes[t], err = k8s.NewK8sProbe(g); err != nil {
logging.GetLogger().Errorf("Failed to initialize K8S probe: %s", err.Error())
return nil, err
}

case "istio":
var err error
probes[t], err = istio.NewProbe(g)
if err != nil {
if probes[t], err = istio.NewIstioProbe(g); err != nil {
logging.GetLogger().Errorf("Failed to initialize istio probe: %s", err.Error())
return nil, err
}
Expand Down
1 change: 0 additions & 1 deletion etc/skydive.yml.default
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,6 @@ k8s:
# list of (sub) probes comprising k8s probe.
# if list is empty then will resolve to all existing (sub) probes.
probes:
- cluster
- container
- cronjob
- deployment
Expand Down
13 changes: 12 additions & 1 deletion tests/k8s/networkpolicy-egress-allow.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
apiVersion: v1
kind: Pod
metadata:
name: skydive-test-networkpolicy-egress-allow
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
Expand All @@ -7,4 +18,4 @@ spec:
egress:
- {}
policyTypes:
- Egress
- Egress
13 changes: 12 additions & 1 deletion tests/k8s/networkpolicy-egress-deny.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
apiVersion: v1
kind: Pod
metadata:
name: skydive-test-networkpolicy-egress-deny
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: skydive-test-networkpolicy-egress-deny
spec:
podSelector: {}
policyTypes:
- Egress
- Egress
26 changes: 24 additions & 2 deletions tests/k8s/networkpolicy-ingress-allow-namespace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@ apiVersion: v1
kind: Namespace
metadata:
name: skydive-test-networkpolicy-ingress-allow-namespace-to
labels:
app: skydive-test-networkpolicy-ingress-allow-namespace-to
---
apiVersion: v1
kind: Pod
metadata:
name: skydive-test-networkpolicy-ingress-allow-namespace-to
namespace: skydive-test-networkpolicy-ingress-allow-namespace-to
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
apiVersion: v1
kind: Namespace
Expand All @@ -12,6 +22,18 @@ metadata:
labels:
app: skydive-test-networkpolicy-ingress-allow-namespace-from
---
apiVersion: v1
kind: Pod
metadata:
name: skydive-test-networkpolicy-ingress-allow-namespace-from
namespace: skydive-test-networkpolicy-ingress-allow-namespace-from
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
Expand Down
13 changes: 12 additions & 1 deletion tests/k8s/networkpolicy-ingress-allow.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
apiVersion: v1
kind: Pod
metadata:
name: skydive-test-networkpolicy-ingress-allow
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: skydive-test-networkpolicy-ingress-allow
spec:
podSelector: {}
ingress:
- {}
- {}
13 changes: 12 additions & 1 deletion tests/k8s/networkpolicy-ingress-deny.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
apiVersion: v1
kind: Pod
metadata:
name: skydive-test-networkpolicy-ingress-deny
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: skydive-test-networkpolicy-ingress-deny
spec:
podSelector: {}
policyTypes:
- Ingress
- Ingress
17 changes: 14 additions & 3 deletions tests/k8s/networkpolicy-namespace.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@

kind: Namespace
apiVersion: v1
metadata:
name: skydive-test-networkpolicy-namespace
labels:
name: skydive-test-networkpolicy-namespace
---
apiVersion: v1
kind: Pod
metadata:
name: skydive-test-networkpolicy-namespace
namespace: skydive-test-networkpolicy-namespace
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
---
kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
Expand All @@ -13,4 +24,4 @@ metadata:
spec:
podSelector: {}
ingress:
- {}
- {}
31 changes: 17 additions & 14 deletions tests/k8s_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ func TestK8sClusterNode(t *testing.T) {
}

func TestK8sContainerNode(t *testing.T) {
testNodeCreationFromConfig(t, k8s.Manager, "container", objName+"-container", "Image", "Labels", "Pod")
testNodeCreationFromConfig(t, k8s.Manager, "container", objName+"-container", "Image", "Pod")
}

func TestK8sCronJobNode(t *testing.T) {
Expand Down Expand Up @@ -226,7 +226,7 @@ func TestK8sNodeNode(t *testing.T) {
}

func TestK8sPersistentVolumeNode(t *testing.T) {
testNodeCreationFromConfig(t, k8s.Manager, "persistentvolume", objName+"-persistentvolume", "Capacity", "AccessModes", "VolumeMode", "ClaimRef", "StorageClassName", "Status")
testNodeCreationFromConfig(t, k8s.Manager, "persistentvolume", objName+"-persistentvolume", "Capacity", "AccessModes", "VolumeMode", "StorageClassName", "Status")
}

func TestK8sPersistentVolumeClaimNode(t *testing.T) {
Expand Down Expand Up @@ -329,7 +329,7 @@ func TestHelloNodeScenario(t *testing.T) {
return err
}

_, err = checkNodeCreation(t, c, k8s.Manager, "node")
node, err := checkNodeCreation(t, c, k8s.Manager, "node")
if err != nil {
return err
}
Expand Down Expand Up @@ -364,6 +364,9 @@ func TestHelloNodeScenario(t *testing.T) {
return err
}

if err = checkEdgeAssociation(t, c, node, pod); err != nil {
return err
}
return nil
},
},
Expand All @@ -384,12 +387,12 @@ func TestK8sNetworkPolicyScenario1(t *testing.T) {
return err
}

namespace, err := checkNodeCreation(t, c, k8s.Manager, "namespace", "Name", name)
pod, err := checkNodeCreation(t, c, k8s.Manager, "pod", "Name", name)
if err != nil {
return err
}

if err = checkEdgeNetworkPolicy(t, c, networkpolicy, namespace, k8s.PolicyTypeIngress, k8s.PolicyTargetAllow, k8s.PolicyPointBegin); err != nil {
if err = checkEdgeNetworkPolicy(t, c, networkpolicy, pod, k8s.PolicyTypeIngress, k8s.PolicyTargetAllow, k8s.PolicyPointBegin); err != nil {
return err
}

Expand Down Expand Up @@ -442,12 +445,12 @@ func testK8sNetworkPolicyDefaultScenario(t *testing.T, policyType k8s.PolicyType
return err
}

ns, err := checkNodeCreation(t, c, k8s.Manager, "namespace", "Name", "default")
pod, err := checkNodeCreation(t, c, k8s.Manager, "pod", "Name", name)
if err != nil {
return err
}

if err = checkEdgeNetworkPolicy(t, c, np, ns, policyType, policyTarget, k8s.PolicyPointBegin); err != nil {
if err = checkEdgeNetworkPolicy(t, c, np, pod, policyType, policyTarget, k8s.PolicyPointBegin); err != nil {
return err
}

Expand All @@ -473,7 +476,7 @@ func TestK8sNetworkPolicyAllowEgressScenario(t *testing.T) {
testK8sNetworkPolicyDefaultScenario(t, k8s.PolicyTypeEgress, k8s.PolicyTargetAllow)
}

func testK8sNetworkPolicyObjectToObjectScenario(t *testing.T, policyType k8s.PolicyType, policyTarget k8s.PolicyTarget, resourceType, fileSuffix string, edgeArgs ...interface{}) {
func testK8sNetworkPolicyObjectToObjectScenario(t *testing.T, policyType k8s.PolicyType, policyTarget k8s.PolicyTarget, fileSuffix string, edgeArgs ...interface{}) {
file := fmt.Sprintf("networkpolicy-%s-%s-%s", policyType, policyTarget, fileSuffix)
name := objName + "-" + file
testRunner(
Expand All @@ -487,12 +490,12 @@ func testK8sNetworkPolicyObjectToObjectScenario(t *testing.T, policyType k8s.Pol
return err
}

begin, err := checkNodeCreation(t, c, k8s.Manager, resourceType, "Name", name+"-to")
begin, err := checkNodeCreation(t, c, k8s.Manager, "pod", "Name", name+"-to")
if err != nil {
return err
}

end, err := checkNodeCreation(t, c, k8s.Manager, resourceType, "Name", name+"-from")
end, err := checkNodeCreation(t, c, k8s.Manager, "pod", "Name", name+"-from")
if err != nil {
return err
}
Expand All @@ -512,15 +515,15 @@ func testK8sNetworkPolicyObjectToObjectScenario(t *testing.T, policyType k8s.Pol
}

func TestK8sNetworkPolicyAllowIngressPodToPodScenario(t *testing.T) {
testK8sNetworkPolicyObjectToObjectScenario(t, k8s.PolicyTypeIngress, k8s.PolicyTargetAllow, "pod", "pod")
testK8sNetworkPolicyObjectToObjectScenario(t, k8s.PolicyTypeIngress, k8s.PolicyTargetAllow, "pod")
}

func TestK8sNetworkPolicyAllowIngressNamespaceToNamepsaceScenario(t *testing.T) {
testK8sNetworkPolicyObjectToObjectScenario(t, k8s.PolicyTypeIngress, k8s.PolicyTargetAllow, "namespace", "namespace")
func TestK8sNetworkPolicyAllowIngressNamespaceToNamespaceScenario(t *testing.T) {
testK8sNetworkPolicyObjectToObjectScenario(t, k8s.PolicyTypeIngress, k8s.PolicyTargetAllow, "namespace")
}

func TestK8sNetworkPolicyAllowIngressPodToPodPortsScenario(t *testing.T) {
testK8sNetworkPolicyObjectToObjectScenario(t, k8s.PolicyTypeIngress, k8s.PolicyTargetAllow, "pod", "ports", "Ports", ":80")
testK8sNetworkPolicyObjectToObjectScenario(t, k8s.PolicyTypeIngress, k8s.PolicyTargetAllow, "ports", "Ports", ":80")
}

func TestK8sServicePodScenario(t *testing.T) {
Expand Down
5 changes: 3 additions & 2 deletions topology/probes/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ func (probe *DockerProbe) registerContainer(id string) {

probe.Graph.Lock()
metadata := graph.Metadata{
"Type": "container",
"Name": info.Name[1:],
"Type": "container",
"Name": info.Name[1:],
"Manager": "docker",
"Docker": map[string]interface{}{
"ContainerID": info.ID,
"ContainerName": info.Name,
Expand Down
47 changes: 0 additions & 47 deletions topology/probes/istio/client.go

This file was deleted.

Loading

0 comments on commit 92acf54

Please sign in to comment.