Repository navigation
Conversation
…eme requirement On-behalf-of: @SAP krzysztof.zagorski@sap.com Signed-off-by: Zaggy21 <k.zaggy@gmail.com>
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the change exposing public lifecycle connectivity helpers and migrating existing callers.
No open issues 🎉
What changed in this PR
Exposes remote-cluster client and REST configuration helpers through the public pkg/lifecycle package, allowing extensions to reuse Greenhouse’s connectivity handling as requested in #2211.
Changes:
- Adds
ClientForClusterwith caller-provided scheme registration andRestConfigForCluster. - Migrates controller, proxy, and existing test call sites to the public helpers.
- Removes the internal
NewK8sClientFromClusterhelper.
| File | Description |
|---|---|
pkg/lifecycle/client.go |
Adds public client and REST configuration helpers. |
internal/proxy/workload_identity.go |
Uses the public REST configuration helper for workload identity. |
internal/proxy/proxy.go |
Uses the public REST configuration helper for kubeconfig transport. |
internal/controller/teamrbac/teamrolebinding_controller.go |
Migrates remote-client creation while preserving known schemes. |
internal/controller/cluster/cluster_controller_test.go |
Updates the existing test to use ClientForCluster. |
internal/clientutil/client.go |
Removes the superseded internal cluster-client helper. |
Review effort: Balanced
abhijith-darshan
left a comment
There was a problem hiding this comment.
the consolidation needs more work since the usage is spread across e2es, tests, webhooks and controllers
clientutil.CreateOrPatchneeds to be replaced withcontrollerutil.CreateOrPatchinternal/clientutil/client_getter.gocan be removed completely and a single getter func can be exposed since the rest config needed is already in lifecycle pkg with validation.KnownSchemesininternal/clientutil/client.gocan also be moved to lifecycleinternal/clientutil/delete.gois only used in trb controller and preset test in one place... so we can drop this whole usae.
So a good start would be to move from internal/clientutil to pkg/clientutil
and start dropping unwanted methods especially the getters are no longer needed in my opinion since we have moved to flux. The real usage is actually in only one place for mirroring images and simple getter is enough.
| // RestConfigForCluster returns a rest.Config for the given remote cluster. | ||
| func RestConfigForCluster(ctx context.Context, kubeClient client.Client, cluster *greenhouseapisv1alpha1.Cluster) (*rest.Config, error) { | ||
| return NewRemoteKubeCfg(ctx, kubeClient, cluster) | ||
| } |
There was a problem hiding this comment.
we can skip this indirect since the NewRemoteKubeCfg func is exposed and has the same signature.
There was a problem hiding this comment.
I've removed the alias and renamed the other function from ClientForCluster to NewRemoteClient to match the style of NewRemoteKubeCfg.
… RestConfigForCluster alias On-behalf-of: @SAP krzysztof.zagorski@sap.com Signed-off-by: Zaggy21 <k.zaggy@gmail.com>
| var KnownSchemes = []func(*runtime.Scheme) error{ | ||
| clientgoscheme.AddToScheme, | ||
| apiextensionsv1.AddToScheme, | ||
| greenhouseapisv1alpha1.AddToScheme, | ||
| greenhouseapisv1alpha2.AddToScheme, | ||
| dexapi.AddToScheme, | ||
| sourcev1.AddToScheme, | ||
| helmv2.AddToScheme, | ||
| kustomizev1.AddToScheme, | ||
| sourcev2.AddToScheme, | ||
| } | ||
|
|
||
| // NewRemoteClient returns a controller-runtime client for the given remote cluster. | ||
| // Callers must provide at least one addToScheme function; an empty scheme will cause resource operations to fail. | ||
| func NewRemoteClient(ctx context.Context, kubeClient client.Client, cluster *greenhouseapisv1alpha1.Cluster, addToScheme ...func(*runtime.Scheme) error) (client.Client, error) { | ||
| cfg, err := NewRemoteKubeCfg(ctx, kubeClient, cluster) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return clientFromConfig(cfg, addToScheme...) | ||
| } | ||
|
|
||
| func clientFromConfig(cfg *rest.Config, addToScheme ...func(*runtime.Scheme) error) (client.Client, error) { | ||
| sch := runtime.NewScheme() | ||
| for _, fn := range addToScheme { | ||
| if err := fn(sch); err != nil { | ||
| return nil, fmt.Errorf("scheme registration: %w", err) | ||
| } | ||
| } | ||
| return client.New(cfg, client.Options{Scheme: sch}) | ||
| } |
There was a problem hiding this comment.
| var KnownSchemes = []func(*runtime.Scheme) error{ | |
| clientgoscheme.AddToScheme, | |
| apiextensionsv1.AddToScheme, | |
| greenhouseapisv1alpha1.AddToScheme, | |
| greenhouseapisv1alpha2.AddToScheme, | |
| dexapi.AddToScheme, | |
| sourcev1.AddToScheme, | |
| helmv2.AddToScheme, | |
| kustomizev1.AddToScheme, | |
| sourcev2.AddToScheme, | |
| } | |
| // NewRemoteClient returns a controller-runtime client for the given remote cluster. | |
| // Callers must provide at least one addToScheme function; an empty scheme will cause resource operations to fail. | |
| func NewRemoteClient(ctx context.Context, kubeClient client.Client, cluster *greenhouseapisv1alpha1.Cluster, addToScheme ...func(*runtime.Scheme) error) (client.Client, error) { | |
| cfg, err := NewRemoteKubeCfg(ctx, kubeClient, cluster) | |
| if err != nil { | |
| return nil, err | |
| } | |
| return clientFromConfig(cfg, addToScheme...) | |
| } | |
| func clientFromConfig(cfg *rest.Config, addToScheme ...func(*runtime.Scheme) error) (client.Client, error) { | |
| sch := runtime.NewScheme() | |
| for _, fn := range addToScheme { | |
| if err := fn(sch); err != nil { | |
| return nil, fmt.Errorf("scheme registration: %w", err) | |
| } | |
| } | |
| return client.New(cfg, client.Options{Scheme: sch}) | |
| } | |
| var knownSchemes = []func(*runtime.Scheme) error{ | |
| clientgoscheme.AddToScheme, | |
| apiextensionsv1.AddToScheme, | |
| } | |
| // NewRemoteClient returns a controller-runtime client for the given remote cluster. | |
| func NewRemoteClient(ctx context.Context, kubeClient client.Client, cluster *greenhouseapisv1alpha1.Cluster) (client.Client, error) { | |
| cfg, err := NewRemoteKubeCfg(ctx, kubeClient, cluster) | |
| if err != nil { | |
| return nil, err | |
| } | |
| sch := runtime.NewScheme() | |
| for _, addScheme := range knownSchemes { | |
| if err := addScheme(sch); err != nil { | |
| return nil, fmt.Errorf("scheme registration: %w", err) | |
| } | |
| } | |
| return client.New(cfg, client.Options{Scheme: sch}) | |
| } |
There was a problem hiding this comment.
remote clusters do not need greenhouse / dex / flux schemes just the well-known schemes is enough
Description
ClientForClusterandRestConfigForClustertopkg/lifecycle/as the public API for building clients and rest configs for remote Greenhouse clustersclientutil.NewK8sClientFromClusterand migrates all internal callers to the new public functionsNewRemoteKubeCfgtoRestConfigForClusterWhat type of PR is this? (check all applicable)
Related Tickets & Documents
Added tests?
Existing unit tests in
pkg/lifecycle/cover the underlyingNewRemoteKubeCfglogic. Controller tests inclusterandteamrbacuse the newClientForCluster.Added to documentation?
Checklist