...

Source file src/k8s.io/kube-aggregator/pkg/controllers/openapi/aggregator/priority.go

Documentation: k8s.io/kube-aggregator/pkg/controllers/openapi/aggregator

     1  /*
     2  Copyright 2017 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package aggregator
    18  
    19  import (
    20  	"sort"
    21  
    22  	apiregistrationv1 "k8s.io/kube-aggregator/pkg/apis/apiregistration/v1"
    23  )
    24  
    25  // byPriority can be used in sort.Sort to sort specs with their priorities.
    26  type byPriority struct {
    27  	apiServices     []*apiregistrationv1.APIService
    28  	groupPriorities map[string]int32
    29  }
    30  
    31  func (a byPriority) Len() int { return len(a.apiServices) }
    32  func (a byPriority) Swap(i, j int) {
    33  	a.apiServices[i], a.apiServices[j] = a.apiServices[j], a.apiServices[i]
    34  }
    35  func (a byPriority) Less(i, j int) bool {
    36  	// All local specs will come first
    37  	if a.apiServices[i].Spec.Service == nil && a.apiServices[j].Spec.Service != nil {
    38  		return true
    39  	}
    40  	if a.apiServices[i].Spec.Service != nil && a.apiServices[j].Spec.Service == nil {
    41  		return false
    42  	}
    43  	// WARNING: This will result in not following priorities for local APIServices.
    44  	if a.apiServices[i].Spec.Service == nil {
    45  		// Sort local specs with their name. This is the order in the delegation chain (aggregator first).
    46  		return a.apiServices[i].Name < a.apiServices[j].Name
    47  	}
    48  	var iPriority, jPriority int32
    49  	if a.apiServices[i].Spec.Group == a.apiServices[j].Spec.Group {
    50  		iPriority = a.apiServices[i].Spec.VersionPriority
    51  		jPriority = a.apiServices[i].Spec.VersionPriority
    52  	} else {
    53  		iPriority = a.groupPriorities[a.apiServices[i].Spec.Group]
    54  		jPriority = a.groupPriorities[a.apiServices[j].Spec.Group]
    55  	}
    56  	if iPriority != jPriority {
    57  		// Sort by priority, higher first
    58  		return iPriority > jPriority
    59  	}
    60  	// Sort by service name.
    61  	return a.apiServices[i].Name < a.apiServices[j].Name
    62  }
    63  
    64  func sortByPriority(apiServices []*apiregistrationv1.APIService) {
    65  	b := byPriority{
    66  		apiServices:     apiServices,
    67  		groupPriorities: map[string]int32{},
    68  	}
    69  	for _, apiService := range apiServices {
    70  		if apiService.Spec.Service == nil {
    71  			continue
    72  		}
    73  		if pr, found := b.groupPriorities[apiService.Spec.Group]; !found || apiService.Spec.GroupPriorityMinimum > pr {
    74  			b.groupPriorities[apiService.Spec.Group] = apiService.Spec.GroupPriorityMinimum
    75  		}
    76  	}
    77  	sort.Sort(b)
    78  }
    79  

View as plain text