k8s.io/apiserver@v0.31.1/pkg/util/apihelpers/helpers.go (about) 1 /* 2 Copyright 2019 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 apihelpers 18 19 import ( 20 "sort" 21 22 flowcontrol "k8s.io/api/flowcontrol/v1" 23 ) 24 25 // SetFlowSchemaCondition sets conditions. 26 func SetFlowSchemaCondition(flowSchema *flowcontrol.FlowSchema, newCondition flowcontrol.FlowSchemaCondition) { 27 existingCondition := GetFlowSchemaConditionByType(flowSchema, newCondition.Type) 28 if existingCondition == nil { 29 flowSchema.Status.Conditions = append(flowSchema.Status.Conditions, newCondition) 30 return 31 } 32 33 if existingCondition.Status != newCondition.Status { 34 existingCondition.Status = newCondition.Status 35 existingCondition.LastTransitionTime = newCondition.LastTransitionTime 36 } 37 38 existingCondition.Reason = newCondition.Reason 39 existingCondition.Message = newCondition.Message 40 } 41 42 // GetFlowSchemaConditionByType gets conditions. 43 func GetFlowSchemaConditionByType(flowSchema *flowcontrol.FlowSchema, conditionType flowcontrol.FlowSchemaConditionType) *flowcontrol.FlowSchemaCondition { 44 for i := range flowSchema.Status.Conditions { 45 if flowSchema.Status.Conditions[i].Type == conditionType { 46 return &flowSchema.Status.Conditions[i] 47 } 48 } 49 return nil 50 } 51 52 // SetPriorityLevelConfigurationCondition sets conditions. 53 func SetPriorityLevelConfigurationCondition(priorityLevel *flowcontrol.PriorityLevelConfiguration, newCondition flowcontrol.PriorityLevelConfigurationCondition) { 54 existingCondition := GetPriorityLevelConfigurationConditionByType(priorityLevel, newCondition.Type) 55 if existingCondition == nil { 56 priorityLevel.Status.Conditions = append(priorityLevel.Status.Conditions, newCondition) 57 return 58 } 59 60 if existingCondition.Status != newCondition.Status { 61 existingCondition.Status = newCondition.Status 62 existingCondition.LastTransitionTime = newCondition.LastTransitionTime 63 } 64 65 existingCondition.Reason = newCondition.Reason 66 existingCondition.Message = newCondition.Message 67 } 68 69 // GetPriorityLevelConfigurationConditionByType gets conditions. 70 func GetPriorityLevelConfigurationConditionByType(priorityLevel *flowcontrol.PriorityLevelConfiguration, conditionType flowcontrol.PriorityLevelConfigurationConditionType) *flowcontrol.PriorityLevelConfigurationCondition { 71 for i := range priorityLevel.Status.Conditions { 72 if priorityLevel.Status.Conditions[i].Type == conditionType { 73 return &priorityLevel.Status.Conditions[i] 74 } 75 } 76 return nil 77 } 78 79 var _ sort.Interface = FlowSchemaSequence{} 80 81 // FlowSchemaSequence holds sorted set of pointers to FlowSchema objects. 82 // FlowSchemaSequence implements `sort.Interface` 83 type FlowSchemaSequence []*flowcontrol.FlowSchema 84 85 func (s FlowSchemaSequence) Len() int { 86 return len(s) 87 } 88 89 func (s FlowSchemaSequence) Less(i, j int) bool { 90 // the flow-schema w/ lower matching-precedence is prior 91 if ip, jp := s[i].Spec.MatchingPrecedence, s[j].Spec.MatchingPrecedence; ip != jp { 92 return ip < jp 93 } 94 // sort alphabetically 95 return s[i].Name < s[j].Name 96 } 97 98 func (s FlowSchemaSequence) Swap(i, j int) { 99 s[i], s[j] = s[j], s[i] 100 }