fix(kube): generate k8s native scheme only once

Signed-off-by: Hidde Beydals <hello@hidde.co>
pull/7401/head
Hidde Beydals 6 years ago
parent b55224ebb9
commit e41184a585

@ -17,16 +17,20 @@ limitations under the License.
package kube // import "helm.sh/helm/v3/pkg/kube" package kube // import "helm.sh/helm/v3/pkg/kube"
import ( import (
"sync"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1" apiextensionsv1beta1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1beta1"
"k8s.io/apimachinery/pkg/api/meta" "k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/apimachinery/pkg/runtime/schema"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/cli-runtime/pkg/resource" "k8s.io/cli-runtime/pkg/resource"
"k8s.io/client-go/kubernetes/scheme" "k8s.io/client-go/kubernetes/scheme"
) )
var k8sNativeScheme *runtime.Scheme
var k8sNativeSchemeOnce sync.Once
// AsVersioned converts the given info into a runtime.Object with the correct // AsVersioned converts the given info into a runtime.Object with the correct
// group and version set // group and version set
func AsVersioned(info *resource.Info) runtime.Object { func AsVersioned(info *resource.Info) runtime.Object {
@ -53,11 +57,13 @@ func convertWithMapper(obj runtime.Object, mapping *meta.RESTMapping) runtime.Ob
// combination with e.g. a versioned kube client. If we would not do this, the client // combination with e.g. a versioned kube client. If we would not do this, the client
// may attempt to perform e.g. a 3-way-merge strategy patch for custom resources. // may attempt to perform e.g. a 3-way-merge strategy patch for custom resources.
func kubernetesNativeScheme() *runtime.Scheme { func kubernetesNativeScheme() *runtime.Scheme {
s := runtime.NewScheme() k8sNativeSchemeOnce.Do(func() {
utilruntime.Must(scheme.AddToScheme(s)) k8sNativeScheme = runtime.NewScheme()
// API extensions are not in the above scheme set, scheme.AddToScheme(k8sNativeScheme)
// and must thus be added separately. // API extensions are not in the above scheme set,
utilruntime.Must(apiextensionsv1beta1.AddToScheme(s)) // and must thus be added separately.
utilruntime.Must(apiextensionsv1.AddToScheme(s)) apiextensionsv1beta1.AddToScheme(k8sNativeScheme)
return s apiextensionsv1.AddToScheme(k8sNativeScheme)
})
return k8sNativeScheme
} }

Loading…
Cancel
Save