55 "fmt"
66 "time"
77
8+ "github.com/Azure/application-gateway-kubernetes-ingress/pkg/k8scontext"
9+ "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-06-01/network"
10+ "github.com/Azure/go-autorest/autorest/to"
811 . "github.com/onsi/ginkgo"
912 . "github.com/onsi/gomega"
1013 "k8s.io/api/core/v1"
@@ -13,10 +16,6 @@ import (
1316 "k8s.io/apimachinery/pkg/util/intstr"
1417 "k8s.io/client-go/kubernetes"
1518 testclient "k8s.io/client-go/kubernetes/fake"
16-
17- "github.com/Azure/application-gateway-kubernetes-ingress/pkg/k8scontext"
18- "github.com/Azure/azure-sdk-for-go/services/network/mgmt/2018-06-01/network"
19- "github.com/Azure/go-autorest/autorest/to"
2019)
2120
2221var _ = Describe ("Tests `appgw.ConfigBuilder`" , func () {
@@ -290,4 +289,111 @@ var _ = Describe("Tests `appgw.ConfigBuilder`", func() {
290289 Expect ((* pathRule .Paths )[0 ]).To (Equal ("/hi" ))
291290 })
292291 })
292+
293+ Context ("Tests Ingress Controller when Service doesn't exists" , func () {
294+ It ("Should be able to create Application Gateway Configuration from Ingress with empty backend pool." , func () {
295+ // Delete the service
296+ options := & metav1.DeleteOptions {}
297+ err := k8sClient .CoreV1 ().Services (ingressNS ).Delete (serviceName , options )
298+ Expect (err ).Should (BeNil (), "Unable to delete service resource due to: %v" , err )
299+
300+ // Delete the Endpoint
301+ err = k8sClient .CoreV1 ().Endpoints (ingressNS ).Delete (serviceName , options )
302+ Expect (err ).Should (BeNil (), "Unable to delete endpoint resource due to: %v" , err )
303+
304+ // Start the informers. This will sync the cache with the latest ingress.
305+ ctxt .Run ()
306+
307+ // Get all the ingresses
308+ ingressList := ctxt .GetHTTPIngressList ()
309+ // There should be only one ingress
310+ Expect (len (ingressList )).To (Equal (1 ), "Expected only one ingress resource but got: %d" , len (ingressList ))
311+ // Make sure it is the ingress we stored.
312+ Expect (ingressList [0 ]).To (Equal (ingress ))
313+
314+ // Add HTTP settings.
315+ configBuilder , err := configBuilder .BackendHTTPSettingsCollection (ingressList )
316+ Expect (err ).Should (BeNil (), "Error in generating the HTTP Settings: %v" , err )
317+
318+ // Retrieve the implementation of the `ConfigBuilder` interface.
319+ appGW := configBuilder .Build ()
320+ // We will have a default HTTP setting that gets added, and an HTTP setting corresponding to port `backendPort`
321+ Expect (len (* appGW .BackendHTTPSettingsCollection )).To (Equal (2 ), "Expected two HTTP setting, but got: %d" , len (* appGW .BackendHTTPSettingsCollection ))
322+
323+ expectedBackend := & ingress .Spec .Rules [0 ].IngressRuleValue .HTTP .Paths [0 ].Backend
324+ httpSettingsName := generateHTTPSettingsName (generateBackendID (ingress , expectedBackend ).serviceFullName (), fmt .Sprintf ("%d" , servicePort ), servicePort )
325+ httpSettings := & network.ApplicationGatewayBackendHTTPSettings {
326+ Etag : to .StringPtr ("*" ),
327+ Name : & httpSettingsName ,
328+ ApplicationGatewayBackendHTTPSettingsPropertiesFormat : & network.ApplicationGatewayBackendHTTPSettingsPropertiesFormat {
329+ Protocol : network .HTTP ,
330+ Port : & servicePort ,
331+ },
332+ }
333+
334+ // Test the default backend HTTP settings.
335+ Expect ((* appGW .BackendHTTPSettingsCollection )[0 ]).To (Equal (defaultBackendHTTPSettings ()))
336+ // Test the ingress backend HTTP setting that we installed.
337+ Expect ((* appGW .BackendHTTPSettingsCollection )[1 ]).To (Equal (* httpSettings ))
338+
339+ // Add backend address pools. We need the HTTP settings before we can add the backend address pools.
340+ configBuilder , err = configBuilder .BackendAddressPools (ingressList )
341+ Expect (err ).Should (BeNil (), "Error in generating the backend address pools: %v" , err )
342+
343+ // Retrieve the implementation of the `ConfigBuilder` interface.
344+ appGW = configBuilder .Build ()
345+ // We will have a default backend address pool that gets added, and a backend pool corresponding to our service.
346+ Expect (len (* appGW .BackendAddressPools )).To (Equal (1 ), "Expected two backend address pools, but got: %d" , len (* appGW .BackendAddressPools ))
347+
348+ // Test the default backend address pool.
349+ Expect ((* appGW .BackendAddressPools )[0 ]).To (Equal (defaultBackendAddressPool ()))
350+
351+ // Add the listeners. We need the backend address pools before we can add HTTP listeners.
352+ configBuilder , err = configBuilder .HTTPListeners (ingressList )
353+ Expect (err ).Should (BeNil (), "Error in generating the HTTP listeners: %v" , err )
354+
355+ // Retrieve the implementation of the `ConfigBuilder` interface.
356+ appGW = configBuilder .Build ()
357+ // Ingress allows listners on port 80 or port 443. Therefore in this particular case we would have only a single listener
358+ Expect (len (* appGW .HTTPListeners )).To (Equal (1 ), "Expected a single HTTP listener, but got: %d" , len (* appGW .HTTPListeners ))
359+
360+ // Test the listener.
361+ appGwIdentifier := Identifier {}
362+ frontendPortID := appGwIdentifier .frontendPortID (generateFrontendPortName (80 ))
363+ httpListenerName := generateHTTPListenerName (frontendListenerIdentifier {80 , domainName })
364+ httpListener := & network.ApplicationGatewayHTTPListener {
365+ Etag : to .StringPtr ("*" ),
366+ Name : & httpListenerName ,
367+ ApplicationGatewayHTTPListenerPropertiesFormat : & network.ApplicationGatewayHTTPListenerPropertiesFormat {
368+ FrontendIPConfiguration : resourceRef ("*" ),
369+ FrontendPort : resourceRef (frontendPortID ),
370+ Protocol : network .HTTP ,
371+ HostName : & domainName ,
372+ },
373+ }
374+
375+ Expect ((* appGW .HTTPListeners )[0 ]).To (Equal (* httpListener ))
376+
377+ // RequestRoutingRules depends on the previous operations
378+ configBuilder , err = configBuilder .RequestRoutingRules (ingressList )
379+ Expect (err ).Should (BeNil (), "Error in generating the routing rules: %v" , err )
380+
381+ // Retrieve the implementation of the `ConfigBuilder` interface.
382+ appGW = configBuilder .Build ()
383+ Expect (len (* appGW .RequestRoutingRules )).To (Equal (1 ), "Expected one routing rule, but got: %d" , len (* appGW .RequestRoutingRules ))
384+ Expect (* ((* appGW .RequestRoutingRules )[0 ].Name )).To (Equal (generateRequestRoutingRuleName (frontendListenerIdentifier {80 , domainName })))
385+ Expect ((* appGW .RequestRoutingRules )[0 ].RuleType ).To (Equal (network .PathBasedRouting ))
386+
387+ // Check the `urlPathMaps`
388+ Expect (len (* appGW .URLPathMaps )).To (Equal (1 ), "Expected one URL path map routing, but got: %d" , len (* appGW .URLPathMaps ))
389+ Expect (* ((* appGW .URLPathMaps )[0 ].Name )).To (Equal (generateURLPathMapName (frontendListenerIdentifier {80 , domainName })))
390+ // Check the `pathRule` stored within the `urlPathMap`.
391+ Expect (len (* ((* appGW .URLPathMaps )[0 ].PathRules ))).To (Equal (1 ), "Expected one path based rule, but got: %d" , len (* ((* appGW .URLPathMaps )[0 ].PathRules )))
392+
393+ pathRule := (* ((* appGW .URLPathMaps )[0 ].PathRules ))[0 ]
394+ Expect (len (* (pathRule .Paths ))).To (Equal (1 ), "Expected a single path in path-based rules, but got: %d" , len (* (pathRule .Paths )))
395+ // Check the exact path that was set.
396+ Expect ((* pathRule .Paths )[0 ]).To (Equal ("/hi" ))
397+ })
398+ })
293399})
0 commit comments