|
| 1 | +package gateway |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + . "github.com/onsi/ginkgo/v2" |
| 7 | + . "github.com/onsi/gomega" |
| 8 | + elbv2gw "sigs.k8s.io/aws-load-balancer-controller/apis/gateway/v1beta1" |
| 9 | + "sigs.k8s.io/aws-load-balancer-controller/test/framework/http" |
| 10 | + "sigs.k8s.io/aws-load-balancer-controller/test/framework/verifier" |
| 11 | + gwv1 "sigs.k8s.io/gateway-api/apis/v1" |
| 12 | + gwbeta1 "sigs.k8s.io/gateway-api/apis/v1beta1" |
| 13 | + "strings" |
| 14 | + "time" |
| 15 | +) |
| 16 | + |
| 17 | +var _ = Describe("test combined ALB and NLB gateways with HTTPRoute and TCPRoute", func() { |
| 18 | + var ( |
| 19 | + ctx context.Context |
| 20 | + albStack ALBTestStack |
| 21 | + nlbStack NLBTestStack |
| 22 | + ) |
| 23 | + |
| 24 | + BeforeEach(func() { |
| 25 | + if !tf.Options.EnableGatewayTests { |
| 26 | + Skip("Skipping gateway tests") |
| 27 | + } |
| 28 | + ctx = context.Background() |
| 29 | + albStack = ALBTestStack{} |
| 30 | + nlbStack = NLBTestStack{} |
| 31 | + }) |
| 32 | + |
| 33 | + AfterEach(func() { |
| 34 | + albStack.Cleanup(ctx, tf) |
| 35 | + nlbStack.Cleanup(ctx, tf) |
| 36 | + }) |
| 37 | + |
| 38 | + Context("with ALB and NLB gateways using IP targets", func() { |
| 39 | + var albDnsName string |
| 40 | + var albARN string |
| 41 | + var nlbDnsName string |
| 42 | + var nlbARN string |
| 43 | + var refGrant *gwbeta1.ReferenceGrant |
| 44 | + It("should provision both ALB and NLB load balancers with HTTPRoute and TCPRoute", func() { |
| 45 | + // ALB Configuration |
| 46 | + albInterf := elbv2gw.LoadBalancerSchemeInternal |
| 47 | + albLbcSpec := elbv2gw.LoadBalancerConfigurationSpec{ |
| 48 | + Scheme: &albInterf, |
| 49 | + } |
| 50 | + |
| 51 | + // NLB Configuration |
| 52 | + nlbInterf := elbv2gw.LoadBalancerSchemeInternetFacing |
| 53 | + nlbLbcSpec := elbv2gw.LoadBalancerConfigurationSpec{ |
| 54 | + Scheme: &nlbInterf, |
| 55 | + } |
| 56 | + |
| 57 | + // Configure TLS for both if certificates are available |
| 58 | + var hasTLS bool |
| 59 | + if len(tf.Options.CertificateARNs) > 0 { |
| 60 | + cert := strings.Split(tf.Options.CertificateARNs, ",")[0] |
| 61 | + |
| 62 | + // ALB HTTPS listener |
| 63 | + albLsConfig := elbv2gw.ListenerConfiguration{ |
| 64 | + ProtocolPort: "HTTPS:443", |
| 65 | + DefaultCertificate: &cert, |
| 66 | + } |
| 67 | + albLbcSpec.ListenerConfigurations = &[]elbv2gw.ListenerConfiguration{albLsConfig} |
| 68 | + hasTLS = true |
| 69 | + } |
| 70 | + |
| 71 | + // IP target type for both |
| 72 | + ipTargetType := elbv2gw.TargetTypeIP |
| 73 | + tgSpec := elbv2gw.TargetGroupConfigurationSpec{ |
| 74 | + DefaultConfiguration: elbv2gw.TargetGroupProps{ |
| 75 | + TargetType: &ipTargetType, |
| 76 | + }, |
| 77 | + } |
| 78 | + lrcSpec := elbv2gw.ListenerRuleConfigurationSpec{} |
| 79 | + |
| 80 | + // ALB Gateway listeners |
| 81 | + albGwListeners := []gwv1.Listener{ |
| 82 | + { |
| 83 | + Name: "http80", |
| 84 | + Port: 80, |
| 85 | + Protocol: gwv1.HTTPProtocolType, |
| 86 | + }, |
| 87 | + } |
| 88 | + if hasTLS { |
| 89 | + albGwListeners = append(albGwListeners, gwv1.Listener{ |
| 90 | + Name: "https443", |
| 91 | + Port: 443, |
| 92 | + Protocol: gwv1.HTTPSProtocolType, |
| 93 | + }) |
| 94 | + } |
| 95 | + |
| 96 | + // HTTPRoute for ALB |
| 97 | + httpr := buildHTTPRoute([]string{}, []gwv1.HTTPRouteRule{}, nil) |
| 98 | + |
| 99 | + By("deploying ALB stack", func() { |
| 100 | + err := albStack.DeployHTTP(ctx, nil, tf, albGwListeners, []*gwv1.HTTPRoute{httpr}, albLbcSpec, tgSpec, lrcSpec, nil, true) |
| 101 | + Expect(err).NotTo(HaveOccurred()) |
| 102 | + }) |
| 103 | + |
| 104 | + By("deploying NLB stack", func() { |
| 105 | + err := nlbStack.DeployFrontendNLB(ctx, albStack, tf, nlbLbcSpec, hasTLS, true) |
| 106 | + Expect(err).NotTo(HaveOccurred()) |
| 107 | + }) |
| 108 | + By("checking alb gateway status for lb dns name", func() { |
| 109 | + time.Sleep(2 * time.Minute) |
| 110 | + albDnsName = albStack.GetLoadBalancerIngressHostName() |
| 111 | + Expect(albDnsName).ToNot(BeEmpty()) |
| 112 | + }) |
| 113 | + By("querying AWS loadbalancer from the dns name", func() { |
| 114 | + var err error |
| 115 | + albARN, err = tf.LBManager.FindLoadBalancerByDNSName(ctx, albDnsName) |
| 116 | + Expect(err).NotTo(HaveOccurred()) |
| 117 | + Expect(albARN).ToNot(BeEmpty()) |
| 118 | + }) |
| 119 | + By("checking nlb gateway status for lb dns name", func() { |
| 120 | + nlbDnsName = nlbStack.GetLoadBalancerIngressHostName() |
| 121 | + Expect(nlbDnsName).ToNot(BeEmpty()) |
| 122 | + }) |
| 123 | + By("querying AWS loadbalancer from the dns name", func() { |
| 124 | + var err error |
| 125 | + nlbARN, err = tf.LBManager.FindLoadBalancerByDNSName(ctx, nlbDnsName) |
| 126 | + Expect(err).NotTo(HaveOccurred()) |
| 127 | + Expect(nlbARN).ToNot(BeEmpty()) |
| 128 | + }) |
| 129 | + By("verify alb configuration", func() { |
| 130 | + expectedTargetGroups := []verifier.ExpectedTargetGroup{ |
| 131 | + { |
| 132 | + Protocol: "HTTP", |
| 133 | + Port: 80, |
| 134 | + NumTargets: int(*albStack.albResourceStack.commonStack.dps[0].Spec.Replicas), |
| 135 | + TargetType: "ip", |
| 136 | + TargetGroupHC: DEFAULT_ALB_TARGET_GROUP_HC, |
| 137 | + }, |
| 138 | + } |
| 139 | + |
| 140 | + listenerPortMap := albStack.albResourceStack.getListenersPortMap() |
| 141 | + |
| 142 | + err := verifier.VerifyAWSLoadBalancerResources(ctx, tf, albARN, verifier.LoadBalancerExpectation{ |
| 143 | + Type: "application", |
| 144 | + Scheme: "internal", |
| 145 | + Listeners: listenerPortMap, |
| 146 | + TargetGroups: expectedTargetGroups, |
| 147 | + }) |
| 148 | + Expect(err).NotTo(HaveOccurred()) |
| 149 | + }) |
| 150 | + By("verify nlb configuration", func() { |
| 151 | + // No ref grants, means no tg or listener. |
| 152 | + expectedTargetGroups := []verifier.ExpectedTargetGroup{} |
| 153 | + |
| 154 | + listenerPortMap := map[string]string{} |
| 155 | + |
| 156 | + err := verifier.VerifyAWSLoadBalancerResources(ctx, tf, nlbARN, verifier.LoadBalancerExpectation{ |
| 157 | + Type: "network", |
| 158 | + Scheme: "internet-facing", |
| 159 | + Listeners: listenerPortMap, |
| 160 | + TargetGroups: expectedTargetGroups, |
| 161 | + }) |
| 162 | + Expect(err).NotTo(HaveOccurred()) |
| 163 | + }) |
| 164 | + By("deploy reference grant that allows nlb <-> alb attachment", func() { |
| 165 | + var err error |
| 166 | + refGrant, err = nlbStack.CreateFENLBReferenceGrant(ctx, tf, albStack.albResourceStack.commonStack.ns) |
| 167 | + Expect(err).NotTo(HaveOccurred()) |
| 168 | + time.Sleep(2 * time.Minute) |
| 169 | + }) |
| 170 | + By("deploy reference grant that allows nlb <-> alb attachment", func() { |
| 171 | + // No ref grants, means no tg or listener. |
| 172 | + expectedTargetGroups := []verifier.ExpectedTargetGroup{ |
| 173 | + { |
| 174 | + Protocol: "TCP", |
| 175 | + Port: 80, |
| 176 | + NumTargets: 1, |
| 177 | + TargetType: "alb", |
| 178 | + TargetGroupHC: &verifier.TargetGroupHC{ |
| 179 | + Protocol: "HTTP", |
| 180 | + Port: "traffic-port", |
| 181 | + Path: "/", |
| 182 | + Interval: 15, |
| 183 | + Timeout: 5, |
| 184 | + HealthyThreshold: 3, |
| 185 | + UnhealthyThreshold: 3, |
| 186 | + }, |
| 187 | + }, |
| 188 | + } |
| 189 | + |
| 190 | + if hasTLS { |
| 191 | + expectedTargetGroups = append(expectedTargetGroups, verifier.ExpectedTargetGroup{ |
| 192 | + Protocol: "TCP", |
| 193 | + Port: 443, |
| 194 | + NumTargets: 1, |
| 195 | + TargetType: "alb", |
| 196 | + TargetGroupHC: &verifier.TargetGroupHC{ |
| 197 | + Protocol: "HTTPS", |
| 198 | + Port: "traffic-port", |
| 199 | + Path: "/", |
| 200 | + Interval: 15, |
| 201 | + Timeout: 5, |
| 202 | + HealthyThreshold: 3, |
| 203 | + UnhealthyThreshold: 3, |
| 204 | + }, |
| 205 | + }) |
| 206 | + } |
| 207 | + |
| 208 | + fmt.Printf("%+v\n", refGrant) |
| 209 | + |
| 210 | + listenerPortMap := nlbStack.nlbResourceStack.getListenersPortMap() |
| 211 | + |
| 212 | + err := verifier.VerifyAWSLoadBalancerResources(ctx, tf, nlbARN, verifier.LoadBalancerExpectation{ |
| 213 | + Type: "network", |
| 214 | + Scheme: "internet-facing", |
| 215 | + Listeners: listenerPortMap, |
| 216 | + TargetGroups: expectedTargetGroups, |
| 217 | + }) |
| 218 | + Expect(err).NotTo(HaveOccurred()) |
| 219 | + }) |
| 220 | + By("verify port 80 works", func() { |
| 221 | + url := fmt.Sprintf("http://%v/any-path", nlbDnsName) |
| 222 | + err := tf.HTTPVerifier.VerifyURL(url, http.ResponseCodeMatches(200)) |
| 223 | + Expect(err).NotTo(HaveOccurred()) |
| 224 | + }) |
| 225 | + if hasTLS { |
| 226 | + By("verify port 443 works", func() { |
| 227 | + url := fmt.Sprintf("https://%v/any-path", nlbDnsName) |
| 228 | + urlOptions := http.URLOptions{ |
| 229 | + InsecureSkipVerify: true, |
| 230 | + } |
| 231 | + err := tf.HTTPVerifier.VerifyURLWithOptions(url, urlOptions, http.ResponseCodeMatches(200)) |
| 232 | + Expect(err).NotTo(HaveOccurred()) |
| 233 | + }) |
| 234 | + } |
| 235 | + By("remove reference grant should remove nlb listener but keep alb listener intact", func() { |
| 236 | + err := tf.K8sClient.Delete(ctx, refGrant) |
| 237 | + Expect(err).NotTo(HaveOccurred()) |
| 238 | + time.Sleep(2 * time.Minute) |
| 239 | + }) |
| 240 | + By("verify alb configuration", func() { |
| 241 | + expectedTargetGroups := []verifier.ExpectedTargetGroup{ |
| 242 | + { |
| 243 | + Protocol: "HTTP", |
| 244 | + Port: 80, |
| 245 | + NumTargets: int(*albStack.albResourceStack.commonStack.dps[0].Spec.Replicas), |
| 246 | + TargetType: "ip", |
| 247 | + TargetGroupHC: DEFAULT_ALB_TARGET_GROUP_HC, |
| 248 | + }, |
| 249 | + } |
| 250 | + |
| 251 | + listenerPortMap := albStack.albResourceStack.getListenersPortMap() |
| 252 | + |
| 253 | + err := verifier.VerifyAWSLoadBalancerResources(ctx, tf, albARN, verifier.LoadBalancerExpectation{ |
| 254 | + Type: "application", |
| 255 | + Scheme: "internal", |
| 256 | + Listeners: listenerPortMap, |
| 257 | + TargetGroups: expectedTargetGroups, |
| 258 | + }) |
| 259 | + Expect(err).NotTo(HaveOccurred()) |
| 260 | + }) |
| 261 | + By("verify nlb configuration", func() { |
| 262 | + // No ref grants, means no tg or listener. |
| 263 | + expectedTargetGroups := []verifier.ExpectedTargetGroup{} |
| 264 | + |
| 265 | + listenerPortMap := map[string]string{} |
| 266 | + |
| 267 | + err := verifier.VerifyAWSLoadBalancerResources(ctx, tf, nlbARN, verifier.LoadBalancerExpectation{ |
| 268 | + Type: "network", |
| 269 | + Scheme: "internet-facing", |
| 270 | + Listeners: listenerPortMap, |
| 271 | + TargetGroups: expectedTargetGroups, |
| 272 | + }) |
| 273 | + Expect(err).NotTo(HaveOccurred()) |
| 274 | + }) |
| 275 | + }) |
| 276 | + }) |
| 277 | +}) |
0 commit comments