k8s.io/kubernetes@v1.29.3/pkg/apis/discovery/v1beta1/conversion.go (about) 1 /* 2 Copyright 2021 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 v1beta1 18 19 import ( 20 corev1 "k8s.io/api/core/v1" 21 "k8s.io/api/discovery/v1beta1" 22 "k8s.io/apimachinery/pkg/conversion" 23 "k8s.io/kubernetes/pkg/apis/discovery" 24 ) 25 26 func Convert_v1beta1_Endpoint_To_discovery_Endpoint(in *v1beta1.Endpoint, out *discovery.Endpoint, s conversion.Scope) error { 27 if err := autoConvert_v1beta1_Endpoint_To_discovery_Endpoint(in, out, s); err != nil { 28 return err 29 } 30 if in.Topology != nil { 31 // Copy Topology into Deprecated Topology 32 out.DeprecatedTopology = make(map[string]string, len(in.Topology)) 33 for k, v := range in.Topology { 34 out.DeprecatedTopology[k] = v 35 } 36 37 // Move zone from the topology map into a field 38 if zone, ok := in.Topology[corev1.LabelTopologyZone]; ok { 39 out.Zone = &zone 40 delete(out.DeprecatedTopology, corev1.LabelTopologyZone) 41 } 42 43 // Remove hostname from the topology map ONLY IF it is the same value as 44 // nodeName. This preserves the (rather odd) ability to have different 45 // values for topology[hostname] and nodename in v1beta1, without showing 46 // duplicate values in v1. 47 if node, ok := in.Topology[corev1.LabelHostname]; ok { 48 if out.NodeName != nil && node == *out.NodeName { 49 delete(out.DeprecatedTopology, corev1.LabelHostname) 50 } 51 } 52 53 // If zone & hostname were the only field in the map or topology was empty 54 // set DeprecatedTopology to nil 55 if len(out.DeprecatedTopology) == 0 { 56 out.DeprecatedTopology = nil 57 } 58 } 59 60 return nil 61 } 62 63 func Convert_discovery_Endpoint_To_v1beta1_Endpoint(in *discovery.Endpoint, out *v1beta1.Endpoint, s conversion.Scope) error { 64 if err := autoConvert_discovery_Endpoint_To_v1beta1_Endpoint(in, out, s); err != nil { 65 return err 66 } 67 68 // If no deprecated topology, zone or node field, no conversion is necessary 69 if in.DeprecatedTopology == nil && in.Zone == nil && in.NodeName == nil { 70 return nil 71 } 72 73 // Copy Deprecated Topology into Topology 74 out.Topology = make(map[string]string, len(in.DeprecatedTopology)) 75 for k, v := range in.DeprecatedTopology { 76 out.Topology[k] = v 77 } 78 79 // Add zone field into the topology map 80 if in.Zone != nil { 81 out.Topology[corev1.LabelTopologyZone] = *in.Zone 82 } 83 84 // Add hostname into the topology map ONLY IF it is not already present. 85 // This preserves the (rather odd) ability to have different values for 86 // topology[hostname] and nodename in v1beta1. 87 if in.NodeName != nil && out.Topology[corev1.LabelHostname] == "" { 88 out.Topology[corev1.LabelHostname] = *in.NodeName 89 } 90 91 return nil 92 }