|
49 | 49 | createPausePod = testutils.CreatePausePod
|
50 | 50 | deletePod = testutils.DeletePod
|
51 | 51 | getPod = testutils.GetPod
|
| 52 | + runPausePod = testutils.RunPausePod |
52 | 53 | initPausePod = testutils.InitPausePod
|
53 | 54 | initTest = testutils.InitTestSchedulerWithNS
|
54 | 55 | podScheduledIn = testutils.PodScheduledIn
|
| 56 | + podScheduled = testutils.PodScheduled |
55 | 57 | podUnschedulable = testutils.PodUnschedulable
|
56 | 58 | waitForPodUnschedulable = testutils.WaitForPodUnschedulable
|
57 | 59 | )
|
@@ -1822,6 +1824,129 @@ func TestPodTopologySpreadFilter(t *testing.T) {
|
1822 | 1824 | }
|
1823 | 1825 | }
|
1824 | 1826 |
|
| 1827 | +func TestNodePortsFilter(t *testing.T) { |
| 1828 | + pause := imageutils.GetPauseImageName() |
| 1829 | + tests := []struct { |
| 1830 | + name string |
| 1831 | + existingPods []*v1.Pod |
| 1832 | + incomingPod *v1.Pod |
| 1833 | + fit bool |
| 1834 | + }{ |
| 1835 | + { |
| 1836 | + name: "Port Conflict on Node", |
| 1837 | + existingPods: []*v1.Pod{ |
| 1838 | + st.MakePod().Name("test-1"). |
| 1839 | + Containers([]v1.Container{st.MakeContainer().Name("test-1-container").Image(pause). |
| 1840 | + ContainerPort([]v1.ContainerPort{ |
| 1841 | + { |
| 1842 | + HostIP: "127.0.0.1", |
| 1843 | + ContainerPort: 8081, |
| 1844 | + HostPort: 8081, |
| 1845 | + Protocol: "TCP", |
| 1846 | + }, |
| 1847 | + { |
| 1848 | + HostIP: "127.0.0.1", |
| 1849 | + ContainerPort: 8082, |
| 1850 | + HostPort: 8082, |
| 1851 | + Protocol: "TCP", |
| 1852 | + }, |
| 1853 | + }).Obj()}). |
| 1854 | + Obj(), |
| 1855 | + }, |
| 1856 | + incomingPod: st.MakePod().Name("test-2"). |
| 1857 | + Containers([]v1.Container{st.MakeContainer().Name("test-2-container").Image(pause). |
| 1858 | + ContainerPort([]v1.ContainerPort{ |
| 1859 | + { |
| 1860 | + HostIP: "127.0.0.1", |
| 1861 | + ContainerPort: 8081, |
| 1862 | + HostPort: 8081, |
| 1863 | + Protocol: "TCP", |
| 1864 | + }, |
| 1865 | + }).Obj()}). |
| 1866 | + Obj(), |
| 1867 | + fit: false, |
| 1868 | + }, |
| 1869 | + { |
| 1870 | + name: "No Port Conflict on Node", |
| 1871 | + existingPods: []*v1.Pod{ |
| 1872 | + st.MakePod().Name("test-1"). |
| 1873 | + Containers([]v1.Container{st.MakeContainer().Name("test-1-container").Image(pause). |
| 1874 | + ContainerPort([]v1.ContainerPort{ |
| 1875 | + { |
| 1876 | + HostIP: "127.0.0.1", |
| 1877 | + HostPort: 8081, |
| 1878 | + ContainerPort: 8081, |
| 1879 | + Protocol: "TCP", |
| 1880 | + }, |
| 1881 | + { |
| 1882 | + HostIP: "127.0.0.1", |
| 1883 | + HostPort: 8082, |
| 1884 | + ContainerPort: 8082, |
| 1885 | + Protocol: "TCP", |
| 1886 | + }, |
| 1887 | + }).Obj()}). |
| 1888 | + Obj(), |
| 1889 | + }, |
| 1890 | + incomingPod: st.MakePod().Name("test-2"). |
| 1891 | + Containers([]v1.Container{st.MakeContainer().Name("test-2-container").Image(pause). |
| 1892 | + ContainerPort([]v1.ContainerPort{ |
| 1893 | + { |
| 1894 | + HostIP: "127.0.0.1", |
| 1895 | + HostPort: 8080, |
| 1896 | + ContainerPort: 8080, |
| 1897 | + Protocol: "TCP", |
| 1898 | + }, |
| 1899 | + }).Obj()}). |
| 1900 | + Obj(), |
| 1901 | + fit: true, |
| 1902 | + }, |
| 1903 | + } |
| 1904 | + for _, tt := range tests { |
| 1905 | + t.Run(tt.name, func(t *testing.T) { |
| 1906 | + testCtx := initTest(t, "node-ports-filter") |
| 1907 | + cs := testCtx.ClientSet |
| 1908 | + ns := testCtx.NS.Name |
| 1909 | + |
| 1910 | + if _, err := createNode(cs, st.MakeNode().Name("node-0").Obj()); err != nil { |
| 1911 | + t.Fatalf("Failed to create node: %v", err) |
| 1912 | + } |
| 1913 | + |
| 1914 | + // set namespace to pods |
| 1915 | + tt.incomingPod.SetNamespace(ns) |
| 1916 | + for i := range tt.existingPods { |
| 1917 | + tt.existingPods[i].SetNamespace(ns) |
| 1918 | + } |
| 1919 | + allPods := append([]*v1.Pod{tt.incomingPod}, tt.existingPods...) |
| 1920 | + defer testutils.CleanupPods(testCtx.Ctx, cs, t, allPods) |
| 1921 | + |
| 1922 | + for _, pod := range tt.existingPods { |
| 1923 | + if _, err := runPausePod(testCtx.ClientSet, pod); err != nil { |
| 1924 | + t.Fatalf("Failed to create existing pod: %v", err) |
| 1925 | + } |
| 1926 | + } |
| 1927 | + |
| 1928 | + testPod, err := cs.CoreV1().Pods(tt.incomingPod.Namespace).Create(testCtx.Ctx, tt.incomingPod, metav1.CreateOptions{}) |
| 1929 | + if err != nil { |
| 1930 | + t.Fatalf("Failed to create pod during test: %v", err) |
| 1931 | + } |
| 1932 | + |
| 1933 | + if tt.fit { |
| 1934 | + err = wait.PollUntilContextTimeout(testCtx.Ctx, pollInterval, wait.ForeverTestTimeout, false, |
| 1935 | + podScheduled(cs, testPod.Namespace, testPod.Name)) |
| 1936 | + if err != nil { |
| 1937 | + t.Errorf("Test Failed: Expected pod %s/%s to be scheduled but got error: %v", testPod.Namespace, testPod.Name, err) |
| 1938 | + } |
| 1939 | + } else { |
| 1940 | + err = wait.PollUntilContextTimeout(testCtx.Ctx, pollInterval, wait.ForeverTestTimeout, false, |
| 1941 | + podUnschedulable(cs, testPod.Namespace, testPod.Name)) |
| 1942 | + if err != nil { |
| 1943 | + t.Errorf("Test Failed: Expected pod %s/%s to be unschedulable but got error: %v", testPod.Namespace, testPod.Name, err) |
| 1944 | + } |
| 1945 | + } |
| 1946 | + }) |
| 1947 | + } |
| 1948 | +} |
| 1949 | + |
1825 | 1950 | var (
|
1826 | 1951 | hardSpread = v1.DoNotSchedule
|
1827 | 1952 | )
|
|
0 commit comments