|
| 1 | +//go:build e2e |
| 2 | +// +build e2e |
| 3 | + |
| 4 | +/* |
| 5 | +Copyright 2021 The Kubernetes Authors. |
| 6 | +
|
| 7 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 8 | +you may not use this file except in compliance with the License. |
| 9 | +You may obtain a copy of the License at |
| 10 | +
|
| 11 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +
|
| 13 | +Unless required by applicable law or agreed to in writing, software |
| 14 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 15 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 | +See the License for the specific language governing permissions and |
| 17 | +limitations under the License. |
| 18 | +*/ |
| 19 | + |
| 20 | +package shared |
| 21 | + |
| 22 | +import ( |
| 23 | + "context" |
| 24 | + "strings" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/aws/aws-sdk-go/aws" |
| 28 | + "github.com/aws/aws-sdk-go/aws/awserr" |
| 29 | + "github.com/aws/aws-sdk-go/service/elb" |
| 30 | + . "github.com/onsi/gomega" |
| 31 | + v1 "k8s.io/api/apps/v1" |
| 32 | + corev1 "k8s.io/api/core/v1" |
| 33 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 34 | + apimachinerytypes "k8s.io/apimachinery/pkg/types" |
| 35 | + "k8s.io/utils/pointer" |
| 36 | + crclient "sigs.k8s.io/controller-runtime/pkg/client" |
| 37 | +) |
| 38 | + |
| 39 | +func CreateLBService(e2eCtx *E2EContext, svcNamespace string, svcName string, k8sclient crclient.Client) string { |
| 40 | + Byf("Creating service of type Load Balancer with name: %s under namespace: %s", svcName, svcNamespace) |
| 41 | + svcSpec := corev1.ServiceSpec{ |
| 42 | + Type: corev1.ServiceTypeLoadBalancer, |
| 43 | + Ports: []corev1.ServicePort{ |
| 44 | + { |
| 45 | + Port: 80, |
| 46 | + Protocol: corev1.ProtocolTCP, |
| 47 | + }, |
| 48 | + }, |
| 49 | + Selector: map[string]string{ |
| 50 | + "app": "nginx", |
| 51 | + }, |
| 52 | + } |
| 53 | + CreateService(svcName, svcNamespace, nil, svcSpec, k8sclient) |
| 54 | + elbName := "" |
| 55 | + Eventually(func() bool { |
| 56 | + svcCreated := &corev1.Service{} |
| 57 | + err := k8sclient.Get(context.TODO(), apimachinerytypes.NamespacedName{Namespace: svcNamespace, Name: svcName}, svcCreated) |
| 58 | + Expect(err).NotTo(HaveOccurred()) |
| 59 | + if lbs := len(svcCreated.Status.LoadBalancer.Ingress); lbs > 0 { |
| 60 | + ingressHostname := svcCreated.Status.LoadBalancer.Ingress[0].Hostname |
| 61 | + elbName = strings.Split(ingressHostname, "-")[0] |
| 62 | + return true |
| 63 | + } |
| 64 | + return false |
| 65 | + }, e2eCtx.E2EConfig.GetIntervals("", "wait-create-service")...).Should(BeTrue()) |
| 66 | + Byf("Created Load Balancer service and ELB name is: %s", elbName) |
| 67 | + return elbName |
| 68 | +} |
| 69 | + |
| 70 | +func DeleteLBService(svcNamespace string, svcName string, k8sclient crclient.Client) { |
| 71 | + svcSpec := corev1.ServiceSpec{ |
| 72 | + Type: corev1.ServiceTypeLoadBalancer, |
| 73 | + Ports: []corev1.ServicePort{ |
| 74 | + { |
| 75 | + Port: 80, |
| 76 | + Protocol: corev1.ProtocolTCP, |
| 77 | + }, |
| 78 | + }, |
| 79 | + Selector: map[string]string{ |
| 80 | + "app": "nginx", |
| 81 | + }, |
| 82 | + } |
| 83 | + deleteService(svcName, svcNamespace, nil, svcSpec, k8sclient) |
| 84 | +} |
| 85 | + |
| 86 | +func CreateService(svcName string, svcNamespace string, labels map[string]string, serviceSpec corev1.ServiceSpec, k8sClient crclient.Client) { |
| 87 | + svcToCreate := corev1.Service{ |
| 88 | + ObjectMeta: metav1.ObjectMeta{ |
| 89 | + Namespace: svcNamespace, |
| 90 | + Name: svcName, |
| 91 | + }, |
| 92 | + Spec: serviceSpec, |
| 93 | + } |
| 94 | + if len(labels) > 0 { |
| 95 | + svcToCreate.ObjectMeta.Labels = labels |
| 96 | + } |
| 97 | + Expect(k8sClient.Create(context.TODO(), &svcToCreate)).NotTo(HaveOccurred()) |
| 98 | +} |
| 99 | + |
| 100 | +func CreateDefaultNginxDeployment(deploymentNamespace, deploymentName string, k8sClient crclient.Client) { |
| 101 | + Byf("Creating Deployment with name: %s under namespace: %s", deploymentName, deploymentNamespace) |
| 102 | + deployment := defaultNginxDeployment(deploymentName, deploymentNamespace) |
| 103 | + Expect(k8sClient.Create(context.TODO(), &deployment)).NotTo(HaveOccurred()) |
| 104 | + Eventually(func() bool { |
| 105 | + getDeployment := &v1.Deployment{} |
| 106 | + err := k8sClient.Get(context.TODO(), apimachinerytypes.NamespacedName{Namespace: deploymentNamespace, Name: deploymentName}, getDeployment) |
| 107 | + Expect(err).NotTo(HaveOccurred()) |
| 108 | + for _, c := range getDeployment.Status.Conditions { |
| 109 | + if c.Type == v1.DeploymentAvailable && c.Status == corev1.ConditionTrue { |
| 110 | + return getDeployment.Status.AvailableReplicas > 0 |
| 111 | + } |
| 112 | + } |
| 113 | + return false |
| 114 | + }, 60*time.Second).Should(BeTrue()) |
| 115 | +} |
| 116 | + |
| 117 | +func DeleteDefaultNginxDeployment(deploymentNamespace, deploymentName string, k8sClient crclient.Client) { |
| 118 | + Byf("Deleting Deployment with name: %s under namespace: %s", deploymentName, deploymentNamespace) |
| 119 | + deployment := defaultNginxDeployment(deploymentName, deploymentNamespace) |
| 120 | + Expect(k8sClient.Delete(context.TODO(), &deployment)).NotTo(HaveOccurred()) |
| 121 | +} |
| 122 | + |
| 123 | +func deleteService(svcName, svcNamespace string, labels map[string]string, serviceSpec corev1.ServiceSpec, k8sClient crclient.Client) { |
| 124 | + svcToDelete := corev1.Service{ |
| 125 | + ObjectMeta: metav1.ObjectMeta{ |
| 126 | + Namespace: svcNamespace, |
| 127 | + Name: svcName, |
| 128 | + }, |
| 129 | + Spec: serviceSpec, |
| 130 | + } |
| 131 | + if len(labels) > 0 { |
| 132 | + svcToDelete.ObjectMeta.Labels = labels |
| 133 | + } |
| 134 | + Expect(k8sClient.Delete(context.TODO(), &svcToDelete)).NotTo(HaveOccurred()) |
| 135 | +} |
| 136 | + |
| 137 | +func VerifyElbExists(e2eCtx *E2EContext, elbName string, exists bool) { |
| 138 | + Byf("Verifying ELB with name %s present and instances are attached", elbName) |
| 139 | + input := &elb.DescribeLoadBalancersInput{ |
| 140 | + LoadBalancerNames: []*string{ |
| 141 | + aws.String(elbName), |
| 142 | + }, |
| 143 | + } |
| 144 | + elbClient := elb.New(e2eCtx.AWSSession) |
| 145 | + elbsOutput, err := elbClient.DescribeLoadBalancers(input) |
| 146 | + if exists { |
| 147 | + Expect(err).NotTo(HaveOccurred()) |
| 148 | + Expect(len(elbsOutput.LoadBalancerDescriptions)).To(Equal(1)) |
| 149 | + Expect(len(elbsOutput.LoadBalancerDescriptions[0].Instances)).Should(BeNumerically(">=", 1)) |
| 150 | + Byf("ELB with name %s exists", elbName) |
| 151 | + } else { |
| 152 | + aerr, ok := err.(awserr.Error) |
| 153 | + Expect(ok).To(BeTrue()) |
| 154 | + Expect(aerr.Code()).To(Equal(elb.ErrCodeAccessPointNotFoundException)) |
| 155 | + Byf("ELB with name %s doesn't exists", elbName) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +func defaultNginxDeployment(deploymentName, deploymentNamespace string) v1.Deployment { |
| 160 | + selector, err := metav1.ParseToLabelSelector("app=nginx") |
| 161 | + Expect(err).To(BeNil()) |
| 162 | + deploymentSpec := v1.DeploymentSpec{ |
| 163 | + Selector: selector, |
| 164 | + Replicas: pointer.Int32(1), |
| 165 | + Template: corev1.PodTemplateSpec{ |
| 166 | + ObjectMeta: metav1.ObjectMeta{ |
| 167 | + Name: "nginx", |
| 168 | + Labels: map[string]string{ |
| 169 | + "app": "nginx", |
| 170 | + }, |
| 171 | + }, |
| 172 | + Spec: corev1.PodSpec{ |
| 173 | + Containers: []corev1.Container{ |
| 174 | + { |
| 175 | + Name: "nginx", |
| 176 | + Image: "k8s.gcr.io/nginx-slim:0.8", |
| 177 | + Ports: []corev1.ContainerPort{{ |
| 178 | + Name: "nginx-port", ContainerPort: int32(80), |
| 179 | + }}, |
| 180 | + }, |
| 181 | + }, |
| 182 | + }, |
| 183 | + }, |
| 184 | + } |
| 185 | + return v1.Deployment{ |
| 186 | + ObjectMeta: metav1.ObjectMeta{ |
| 187 | + Namespace: deploymentNamespace, |
| 188 | + Name: deploymentName, |
| 189 | + Labels: map[string]string{ |
| 190 | + "app": "nginx", |
| 191 | + }, |
| 192 | + }, |
| 193 | + Spec: deploymentSpec, |
| 194 | + } |
| 195 | +} |
0 commit comments