Skip to content

Commit

Permalink
Vendor the lazy restmapper from controller-runtime v0.19
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <[email protected]>
  • Loading branch information
stefanprodan committed Feb 24, 2025
1 parent 00d1ceb commit 7c5e477
Show file tree
Hide file tree
Showing 3 changed files with 395 additions and 8 deletions.
4 changes: 2 additions & 2 deletions runtime/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/client-go/rest"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client/apiutil"

"github.com/fluxcd/cli-utils/pkg/flowcontrol"
)
Expand Down Expand Up @@ -95,7 +94,8 @@ func NewDynamicRESTMapper(restConfig *rest.Config) (meta.RESTMapper, error) {
if err != nil {
return nil, err
}
restMapper, err := apiutil.NewDynamicRESTMapper(restConfig, httpClient)

restMapper, err := NewLazyRESTMapper(restConfig, httpClient)
if err != nil {
return nil, err
}
Expand Down
30 changes: 24 additions & 6 deletions runtime/client/impersonator.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
// Impersonator holds the state for impersonating a Kubernetes account.
type Impersonator struct {
rc.Client
statusPoller *polling.StatusPoller
pollingOpts polling.Options
kubeConfigRef *meta.KubeConfigReference
kubeConfigOpts KubeConfigOptions
Expand All @@ -49,19 +48,17 @@ type Impersonator struct {

// NewImpersonator creates an Impersonator from the given arguments.
func NewImpersonator(kubeClient rc.Client,
statusPoller *polling.StatusPoller,
pollingOpts polling.Options,
kubeConfigRef *meta.KubeConfigReference,
kubeConfigOpts KubeConfigOptions,
defaultServiceAccount string,
serviceAccountName string,
namespace string) *Impersonator {
return NewImpersonatorWithScheme(kubeClient, statusPoller, pollingOpts, kubeConfigRef, kubeConfigOpts, defaultServiceAccount, serviceAccountName, namespace, kubeClient.Scheme())
return NewImpersonatorWithScheme(kubeClient, pollingOpts, kubeConfigRef, kubeConfigOpts, defaultServiceAccount, serviceAccountName, namespace, kubeClient.Scheme())
}

// NewImpersonatorWithScheme creates an Impersonator from the given arguments with a client runtime scheme.
func NewImpersonatorWithScheme(kubeClient rc.Client,
statusPoller *polling.StatusPoller,
pollingOpts polling.Options,
kubeConfigRef *meta.KubeConfigReference,
kubeConfigOpts KubeConfigOptions,
Expand All @@ -71,7 +68,6 @@ func NewImpersonatorWithScheme(kubeClient rc.Client,
scheme *runtime.Scheme) *Impersonator {
return &Impersonator{
Client: kubeClient,
statusPoller: statusPoller,
pollingOpts: pollingOpts,
kubeConfigRef: kubeConfigRef,
kubeConfigOpts: kubeConfigOpts,
Expand All @@ -94,7 +90,7 @@ func (i *Impersonator) GetClient(ctx context.Context) (rc.Client, *polling.Statu
case i.defaultServiceAccount != "" || i.serviceAccountName != "":
return i.clientForServiceAccountOrDefault()
default:
return i.Client, i.statusPoller, nil
return i.defaultClient()
}
}

Expand Down Expand Up @@ -147,7 +143,29 @@ func (i *Impersonator) clientForServiceAccountOrDefault() (rc.Client, *polling.S

statusPoller := polling.NewStatusPoller(client, restMapper, i.pollingOpts)
return client, statusPoller, err
}

func (i *Impersonator) defaultClient() (rc.Client, *polling.StatusPoller, error) {
restConfig, err := config.GetConfig()
if err != nil {
return nil, nil, err
}

restMapper, err := NewDynamicRESTMapper(restConfig)
if err != nil {
return nil, nil, err
}

client, err := rc.New(restConfig, rc.Options{
Scheme: i.scheme,
Mapper: restMapper,
})
if err != nil {
return nil, nil, err
}

statusPoller := polling.NewStatusPoller(client, restMapper, i.pollingOpts)
return client, statusPoller, err
}

func (i *Impersonator) clientForKubeConfig(ctx context.Context) (rc.Client, *polling.StatusPoller, error) {
Expand Down
Loading

0 comments on commit 7c5e477

Please sign in to comment.