-
Notifications
You must be signed in to change notification settings - Fork 260
[NPM] Ignoring hostnetwork pods from being added into Ipsets #776
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -202,3 +202,183 @@ func TestDeletePod(t *testing.T) { | |
| } | ||
| npMgr.Unlock() | ||
| } | ||
|
|
||
| func TestAddHostNetworkPod(t *testing.T) { | ||
| npMgr := &NetworkPolicyManager{ | ||
| nsMap: make(map[string]*namespace), | ||
| podMap: make(map[string]string), | ||
| TelemetryEnabled: false, | ||
| } | ||
|
|
||
| allNs, err := newNs(util.KubeAllNamespacesFlag) | ||
| if err != nil { | ||
| panic(err.Error) | ||
| } | ||
| npMgr.nsMap[util.KubeAllNamespacesFlag] = allNs | ||
|
|
||
| ipsMgr := ipsm.NewIpsetManager() | ||
| if err := ipsMgr.Save(util.IpsetTestConfigFile); err != nil { | ||
| t.Errorf("TestAddHostNetworkPod failed @ ipsMgr.Save") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add error to test logging. gives more context as to why something failed. same for below
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Logged a work item to fix this for all NPM tests. |
||
| } | ||
|
|
||
| defer func() { | ||
| if err := ipsMgr.Restore(util.IpsetTestConfigFile); err != nil { | ||
| t.Errorf("TestAddHostNetworkPod failed @ ipsMgr.Restore") | ||
| } | ||
| }() | ||
|
|
||
| podObj := &corev1.Pod{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-pod", | ||
| Namespace: "test-namespace", | ||
| Labels: map[string]string{ | ||
| "app": "test-pod", | ||
| }, | ||
| }, | ||
| Status: corev1.PodStatus{ | ||
| Phase: "Running", | ||
| PodIP: "1.2.3.4", | ||
| }, | ||
| Spec: corev1.PodSpec{ | ||
| HostNetwork: true, | ||
| }, | ||
| } | ||
|
|
||
| npMgr.Lock() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. curious about locks being taken, since this variable is scoped to the test. what else could be modifying this?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These are all compartmentalized tests, in the current state they are in, no two tests (i have to double check) share the npMgr. So the lock should be scop is limited to a given testcase.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose my question is, is it necessary to take locks? |
||
| if err := npMgr.AddPod(podObj); err != nil { | ||
| t.Errorf("TestAddHostNetworkPod failed @ AddPod") | ||
| } | ||
|
|
||
| if len(npMgr.podMap) >= 1 { | ||
| t.Errorf("TestAddHostNetworkPod failed @ podMap length check") | ||
| } | ||
| npMgr.Unlock() | ||
| } | ||
|
|
||
| func TestUpdateHostNetworkPod(t *testing.T) { | ||
| npMgr := &NetworkPolicyManager{ | ||
| nsMap: make(map[string]*namespace), | ||
| podMap: make(map[string]string), | ||
| TelemetryEnabled: false, | ||
| } | ||
|
|
||
| allNs, err := newNs(util.KubeAllNamespacesFlag) | ||
| if err != nil { | ||
| panic(err.Error) | ||
| } | ||
| npMgr.nsMap[util.KubeAllNamespacesFlag] = allNs | ||
|
|
||
| ipsMgr := ipsm.NewIpsetManager() | ||
| if err := ipsMgr.Save(util.IpsetTestConfigFile); err != nil { | ||
| t.Errorf("TestUpdateHostNetworkPod failed @ ipsMgr.Save") | ||
| } | ||
|
|
||
| defer func() { | ||
| if err := ipsMgr.Restore(util.IpsetTestConfigFile); err != nil { | ||
| t.Errorf("TestUpdateHostNetworkPod failed @ ipsMgr.Restore") | ||
| } | ||
| }() | ||
|
|
||
| // HostNetwork check is done on the oldPodObj, | ||
| // so intentionally not adding hostnet true in newPodObj | ||
| oldPodObj := &corev1.Pod{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "old-test-pod", | ||
| Namespace: "test-namespace", | ||
| Labels: map[string]string{ | ||
| "app": "old-test-pod", | ||
| }, | ||
| }, | ||
| Status: corev1.PodStatus{ | ||
| Phase: "Running", | ||
| PodIP: "1.2.3.4", | ||
| }, | ||
| Spec: corev1.PodSpec{ | ||
| HostNetwork: true, | ||
| }, | ||
| } | ||
|
|
||
| newPodObj := &corev1.Pod{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "new-test-pod", | ||
| Namespace: "test-namespace", | ||
| Labels: map[string]string{ | ||
| "app": "new-test-pod", | ||
| }, | ||
| }, | ||
| Status: corev1.PodStatus{ | ||
| Phase: "Running", | ||
| PodIP: "4.3.2.1", | ||
| }, | ||
| } | ||
|
|
||
| npMgr.Lock() | ||
| if err := npMgr.AddPod(oldPodObj); err != nil { | ||
| t.Errorf("TestUpdateHostNetworkPod failed @ AddPod") | ||
| } | ||
|
|
||
csfmomo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err := npMgr.UpdatePod(oldPodObj, newPodObj); err != nil { | ||
| t.Errorf("TestUpdateHostNetworkPod failed @ UpdatePod") | ||
| } | ||
|
|
||
| if len(npMgr.podMap) >= 1 { | ||
| t.Errorf("TestUpdateHostNetworkPod failed @ podMap length check") | ||
| } | ||
| npMgr.Unlock() | ||
| } | ||
|
|
||
| func TestDeleteHostNetworkPod(t *testing.T) { | ||
| npMgr := &NetworkPolicyManager{ | ||
| nsMap: make(map[string]*namespace), | ||
| podMap: make(map[string]string), | ||
| TelemetryEnabled: false, | ||
| } | ||
|
|
||
| allNs, err := newNs(util.KubeAllNamespacesFlag) | ||
| if err != nil { | ||
| panic(err.Error) | ||
| } | ||
| npMgr.nsMap[util.KubeAllNamespacesFlag] = allNs | ||
|
|
||
| ipsMgr := ipsm.NewIpsetManager() | ||
| if err := ipsMgr.Save(util.IpsetTestConfigFile); err != nil { | ||
| t.Errorf("TestDeleteHostNetworkPod failed @ ipsMgr.Save") | ||
| } | ||
|
|
||
| defer func() { | ||
| if err := ipsMgr.Restore(util.IpsetTestConfigFile); err != nil { | ||
| t.Errorf("TestDeleteHostNetworkPod failed @ ipsMgr.Restore") | ||
| } | ||
| }() | ||
|
|
||
| podObj := &corev1.Pod{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-pod", | ||
| Namespace: "test-namespace", | ||
| Labels: map[string]string{ | ||
| "app": "test-pod", | ||
| }, | ||
| }, | ||
| Status: corev1.PodStatus{ | ||
| Phase: "Running", | ||
| PodIP: "1.2.3.4", | ||
| }, | ||
| Spec: corev1.PodSpec{ | ||
| HostNetwork: true, | ||
| }, | ||
| } | ||
|
|
||
| npMgr.Lock() | ||
| if err := npMgr.AddPod(podObj); err != nil { | ||
| t.Errorf("TestDeleteHostNetworkPod failed @ AddPod") | ||
| } | ||
|
|
||
| if len(npMgr.podMap) >= 1 { | ||
| t.Errorf("TestDeleteHostNetworkPod failed @ podMap length check") | ||
| } | ||
|
|
||
| if err := npMgr.DeletePod(podObj); err != nil { | ||
| t.Errorf("TestDeleteHostNetworkPod failed @ DeletePod") | ||
| } | ||
csfmomo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| npMgr.Unlock() | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't panic on tests. use
t.Fatal. same for belowThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Logged a work item to fix this for all NPM tests.