|
| 1 | +package test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "testing" |
| 7 | + |
| 8 | + . "github.com/onsi/gomega" |
| 9 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 10 | + |
| 11 | + appsv1 "k8s.io/api/apps/v1" |
| 12 | + corev1 "k8s.io/api/core/v1" |
| 13 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 14 | + "k8s.io/apimachinery/pkg/util/intstr" |
| 15 | +) |
| 16 | + |
| 17 | +func TestLoadbalacnerBasic(t *testing.T) { |
| 18 | + |
| 19 | + g := NewGomegaWithT(t) |
| 20 | + |
| 21 | + mirrorDeploy, err := deployMirrorPods(e2eTest.tenantClient) |
| 22 | + g.Expect(err).ShouldNot(HaveOccurred()) |
| 23 | + |
| 24 | + lbls := map[string]string{"app": "mirror-pod"} |
| 25 | + // Create a service of type: LoadBalacner |
| 26 | + svc := &corev1.Service{ |
| 27 | + ObjectMeta: metav1.ObjectMeta{ |
| 28 | + Name: "echo-pods", |
| 29 | + Namespace: "default", |
| 30 | + }, |
| 31 | + Spec: corev1.ServiceSpec{ |
| 32 | + Ports: []corev1.ServicePort{ |
| 33 | + {Name: "http", Protocol: "TCP", Port: 80, TargetPort: intstr.FromInt(8080)}, |
| 34 | + {Name: "https", Protocol: "TCP", Port: 443, TargetPort: intstr.FromInt(8443)}, |
| 35 | + }, |
| 36 | + Selector: lbls, |
| 37 | + Type: "LoadBalancer", |
| 38 | + }, |
| 39 | + } |
| 40 | + |
| 41 | + fmt.Println("Creating Service") |
| 42 | + err = e2eTest.tenantClient.Create(context.TODO(), svc) |
| 43 | + g.Expect(err).ShouldNot(HaveOccurred()) |
| 44 | + |
| 45 | + g.Eventually(func() string { |
| 46 | + err = e2eTest.tenantClient.Get(context.TODO(), client.ObjectKeyFromObject(svc), svc) |
| 47 | + if len(svc.Status.LoadBalancer.Ingress) == 0 { |
| 48 | + return "" |
| 49 | + } |
| 50 | + return svc.Status.LoadBalancer.Ingress[0].IP |
| 51 | + }, "2m", "5s").ShouldNot(BeEmpty()) |
| 52 | + |
| 53 | + // Cleanup |
| 54 | + err = cleanUp(mirrorDeploy, svc) |
| 55 | + g.Expect(err).ShouldNot(HaveOccurred()) |
| 56 | + |
| 57 | + g.Eventually(func() error { |
| 58 | + return e2eTest.tenantClient.Get(context.TODO(), client.ObjectKeyFromObject(svc), svc) |
| 59 | + }, "2m", "5s").ShouldNot(BeNil()) |
| 60 | +} |
| 61 | + |
| 62 | +func TestLoadbalacnerProxy(t *testing.T) { |
| 63 | + g := NewGomegaWithT(t) |
| 64 | + |
| 65 | + _, err := deployMirrorPods(e2eTest.tenantClient) |
| 66 | + g.Expect(err).ShouldNot(HaveOccurred()) |
| 67 | + |
| 68 | + lbls := map[string]string{"app": "mirror-pod"} |
| 69 | + // Create a service of type: LoadBalacner |
| 70 | + svc := &corev1.Service{ |
| 71 | + ObjectMeta: metav1.ObjectMeta{ |
| 72 | + Name: "echo-pods", |
| 73 | + Namespace: "default", |
| 74 | + Annotations: map[string]string{ |
| 75 | + "kubernetes.civo.com/loadbalancer-enable-proxy-protocol": "send-proxy", |
| 76 | + }, |
| 77 | + }, |
| 78 | + Spec: corev1.ServiceSpec{ |
| 79 | + Ports: []corev1.ServicePort{ |
| 80 | + {Name: "http", Protocol: "TCP", Port: 80, TargetPort: intstr.FromInt(8081)}, |
| 81 | + {Name: "https", Protocol: "TCP", Port: 443, TargetPort: intstr.FromInt(8444)}, |
| 82 | + }, |
| 83 | + Selector: lbls, |
| 84 | + Type: "LoadBalancer", |
| 85 | + }, |
| 86 | + } |
| 87 | + |
| 88 | + fmt.Println("Creating Service") |
| 89 | + err = e2eTest.tenantClient.Create(context.TODO(), svc) |
| 90 | + g.Expect(err).ShouldNot(HaveOccurred()) |
| 91 | + |
| 92 | + g.Eventually(func() string { |
| 93 | + err = e2eTest.tenantClient.Get(context.TODO(), client.ObjectKeyFromObject(svc), svc) |
| 94 | + if len(svc.Status.LoadBalancer.Ingress) == 0 { |
| 95 | + return "" |
| 96 | + } |
| 97 | + return svc.Status.LoadBalancer.Ingress[0].IP |
| 98 | + }, "2m", "5s").ShouldNot(BeEmpty()) |
| 99 | + |
| 100 | + /* |
| 101 | + // Cleanup |
| 102 | + err = cleanUp(mirrorDeploy, svc) |
| 103 | + g.Expect(err).ShouldNot(HaveOccurred()) |
| 104 | +
|
| 105 | + g.Eventually(func() error { |
| 106 | + return e2eTest.tenantClient.Get(context.TODO(), client.ObjectKeyFromObject(svc), svc) |
| 107 | + }, "2m", "5s").ShouldNot(BeNil()) |
| 108 | + */ |
| 109 | +} |
| 110 | + |
| 111 | +func cleanUp(mirrorDeploy *appsv1.Deployment, svc *corev1.Service) error { |
| 112 | + err := e2eTest.tenantClient.Delete(context.TODO(), svc) |
| 113 | + if err != nil { |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + return e2eTest.tenantClient.Delete(context.TODO(), mirrorDeploy) |
| 118 | +} |
| 119 | + |
| 120 | +func deployMirrorPods(c client.Client) (*appsv1.Deployment, error) { |
| 121 | + lbls := map[string]string{"app": "mirror-pod"} |
| 122 | + replicas := int32(2) |
| 123 | + mirrorDeploy := &appsv1.Deployment{ |
| 124 | + ObjectMeta: metav1.ObjectMeta{ |
| 125 | + Name: "echo-pods", |
| 126 | + Namespace: "default", |
| 127 | + }, |
| 128 | + Spec: appsv1.DeploymentSpec{ |
| 129 | + Selector: &metav1.LabelSelector{ |
| 130 | + MatchLabels: lbls, |
| 131 | + }, |
| 132 | + Replicas: &replicas, |
| 133 | + Template: corev1.PodTemplateSpec{ |
| 134 | + ObjectMeta: metav1.ObjectMeta{ |
| 135 | + Labels: lbls, |
| 136 | + Annotations: map[string]string{ |
| 137 | + "danm.k8s.io/interfaces": "[{\"tenantNetwork\":\"tenant-vxlan\", \"ip\":\"dynamic\"}]", |
| 138 | + }, |
| 139 | + }, |
| 140 | + Spec: corev1.PodSpec{ |
| 141 | + Containers: []corev1.Container{ |
| 142 | + { |
| 143 | + Name: "mirror-pod", |
| 144 | + Image: "dmajrekar/nginx-echo:latest", |
| 145 | + ImagePullPolicy: corev1.PullIfNotPresent, |
| 146 | + Ports: []corev1.ContainerPort{ |
| 147 | + {Protocol: "TCP", ContainerPort: 8080}, |
| 148 | + {Protocol: "TCP", ContainerPort: 8081}, |
| 149 | + {Protocol: "TCP", ContainerPort: 8443}, |
| 150 | + {Protocol: "TCP", ContainerPort: 8444}, |
| 151 | + }, |
| 152 | + }, |
| 153 | + }, |
| 154 | + }, |
| 155 | + }, |
| 156 | + }, |
| 157 | + } |
| 158 | + |
| 159 | + fmt.Println("Creating mirror deployment") |
| 160 | + err := c.Create(context.TODO(), mirrorDeploy) |
| 161 | + return mirrorDeploy, err |
| 162 | + |
| 163 | +} |
0 commit comments