Skip to content
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

Withdraw announcement w/o healthy endpoints for TrafficPolicy Cluster #314

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions speaker/bgp_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,41 @@ func nodeHasHealthyEndpoint(eps *v1.Endpoints, node string) bool {
return false
}

func healthyEndpointExists(eps *v1.Endpoints) bool {
ready := map[string]bool{}
for _, subset := range eps.Subsets {
for _, ep := range subset.Addresses {
if _, ok := ready[ep.IP]; !ok {
// Only set true if nothing else has expressed an
// opinion. This means that false will take precedence
// if there's any unready ports for a given endpoint.
ready[ep.IP] = true
}
}
for _, ep := range subset.NotReadyAddresses {
ready[ep.IP] = false
}
}

for _, r := range ready {
if r {
// At least one fully healthy endpoint on this machine.
return true
}
}
return false
}

func (c *bgpController) ShouldAnnounce(l log.Logger, name string, svc *v1.Service, eps *v1.Endpoints) string {
// Should we advertise? Yes, if externalTrafficPolicy is Cluster,
// or Local && there's a ready local endpoint.
// Should we advertise?
// Yes, if externalTrafficPolicy is
// Cluster && any healthy endpoint exists
// or
// Local && there's a ready local endpoint.
if svc.Spec.ExternalTrafficPolicy == v1.ServiceExternalTrafficPolicyTypeLocal && !nodeHasHealthyEndpoint(eps, c.myNode) {
return "noLocalEndpoints"
} else if !healthyEndpointExists(eps) {
return "noEndpoints"
}
return ""
}
Expand Down
80 changes: 80 additions & 0 deletions speaker/bgp_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,86 @@ func TestBGPSpeaker(t *testing.T) {
},
},

{
desc: "Endpoint list is empty",
balancer: "test1",
svc: &v1.Service{
Spec: v1.ServiceSpec{
Type: "LoadBalancer",
ExternalTrafficPolicy: "Cluster",
},
Status: statusAssigned("10.20.30.1"),
},
eps: &v1.Endpoints{},
wantAds: map[string][]*bgp.Advertisement{
"1.2.3.4:0": nil,
},
},

{
desc: "Endpoint list contains only unhealthy endpoints",
balancer: "test1",
svc: &v1.Service{
Spec: v1.ServiceSpec{
Type: "LoadBalancer",
ExternalTrafficPolicy: "Cluster",
},
Status: statusAssigned("10.20.30.1"),
},
eps: &v1.Endpoints{
Subsets: []v1.EndpointSubset{
{
NotReadyAddresses: []v1.EndpointAddress{
{
IP: "2.3.4.5",
NodeName: strptr("iris"),
},
},
},
},
},
wantAds: map[string][]*bgp.Advertisement{
"1.2.3.4:0": nil,
},
},

{
desc: "Endpoint list contains some unhealthy endpoints",
balancer: "test1",
svc: &v1.Service{
Spec: v1.ServiceSpec{
Type: "LoadBalancer",
ExternalTrafficPolicy: "Cluster",
},
Status: statusAssigned("10.20.30.1"),
},
eps: &v1.Endpoints{
Subsets: []v1.EndpointSubset{
{
Addresses: []v1.EndpointAddress{
{
IP: "2.3.4.5",
NodeName: strptr("iris"),
},
},
NotReadyAddresses: []v1.EndpointAddress{
{
IP: "2.3.4.6",
NodeName: strptr("pandora"),
},
},
},
},
},
wantAds: map[string][]*bgp.Advertisement{
"1.2.3.4:0": {
{
Prefix: ipnet("10.20.30.1/32"),
},
},
},
},

{
desc: "Multiple advertisement config",
config: &config.Config{
Expand Down