| 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 |
metainternalversionscheme "k8s.io/apimachinery/pkg/apis/meta/internalversion/scheme" |
| 24 |
"k8s.io/apimachinery/pkg/runtime" |
| 25 |
"k8s.io/apimachinery/pkg/runtime/serializer" |
| 26 |
"k8s.io/apimachinery/pkg/runtime/serializer/cbor" |
| 27 |
"k8s.io/apiserver/pkg/features" |
| 28 |
utilfeature "k8s.io/apiserver/pkg/util/feature" |
| 29 |
) |
| 30 |
|
| 31 |
// SetFlowSchemaCondition sets conditions. |
| 32 |
func SetFlowSchemaCondition(flowSchema *flowcontrol.FlowSchema, newCondition flowcontrol.FlowSchemaCondition) { |
| 33 |
existingCondition := GetFlowSchemaConditionByType(flowSchema, newCondition.Type) |
| 34 |
if existingCondition == nil { |
| 35 |
flowSchema.Status.Conditions = append(flowSchema.Status.Conditions, newCondition) |
| 36 |
return |
| 37 |
} |
| 38 |
|
| 39 |
if existingCondition.Status != newCondition.Status { |
| 40 |
existingCondition.Status = newCondition.Status |
| 41 |
existingCondition.LastTransitionTime = newCondition.LastTransitionTime |
| 42 |
} |
| 43 |
|
| 44 |
existingCondition.Reason = newCondition.Reason |
| 45 |
existingCondition.Message = newCondition.Message |
| 46 |
} |
| 47 |
|
| 48 |
// GetFlowSchemaConditionByType gets conditions. |
| 49 |
func GetFlowSchemaConditionByType(flowSchema *flowcontrol.FlowSchema, conditionType flowcontrol.FlowSchemaConditionType) *flowcontrol.FlowSchemaCondition { |
| 50 |
for i := range flowSchema.Status.Conditions { |
| 51 |
if flowSchema.Status.Conditions[i].Type == conditionType { |
| 52 |
return &flowSchema.Status.Conditions[i] |
| 53 |
} |
| 54 |
} |
| 55 |
return nil |
| 56 |
} |
| 57 |
|
| 58 |
// SetPriorityLevelConfigurationCondition sets conditions. |
| 59 |
func SetPriorityLevelConfigurationCondition(priorityLevel *flowcontrol.PriorityLevelConfiguration, newCondition flowcontrol.PriorityLevelConfigurationCondition) { |
| 60 |
existingCondition := GetPriorityLevelConfigurationConditionByType(priorityLevel, newCondition.Type) |
| 61 |
if existingCondition == nil { |
| 62 |
priorityLevel.Status.Conditions = append(priorityLevel.Status.Conditions, newCondition) |
| 63 |
return |
| 64 |
} |
| 65 |
|
| 66 |
if existingCondition.Status != newCondition.Status { |
| 67 |
existingCondition.Status = newCondition.Status |
| 68 |
existingCondition.LastTransitionTime = newCondition.LastTransitionTime |
| 69 |
} |
| 70 |
|
| 71 |
existingCondition.Reason = newCondition.Reason |
| 72 |
existingCondition.Message = newCondition.Message |
| 73 |
} |
| 74 |
|
| 75 |
// GetPriorityLevelConfigurationConditionByType gets conditions. |
| 76 |
func GetPriorityLevelConfigurationConditionByType(priorityLevel *flowcontrol.PriorityLevelConfiguration, conditionType flowcontrol.PriorityLevelConfigurationConditionType) *flowcontrol.PriorityLevelConfigurationCondition { |
| 77 |
for i := range priorityLevel.Status.Conditions { |
| 78 |
if priorityLevel.Status.Conditions[i].Type == conditionType { |
| 79 |
return &priorityLevel.Status.Conditions[i] |
| 80 |
} |
| 81 |
} |
| 82 |
return nil |
| 83 |
} |
| 84 |
|
| 85 |
var _ sort.Interface = FlowSchemaSequence{} |
| 86 |
|
| 87 |
// FlowSchemaSequence holds sorted set of pointers to FlowSchema objects. |
| 88 |
// FlowSchemaSequence implements `sort.Interface` |
| 89 |
type FlowSchemaSequence []*flowcontrol.FlowSchema |
| 90 |
|
| 91 |
func (s FlowSchemaSequence) Len() int { |
| 92 |
return len(s) |
| 93 |
} |
| 94 |
|
| 95 |
func (s FlowSchemaSequence) Less(i, j int) bool { |
| 96 |
// the flow-schema w/ lower matching-precedence is prior |
| 97 |
if ip, jp := s[i].Spec.MatchingPrecedence, s[j].Spec.MatchingPrecedence; ip != jp { |
| 98 |
return ip < jp |
| 99 |
} |
| 100 |
// sort alphabetically |
| 101 |
return s[i].Name < s[j].Name |
| 102 |
} |
| 103 |
|
| 104 |
func (s FlowSchemaSequence) Swap(i, j int) { |
| 105 |
s[i], s[j] = s[j], s[i] |
| 106 |
} |
| 107 |
|
| 108 |
var metaInternalVersionCodecsWithCBOR = serializer.NewCodecFactory(metainternalversionscheme.Scheme, serializer.WithSerializer(cbor.NewSerializerInfo)) |
| 109 |
|
| 110 |
// GetMetaInternalVersionCodecs returns a negotiated serializer that recognizes the types from |
| 111 |
// k8s.io/apimachinery/pkg/apis/meta/internalversion/scheme.Scheme. It will or will include a CBOR |
| 112 |
// serializer if CBOR is enabled. |
| 113 |
func GetMetaInternalVersionCodecs() runtime.NegotiatedSerializer { |
| 114 |
if utilfeature.DefaultFeatureGate.Enabled(features.CBORServingAndStorage) { |
| 115 |
return metaInternalVersionCodecsWithCBOR |
| 116 |
} |
| 117 |
return metainternalversionscheme.Codecs |
| 118 |
} |