Skip to content

Commit 863e8c5

Browse files
probe: set hostname based on backend hostname if provided; respect readiness/livesness scheme fix (#794)
* probe: set hostname based on backend hostname if provided * respect readiness probe scheme
1 parent 3a120bb commit 863e8c5

File tree

2 files changed

+57
-16
lines changed

2 files changed

+57
-16
lines changed

pkg/appgw/appgw_test.go

Lines changed: 50 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -726,9 +726,9 @@ var _ = Describe("Tests `appgw.ConfigBuilder`", func() {
726726
})
727727

728728
Context("Tests Ingress Controller Annotations", func() {
729-
It("Should be able to create Application Gateway Configuration from Ingress with all annotations.", func() {
729+
BeforeEach(func() {
730730
ingress, err := k8sClient.ExtensionsV1beta1().Ingresses(ingressNS).Get(ingressName, metav1.GetOptions{})
731-
Ω(err).ToNot(HaveOccurred(), "Unable to create ingress resource due to: %v", err)
731+
Ω(err).ToNot(HaveOccurred(), "Unable to get ingress resource due to: %v", err)
732732

733733
// Set the ingress annotations for this ingress.
734734
ingress.Annotations[annotations.BackendPathPrefixKey] = "/test"
@@ -742,27 +742,33 @@ var _ = Describe("Tests `appgw.ConfigBuilder`", func() {
742742
_, err = k8sClient.ExtensionsV1beta1().Ingresses(ingressNS).Update(ingress)
743743
Ω(err).ToNot(HaveOccurred(), "Unable to update ingress resource due to: %v", err)
744744

745+
pod, err := k8sClient.CoreV1().Pods(ingressNS).Get(serviceName, metav1.GetOptions{})
746+
Ω(err).ToNot(HaveOccurred(), "Unable to get pod resource due to: %v", err)
747+
748+
// remove the probe to see the effect of annotations on probe
749+
pod.Spec.Containers[0].ReadinessProbe = nil
750+
pod.Spec.Containers[0].LivenessProbe = nil
751+
752+
// Update the pod.
753+
_, err = k8sClient.CoreV1().Pods(ingressNS).Update(pod)
754+
Ω(err).ToNot(HaveOccurred(), "Unable to update pod resource due to: %v", err)
755+
745756
// Start the informers. This will sync the cache with the latest ingress.
746757
err = ctxt.Run(stopChannel, true, environment.GetFakeEnv())
747758
Ω(err).ToNot(HaveOccurred())
748759

749760
// Wait for the controller to receive an ingress update.
750761
ingressEvent()
751762

752-
// Method to test all the ingress that have been added to the K8s context.
753-
annotationIngress := func() []*v1beta1.Ingress {
754-
// Get all the ingresses
755-
ingressList := ctxt.ListHTTPIngresses()
756-
// There should be only one ingress
757-
Expect(len(ingressList)).To(Equal(1), "Expected only one ingress resource but got: %d", len(ingressList))
758-
// Make sure it is the ingress we stored.
759-
Expect(ingressList[0]).To(Equal(ingress))
760-
761-
return ingressList
762-
}
763-
764763
// Get all the ingresses
765-
ingressList := annotationIngress()
764+
ingressList := ctxt.ListHTTPIngresses()
765+
// There should be only one ingress
766+
Expect(len(ingressList)).To(Equal(1), "Expected only one ingress resource but got: %d", len(ingressList))
767+
// Make sure it is the ingress we stored.
768+
Expect(ingressList[0]).To(Equal(ingress))
769+
})
770+
771+
It("Should be able to create Application Gateway Configuration from Ingress with all annotations.", func() {
766772

767773
annotationsHTTPSettingsChecker := func(appGW *n.ApplicationGatewayPropertiesFormat) {
768774
expectedBackend := &ingress.Spec.Rules[0].IngressRuleValue.HTTP.Paths[0].Backend
@@ -799,10 +805,38 @@ var _ = Describe("Tests `appgw.ConfigBuilder`", func() {
799805
Expect(backendSettings).To(ContainElement(*httpSettings))
800806
}
801807

808+
annotationHealthProbesChecker := func(appGW *n.ApplicationGatewayPropertiesFormat) {
809+
expectedBackend := &ingress.Spec.Rules[0].IngressRuleValue.HTTP.Paths[0].Backend
810+
probeName := generateProbeName(expectedBackend.ServiceName, expectedBackend.ServicePort.String(), ingress)
811+
probe := &n.ApplicationGatewayProbe{
812+
Name: &probeName,
813+
ID: to.StringPtr(appGwIdentifier.probeID(probeName)),
814+
ApplicationGatewayProbePropertiesFormat: &n.ApplicationGatewayProbePropertiesFormat{
815+
Protocol: n.HTTP,
816+
Host: to.StringPtr("www.backend.com"),
817+
Path: to.StringPtr("/test"),
818+
Interval: to.Int32Ptr(30),
819+
UnhealthyThreshold: to.Int32Ptr(3),
820+
Timeout: to.Int32Ptr(30),
821+
Match: &n.ApplicationGatewayProbeHealthResponseMatch{},
822+
PickHostNameFromBackendHTTPSettings: to.BoolPtr(false),
823+
MinServers: to.Int32Ptr(0),
824+
},
825+
}
826+
827+
probes := *appGW.Probes
828+
Expect(len(probes)).To(Equal(3))
829+
830+
// Test the default health probe.
831+
Expect(probes).To(ContainElement(defaultProbe(appGwIdentifier, n.HTTP)))
832+
// Test the ingress health probe that we installed.
833+
Expect(probes).To(ContainElement(*probe))
834+
}
835+
ingressList := ctxt.ListHTTPIngresses()
802836
testAGConfig(ingressList, serviceList, appGwConfigSettings{
803837
healthProbesCollection: appGWSettingsChecker{
804838
total: 2,
805-
checker: defaultHealthProbesChecker,
839+
checker: annotationHealthProbesChecker,
806840
},
807841
backendHTTPSettingsCollection: appGWSettingsChecker{
808842
total: 2,

pkg/appgw/health_probes.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ func (c *appGwConfigBuilder) generateHealthProbe(backendID backendIdentifier) *n
9696
probe.Host = hostName
9797
}
9898

99+
if hostName, err := annotations.BackendHostName(backendID.Ingress); err == nil {
100+
probe.Host = to.StringPtr(hostName)
101+
}
102+
99103
pathPrefix, err := annotations.BackendPathPrefix(backendID.Ingress)
100104
if err == nil {
101105
probe.Path = to.StringPtr(pathPrefix)
@@ -122,6 +126,9 @@ func (c *appGwConfigBuilder) generateHealthProbe(backendID backendIdentifier) *n
122126
if k8sProbeForServiceContainer.Handler.HTTPGet.Scheme == v1.URISchemeHTTPS {
123127
probe.Protocol = n.HTTPS
124128
}
129+
if k8sProbeForServiceContainer.Handler.HTTPGet.Scheme == v1.URISchemeHTTP {
130+
probe.Protocol = n.HTTP
131+
}
125132
if k8sProbeForServiceContainer.PeriodSeconds != 0 {
126133
probe.Interval = to.Int32Ptr(k8sProbeForServiceContainer.PeriodSeconds)
127134
}

0 commit comments

Comments
 (0)