|
1 | 1 | package util |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "encoding/base64" |
5 | 6 | "fmt" |
| 7 | + "k8s.io/apimachinery/pkg/util/intstr" |
| 8 | + "k8s.io/client-go/informers" |
| 9 | + corelisters "k8s.io/client-go/listers/core/v1" |
| 10 | + "k8s.io/client-go/tools/cache" |
6 | 11 | "log" |
7 | 12 | "os" |
8 | 13 | "strings" |
@@ -87,14 +92,42 @@ func GetServiceListResource(namespace string, name string, port int32) *v1.Servi |
87 | 92 | ExternalTrafficPolicy: v1.ServiceExternalTrafficPolicyTypeLocal, |
88 | 93 | }, |
89 | 94 | } |
| 95 | + testService3 := v1.Service{ |
| 96 | + ObjectMeta: metav1.ObjectMeta{ |
| 97 | + Name: "service-with-named-target-port", |
| 98 | + Namespace: namespace, |
| 99 | + }, |
| 100 | + Spec: v1.ServiceSpec{ |
| 101 | + Selector: map[string]string{"app": name}, |
| 102 | + Ports: []v1.ServicePort{ |
| 103 | + { |
| 104 | + Protocol: v1.ProtocolTCP, |
| 105 | + Port: 8081, |
| 106 | + NodePort: 30225, |
| 107 | + TargetPort: intstr.FromString("test_port_name"), |
| 108 | + Name: "port_8081", |
| 109 | + }, |
| 110 | + { |
| 111 | + Protocol: v1.ProtocolTCP, |
| 112 | + Port: 8082, |
| 113 | + NodePort: 30226, |
| 114 | + TargetPort: intstr.FromInt(1001), |
| 115 | + Name: "port_8082", |
| 116 | + }, |
| 117 | + }, |
| 118 | + ExternalTrafficPolicy: v1.ServiceExternalTrafficPolicyTypeLocal, |
| 119 | + }, |
| 120 | + } |
90 | 121 | var services []v1.Service |
91 | 122 | services = append(services, testService) |
92 | 123 | services = append(services, testService2) |
| 124 | + services = append(services, testService3) |
93 | 125 |
|
94 | 126 | return &v1.ServiceList{ |
95 | 127 | Items: services, |
96 | 128 | } |
97 | 129 | } |
| 130 | + |
98 | 131 | func GetServiceListResourceWithPortName(namespace string, name string, port int32, portName string) *v1.ServiceList { |
99 | 132 | testService := v1.Service{ |
100 | 133 | ObjectMeta: metav1.ObjectMeta{ |
@@ -239,10 +272,32 @@ func GetEndpointsResourceList(name string, namespace string, allCase bool) *v1.E |
239 | 272 | Ports: []v1.EndpointPort{{Port: 1000}}, |
240 | 273 | }}, |
241 | 274 | } |
| 275 | + endpoint3 := v1.Endpoints{ |
| 276 | + ObjectMeta: metav1.ObjectMeta{ |
| 277 | + Name: "service-with-named-target-port", |
| 278 | + Namespace: namespace, |
| 279 | + ResourceVersion: "1", |
| 280 | + }, |
| 281 | + Subsets: []v1.EndpointSubset{{ |
| 282 | + Addresses: []v1.EndpointAddress{{ |
| 283 | + IP: "6.7.8.9", |
| 284 | + Hostname: "", |
| 285 | + NodeName: &emptyNodeName, |
| 286 | + TargetRef: &v1.ObjectReference{ |
| 287 | + Kind: "Pod", |
| 288 | + Namespace: "default", |
| 289 | + Name: "testpod", |
| 290 | + UID: "999", |
| 291 | + }, |
| 292 | + }}, |
| 293 | + Ports: []v1.EndpointPort{{Port: 1000, Name: "port_8081"}, {Port: 1001, Name: "port_8082"}}, |
| 294 | + }}, |
| 295 | + } |
242 | 296 |
|
243 | 297 | var endpoints []v1.Endpoints |
244 | 298 | endpoints = append(endpoints, endpoint) |
245 | 299 | endpoints = append(endpoints, endpoint2) |
| 300 | + endpoints = append(endpoints, endpoint3) |
246 | 301 |
|
247 | 302 | return &v1.EndpointsList{ |
248 | 303 | Items: endpoints, |
@@ -326,11 +381,33 @@ func GetEndpointsResourceListAllCase(name string, namespace string) *v1.Endpoint |
326 | 381 | }}, |
327 | 382 | }}, |
328 | 383 | } |
| 384 | + endpoint4 := v1.Endpoints{ |
| 385 | + ObjectMeta: metav1.ObjectMeta{ |
| 386 | + Name: "service-with-named-target-port", |
| 387 | + Namespace: namespace, |
| 388 | + ResourceVersion: "1", |
| 389 | + }, |
| 390 | + Subsets: []v1.EndpointSubset{{ |
| 391 | + Addresses: []v1.EndpointAddress{{ |
| 392 | + IP: "6.7.8.9", |
| 393 | + Hostname: "", |
| 394 | + NodeName: &emptyNodeName, |
| 395 | + TargetRef: &v1.ObjectReference{ |
| 396 | + Kind: "Pod", |
| 397 | + Namespace: "default", |
| 398 | + Name: "testpod", |
| 399 | + UID: "999", |
| 400 | + }, |
| 401 | + }}, |
| 402 | + Ports: []v1.EndpointPort{{Port: 1000, Name: "port_8081"}, {Port: 1001, Name: "port_8082"}}, |
| 403 | + }}, |
| 404 | + } |
329 | 405 |
|
330 | 406 | var endpoints []v1.Endpoints |
331 | 407 | endpoints = append(endpoints, endpoint) |
332 | 408 | endpoints = append(endpoints, endpoint2) |
333 | 409 | endpoints = append(endpoints, endpoint3) |
| 410 | + endpoints = append(endpoints, endpoint4) |
334 | 411 |
|
335 | 412 | return &v1.EndpointsList{ |
336 | 413 | Items: endpoints, |
@@ -432,6 +509,8 @@ func SampleLoadBalancerResponse() ociloadbalancer.GetLoadBalancerResponse { |
432 | 509 | etag := "testTag" |
433 | 510 | lbId := "id" |
434 | 511 | backendSetName := GenerateBackendSetName("default", "testecho1", 80) |
| 512 | + backendSetName2 := GenerateBackendSetName("default", "service-with-named-target-port", 8081) |
| 513 | + backendSetName3 := GenerateBackendSetName("default", "service-with-named-target-port", 8082) |
435 | 514 | name := "testecho1-999" |
436 | 515 | port := 80 |
437 | 516 | ip := "127.89.90.90" |
@@ -519,6 +598,24 @@ func SampleLoadBalancerResponse() ociloadbalancer.GetLoadBalancerResponse { |
519 | 598 | SessionPersistenceConfiguration: nil, |
520 | 599 | LbCookieSessionPersistenceConfiguration: nil, |
521 | 600 | }, |
| 601 | + backendSetName2: { |
| 602 | + Name: &backendSetName2, |
| 603 | + Policy: &policy, |
| 604 | + Backends: backends, |
| 605 | + HealthChecker: healthChecker, |
| 606 | + SslConfiguration: nil, |
| 607 | + SessionPersistenceConfiguration: nil, |
| 608 | + LbCookieSessionPersistenceConfiguration: nil, |
| 609 | + }, |
| 610 | + backendSetName3: { |
| 611 | + Name: &backendSetName3, |
| 612 | + Policy: &policy, |
| 613 | + Backends: backends, |
| 614 | + HealthChecker: healthChecker, |
| 615 | + SslConfiguration: nil, |
| 616 | + SessionPersistenceConfiguration: nil, |
| 617 | + LbCookieSessionPersistenceConfiguration: nil, |
| 618 | + }, |
522 | 619 | }, |
523 | 620 | PathRouteSets: nil, |
524 | 621 | FreeformTags: nil, |
@@ -673,3 +770,66 @@ func GetNodesList() *v1.NodeList { |
673 | 770 | Items: nodes, |
674 | 771 | } |
675 | 772 | } |
| 773 | + |
| 774 | +func GetServicePortResource(name string, port int32, targetPort intstr.IntOrString, nodePort int32) v1.ServicePort { |
| 775 | + return v1.ServicePort{ |
| 776 | + Name: name, |
| 777 | + Port: port, |
| 778 | + TargetPort: targetPort, |
| 779 | + NodePort: nodePort, |
| 780 | + } |
| 781 | +} |
| 782 | + |
| 783 | +func GetServiceResource(namespace string, name string, ports []v1.ServicePort) *v1.Service { |
| 784 | + return &v1.Service{ |
| 785 | + ObjectMeta: metav1.ObjectMeta{ |
| 786 | + Name: name, |
| 787 | + Namespace: namespace, |
| 788 | + }, |
| 789 | + Spec: v1.ServiceSpec{ |
| 790 | + Selector: map[string]string{"app": name}, |
| 791 | + Ports: ports, |
| 792 | + ExternalTrafficPolicy: v1.ServiceExternalTrafficPolicyTypeLocal, |
| 793 | + }, |
| 794 | + } |
| 795 | +} |
| 796 | + |
| 797 | +func GetEndpointsResource(namespace string, name string, ports []v1.EndpointPort) v1.Endpoints { |
| 798 | + var emptyNodeName string |
| 799 | + return v1.Endpoints{ |
| 800 | + ObjectMeta: metav1.ObjectMeta{ |
| 801 | + Name: name, |
| 802 | + Namespace: namespace, |
| 803 | + ResourceVersion: "1", |
| 804 | + }, |
| 805 | + Subsets: []v1.EndpointSubset{{ |
| 806 | + Addresses: []v1.EndpointAddress{{IP: "6.7.8.9", NodeName: &emptyNodeName}}, |
| 807 | + Ports: ports, |
| 808 | + }}, |
| 809 | + } |
| 810 | +} |
| 811 | + |
| 812 | +func GetIngressServiceBackendResource(name string, portName string, portNumber int32) networkingv1.IngressServiceBackend { |
| 813 | + return networkingv1.IngressServiceBackend{ |
| 814 | + Name: name, |
| 815 | + Port: networkingv1.ServiceBackendPort{ |
| 816 | + Name: portName, |
| 817 | + Number: portNumber, |
| 818 | + }, |
| 819 | + } |
| 820 | +} |
| 821 | + |
| 822 | +func GetEndpointsListerResource(endpointsList *v1.EndpointsList) corelisters.EndpointsLister { |
| 823 | + ctx, cancel := context.WithCancel(context.Background()) |
| 824 | + defer cancel() |
| 825 | + |
| 826 | + client := fakeclientset.NewSimpleClientset() |
| 827 | + UpdateFakeClientCall(client, "list", "endpoints", endpointsList) |
| 828 | + informerFactory := informers.NewSharedInformerFactory(client, 0) |
| 829 | + endpointInformer := informerFactory.Core().V1().Endpoints() |
| 830 | + endpointsLister := endpointInformer.Lister() |
| 831 | + informerFactory.Start(ctx.Done()) |
| 832 | + cache.WaitForCacheSync(ctx.Done(), endpointInformer.Informer().HasSynced) |
| 833 | + |
| 834 | + return endpointsLister |
| 835 | +} |
0 commit comments