Update status check endpoint 
[multicloud/k8s.git] / src / k8splugin / internal / statuscheck / converter.go
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 statuscheck // import "helm.sh/helm/v3/pkg/kube"
18
19 import (
20         "sync"
21
22         apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
23         apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
24         "k8s.io/apimachinery/pkg/api/meta"
25         "k8s.io/apimachinery/pkg/runtime"
26         "k8s.io/apimachinery/pkg/runtime/schema"
27         "k8s.io/cli-runtime/pkg/resource"
28         "k8s.io/client-go/kubernetes/scheme"
29 )
30
31 var k8sNativeScheme *runtime.Scheme
32 var k8sNativeSchemeOnce sync.Once
33
34 // AsVersioned converts the given info into a runtime.Object with the correct
35 // group and version set
36 func AsVersioned(info *resource.Info) runtime.Object {
37         return convertWithMapper(info.Object, info.Mapping)
38 }
39
40 // convertWithMapper converts the given object with the optional provided
41 // RESTMapping. If no mapping is provided, the default schema versioner is used
42 func convertWithMapper(obj runtime.Object, mapping *meta.RESTMapping) runtime.Object {
43         s := kubernetesNativeScheme()
44         var gv = runtime.GroupVersioner(schema.GroupVersions(s.PrioritizedVersionsAllGroups()))
45         if mapping != nil {
46                 gv = mapping.GroupVersionKind.GroupVersion()
47         }
48         if obj, err := runtime.ObjectConvertor(s).ConvertToVersion(obj, gv); err == nil {
49                 return obj
50         }
51         return obj
52 }
53
54 // kubernetesNativeScheme returns a clean *runtime.Scheme with _only_ Kubernetes
55 // native resources added to it. This is required to break free of custom resources
56 // that may have been added to scheme.Scheme due to Helm being used as a package in
57 // combination with e.g. a versioned kube client. If we would not do this, the client
58 // may attempt to perform e.g. a 3-way-merge strategy patch for custom resources.
59 func kubernetesNativeScheme() *runtime.Scheme {
60         k8sNativeSchemeOnce.Do(func() {
61                 k8sNativeScheme = runtime.NewScheme()
62                 scheme.AddToScheme(k8sNativeScheme)
63                 // API extensions are not in the above scheme set,
64                 // and must thus be added separately.
65                 apiextensionsv1beta1.AddToScheme(k8sNativeScheme)
66                 apiextensionsv1.AddToScheme(k8sNativeScheme)
67         })
68         return k8sNativeScheme
69 }