k8s.io/kubernetes@v1.29.3/pkg/apis/flowcontrol/v1beta3/conversion.go (about) 1 /* 2 Copyright 2023 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 v1beta3 18 19 import ( 20 "k8s.io/api/flowcontrol/v1beta3" 21 "k8s.io/apimachinery/pkg/conversion" 22 "k8s.io/kubernetes/pkg/apis/flowcontrol" 23 ) 24 25 func Convert_v1beta3_PriorityLevelConfiguration_To_flowcontrol_PriorityLevelConfiguration(in *v1beta3.PriorityLevelConfiguration, out *flowcontrol.PriorityLevelConfiguration, s conversion.Scope) error { 26 if err := autoConvert_v1beta3_PriorityLevelConfiguration_To_flowcontrol_PriorityLevelConfiguration(in, out, nil); err != nil { 27 return err 28 } 29 30 // during v1beta3 -> internal conversion: 31 // - remove the roundtrip annotation for the 'NominalConcurrencyShares' field 32 // - make sure we don't mutate the source (v1beta3) object's annotations 33 annotations, copied := dropPriorityLevelConcurrencyShareDefaultAnnotation(out.ObjectMeta.Annotations) 34 if copied { 35 out.ObjectMeta.Annotations = annotations 36 } 37 return nil 38 } 39 40 func Convert_flowcontrol_PriorityLevelConfiguration_To_v1beta3_PriorityLevelConfiguration(in *flowcontrol.PriorityLevelConfiguration, out *v1beta3.PriorityLevelConfiguration, s conversion.Scope) error { 41 if err := autoConvert_flowcontrol_PriorityLevelConfiguration_To_v1beta3_PriorityLevelConfiguration(in, out, nil); err != nil { 42 return err 43 } 44 45 // during internal -> v1beta3 conversion: 46 // - add the roundtrip annotation for the 'NominalConcurrencyShares' field, 47 // IIF the 'NominalConcurrencyShares' field has a value of zero. 48 // - make sure we don't mutate the source (internal) object's annotations 49 if limited := in.Spec.Limited; limited != nil && limited.NominalConcurrencyShares == 0 { 50 annotations, copied := addPriorityLevelConcurrencyShareDefaultAnnotation(out.ObjectMeta.Annotations) 51 if copied { 52 out.ObjectMeta.Annotations = annotations 53 } 54 } 55 56 return nil 57 } 58 59 func dropPriorityLevelConcurrencyShareDefaultAnnotation(in map[string]string) (map[string]string, bool) { 60 if _, ok := in[v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey]; !ok { 61 return in, false 62 } 63 64 out := copyStringMap(in) 65 delete(out, v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey) 66 return out, true 67 } 68 69 func addPriorityLevelConcurrencyShareDefaultAnnotation(in map[string]string) (map[string]string, bool) { 70 if _, ok := in[v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey]; ok { 71 return in, false 72 } 73 74 out := copyStringMap(in) 75 out[v1beta3.PriorityLevelPreserveZeroConcurrencySharesKey] = "" 76 return out, true 77 } 78 79 // copyStringMap returns a copy of the input map. 80 // If input is nil, an empty map is returned. 81 func copyStringMap(in map[string]string) map[string]string { 82 out := make(map[string]string, len(in)) 83 for k, v := range in { 84 out[k] = v 85 } 86 return out 87 }