Skip to content

Commit 70fc9d8

Browse files
authored
some fixes + new test (#36)
1 parent 07dde94 commit 70fc9d8

34 files changed

+1446
-21
lines changed

pkg/analyzer/scan.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func parseServiceResource(svcSpec v1.ServiceSpec, serviceCtx *common.Service) er
188188
for _, p := range svcSpec.Ports {
189189
n := common.SvcNetworkAttr{}
190190
n.Port = int(p.Port)
191-
n.TargetPort = int(p.TargetPort.IntVal)
191+
n.TargetPort = p.TargetPort
192192
n.Protocol = string(p.Protocol)
193193
serviceCtx.Resource.Network = append(serviceCtx.Resource.Network, n)
194194
}

pkg/common/types.go

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package common
22

3+
import (
4+
"k8s.io/apimachinery/pkg/util/intstr"
5+
)
6+
37
//InArgs :
48
type InArgs struct {
59
DirPath *string
@@ -46,9 +50,9 @@ type NetworkAttr struct {
4650

4751
//SvcNetworkAttr :
4852
type SvcNetworkAttr struct {
49-
Port int `json:"port,omitempty"`
50-
TargetPort int `json:"target_port,omitempty"`
51-
Protocol string `json:"protocol,omitempty"`
53+
Port int `json:"port,omitempty"`
54+
TargetPort intstr.IntOrString `json:"target_port,omitempty"`
55+
Protocol string `json:"protocol,omitempty"`
5256
}
5357

5458
//Service :

pkg/controller/controller_test.go

+25-14
Original file line numberDiff line numberDiff line change
@@ -57,25 +57,36 @@ func TestDirScan(t *testing.T) {
5757
os.Remove(outFile)
5858
}
5959

60+
type TestDetails struct {
61+
dirPath string
62+
outFile string
63+
expectedOutput string
64+
}
65+
6066
func TestNetpolsJsonOutput(t *testing.T) {
6167
currentDir, _ := os.Getwd()
62-
dirPath := filepath.Join(currentDir, "../../", "tests", "onlineboutique", "kubernetes-manifests.yaml")
63-
outFile := filepath.Join(currentDir, "../../", "tests", "onlineboutique", "output.json")
64-
expectedOutput := filepath.Join(currentDir, "../../", "tests", "onlineboutique", "expected_netpol_output.json")
65-
args := getTestArgs(dirPath, outFile, true)
66-
67-
Start(args)
68-
69-
res, err := compareFiles(expectedOutput, outFile)
68+
tests := map[string]TestDetails{} //map from test name to test details
69+
tests["onlineboutique"] = TestDetails{dirPath: filepath.Join(currentDir, "../../", "tests", "onlineboutique", "kubernetes-manifests.yaml"),
70+
outFile: filepath.Join(currentDir, "../../", "tests", "onlineboutique", "output.json"),
71+
expectedOutput: filepath.Join(currentDir, "../../", "tests", "onlineboutique", "expected_netpol_output.json")}
72+
tests["sockshop"] = TestDetails{dirPath: filepath.Join(currentDir, "../../", "tests", "sockshop", "manifests"),
73+
outFile: filepath.Join(currentDir, "../../", "tests", "sockshop", "output.json"),
74+
expectedOutput: filepath.Join(currentDir, "../../", "tests", "sockshop", "expected_netpol_output.json")}
75+
76+
for testName, testDetails := range tests {
77+
args := getTestArgs(testDetails.dirPath, testDetails.outFile, true)
78+
Start(args)
79+
res, err := compareFiles(testDetails.expectedOutput, testDetails.outFile)
80+
if err != nil {
81+
t.Fatalf("Test %v: expected err to be nil, but got %v", testName, err)
82+
}
83+
if !res {
84+
t.Fatalf("Test %v: expected res to be true, but got false", testName)
85+
}
86+
os.Remove(testDetails.outFile)
7087

71-
if err != nil {
72-
t.Fatalf("expected err to be nil, but got %v", err)
73-
}
74-
if !res {
75-
t.Fatalf("expected res to be true, but got false")
7688
}
7789

78-
os.Remove(outFile)
7990
}
8091

8192
func TestNetpolsInterface(t *testing.T) {

pkg/controller/synth_netpols.go

+8-3
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,13 @@ func determineConnectivityPerDeployment(connections []common.Connections) []*Dep
5454
target_ports := toNetpolPorts(conn.Link.Resource.Network) // TODO: filter by src ports
5555

5656
egressNetpolPeer := []network.NetworkPolicyPeer{{PodSelector: getDeployConnSelector(dstDeploy)}}
57-
srcDeploy.addEgressRule(egressNetpolPeer, target_ports)
57+
if srcDeploy != nil {
58+
srcDeploy.addEgressRule(egressNetpolPeer, target_ports)
59+
}
5860
var ingressNetpolPeer []network.NetworkPolicyPeer
5961
if len(conn.Source.Resource.Name) == 0 {
6062
ingressNetpolPeer = append(ingressNetpolPeer, network.NetworkPolicyPeer{})
61-
} else if conn.Link.Resource.Type != "LoadBalancer" {
63+
} else if conn.Link.Resource.Type != "LoadBalancer" && conn.Link.Resource.Type != "NodePort" {
6264
netpolPeer := network.NetworkPolicyPeer{PodSelector: getDeployConnSelector(srcDeploy)}
6365
ingressNetpolPeer = append(ingressNetpolPeer, netpolPeer)
6466
}
@@ -77,6 +79,9 @@ func determineConnectivityPerDeployment(connections []common.Connections) []*Dep
7779
}
7880

7981
func findOrAddDeploymentConn(resource common.Resource, deployConns map[string]*DeploymentConnectivity) *DeploymentConnectivity {
82+
if len(resource.Resource.Name) == 0 {
83+
return nil
84+
}
8085
if deployConn, found := deployConns[resource.Resource.Name]; found {
8186
return deployConn
8287
}
@@ -100,7 +105,7 @@ func toNetpolPorts(ports []common.SvcNetworkAttr) []network.NetworkPolicyPort {
100105
var netpolPorts []network.NetworkPolicyPort
101106
for _, port := range ports {
102107
protocol := toCoreProtocol(port.Protocol)
103-
portNum := intstr.FromInt(port.TargetPort)
108+
portNum := port.TargetPort
104109
netpolPort := network.NetworkPolicyPort{
105110
Protocol: &protocol,
106111
Port: &portNum,

0 commit comments

Comments
 (0)