Skip to content

Commit

Permalink
Merge pull request #288 from rramkumar1/bug-fix
Browse files Browse the repository at this point in the history
Fix bug with ensuring BackendService settings + health checks
  • Loading branch information
nicksardo committed May 31, 2018
2 parents 644020f + 7038e70 commit d995a64
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
43 changes: 26 additions & 17 deletions pkg/backends/backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,17 @@ func (b *Backends) Get(name string, isAlpha bool) (*BackendService, error) {
return &BackendService{Ga: beGa, Alpha: beAlpha}, nil
}

func (b *Backends) ensureHealthCheck(sp utils.ServicePort) (string, error) {
func (b *Backends) ensureHealthCheck(sp utils.ServicePort, hasLegacyHC bool) (string, error) {
hc := b.healthChecker.New(sp)
existingLegacyHC, err := b.healthChecker.GetLegacy(sp.NodePort)
if err != nil && !utils.IsNotFoundError(err) {
return "", err
}

if existingLegacyHC != nil {
glog.V(4).Infof("Applying settings of existing health check to newer health check on port %+v", sp)
applyLegacyHCToHC(existingLegacyHC, hc)
if hasLegacyHC {
existingLegacyHC, err := b.healthChecker.GetLegacy(sp.NodePort)
if err != nil && !utils.IsNotFoundError(err) {
return "", err
}
if existingLegacyHC != nil {
glog.V(4).Infof("Applying settings of existing health check to newer health check on port %+v", sp)
applyLegacyHCToHC(existingLegacyHC, hc)
}
} else if b.prober != nil {
probe, err := b.prober.GetProbe(sp)
if err != nil {
Expand Down Expand Up @@ -321,14 +322,24 @@ func (b *Backends) ensureBackendService(sp utils.ServicePort, igLinks []string)
b.snapshotter.Add(beName, be)
}()

// Ensure health check for backend service exists
hcLink, err := b.ensureHealthCheck(sp)
be, _ = b.Get(beName, sp.IsAlpha())
hasLegacyHC := false
if be != nil {
// If the backend already exists, find out if it is using a legacy health check.
existingHCLink := be.GetHealthCheckLink()
if strings.Contains(existingHCLink, "/httpHealthChecks/") {
hasLegacyHC = true
}
}

// Ensure health check for backend service exists. Note that hasLegacyHC
// will dictate whether we search for an existing legacy health check.
hcLink, err := b.ensureHealthCheck(sp, hasLegacyHC)
if err != nil {
return err
}

// Verify existance of a backend service for the proper port, but do not specify any backends/igs
be, _ = b.Get(beName, sp.IsAlpha())
if be == nil {
namedPort := &compute.NamedPort{
Name: b.namer.NamedPort(sp.NodePort),
Expand All @@ -343,19 +354,17 @@ func (b *Backends) ensureBackendService(sp utils.ServicePort, igLinks []string)
}

needUpdate := be.ensureProtocol(sp)
needUpdate = needUpdate || be.ensureHealthCheckLink(hcLink)
needUpdate = needUpdate || be.ensureDescription(sp.Description())
needUpdate = be.ensureHealthCheckLink(hcLink) || needUpdate
needUpdate = be.ensureDescription(sp.Description()) || needUpdate

if needUpdate {
if err = b.update(be); err != nil {
return err
}
}

existingHCLink := be.GetHealthCheckLink()

// If previous health check was legacy type, we need to delete it.
if existingHCLink != hcLink && strings.Contains(existingHCLink, "/httpHealthChecks/") {
if hasLegacyHC {
if err = b.healthChecker.DeleteLegacy(sp.NodePort); err != nil {
glog.Warning("Failed to delete legacy HttpHealthCheck %v; Will not try again, err: %v", beName, err)
}
Expand Down
21 changes: 18 additions & 3 deletions pkg/backends/backends_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,23 +166,38 @@ func TestHealthCheckMigration(t *testing.T) {
pool, hcp := newTestJig(f, fakeIGs, false)

p := utils.ServicePort{NodePort: 7000, Protocol: annotations.ProtocolHTTP}
beName := p.BackendName(defaultNamer)

// Create a legacy health check and insert it into the HC provider.
legacyHC := &compute.HttpHealthCheck{
Name: p.BackendName(defaultNamer),
Name: beName,
RequestPath: "/my-healthz-path",
Host: "k8s.io",
Description: "My custom HC",
UnhealthyThreshold: 30,
CheckIntervalSec: 40,
}
hcp.CreateHttpHealthCheck(legacyHC)
if err := hcp.CreateHttpHealthCheck(legacyHC); err != nil {
t.Fatalf("unexpected error creating http health check %v", err)
}

// Verify legacy check exists
legacyHC, err := hcp.GetHttpHealthCheck(beName)
if err != nil {
t.Fatalf("unexpected error getting http health check %v", err)
}

// Create backend service with expected name and link to legacy health check
f.CreateGlobalBackendService(&compute.BackendService{
Name: beName,
HealthChecks: []string{legacyHC.SelfLink},
})

// Add the service port to the backend pool
pool.Ensure([]utils.ServicePort{p}, nil)

// Assert the proper health check was created
hc, _ := pool.healthChecker.Get(p.BackendName(defaultNamer), p.IsAlpha())
hc, _ := pool.healthChecker.Get(beName, p.IsAlpha())
if hc == nil || hc.Protocol() != p.Protocol {
t.Fatalf("Expected %s health check, received %v: ", p.Protocol, hc)
}
Expand Down

0 comments on commit d995a64

Please sign in to comment.