Skip to content

Commit

Permalink
health_probes: Fix probe generation to return nil when service doesn'…
Browse files Browse the repository at this point in the history
…t exists (#206)
  • Loading branch information
akshaysngupta authored May 23, 2019
1 parent 64723c3 commit 8a11366
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 31 deletions.
64 changes: 33 additions & 31 deletions pkg/appgw/health_probes.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,40 +70,42 @@ func (builder *appGwConfigBuilder) HealthProbesCollection(ingressList [](*v1beta
}

func (builder *appGwConfigBuilder) generateHealthProbe(backendID backendIdentifier) *network.ApplicationGatewayProbe {
probe := defaultProbe()
service := builder.k8sContext.GetService(backendID.serviceKey())
if service != nil {
probe.Name = to.StringPtr(generateProbeName(backendID.Path.Backend.ServiceName, backendID.Path.Backend.ServicePort.String(), backendID.Ingress.Name))
if backendID.Rule != nil && len(backendID.Rule.Host) != 0 {
probe.Host = to.StringPtr(backendID.Rule.Host)
}
if service == nil {
return nil
}

if len(annotations.BackendPathPrefix(backendID.Ingress)) != 0 {
probe.Path = to.StringPtr(annotations.BackendPathPrefix(backendID.Ingress))
} else if backendID.Path != nil && len(backendID.Path.Path) != 0 {
probe.Path = to.StringPtr(backendID.Path.Path)
}
probe := defaultProbe()
probe.Name = to.StringPtr(generateProbeName(backendID.Path.Backend.ServiceName, backendID.Path.Backend.ServicePort.String(), backendID.Ingress.Name))
if backendID.Rule != nil && len(backendID.Rule.Host) != 0 {
probe.Host = to.StringPtr(backendID.Rule.Host)
}

k8sProbeForServiceContainer := builder.getProbeForServiceContainer(service, backendID)
if k8sProbeForServiceContainer != nil {
if len(k8sProbeForServiceContainer.Handler.HTTPGet.Host) != 0 {
probe.Host = to.StringPtr(k8sProbeForServiceContainer.Handler.HTTPGet.Host)
}
if len(k8sProbeForServiceContainer.Handler.HTTPGet.Path) != 0 {
probe.Path = to.StringPtr(k8sProbeForServiceContainer.Handler.HTTPGet.Path)
}
if k8sProbeForServiceContainer.Handler.HTTPGet.Scheme == v1.URISchemeHTTPS {
probe.Protocol = network.HTTPS
}
if k8sProbeForServiceContainer.PeriodSeconds != 0 {
probe.Interval = to.Int32Ptr(k8sProbeForServiceContainer.PeriodSeconds)
}
if k8sProbeForServiceContainer.TimeoutSeconds != 0 {
probe.Timeout = to.Int32Ptr(k8sProbeForServiceContainer.TimeoutSeconds)
}
if k8sProbeForServiceContainer.FailureThreshold != 0 {
probe.UnhealthyThreshold = to.Int32Ptr(k8sProbeForServiceContainer.FailureThreshold)
}
if len(annotations.BackendPathPrefix(backendID.Ingress)) != 0 {
probe.Path = to.StringPtr(annotations.BackendPathPrefix(backendID.Ingress))
} else if backendID.Path != nil && len(backendID.Path.Path) != 0 {
probe.Path = to.StringPtr(backendID.Path.Path)
}

k8sProbeForServiceContainer := builder.getProbeForServiceContainer(service, backendID)
if k8sProbeForServiceContainer != nil {
if len(k8sProbeForServiceContainer.Handler.HTTPGet.Host) != 0 {
probe.Host = to.StringPtr(k8sProbeForServiceContainer.Handler.HTTPGet.Host)
}
if len(k8sProbeForServiceContainer.Handler.HTTPGet.Path) != 0 {
probe.Path = to.StringPtr(k8sProbeForServiceContainer.Handler.HTTPGet.Path)
}
if k8sProbeForServiceContainer.Handler.HTTPGet.Scheme == v1.URISchemeHTTPS {
probe.Protocol = network.HTTPS
}
if k8sProbeForServiceContainer.PeriodSeconds != 0 {
probe.Interval = to.Int32Ptr(k8sProbeForServiceContainer.PeriodSeconds)
}
if k8sProbeForServiceContainer.TimeoutSeconds != 0 {
probe.Timeout = to.Int32Ptr(k8sProbeForServiceContainer.TimeoutSeconds)
}
if k8sProbeForServiceContainer.FailureThreshold != 0 {
probe.UnhealthyThreshold = to.Int32Ptr(k8sProbeForServiceContainer.FailureThreshold)
}
}

Expand Down
44 changes: 44 additions & 0 deletions pkg/appgw/health_probes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,48 @@ var _ = Describe("configure App Gateway health probes", func() {
Expect(*actual).To(ContainElement(probeForOtherHost))
})
})

Context("use default probe when service doesn't exists", func() {
cb := newConfigBuilderFixture(nil)

pod := newPodFixture(testFixturesServiceName, testFixturesNamespace, testFixturesContainerName, testFixturesContainerPort)
_ = cb.k8sContext.Caches.Pods.Add(pod)

ingressList := []*v1beta1.Ingress{
newIngressFixture(),
}

// !! Action !!
_, _ = cb.HealthProbesCollection(ingressList)
actual := cb.appGwConfig.Probes

// We expect our health probe configurator to have arrived at this final setup
defaultProbe := network.ApplicationGatewayProbe{

ApplicationGatewayProbePropertiesFormat: &network.ApplicationGatewayProbePropertiesFormat{
Protocol: network.HTTP,
Host: to.StringPtr("localhost"),
Path: to.StringPtr("/"),
Interval: to.Int32Ptr(30),
Timeout: to.Int32Ptr(30),
UnhealthyThreshold: to.Int32Ptr(3),
PickHostNameFromBackendHTTPSettings: nil,
MinServers: nil,
Match: nil,
ProvisioningState: nil,
},
Name: to.StringPtr(agPrefix + "defaultprobe"),
Etag: nil,
Type: nil,
ID: nil,
}

It("should have exactly 1 record", func() {
Expect(len(*actual)).To(Equal(1))
})

It("should have created 1 default probe", func() {
Expect(*actual).To(ContainElement(defaultProbe))
})
})
})

0 comments on commit 8a11366

Please sign in to comment.