1/* 2Copyright 2014 The Kubernetes Authors. 3 4Licensed under the Apache License, Version 2.0 (the "License"); 5you may not use this file except in compliance with the License. 6You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10Unless required by applicable law or agreed to in writing, software 11distributed under the License is distributed on an "AS IS" BASIS, 12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13See the License for the specific language governing permissions and 14limitations under the License. 15*/ 16 17package v1 18 19import ( 20 "fmt" 21 "sort" 22 23 "k8s.io/apimachinery/pkg/conversion" 24 "k8s.io/apimachinery/pkg/runtime" 25 "k8s.io/client-go/tools/clientcmd/api" 26) 27 28func addConversionFuncs(scheme *runtime.Scheme) error { 29 return scheme.AddConversionFuncs( 30 func(in *Cluster, out *api.Cluster, s conversion.Scope) error { 31 return s.DefaultConvert(in, out, conversion.IgnoreMissingFields) 32 }, 33 func(in *api.Cluster, out *Cluster, s conversion.Scope) error { 34 return s.DefaultConvert(in, out, conversion.IgnoreMissingFields) 35 }, 36 func(in *Preferences, out *api.Preferences, s conversion.Scope) error { 37 return s.DefaultConvert(in, out, conversion.IgnoreMissingFields) 38 }, 39 func(in *api.Preferences, out *Preferences, s conversion.Scope) error { 40 return s.DefaultConvert(in, out, conversion.IgnoreMissingFields) 41 }, 42 func(in *AuthInfo, out *api.AuthInfo, s conversion.Scope) error { 43 return s.DefaultConvert(in, out, conversion.IgnoreMissingFields) 44 }, 45 func(in *api.AuthInfo, out *AuthInfo, s conversion.Scope) error { 46 return s.DefaultConvert(in, out, conversion.IgnoreMissingFields) 47 }, 48 func(in *Context, out *api.Context, s conversion.Scope) error { 49 return s.DefaultConvert(in, out, conversion.IgnoreMissingFields) 50 }, 51 func(in *api.Context, out *Context, s conversion.Scope) error { 52 return s.DefaultConvert(in, out, conversion.IgnoreMissingFields) 53 }, 54 55 func(in *Config, out *api.Config, s conversion.Scope) error { 56 out.CurrentContext = in.CurrentContext 57 if err := s.Convert(&in.Preferences, &out.Preferences, 0); err != nil { 58 return err 59 } 60 61 out.Clusters = make(map[string]*api.Cluster) 62 if err := s.Convert(&in.Clusters, &out.Clusters, 0); err != nil { 63 return err 64 } 65 out.AuthInfos = make(map[string]*api.AuthInfo) 66 if err := s.Convert(&in.AuthInfos, &out.AuthInfos, 0); err != nil { 67 return err 68 } 69 out.Contexts = make(map[string]*api.Context) 70 if err := s.Convert(&in.Contexts, &out.Contexts, 0); err != nil { 71 return err 72 } 73 out.Extensions = make(map[string]runtime.Object) 74 if err := s.Convert(&in.Extensions, &out.Extensions, 0); err != nil { 75 return err 76 } 77 return nil 78 }, 79 func(in *api.Config, out *Config, s conversion.Scope) error { 80 out.CurrentContext = in.CurrentContext 81 if err := s.Convert(&in.Preferences, &out.Preferences, 0); err != nil { 82 return err 83 } 84 85 out.Clusters = make([]NamedCluster, 0, 0) 86 if err := s.Convert(&in.Clusters, &out.Clusters, 0); err != nil { 87 return err 88 } 89 out.AuthInfos = make([]NamedAuthInfo, 0, 0) 90 if err := s.Convert(&in.AuthInfos, &out.AuthInfos, 0); err != nil { 91 return err 92 } 93 out.Contexts = make([]NamedContext, 0, 0) 94 if err := s.Convert(&in.Contexts, &out.Contexts, 0); err != nil { 95 return err 96 } 97 out.Extensions = make([]NamedExtension, 0, 0) 98 if err := s.Convert(&in.Extensions, &out.Extensions, 0); err != nil { 99 return err 100 } 101 return nil 102 }, 103 func(in *[]NamedCluster, out *map[string]*api.Cluster, s conversion.Scope) error { 104 for _, curr := range *in { 105 newCluster := api.NewCluster() 106 if err := s.Convert(&curr.Cluster, newCluster, 0); err != nil { 107 return err 108 } 109 if (*out)[curr.Name] == nil { 110 (*out)[curr.Name] = newCluster 111 } else { 112 return fmt.Errorf("error converting *[]NamedCluster into *map[string]*api.Cluster: duplicate name \"%v\" in list: %v", curr.Name, *in) 113 } 114 } 115 116 return nil 117 }, 118 func(in *map[string]*api.Cluster, out *[]NamedCluster, s conversion.Scope) error { 119 allKeys := make([]string, 0, len(*in)) 120 for key := range *in { 121 allKeys = append(allKeys, key) 122 } 123 sort.Strings(allKeys) 124 125 for _, key := range allKeys { 126 newCluster := (*in)[key] 127 oldCluster := &Cluster{} 128 if err := s.Convert(newCluster, oldCluster, 0); err != nil { 129 return err 130 } 131 132 namedCluster := NamedCluster{key, *oldCluster} 133 *out = append(*out, namedCluster) 134 } 135 136 return nil 137 }, 138 func(in *[]NamedAuthInfo, out *map[string]*api.AuthInfo, s conversion.Scope) error { 139 for _, curr := range *in { 140 newAuthInfo := api.NewAuthInfo() 141 if err := s.Convert(&curr.AuthInfo, newAuthInfo, 0); err != nil { 142 return err 143 } 144 if (*out)[curr.Name] == nil { 145 (*out)[curr.Name] = newAuthInfo 146 } else { 147 return fmt.Errorf("error converting *[]NamedAuthInfo into *map[string]*api.AuthInfo: duplicate name \"%v\" in list: %v", curr.Name, *in) 148 } 149 } 150 151 return nil 152 }, 153 func(in *map[string]*api.AuthInfo, out *[]NamedAuthInfo, s conversion.Scope) error { 154 allKeys := make([]string, 0, len(*in)) 155 for key := range *in { 156 allKeys = append(allKeys, key) 157 } 158 sort.Strings(allKeys) 159 160 for _, key := range allKeys { 161 newAuthInfo := (*in)[key] 162 oldAuthInfo := &AuthInfo{} 163 if err := s.Convert(newAuthInfo, oldAuthInfo, 0); err != nil { 164 return err 165 } 166 167 namedAuthInfo := NamedAuthInfo{key, *oldAuthInfo} 168 *out = append(*out, namedAuthInfo) 169 } 170 171 return nil 172 }, 173 func(in *[]NamedContext, out *map[string]*api.Context, s conversion.Scope) error { 174 for _, curr := range *in { 175 newContext := api.NewContext() 176 if err := s.Convert(&curr.Context, newContext, 0); err != nil { 177 return err 178 } 179 if (*out)[curr.Name] == nil { 180 (*out)[curr.Name] = newContext 181 } else { 182 return fmt.Errorf("error converting *[]NamedContext into *map[string]*api.Context: duplicate name \"%v\" in list: %v", curr.Name, *in) 183 } 184 } 185 186 return nil 187 }, 188 func(in *map[string]*api.Context, out *[]NamedContext, s conversion.Scope) error { 189 allKeys := make([]string, 0, len(*in)) 190 for key := range *in { 191 allKeys = append(allKeys, key) 192 } 193 sort.Strings(allKeys) 194 195 for _, key := range allKeys { 196 newContext := (*in)[key] 197 oldContext := &Context{} 198 if err := s.Convert(newContext, oldContext, 0); err != nil { 199 return err 200 } 201 202 namedContext := NamedContext{key, *oldContext} 203 *out = append(*out, namedContext) 204 } 205 206 return nil 207 }, 208 func(in *[]NamedExtension, out *map[string]runtime.Object, s conversion.Scope) error { 209 for _, curr := range *in { 210 var newExtension runtime.Object 211 if err := s.Convert(&curr.Extension, &newExtension, 0); err != nil { 212 return err 213 } 214 if (*out)[curr.Name] == nil { 215 (*out)[curr.Name] = newExtension 216 } else { 217 return fmt.Errorf("error converting *[]NamedExtension into *map[string]runtime.Object: duplicate name \"%v\" in list: %v", curr.Name, *in) 218 } 219 } 220 221 return nil 222 }, 223 func(in *map[string]runtime.Object, out *[]NamedExtension, s conversion.Scope) error { 224 allKeys := make([]string, 0, len(*in)) 225 for key := range *in { 226 allKeys = append(allKeys, key) 227 } 228 sort.Strings(allKeys) 229 230 for _, key := range allKeys { 231 newExtension := (*in)[key] 232 oldExtension := &runtime.RawExtension{} 233 if err := s.Convert(newExtension, oldExtension, 0); err != nil { 234 return err 235 } 236 237 namedExtension := NamedExtension{key, *oldExtension} 238 *out = append(*out, namedExtension) 239 } 240 241 return nil 242 }, 243 ) 244} 245