Updating instances interface to accept all named ports at once
This commit is contained in:
parent
abc8b9de51
commit
3dcdc2600e
11 changed files with 135 additions and 93 deletions
|
|
@ -210,24 +210,43 @@ func (b *Backends) create(namedPort *compute.NamedPort, hcLink string, sp Servic
|
|||
return b.Get(namedPort.Port)
|
||||
}
|
||||
|
||||
// Add will get or create a Backend for the given port.
|
||||
// Ensure will update or create Backends for the given ports.
|
||||
// Uses the given instance groups if non-nil, else creates instance groups.
|
||||
func (b *Backends) Add(p ServicePort, igs []*compute.InstanceGroup) error {
|
||||
// We must track the port even if creating the backend failed, because
|
||||
// we might've created a health-check for it.
|
||||
be := &compute.BackendService{}
|
||||
defer func() { b.snapshotter.Add(portKey(p.Port), be) }()
|
||||
|
||||
var err error
|
||||
func (b *Backends) Ensure(svcPorts []ServicePort, igs []*compute.InstanceGroup) error {
|
||||
glog.V(3).Infof("Sync: backends %v", svcPorts)
|
||||
// Ideally callers should pass the instance groups to prevent recomputing them here.
|
||||
// Igs can be nil in scenarios where we do not have instance groups such as
|
||||
// while syncing default backend service.
|
||||
if igs == nil {
|
||||
igs, _, err = instances.EnsureInstanceGroupsAndPorts(b.nodePool, b.namer, p.Port)
|
||||
ports := []int64{}
|
||||
for _, p := range svcPorts {
|
||||
ports = append(ports, p.Port)
|
||||
}
|
||||
var err error
|
||||
igs, _, err = instances.EnsureInstanceGroupsAndPorts(b.nodePool, b.namer, ports)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// create backends for new ports, perform an edge hop for existing ports
|
||||
for _, port := range svcPorts {
|
||||
if err := b.ensureBackendService(port, igs); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ensureBackendService will update or create a Backend for the given port.
|
||||
// It assumes that the instance groups have been created and required named port has been added.
|
||||
// If not, then Ensure should be called instead.
|
||||
func (b *Backends) ensureBackendService(p ServicePort, igs []*compute.InstanceGroup) error {
|
||||
// We must track the ports even if creating the backends failed, because
|
||||
// we might've created health-check for them.
|
||||
be := &compute.BackendService{}
|
||||
defer func() { b.snapshotter.Add(portKey(p.Port), be) }()
|
||||
|
||||
var err error
|
||||
|
||||
// Ensure health check for backend service exists
|
||||
hcLink, err := b.ensureHealthCheck(p)
|
||||
|
|
@ -388,19 +407,6 @@ func (b *Backends) edgeHop(be *compute.BackendService, igs []*compute.InstanceGr
|
|||
return fmt.Errorf("received errors when updating backend service: %v", strings.Join(errs, "\n"))
|
||||
}
|
||||
|
||||
// Sync syncs backend services corresponding to ports in the given list.
|
||||
func (b *Backends) Sync(svcNodePorts []ServicePort, igs []*compute.InstanceGroup) error {
|
||||
glog.V(3).Infof("Sync: backends %v", svcNodePorts)
|
||||
|
||||
// create backends for new ports, perform an edge hop for existing ports
|
||||
for _, port := range svcNodePorts {
|
||||
if err := b.Add(port, igs); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GC garbage collects services corresponding to ports in the given list.
|
||||
func (b *Backends) GC(svcNodePorts []ServicePort) error {
|
||||
knownPorts := sets.NewString()
|
||||
|
|
|
|||
|
|
@ -80,7 +80,7 @@ func TestBackendPoolAdd(t *testing.T) {
|
|||
// Add a backend for a port, then re-add the same port and
|
||||
// make sure it corrects a broken link from the backend to
|
||||
// the instance group.
|
||||
err := pool.Add(nodePort, nil)
|
||||
err := pool.Ensure([]ServicePort{nodePort}, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("Did not find expect error when adding a nodeport: %v, err: %v", nodePort, err)
|
||||
}
|
||||
|
|
@ -95,10 +95,11 @@ func TestBackendPoolAdd(t *testing.T) {
|
|||
t.Fatalf("Backend %v has wrong port %v, expected %v", be.Name, be.Port, nodePort)
|
||||
}
|
||||
|
||||
// Check that the instance group has the new port
|
||||
// Check that the instance group has the new port.
|
||||
ig, err := fakeIGs.GetInstanceGroup(namer.IGName(), defaultZone)
|
||||
var found bool
|
||||
for _, port := range fakeIGs.Ports {
|
||||
if port == nodePort.Port {
|
||||
for _, port := range ig.NamedPorts {
|
||||
if port.Port == nodePort.Port {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
|
|
@ -143,7 +144,7 @@ func TestHealthCheckMigration(t *testing.T) {
|
|||
hcp.CreateHttpHealthCheck(legacyHC)
|
||||
|
||||
// Add the service port to the backend pool
|
||||
pool.Add(p, nil)
|
||||
pool.Ensure([]ServicePort{p}, nil)
|
||||
|
||||
// Assert the proper health check was created
|
||||
hc, _ := pool.healthChecker.Get(p.Port)
|
||||
|
|
@ -168,7 +169,7 @@ func TestBackendPoolUpdate(t *testing.T) {
|
|||
namer := utils.Namer{}
|
||||
|
||||
p := ServicePort{Port: 3000, Protocol: utils.ProtocolHTTP}
|
||||
pool.Add(p, nil)
|
||||
pool.Ensure([]ServicePort{p}, nil)
|
||||
beName := namer.BeName(p.Port)
|
||||
|
||||
be, err := f.GetGlobalBackendService(beName)
|
||||
|
|
@ -188,7 +189,7 @@ func TestBackendPoolUpdate(t *testing.T) {
|
|||
|
||||
// Update service port to encrypted
|
||||
p.Protocol = utils.ProtocolHTTPS
|
||||
pool.Sync([]ServicePort{p}, nil)
|
||||
pool.Ensure([]ServicePort{p}, nil)
|
||||
|
||||
be, err = f.GetGlobalBackendService(beName)
|
||||
if err != nil {
|
||||
|
|
@ -214,7 +215,7 @@ func TestBackendPoolChaosMonkey(t *testing.T) {
|
|||
namer := utils.Namer{}
|
||||
|
||||
nodePort := ServicePort{Port: 8080, Protocol: utils.ProtocolHTTP}
|
||||
pool.Add(nodePort, nil)
|
||||
pool.Ensure([]ServicePort{nodePort}, nil)
|
||||
beName := namer.BeName(nodePort.Port)
|
||||
|
||||
be, _ := f.GetGlobalBackendService(beName)
|
||||
|
|
@ -227,7 +228,7 @@ func TestBackendPoolChaosMonkey(t *testing.T) {
|
|||
f.calls = []int{}
|
||||
f.UpdateGlobalBackendService(be)
|
||||
|
||||
pool.Add(nodePort, nil)
|
||||
pool.Ensure([]ServicePort{nodePort}, nil)
|
||||
for _, call := range f.calls {
|
||||
if call == utils.Create {
|
||||
t.Fatalf("Unexpected create for existing backend service")
|
||||
|
|
@ -260,10 +261,10 @@ func TestBackendPoolSync(t *testing.T) {
|
|||
f := NewFakeBackendServices(noOpErrFunc)
|
||||
fakeIGs := instances.NewFakeInstanceGroups(sets.NewString())
|
||||
pool, _ := newTestJig(f, fakeIGs, true)
|
||||
pool.Add(ServicePort{Port: 81}, nil)
|
||||
pool.Add(ServicePort{Port: 90}, nil)
|
||||
if err := pool.Sync(svcNodePorts, nil); err != nil {
|
||||
t.Errorf("Expected backend pool to sync, err: %v", err)
|
||||
pool.Ensure([]ServicePort{ServicePort{Port: 81}}, nil)
|
||||
pool.Ensure([]ServicePort{ServicePort{Port: 90}}, nil)
|
||||
if err := pool.Ensure(svcNodePorts, nil); err != nil {
|
||||
t.Errorf("Expected backend pool to add node ports, err: %v", err)
|
||||
}
|
||||
if err := pool.GC(svcNodePorts); err != nil {
|
||||
t.Errorf("Expected backend pool to GC, err: %v", err)
|
||||
|
|
@ -361,7 +362,7 @@ func TestBackendPoolDeleteLegacyHealthChecks(t *testing.T) {
|
|||
})
|
||||
|
||||
// Have pool sync the above backend service
|
||||
bp.Add(ServicePort{Port: 80, Protocol: utils.ProtocolHTTPS}, nil)
|
||||
bp.Ensure([]ServicePort{ServicePort{Port: 80, Protocol: utils.ProtocolHTTPS}}, nil)
|
||||
|
||||
// Verify the legacy health check has been deleted
|
||||
_, err = hcp.GetHttpHealthCheck(beName)
|
||||
|
|
@ -388,7 +389,7 @@ func TestBackendPoolShutdown(t *testing.T) {
|
|||
namer := utils.Namer{}
|
||||
|
||||
// Add a backend-service and verify that it doesn't exist after Shutdown()
|
||||
pool.Add(ServicePort{Port: 80}, nil)
|
||||
pool.Ensure([]ServicePort{ServicePort{Port: 80}}, nil)
|
||||
pool.Shutdown()
|
||||
if _, err := f.GetGlobalBackendService(namer.BeName(80)); err == nil {
|
||||
t.Fatalf("%v", err)
|
||||
|
|
@ -402,7 +403,7 @@ func TestBackendInstanceGroupClobbering(t *testing.T) {
|
|||
namer := utils.Namer{}
|
||||
|
||||
// This will add the instance group k8s-ig to the instance pool
|
||||
pool.Add(ServicePort{Port: 80}, nil)
|
||||
pool.Ensure([]ServicePort{ServicePort{Port: 80}}, nil)
|
||||
|
||||
be, err := f.GetGlobalBackendService(namer.BeName(80))
|
||||
if err != nil {
|
||||
|
|
@ -420,7 +421,7 @@ func TestBackendInstanceGroupClobbering(t *testing.T) {
|
|||
}
|
||||
|
||||
// Make sure repeated adds don't clobber the inserted instance group
|
||||
pool.Add(ServicePort{Port: 80}, nil)
|
||||
pool.Ensure([]ServicePort{ServicePort{Port: 80}}, nil)
|
||||
be, err = f.GetGlobalBackendService(namer.BeName(80))
|
||||
if err != nil {
|
||||
t.Fatalf("%v", err)
|
||||
|
|
@ -462,7 +463,7 @@ func TestBackendCreateBalancingMode(t *testing.T) {
|
|||
return nil
|
||||
}
|
||||
|
||||
pool.Add(nodePort, nil)
|
||||
pool.Ensure([]ServicePort{nodePort}, nil)
|
||||
be, err := f.GetGlobalBackendService(namer.BeName(nodePort.Port))
|
||||
if err != nil {
|
||||
t.Fatalf("%v", err)
|
||||
|
|
|
|||
|
|
@ -30,10 +30,9 @@ type probeProvider interface {
|
|||
// as gce backendServices, and sync them through the BackendServices interface.
|
||||
type BackendPool interface {
|
||||
Init(p probeProvider)
|
||||
Add(port ServicePort, igs []*compute.InstanceGroup) error
|
||||
Ensure(ports []ServicePort, igs []*compute.InstanceGroup) error
|
||||
Get(port int64) (*compute.BackendService, error)
|
||||
Delete(port int64) error
|
||||
Sync(ports []ServicePort, igs []*compute.InstanceGroup) error
|
||||
GC(ports []ServicePort) error
|
||||
Shutdown() error
|
||||
Status(name string) string
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue