proxier_test.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. // +build windows
  2. /*
  3. Copyright 2018 The Kubernetes Authors.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.apache.org/licenses/LICENSE-2.0
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. */
  14. package winkernel
  15. import (
  16. "k8s.io/api/core/v1"
  17. "k8s.io/apimachinery/pkg/types"
  18. "k8s.io/kubernetes/pkg/proxy"
  19. "net"
  20. "strings"
  21. "testing"
  22. "time"
  23. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  24. )
  25. const testHostName = "test-hostname"
  26. const macAddress = "00-11-22-33-44-55"
  27. const clusterCIDR = "192.168.1.0/24"
  28. const destinationPrefix = "192.168.2.0/24"
  29. const providerAddress = "10.0.0.3"
  30. const guid = "123ABC"
  31. type fakeHealthChecker struct {
  32. services map[types.NamespacedName]uint16
  33. endpoints map[types.NamespacedName]int
  34. }
  35. func newFakeHealthChecker() *fakeHealthChecker {
  36. return &fakeHealthChecker{
  37. services: map[types.NamespacedName]uint16{},
  38. endpoints: map[types.NamespacedName]int{},
  39. }
  40. }
  41. func (fake *fakeHealthChecker) SyncServices(newServices map[types.NamespacedName]uint16) error {
  42. fake.services = newServices
  43. return nil
  44. }
  45. func (fake *fakeHealthChecker) SyncEndpoints(newEndpoints map[types.NamespacedName]int) error {
  46. fake.endpoints = newEndpoints
  47. return nil
  48. }
  49. type fakeHNS struct{}
  50. func newFakeHNS() *fakeHNS {
  51. return &fakeHNS{}
  52. }
  53. func (hns fakeHNS) getNetworkByName(name string) (*hnsNetworkInfo, error) {
  54. var remoteSubnets []*remoteSubnetInfo
  55. rs := &remoteSubnetInfo{
  56. destinationPrefix: destinationPrefix,
  57. isolationID: 4096,
  58. providerAddress: providerAddress,
  59. drMacAddress: macAddress,
  60. }
  61. remoteSubnets = append(remoteSubnets, rs)
  62. return &hnsNetworkInfo{
  63. id: strings.ToUpper(guid),
  64. name: name,
  65. networkType: "Overlay",
  66. remoteSubnets: remoteSubnets,
  67. }, nil
  68. }
  69. func (hns fakeHNS) getEndpointByID(id string) (*endpointsInfo, error) {
  70. return nil, nil
  71. }
  72. func (hns fakeHNS) getEndpointByIpAddress(ip string, networkName string) (*endpointsInfo, error) {
  73. _, ipNet, _ := net.ParseCIDR(destinationPrefix)
  74. if ipNet.Contains(net.ParseIP(ip)) {
  75. return &endpointsInfo{
  76. ip: ip,
  77. isLocal: true,
  78. macAddress: macAddress,
  79. hnsID: guid,
  80. hns: hns,
  81. }, nil
  82. }
  83. return nil, nil
  84. }
  85. func (hns fakeHNS) createEndpoint(ep *endpointsInfo, networkName string) (*endpointsInfo, error) {
  86. return &endpointsInfo{
  87. ip: ep.ip,
  88. isLocal: ep.isLocal,
  89. macAddress: ep.macAddress,
  90. hnsID: guid,
  91. hns: hns,
  92. }, nil
  93. }
  94. func (hns fakeHNS) deleteEndpoint(hnsID string) error {
  95. return nil
  96. }
  97. func (hns fakeHNS) getLoadBalancer(endpoints []endpointsInfo, flags loadBalancerFlags, sourceVip string, vip string, protocol uint16, internalPort uint16, externalPort uint16) (*loadBalancerInfo, error) {
  98. return &loadBalancerInfo{
  99. hnsID: guid,
  100. }, nil
  101. }
  102. func (hns fakeHNS) deleteLoadBalancer(hnsID string) error {
  103. return nil
  104. }
  105. func NewFakeProxier(syncPeriod time.Duration, minSyncPeriod time.Duration, clusterCIDR string, hostname string, nodeIP net.IP, networkType string) *Proxier {
  106. sourceVip := "192.168.1.2"
  107. hnsNetworkInfo := &hnsNetworkInfo{
  108. name: "TestNetwork",
  109. networkType: networkType,
  110. }
  111. proxier := &Proxier{
  112. portsMap: make(map[localPort]closeable),
  113. serviceMap: make(proxyServiceMap),
  114. serviceChanges: newServiceChangeMap(),
  115. endpointsMap: make(proxyEndpointsMap),
  116. endpointsChanges: newEndpointsChangeMap(hostname),
  117. clusterCIDR: clusterCIDR,
  118. hostname: testHostName,
  119. nodeIP: nodeIP,
  120. healthChecker: newFakeHealthChecker(),
  121. network: *hnsNetworkInfo,
  122. sourceVip: sourceVip,
  123. hostMac: macAddress,
  124. isDSR: false,
  125. hns: newFakeHNS(),
  126. }
  127. return proxier
  128. }
  129. func TestCreateServiceVip(t *testing.T) {
  130. syncPeriod := 30 * time.Second
  131. proxier := NewFakeProxier(syncPeriod, syncPeriod, clusterCIDR, "testhost", net.ParseIP("10.0.0.1"), "Overlay")
  132. if proxier == nil {
  133. t.Error()
  134. }
  135. svcIP := "10.20.30.41"
  136. svcPort := 80
  137. svcNodePort := 3001
  138. svcExternalIPs := "50.60.70.81"
  139. svcPortName := proxy.ServicePortName{
  140. NamespacedName: makeNSN("ns1", "svc1"),
  141. Port: "p80",
  142. }
  143. timeoutSeconds := v1.DefaultClientIPServiceAffinitySeconds
  144. makeServiceMap(proxier,
  145. makeTestService(svcPortName.Namespace, svcPortName.Name, func(svc *v1.Service) {
  146. svc.Spec.Type = "NodePort"
  147. svc.Spec.ClusterIP = svcIP
  148. svc.Spec.ExternalIPs = []string{svcExternalIPs}
  149. svc.Spec.SessionAffinity = v1.ServiceAffinityClientIP
  150. svc.Spec.SessionAffinityConfig = &v1.SessionAffinityConfig{
  151. ClientIP: &v1.ClientIPConfig{
  152. TimeoutSeconds: &timeoutSeconds,
  153. },
  154. }
  155. svc.Spec.Ports = []v1.ServicePort{{
  156. Name: svcPortName.Port,
  157. Port: int32(svcPort),
  158. Protocol: v1.ProtocolTCP,
  159. NodePort: int32(svcNodePort),
  160. }}
  161. }),
  162. )
  163. makeEndpointsMap(proxier)
  164. proxier.syncProxyRules()
  165. if proxier.serviceMap[svcPortName].remoteEndpoint == nil {
  166. t.Error()
  167. }
  168. if proxier.serviceMap[svcPortName].remoteEndpoint.ip != svcIP {
  169. t.Error()
  170. }
  171. }
  172. func TestCreateRemoteEndpointOverlay(t *testing.T) {
  173. syncPeriod := 30 * time.Second
  174. proxier := NewFakeProxier(syncPeriod, syncPeriod, clusterCIDR, "testhost", net.ParseIP("10.0.0.1"), "Overlay")
  175. if proxier == nil {
  176. t.Error()
  177. }
  178. svcIP := "10.20.30.41"
  179. svcPort := 80
  180. svcNodePort := 3001
  181. svcPortName := proxy.ServicePortName{
  182. NamespacedName: makeNSN("ns1", "svc1"),
  183. Port: "p80",
  184. }
  185. makeServiceMap(proxier,
  186. makeTestService(svcPortName.Namespace, svcPortName.Name, func(svc *v1.Service) {
  187. svc.Spec.Type = "NodePort"
  188. svc.Spec.ClusterIP = svcIP
  189. svc.Spec.Ports = []v1.ServicePort{{
  190. Name: svcPortName.Port,
  191. Port: int32(svcPort),
  192. Protocol: v1.ProtocolTCP,
  193. NodePort: int32(svcNodePort),
  194. }}
  195. }),
  196. )
  197. makeEndpointsMap(proxier,
  198. makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) {
  199. ept.Subsets = []v1.EndpointSubset{{
  200. Addresses: []v1.EndpointAddress{{
  201. IP: epIpAddressRemote,
  202. }},
  203. Ports: []v1.EndpointPort{{
  204. Name: svcPortName.Port,
  205. Port: int32(svcPort),
  206. }},
  207. }}
  208. }),
  209. )
  210. proxier.syncProxyRules()
  211. if proxier.endpointsMap[svcPortName][0].hnsID != guid {
  212. t.Errorf("%v does not match %v", proxier.endpointsMap[svcPortName][0].hnsID, guid)
  213. }
  214. }
  215. func TestCreateRemoteEndpointL2Bridge(t *testing.T) {
  216. syncPeriod := 30 * time.Second
  217. proxier := NewFakeProxier(syncPeriod, syncPeriod, clusterCIDR, "testhost", net.ParseIP("10.0.0.1"), "L2Bridge")
  218. if proxier == nil {
  219. t.Error()
  220. }
  221. svcIP := "10.20.30.41"
  222. svcPort := 80
  223. svcNodePort := 3001
  224. svcPortName := proxy.ServicePortName{
  225. NamespacedName: makeNSN("ns1", "svc1"),
  226. Port: "p80",
  227. }
  228. makeServiceMap(proxier,
  229. makeTestService(svcPortName.Namespace, svcPortName.Name, func(svc *v1.Service) {
  230. svc.Spec.Type = "NodePort"
  231. svc.Spec.ClusterIP = svcIP
  232. svc.Spec.Ports = []v1.ServicePort{{
  233. Name: svcPortName.Port,
  234. Port: int32(svcPort),
  235. Protocol: v1.ProtocolTCP,
  236. NodePort: int32(svcNodePort),
  237. }}
  238. }),
  239. )
  240. makeEndpointsMap(proxier,
  241. makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) {
  242. ept.Subsets = []v1.EndpointSubset{{
  243. Addresses: []v1.EndpointAddress{{
  244. IP: epIpAddressRemote,
  245. }},
  246. Ports: []v1.EndpointPort{{
  247. Name: svcPortName.Port,
  248. Port: int32(svcPort),
  249. }},
  250. }}
  251. }),
  252. )
  253. proxier.syncProxyRules()
  254. if proxier.endpointsMap[svcPortName][0].hnsID != guid {
  255. t.Errorf("%v does not match %v", proxier.endpointsMap[svcPortName][0].hnsID, guid)
  256. }
  257. }
  258. func TestCreateLoadBalancer(t *testing.T) {
  259. syncPeriod := 30 * time.Second
  260. proxier := NewFakeProxier(syncPeriod, syncPeriod, clusterCIDR, "testhost", net.ParseIP("10.0.0.1"), "Overlay")
  261. if proxier == nil {
  262. t.Error()
  263. }
  264. svcIP := "10.20.30.41"
  265. svcPort := 80
  266. svcNodePort := 3001
  267. svcPortName := proxy.ServicePortName{
  268. NamespacedName: makeNSN("ns1", "svc1"),
  269. Port: "p80",
  270. }
  271. makeServiceMap(proxier,
  272. makeTestService(svcPortName.Namespace, svcPortName.Name, func(svc *v1.Service) {
  273. svc.Spec.Type = "NodePort"
  274. svc.Spec.ClusterIP = svcIP
  275. svc.Spec.Ports = []v1.ServicePort{{
  276. Name: svcPortName.Port,
  277. Port: int32(svcPort),
  278. Protocol: v1.ProtocolTCP,
  279. NodePort: int32(svcNodePort),
  280. }}
  281. }),
  282. )
  283. makeEndpointsMap(proxier,
  284. makeTestEndpoints(svcPortName.Namespace, svcPortName.Name, func(ept *v1.Endpoints) {
  285. ept.Subsets = []v1.EndpointSubset{{
  286. Addresses: []v1.EndpointAddress{{
  287. IP: epIpAddressRemote,
  288. }},
  289. Ports: []v1.EndpointPort{{
  290. Name: svcPortName.Port,
  291. Port: int32(svcPort),
  292. }},
  293. }}
  294. }),
  295. )
  296. proxier.syncProxyRules()
  297. if proxier.serviceMap[svcPortName].hnsID != guid {
  298. t.Errorf("%v does not match %v", proxier.serviceMap[svcPortName].hnsID, guid)
  299. }
  300. }
  301. func makeNSN(namespace, name string) types.NamespacedName {
  302. return types.NamespacedName{Namespace: namespace, Name: name}
  303. }
  304. func makeServiceMap(proxier *Proxier, allServices ...*v1.Service) {
  305. for i := range allServices {
  306. proxier.OnServiceAdd(allServices[i])
  307. }
  308. proxier.mu.Lock()
  309. defer proxier.mu.Unlock()
  310. proxier.servicesSynced = true
  311. }
  312. func makeTestService(namespace, name string, svcFunc func(*v1.Service)) *v1.Service {
  313. svc := &v1.Service{
  314. ObjectMeta: metav1.ObjectMeta{
  315. Name: name,
  316. Namespace: namespace,
  317. Annotations: map[string]string{},
  318. },
  319. Spec: v1.ServiceSpec{},
  320. Status: v1.ServiceStatus{},
  321. }
  322. svcFunc(svc)
  323. return svc
  324. }
  325. func makeEndpointsMap(proxier *Proxier, allEndpoints ...*v1.Endpoints) {
  326. for i := range allEndpoints {
  327. proxier.OnEndpointsAdd(allEndpoints[i])
  328. }
  329. proxier.mu.Lock()
  330. defer proxier.mu.Unlock()
  331. proxier.endpointsSynced = true
  332. }
  333. func makeTestEndpoints(namespace, name string, eptFunc func(*v1.Endpoints)) *v1.Endpoints {
  334. ept := &v1.Endpoints{
  335. ObjectMeta: metav1.ObjectMeta{
  336. Name: name,
  337. Namespace: namespace,
  338. },
  339. }
  340. eptFunc(ept)
  341. return ept
  342. }