-
Notifications
You must be signed in to change notification settings - Fork 299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refactor Backend GC #810
Merged
k8s-ci-robot
merged 1 commit into
kubernetes:master
from
spencerhance:l7-ilb-backends-gc
Aug 13, 2019
Merged
Refactor Backend GC #810
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ package backends | |
import ( | ||
"context" | ||
"fmt" | ||
"k8s.io/ingress-gce/pkg/composite" | ||
"net/http" | ||
"reflect" | ||
"testing" | ||
|
@@ -39,6 +40,78 @@ import ( | |
"k8s.io/legacy-cloud-providers/gce" | ||
) | ||
|
||
// portset helps keep track of service ports during GC tests | ||
type portset struct { | ||
// all represents the set all of service ports in the test | ||
all map[utils.ServicePort]bool | ||
// existing represents what should exist in GCE | ||
existing map[utils.ServicePort]bool | ||
} | ||
|
||
func newPortset(ports []utils.ServicePort) *portset { | ||
ps := portset{all: map[utils.ServicePort]bool{}, existing: map[utils.ServicePort]bool{}} | ||
for _, sp := range ports { | ||
ps.all[sp] = true | ||
} | ||
return &ps | ||
} | ||
|
||
func (p *portset) existingPorts() []utils.ServicePort { | ||
var result []utils.ServicePort | ||
for sp, _ := range p.existing { | ||
result = append(result, sp) | ||
} | ||
return result | ||
} | ||
|
||
// Add to 'existing' from all | ||
func (p *portset) add(ports []utils.ServicePort) error { | ||
for _, sp := range ports { | ||
// Sanity check | ||
if found := p.all[sp]; !found { | ||
return fmt.Errorf("%+v not found in p.all", sp) | ||
} | ||
p.existing[sp] = true | ||
} | ||
return nil | ||
} | ||
|
||
// Delete from 'existing' | ||
func (p *portset) del(ports []utils.ServicePort) error { | ||
for _, sp := range ports { | ||
found := p.existing[sp] | ||
if !found { | ||
return fmt.Errorf("%+v not found in p.existing", sp) | ||
} | ||
delete(p.existing, sp) | ||
} | ||
return nil | ||
} | ||
|
||
// check() iterates through all and checks that the ports in 'existing' exist in gce, and that those | ||
// that are not in 'existing' do not exist | ||
func (p *portset) check(fakeGCE *gce.Cloud) error { | ||
for sp, _ := range p.all { | ||
_, found := p.existing[sp] | ||
beName := sp.BackendName(defaultNamer) | ||
key, err := composite.CreateKey(fakeGCE, beName, features.ScopeFromServicePort(&sp)) | ||
if err != nil { | ||
return fmt.Errorf("Error creating key for backend service %s: %v", beName, err) | ||
} | ||
|
||
if found { | ||
if _, err := composite.GetBackendService(fakeGCE, key, features.VersionFromServicePort(&sp)); err != nil { | ||
return fmt.Errorf("backend for port %+v should exist, but got: %v", sp.NodePort, err) | ||
} | ||
} else { | ||
if bs, err := composite.GetBackendService(fakeGCE, key, features.VersionFromServicePort(&sp)); !utils.IsHTTPErrorCode(err, http.StatusNotFound) { | ||
return fmt.Errorf("backend for port %+v should not exist, but got %v", sp, bs) | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't we need to check things in p.all not in p.existing don't exist? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
return nil | ||
} | ||
|
||
var ( | ||
defaultNamer = utils.NewNamer("uid1", "fw1") | ||
defaultBackendSvc = types.NamespacedName{Namespace: "system", Name: "default"} | ||
|
@@ -65,6 +138,7 @@ func newTestSyncer(fakeGCE *gce.Cloud) *backendSyncer { | |
backendPool: fakeBackendPool, | ||
healthChecker: fakeHealthChecks, | ||
namer: defaultNamer, | ||
cloud: fakeGCE, | ||
} | ||
|
||
probes := map[utils.ServicePort]*api_v1.Probe{{NodePort: 443, Protocol: annotations.ProtocolHTTPS}: existingProbe} | ||
|
@@ -76,6 +150,7 @@ func newTestSyncer(fakeGCE *gce.Cloud) *backendSyncer { | |
(fakeGCE.Compute().(*cloud.MockGCE)).MockBackendServices.UpdateHook = mock.UpdateBackendServiceHook | ||
(fakeGCE.Compute().(*cloud.MockGCE)).MockHealthChecks.UpdateHook = mock.UpdateHealthCheckHook | ||
(fakeGCE.Compute().(*cloud.MockGCE)).MockAlphaHealthChecks.UpdateHook = mock.UpdateAlphaHealthCheckHook | ||
(fakeGCE.Compute().(*cloud.MockGCE)).MockAlphaRegionHealthChecks.UpdateHook = mock.UpdateAlphaRegionHealthCheckHook | ||
(fakeGCE.Compute().(*cloud.MockGCE)).MockBetaHealthChecks.UpdateHook = mock.UpdateBetaHealthCheckHook | ||
|
||
return syncer | ||
|
@@ -212,6 +287,7 @@ func TestSyncUpdateHTTP2(t *testing.T) { | |
} | ||
} | ||
|
||
// Test GC with both ELB and ILBs | ||
func TestGC(t *testing.T) { | ||
fakeGCE := gce.NewFakeGCECloud(gce.DefaultTestClusterValues()) | ||
syncer := newTestSyncer(fakeGCE) | ||
|
@@ -221,49 +297,88 @@ func TestGC(t *testing.T) { | |
{NodePort: 82, Protocol: annotations.ProtocolHTTPS}, | ||
{NodePort: 83, Protocol: annotations.ProtocolHTTP}, | ||
} | ||
ps := newPortset(svcNodePorts) | ||
if err := ps.add(svcNodePorts); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := syncer.Sync(svcNodePorts); err != nil { | ||
t.Fatalf("Expected syncer to add backends with error, err: %v", err) | ||
if err := syncer.Sync(ps.existingPorts()); err != nil { | ||
t.Fatalf("syncer.Sync(%+v) = %v, want nil ", ps.existingPorts(), err) | ||
} | ||
// Check that all backends were created. | ||
for _, sp := range svcNodePorts { | ||
beName := sp.BackendName(defaultNamer) | ||
if _, err := fakeGCE.GetGlobalBackendService(beName); err != nil { | ||
t.Fatalf("Expected to find backend for port %v, err: %v", sp.NodePort, err) | ||
} | ||
|
||
if err := ps.check(fakeGCE); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Run a no-op GC (i.e nothing is actually cleaned up) | ||
if err := syncer.GC(svcNodePorts); err != nil { | ||
t.Fatalf("Expected backend pool to GC, err: %v", err) | ||
if err := syncer.GC(ps.existingPorts()); err != nil { | ||
t.Fatalf("syncer.GC(%+v) = %v, want nil", ps.existingPorts(), err) | ||
} | ||
// Ensure that no backends were actually deleted | ||
for _, sp := range svcNodePorts { | ||
beName := sp.BackendName(defaultNamer) | ||
if _, err := fakeGCE.GetGlobalBackendService(beName); err != nil { | ||
t.Fatalf("Expected to find backend for port %v, err: %v", sp.NodePort, err) | ||
} | ||
|
||
// Check that nothing was deleted | ||
if err := ps.check(fakeGCE); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
deletedPorts := []utils.ServicePort{svcNodePorts[1], svcNodePorts[2]} | ||
svcNodePorts = []utils.ServicePort{svcNodePorts[0]} | ||
if err := syncer.GC(svcNodePorts); err != nil { | ||
t.Fatalf("Expected backend pool to GC, err: %v", err) | ||
if err := ps.del([]utils.ServicePort{svcNodePorts[1], svcNodePorts[2]}); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Ensure that 2 out of the 3 backends were deleted | ||
for _, sp := range deletedPorts { | ||
beName := sp.BackendName(defaultNamer) | ||
if _, err := fakeGCE.GetGlobalBackendService(beName); err == nil { | ||
t.Fatalf("Expected to not find backend for port %v", sp.NodePort) | ||
} | ||
if err := syncer.GC(ps.existingPorts()); err != nil { | ||
t.Fatalf("syncer.GC(%+v) = %v, want nil", ps.existingPorts(), err) | ||
} | ||
|
||
// Ensure that the 1 remaining backend exists | ||
for _, sp := range svcNodePorts { | ||
beName := sp.BackendName(defaultNamer) | ||
if _, err := fakeGCE.GetGlobalBackendService(beName); err != nil { | ||
t.Fatalf("Expected to find backend for port %v, err: %v", sp.NodePort, err) | ||
} | ||
if err := ps.check(fakeGCE); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
// Test GC with both ELB and ILBs | ||
bowei marked this conversation as resolved.
Show resolved
Hide resolved
|
||
func TestGCMixed(t *testing.T) { | ||
fakeGCE := gce.NewFakeGCECloud(gce.DefaultTestClusterValues()) | ||
syncer := newTestSyncer(fakeGCE) | ||
|
||
svcNodePorts := []utils.ServicePort{ | ||
{NodePort: 81, Protocol: annotations.ProtocolHTTP}, | ||
{NodePort: 82, Protocol: annotations.ProtocolHTTPS}, | ||
{NodePort: 83, Protocol: annotations.ProtocolHTTP}, | ||
{NodePort: 84, Protocol: annotations.ProtocolHTTP, NEGEnabled: true, L7ILBEnabled: true}, | ||
{NodePort: 85, Protocol: annotations.ProtocolHTTPS, NEGEnabled: true, L7ILBEnabled: true}, | ||
{NodePort: 86, Protocol: annotations.ProtocolHTTP, NEGEnabled: true, L7ILBEnabled: true}, | ||
} | ||
ps := newPortset(svcNodePorts) | ||
if err := ps.add(svcNodePorts); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := syncer.Sync(ps.existingPorts()); err != nil { | ||
t.Fatalf("syncer.Sync(%+v) = %v, want nil ", ps.existingPorts(), err) | ||
} | ||
|
||
if err := ps.check(fakeGCE); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Run a no-op GC (i.e nothing is actually cleaned up) | ||
if err := syncer.GC(ps.existingPorts()); err != nil { | ||
t.Fatalf("syncer.GC(%+v) = %v, want nil", ps.existingPorts(), err) | ||
} | ||
|
||
// Check that nothing was deleted | ||
if err := ps.check(fakeGCE); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := ps.del([]utils.ServicePort{svcNodePorts[1], svcNodePorts[2]}); err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if err := syncer.GC(ps.existingPorts()); err != nil { | ||
t.Fatalf("syncer.GC(%+v) = %v, want nil", ps.existingPorts(), err) | ||
} | ||
|
||
if err := ps.check(fakeGCE); err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is a TODO, but we may want to consider continuing on error so a single error does not block all GC operations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tracking here: #797