From 3026c1b634af842d1238fd98c3e3a6e1e3e93d51 Mon Sep 17 00:00:00 2001 From: Zhongcheng Lao Date: Sat, 31 Dec 2022 16:35:33 +0800 Subject: [PATCH 1/2] Allow to pass scheme when getting kubeclient Signed-off-by: Zhongcheng Lao --- runtime/client/impersonator.go | 36 ++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/runtime/client/impersonator.go b/runtime/client/impersonator.go index bed56882..aa725968 100644 --- a/runtime/client/impersonator.go +++ b/runtime/client/impersonator.go @@ -22,6 +22,7 @@ import ( corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/types" "k8s.io/client-go/rest" "k8s.io/client-go/tools/clientcmd" @@ -43,6 +44,7 @@ type Impersonator struct { defaultServiceAccount string serviceAccountName string namespace string + scheme *runtime.Scheme } // NewImpersonator creates an Impersonator from the given arguments. @@ -63,6 +65,30 @@ func NewImpersonator(kubeClient rc.Client, defaultServiceAccount: defaultServiceAccount, serviceAccountName: serviceAccountName, namespace: namespace, + scheme: kubeClient.Scheme(), + } +} + +// NewImpersonator creates an Impersonator from the given arguments. +func NewImpersonatorWithScheme(kubeClient rc.Client, + statusPoller *polling.StatusPoller, + pollingOpts polling.Options, + kubeConfigRef *meta.KubeConfigReference, + kubeConfigOpts KubeConfigOptions, + defaultServiceAccount string, + serviceAccountName string, + namespace string, + scheme *runtime.Scheme) *Impersonator { + return &Impersonator{ + Client: kubeClient, + statusPoller: statusPoller, + pollingOpts: pollingOpts, + kubeConfigRef: kubeConfigRef, + kubeConfigOpts: kubeConfigOpts, + defaultServiceAccount: defaultServiceAccount, + serviceAccountName: serviceAccountName, + namespace: namespace, + scheme: scheme, } } @@ -121,7 +147,10 @@ func (i *Impersonator) clientForServiceAccountOrDefault() (rc.Client, *polling.S return nil, nil, err } - client, err := rc.New(restConfig, rc.Options{Mapper: restMapper}) + client, err := rc.New(restConfig, rc.Options{ + Scheme: i.scheme, + Mapper: restMapper, + }) if err != nil { return nil, nil, err } @@ -150,7 +179,10 @@ func (i *Impersonator) clientForKubeConfig(ctx context.Context) (rc.Client, *pol return nil, nil, err } - client, err := rc.New(restConfig, rc.Options{Mapper: restMapper}) + client, err := rc.New(restConfig, rc.Options{ + Scheme: i.scheme, + Mapper: restMapper, + }) if err != nil { return nil, nil, err } From f58ab9e6c08c92edffbdc1a89e9b0a98236c572d Mon Sep 17 00:00:00 2001 From: Zhongcheng Lao Date: Mon, 9 Jan 2023 13:23:19 +0800 Subject: [PATCH 2/2] Address PR comments Signed-off-by: Zhongcheng Lao --- runtime/client/impersonator.go | 14 ++------------ 1 file changed, 2 insertions(+), 12 deletions(-) diff --git a/runtime/client/impersonator.go b/runtime/client/impersonator.go index aa725968..db3cf635 100644 --- a/runtime/client/impersonator.go +++ b/runtime/client/impersonator.go @@ -56,20 +56,10 @@ func NewImpersonator(kubeClient rc.Client, defaultServiceAccount string, serviceAccountName string, namespace string) *Impersonator { - return &Impersonator{ - Client: kubeClient, - statusPoller: statusPoller, - pollingOpts: pollingOpts, - kubeConfigRef: kubeConfigRef, - kubeConfigOpts: kubeConfigOpts, - defaultServiceAccount: defaultServiceAccount, - serviceAccountName: serviceAccountName, - namespace: namespace, - scheme: kubeClient.Scheme(), - } + return NewImpersonatorWithScheme(kubeClient, statusPoller, pollingOpts, kubeConfigRef, kubeConfigOpts, defaultServiceAccount, serviceAccountName, namespace, kubeClient.Scheme()) } -// NewImpersonator creates an Impersonator from the given arguments. +// NewImpersonatorWithScheme creates an Impersonator from the given arguments with a client runtime scheme. func NewImpersonatorWithScheme(kubeClient rc.Client, statusPoller *polling.StatusPoller, pollingOpts polling.Options,