...

Source file src/k8s.io/kube-aggregator/pkg/apis/apiregistration/helpers.go

Documentation: k8s.io/kube-aggregator/pkg/apis/apiregistration

     1  /*
     2  Copyright 2016 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 apiregistration
    18  
    19  import (
    20  	"sort"
    21  
    22  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"k8s.io/apimachinery/pkg/version"
    24  )
    25  
    26  // SortedByGroupAndVersion sorts APIServices into their different groups, and then sorts them based on their versions.
    27  // For example, the first element of the first array contains the APIService with the highest version number, in the
    28  // group with the highest priority; while the last element of the last array contains the APIService with the lowest
    29  // version number, in the group with the lowest priority.
    30  func SortedByGroupAndVersion(servers []*APIService) [][]*APIService {
    31  	serversByGroupPriorityMinimum := ByGroupPriorityMinimum(servers)
    32  	sort.Sort(serversByGroupPriorityMinimum)
    33  
    34  	ret := [][]*APIService{}
    35  	for _, curr := range serversByGroupPriorityMinimum {
    36  		// check to see if we already have an entry for this group
    37  		existingIndex := -1
    38  		for j, groupInReturn := range ret {
    39  			if groupInReturn[0].Spec.Group == curr.Spec.Group {
    40  				existingIndex = j
    41  				break
    42  			}
    43  		}
    44  
    45  		if existingIndex >= 0 {
    46  			ret[existingIndex] = append(ret[existingIndex], curr)
    47  			sort.Sort(ByVersionPriority(ret[existingIndex]))
    48  			continue
    49  		}
    50  
    51  		ret = append(ret, []*APIService{curr})
    52  	}
    53  
    54  	return ret
    55  }
    56  
    57  // ByGroupPriorityMinimum sorts with the highest group number first, then by name.
    58  // This is not a simple reverse, because we want the name sorting to be alpha, not
    59  // reverse alpha.
    60  type ByGroupPriorityMinimum []*APIService
    61  
    62  func (s ByGroupPriorityMinimum) Len() int      { return len(s) }
    63  func (s ByGroupPriorityMinimum) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
    64  func (s ByGroupPriorityMinimum) Less(i, j int) bool {
    65  	if s[i].Spec.GroupPriorityMinimum != s[j].Spec.GroupPriorityMinimum {
    66  		return s[i].Spec.GroupPriorityMinimum > s[j].Spec.GroupPriorityMinimum
    67  	}
    68  	return s[i].Name < s[j].Name
    69  }
    70  
    71  // ByVersionPriority sorts with the highest version number first, then by name.
    72  // This is not a simple reverse, because we want the name sorting to be alpha, not
    73  // reverse alpha.
    74  type ByVersionPriority []*APIService
    75  
    76  func (s ByVersionPriority) Len() int      { return len(s) }
    77  func (s ByVersionPriority) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
    78  func (s ByVersionPriority) Less(i, j int) bool {
    79  	if s[i].Spec.VersionPriority != s[j].Spec.VersionPriority {
    80  		return s[i].Spec.VersionPriority > s[j].Spec.VersionPriority
    81  	}
    82  	return version.CompareKubeAwareVersionStrings(s[i].Spec.Version, s[j].Spec.Version) > 0
    83  }
    84  
    85  // NewLocalAvailableAPIServiceCondition returns a condition for an available local APIService.
    86  func NewLocalAvailableAPIServiceCondition() APIServiceCondition {
    87  	return APIServiceCondition{
    88  		Type:               Available,
    89  		Status:             ConditionTrue,
    90  		LastTransitionTime: metav1.Now(),
    91  		Reason:             "Local",
    92  		Message:            "Local APIServices are always available",
    93  	}
    94  }
    95  
    96  // GetAPIServiceConditionByType gets an *APIServiceCondition by APIServiceConditionType if present
    97  func GetAPIServiceConditionByType(apiService *APIService, conditionType APIServiceConditionType) *APIServiceCondition {
    98  	for i := range apiService.Status.Conditions {
    99  		if apiService.Status.Conditions[i].Type == conditionType {
   100  			return &apiService.Status.Conditions[i]
   101  		}
   102  	}
   103  	return nil
   104  }
   105  
   106  // SetAPIServiceCondition sets the status condition.  It either overwrites the existing one or
   107  // creates a new one
   108  func SetAPIServiceCondition(apiService *APIService, newCondition APIServiceCondition) {
   109  	existingCondition := GetAPIServiceConditionByType(apiService, newCondition.Type)
   110  	if existingCondition == nil {
   111  		apiService.Status.Conditions = append(apiService.Status.Conditions, newCondition)
   112  		return
   113  	}
   114  
   115  	if existingCondition.Status != newCondition.Status {
   116  		existingCondition.Status = newCondition.Status
   117  		existingCondition.LastTransitionTime = newCondition.LastTransitionTime
   118  	}
   119  
   120  	existingCondition.Reason = newCondition.Reason
   121  	existingCondition.Message = newCondition.Message
   122  }
   123  
   124  // IsAPIServiceConditionTrue indicates if the condition is present and strictly true
   125  func IsAPIServiceConditionTrue(apiService *APIService, conditionType APIServiceConditionType) bool {
   126  	condition := GetAPIServiceConditionByType(apiService, conditionType)
   127  	return condition != nil && condition.Status == ConditionTrue
   128  }
   129  

View as plain text