Skip to content

Commit

Permalink
actionConfigGetter: allow custom mapping from object to rest.Config
Browse files Browse the repository at this point in the history
Signed-off-by: Joe Lanford <joe.lanford@gmail.com>
  • Loading branch information
joelanford committed Apr 10, 2024
1 parent cfc667c commit 224a2f4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 63 deletions.
7 changes: 4 additions & 3 deletions pkg/client/actionclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package client

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
Expand All @@ -40,7 +41,7 @@ import (
)

type ActionClientGetter interface {
ActionClientFor(obj client.Object) (ActionInterface, error)
ActionClientFor(ctx context.Context, obj client.Object) (ActionInterface, error)
}

type ActionClientGetterFunc func(obj client.Object) (ActionInterface, error)
Expand Down Expand Up @@ -140,8 +141,8 @@ type actionClientGetter struct {

var _ ActionClientGetter = &actionClientGetter{}

func (hcg *actionClientGetter) ActionClientFor(obj client.Object) (ActionInterface, error) {
actionConfig, err := hcg.acg.ActionConfigFor(obj)
func (hcg *actionClientGetter) ActionClientFor(ctx context.Context, obj client.Object) (ActionInterface, error) {
actionConfig, err := hcg.acg.ActionConfigFor(ctx, obj)
if err != nil {
return nil, err
}
Expand Down
104 changes: 65 additions & 39 deletions pkg/client/actionconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,38 +28,29 @@ import (
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/discovery"
"k8s.io/client-go/discovery/cached/memory"
v1 "k8s.io/client-go/kubernetes/typed/core/v1"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
)

type ActionConfigGetter interface {
ActionConfigFor(obj client.Object) (*action.Configuration, error)
ActionConfigFor(ctx context.Context, obj client.Object) (*action.Configuration, error)
}

func NewActionConfigGetter(cfg *rest.Config, rm meta.RESTMapper, log logr.Logger, opts ...ActionConfigGetterOption) (ActionConfigGetter, error) {
rcg := newRESTClientGetter(cfg, rm, "")
// Setup the debug log function that Helm will use
debugLog := func(format string, v ...interface{}) {
if log.GetSink() != nil {
log.V(1).Info(fmt.Sprintf(format, v...))
}
}

kc := kube.New(rcg)
kc.Log = debugLog

kcs, err := kc.Factory.KubernetesClientSet()
func NewActionConfigGetter(baseRestConfig *rest.Config, rm meta.RESTMapper, opts ...ActionConfigGetterOption) (ActionConfigGetter, error) {
anonymousCfg := rest.AnonymousClientConfig(baseRestConfig)
dc, err := discovery.NewDiscoveryClientForConfig(anonymousCfg)
if err != nil {
return nil, fmt.Errorf("creating kubernetes client set: %w", err)
return nil, fmt.Errorf("create discovery client: %v", err)
}
cdc := memory.NewMemCacheClient(dc)

acg := &actionConfigGetter{
kubeClient: kc,
kubeClientSet: kcs,
debugLog: debugLog,
restClientGetter: rcg.restClientGetter,
baseRestConfig: baseRestConfig,
restMapper: rm,
discoveryClient: cdc,
}
for _, o := range opts {
o(acg)
Expand All @@ -70,6 +61,11 @@ func NewActionConfigGetter(cfg *rest.Config, rm meta.RESTMapper, log logr.Logger
if acg.objectToStorageNamespace == nil {
acg.objectToStorageNamespace = getObjectNamespace
}
if acg.objectToRestConfig == nil {
acg.objectToRestConfig = func(_ context.Context, _ client.Object, baseRestConfig *rest.Config) (*rest.Config, error) {
return rest.CopyConfig(baseRestConfig), nil
}
}
return acg, nil
}

Expand Down Expand Up @@ -97,28 +93,56 @@ func DisableStorageOwnerRefInjection(v bool) ActionConfigGetterOption {
}
}

func RestConfigMapper(f func(context.Context, client.Object, *rest.Config) (*rest.Config, error)) ActionConfigGetterOption {
return func(getter *actionConfigGetter) {
getter.objectToRestConfig = f
}
}

func getObjectNamespace(obj client.Object) (string, error) {
return obj.GetNamespace(), nil
}

type actionConfigGetter struct {
kubeClient *kube.Client
kubeClientSet kubernetes.Interface
debugLog func(string, ...interface{})
restClientGetter *restClientGetter
baseRestConfig *rest.Config
restMapper meta.RESTMapper
discoveryClient discovery.CachedDiscoveryInterface

objectToClientNamespace ObjectToStringMapper
objectToStorageNamespace ObjectToStringMapper
objectToRestConfig func(context.Context, client.Object, *rest.Config) (*rest.Config, error)
disableStorageOwnerRefInjection bool
}

func (acg *actionConfigGetter) ActionConfigFor(obj client.Object) (*action.Configuration, error) {
func (acg *actionConfigGetter) ActionConfigFor(ctx context.Context, obj client.Object) (*action.Configuration, error) {
storageNs, err := acg.objectToStorageNamespace(obj)
if err != nil {
return nil, fmt.Errorf("get storage namespace from object: %v", err)
return nil, fmt.Errorf("get storage namespace for object: %v", err)
}

restConfig, err := acg.objectToRestConfig(ctx, obj, acg.baseRestConfig)
if err != nil {
return nil, fmt.Errorf("get rest config for object: %v", err)
}

clientNamespace, err := acg.objectToClientNamespace(obj)
if err != nil {
return nil, fmt.Errorf("get client namespace for object: %v", err)
}

secretClient := acg.kubeClientSet.CoreV1().Secrets(storageNs)
rcg := newRESTClientGetter(restConfig, acg.restMapper, acg.discoveryClient, clientNamespace)
kc := kube.New(rcg)
kc.Namespace = clientNamespace

kcs, err := kc.Factory.KubernetesClientSet()
if err != nil {
return nil, fmt.Errorf("create kubernetes clientset: %v", err)
}

// Setup the debug log function that Helm will use
debugLog := getDebugLogger(ctx)

secretClient := kcs.CoreV1().Secrets(storageNs)
if !acg.disableStorageOwnerRefInjection {
ownerRef := metav1.NewControllerRef(obj, obj.GetObjectKind().GroupVersionKind())
secretClient = &ownerRefSecretClient{
Expand All @@ -127,27 +151,29 @@ func (acg *actionConfigGetter) ActionConfigFor(obj client.Object) (*action.Confi
}
}
d := driver.NewSecrets(secretClient)

// Also, use the debug log for the storage driver
d.Log = acg.debugLog
d.Log = debugLog

// Initialize the storage backend
s := storage.Init(d)

kubeClient := *acg.kubeClient
kubeClient.Namespace, err = acg.objectToClientNamespace(obj)
if err != nil {
return nil, fmt.Errorf("get client namespace from object: %v", err)
}

return &action.Configuration{
RESTClientGetter: acg.restClientGetter.ForNamespace(kubeClient.Namespace),
RESTClientGetter: rcg,
Releases: s,
KubeClient: &kubeClient,
Log: acg.debugLog,
KubeClient: kc,
Log: debugLog,
}, nil
}

func getDebugLogger(ctx context.Context) func(format string, v ...interface{}) {
logger, err := logr.FromContext(ctx)
if err != nil {
return func(format string, v ...interface{}) {}
}
return func(format string, v ...interface{}) {
logger.V(1).Info(fmt.Sprintf(format, v...))
}
}

var _ v1.SecretInterface = &ownerRefSecretClient{}

type ownerRefSecretClient struct {
Expand Down
29 changes: 8 additions & 21 deletions pkg/client/restclientgetter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,26 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/discovery"
cached "k8s.io/client-go/discovery/cached"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
)

func newRESTClientGetter(cfg *rest.Config, rm meta.RESTMapper, ns string) *namespacedRCG {
func newRESTClientGetter(cfg *rest.Config, rm meta.RESTMapper, discoveryClient discovery.CachedDiscoveryInterface, ns string) *namespacedRCG {
return &namespacedRCG{
restClientGetter: &restClientGetter{
restConfig: cfg,
restMapper: rm,
restConfig: cfg,
restMapper: rm,
discoveryClient: discoveryClient,
},
namespaceConfig: namespaceClientConfig{ns},
}
}

type restClientGetter struct {
restConfig *rest.Config
restMapper meta.RESTMapper
restConfig *rest.Config
restMapper meta.RESTMapper
discoveryClient discovery.CachedDiscoveryInterface

setupDiscoveryClient sync.Once
cachedDiscoveryClient discovery.CachedDiscoveryInterface
Expand All @@ -51,21 +52,7 @@ func (c *restClientGetter) ToRESTConfig() (*rest.Config, error) {
}

func (c *restClientGetter) ToDiscoveryClient() (discovery.CachedDiscoveryInterface, error) {
var (
dc discovery.DiscoveryInterface
err error
)
c.setupDiscoveryClient.Do(func() {
dc, err = discovery.NewDiscoveryClientForConfig(c.restConfig)
if err != nil {
return
}
c.cachedDiscoveryClient = cached.NewMemCacheClient(dc)
})
if err != nil {
return nil, err
}
return c.cachedDiscoveryClient, nil
return c.discoveryClient, nil
}

func (c *restClientGetter) ToRESTMapper() (meta.RESTMapper, error) {
Expand Down

0 comments on commit 224a2f4

Please sign in to comment.