Skip to content

Commit

Permalink
Merge pull request #225 from frobware/fix-idled-services
Browse files Browse the repository at this point in the history
Bug 1900989: Move idle check from endpoints to service
  • Loading branch information
openshift-merge-robot committed Jan 28, 2021
2 parents 25b1656 + 3b24611 commit 30e2fb5
Showing 1 changed file with 41 additions and 21 deletions.
62 changes: 41 additions & 21 deletions pkg/router/template/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,42 +254,62 @@ func getPartsFromEndpointsKey(key ServiceUnitKey) (string, string) {
return namespace, name
}

// subsetHasAddresses returns true if subsets has any addresses.
func subsetHasAddresses(subsets []kapi.EndpointSubset) bool {
for i := range subsets {
if len(subsets[i].Addresses) > 0 {
return true
}
}
return false
}

// serviceIsIdled return true if the service has been annotated with
// unidlingapi.IdledAtAnnotation.
func serviceIsIdled(service *kapi.Service) bool {
value, _ := service.Annotations[unidlingapi.IdledAtAnnotation]
return len(value) > 0
}

// createRouterEndpoints creates openshift router endpoints based on k8s endpoints
func createRouterEndpoints(endpoints *kapi.Endpoints, excludeUDP bool, lookupSvc ServiceLookup) []Endpoint {
// check if this service is currently idled
wasIdled := false
subsets := endpoints.Subsets
if _, ok := endpoints.Annotations[unidlingapi.IdledAtAnnotation]; ok && len(endpoints.Subsets) == 0 {

if !subsetHasAddresses(subsets) {
service, err := lookupSvc.LookupService(endpoints)
if err != nil {
utilruntime.HandleError(fmt.Errorf("unable to find idled service corresponding to idled endpoints %s/%s: %v", endpoints.Namespace, endpoints.Name, err))
utilruntime.HandleError(fmt.Errorf("unable to find service %s/%s: %v", endpoints.Namespace, endpoints.Name, err))
return []Endpoint{}
}

if !isServiceIPSet(service) {
utilruntime.HandleError(fmt.Errorf("headless service %s/%s was marked as idled, but cannot setup unidling without a cluster IP", endpoints.Namespace, endpoints.Name))
return []Endpoint{}
}
if serviceIsIdled(service) {
if !isServiceIPSet(service) {
utilruntime.HandleError(fmt.Errorf("headless service %s/%s was marked as idled, but cannot setup unidling without a cluster IP", endpoints.Namespace, endpoints.Name))
return []Endpoint{}
}

svcSubset := kapi.EndpointSubset{
Addresses: []kapi.EndpointAddress{
{
IP: service.Spec.ClusterIP,
svcSubset := kapi.EndpointSubset{
Addresses: []kapi.EndpointAddress{
{
IP: service.Spec.ClusterIP,
},
},
},
}
}

for _, port := range service.Spec.Ports {
endptPort := kapi.EndpointPort{
Name: port.Name,
Port: port.Port,
Protocol: port.Protocol,
for _, port := range service.Spec.Ports {
endptPort := kapi.EndpointPort{
Name: port.Name,
Port: port.Port,
Protocol: port.Protocol,
}
svcSubset.Ports = append(svcSubset.Ports, endptPort)
}
svcSubset.Ports = append(svcSubset.Ports, endptPort)
}

subsets = []kapi.EndpointSubset{svcSubset}
wasIdled = true
subsets = []kapi.EndpointSubset{svcSubset}
wasIdled = true
}
}

out := make([]Endpoint, 0, len(endpoints.Subsets)*4)
Expand Down

0 comments on commit 30e2fb5

Please sign in to comment.