github.com/uhthomas/helm@v3.0.0-beta.3+incompatible/pkg/kube/converter.go (about) 1 /* 2 Copyright The Helm 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 kube // import "helm.sh/helm/pkg/kube" 18 19 import ( 20 "k8s.io/apimachinery/pkg/api/meta" 21 "k8s.io/apimachinery/pkg/runtime" 22 "k8s.io/apimachinery/pkg/runtime/schema" 23 "k8s.io/cli-runtime/pkg/resource" 24 "k8s.io/client-go/kubernetes/scheme" 25 ) 26 27 // AsVersioned converts the given info into a runtime.Object with the correct 28 // group and version set 29 func AsVersioned(info *resource.Info) runtime.Object { 30 return convertWithMapper(info.Object, info.Mapping) 31 } 32 33 // convertWithMapper converts the given object with the optional provided 34 // RESTMapping. If no mapping is provided, the default schema versioner is used 35 func convertWithMapper(obj runtime.Object, mapping *meta.RESTMapping) runtime.Object { 36 var gv = runtime.GroupVersioner(schema.GroupVersions(scheme.Scheme.PrioritizedVersionsAllGroups())) 37 if mapping != nil { 38 gv = mapping.GroupVersionKind.GroupVersion() 39 } 40 if obj, err := runtime.ObjectConvertor(scheme.Scheme).ConvertToVersion(obj, gv); err == nil { 41 return obj 42 } 43 return obj 44 }