Skip to content

feat(lifecycle): expose ClientForCluster and RestConfigForCluster in public pkg - #2276

Draft
Zaggy21 wants to merge 11 commits into
mainfrom
feat/consolidate-clientutil-into-shared-lifecycle-pkg
Draft

Zaggy21 wants to merge 11 commits into
mainfrom
feat/consolidate-clientutil-into-shared-lifecycle-pkg

Conversation

@Zaggy21

@Zaggy21 Zaggy21 commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

Description

  • Adds ClientForCluster and RestConfigForCluster to pkg/lifecycle/ as the public API for building clients and rest configs for remote Greenhouse clusters
  • Removes clientutil.NewK8sClientFromCluster and migrates all internal callers to the new public functions
  • Migrates proxy package from NewRemoteKubeCfg to RestConfigForCluster

What type of PR is this? (check all applicable)

  • 🍕 Feature
  • 🐛 Bug Fix
  • 📝 Documentation Update
  • 🎨 Style
  • 🧑‍💻 Code Refactor
  • 🔥 Performance Improvements
  • ✅ Test
  • 🤖 Build
  • 🔁 CI
  • 📦 Chore (Release)
  • ⏩ Revert

Related Tickets & Documents

Added tests?

  • 👍 yes
  • 🙅 no, because they aren't needed
  • 🙋 no, because I need help
  • Separate ticket for tests # (issue/pr)

Existing unit tests in pkg/lifecycle/ cover the underlying NewRemoteKubeCfg logic. Controller tests in cluster and teamrbac use the new ClientForCluster.

Added to documentation?

  • 📜 README.md
  • 🤝 Documentation pages updated
  • 🙅 no documentation needed
  • (if applicable) generated OpenAPI docs for CRD changes

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • My changes generate no new warnings
  • New and existing unit tests pass locally with my changes

…lic pkg

On-behalf-of: @SAP krzysztof.zagorski@sap.com
Signed-off-by: Zaggy21 <k.zaggy@gmail.com>
…ForCluster

On-behalf-of: @SAP krzysztof.zagorski@sap.com
Signed-off-by: Zaggy21 <k.zaggy@gmail.com>
On-behalf-of: @SAP krzysztof.zagorski@sap.com
Signed-off-by: Zaggy21 <k.zaggy@gmail.com>
@Zaggy21 Zaggy21 linked an issue Oct 27, 2026 that may be closed by this pull request
3 tasks
…eme requirement

On-behalf-of: @SAP krzysztof.zagorski@sap.com
Signed-off-by: Zaggy21 <k.zaggy@gmail.com>
@Zaggy21
Zaggy21 marked this pull request as ready for review October 28, 2026 15:17
@Zaggy21
Zaggy21 requested a review from a team as a code owner October 28, 2026 15:17
Copilot AI balanced review requested due to automatic review settings October 28, 2026 15:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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 ClientForCluster with caller-provided scheme registration and RestConfigForCluster.
  • Migrates controller, proxy, and existing test call sites to the public helpers.
  • Removes the internal NewK8sClientFromCluster helper.
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

jellonek
jellonek previously approved these changes Oct 28, 2026

@abhijith-darshan abhijith-darshan left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the consolidation needs more work since the usage is spread across e2es, tests, webhooks and controllers

  • clientutil.CreateOrPatch needs to be replaced with controllerutil.CreateOrPatch
  • internal/clientutil/client_getter.go can be removed completely and a single getter func can be exposed since the rest config needed is already in lifecycle pkg with validation.
  • KnownSchemes in internal/clientutil/client.go can also be moved to lifecycle
  • internal/clientutil/delete.go is 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.

Comment thread pkg/lifecycle/client.go Outdated
Comment on lines +27 to +30
// 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)
}

@abhijith-darshan abhijith-darshan Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we can skip this indirect since the NewRemoteKubeCfg func is exposed and has the same signature.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed the alias and renamed the other function from ClientForCluster to NewRemoteClient to match the style of NewRemoteKubeCfg.

@Zaggy21
Zaggy21 marked this pull request as draft October 29, 2026 10:55
… RestConfigForCluster alias

On-behalf-of: @SAP krzysztof.zagorski@sap.com
Signed-off-by: Zaggy21 <k.zaggy@gmail.com>
…g/lifecycle

On-behalf-of: @SAP krzysztof.zagorski@sap.com
Signed-off-by: Zaggy21 <k.zaggy@gmail.com>
On-behalf-of: @SAP krzysztof.zagorski@sap.com
Signed-off-by: Zaggy21 <k.zaggy@gmail.com>
@github-actions github-actions Bot added size/L and removed size/M labels Oct 29, 2026
…OrPatch

On-behalf-of: @SAP krzysztof.zagorski@sap.com
Signed-off-by: Zaggy21 <k.zaggy@gmail.com>
On-behalf-of: @SAP krzysztof.zagorski@sap.com
Signed-off-by: Zaggy21 <k.zaggy@gmail.com>
Comment thread pkg/lifecycle/client.go
Comment on lines +25 to +55
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})
}

@abhijith-darshan abhijith-darshan Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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})
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remote clusters do not need greenhouse / dex / flux schemes just the well-known schemes is enough

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEAT] - consolidate clientutil pkg into shared lifecycle pkg

4 participants