Skip to content

feat(sync): zero-flag usage via kubeconfig context and connection header - #89

Merged
onuryilmaz merged 5 commits into
mainfrom
feat/sync-simplify-79
Nov 14, 2026
Merged

onuryilmaz merged 5 commits into
mainfrom
feat/sync-simplify-79

Conversation

@onuryilmaz

Copy link
Copy Markdown
Contributor

Closes #79.

Summary

  • Namespace auto-detection: --greenhouse-cluster-namespace is no longer required. Resolution order: explicit flag → ~/.cloudctl.yaml → namespace of the active kubeconfig context (org name == namespace set by bootstrap). Running cloudctl sync with no flags just works after bootstrap.
  • Write-target fallback: When neither --remote-cluster-kubeconfig nor KUBECONFIG is set, the write target falls back to ~/.kube/config, mirroring kubectl behaviour.
  • Connection info header: Every sync now prints a one-line summary (kubeconfig:, context:, namespace:, and optionally writing: when the write target differs) before the cluster list, making it immediately clear which Greenhouse org was synced and where the result was written.
  • Crash fix for --include-not-ready: mergeKubeconfig previously returned a hard error when a ClusterKubeconfig had contexts but no matching authinfos in its spec (e.g. not-ready clusters). These are now skipped with a debug log instead of failing the whole sync.

Internal cleanups

  • resolveDisplayPath delegates to resolveWriteTarget (single path-resolution logic, no duplication).
  • resolveDisplayContext + namespaceFromKubeconfig merged into loadKubeconfigContext (one kubeconfig load instead of two).
  • conn is built after the file-existence check so error messages always show the real context name.

Test plan

  • make test passes (ok cmd, ok cmd/output)
  • cloudctl sync (no flags) works when the active context has a namespace set
  • cloudctl sync --include-not-ready no longer crashes on orgs with not-ready clusters
  • Connection header appears at top of sync output with correct kubeconfig path and context name
  • writing: line appears only when the write target differs from the kubeconfig source

When --include-not-ready is set, some ClusterKubeconfigs have contexts
in their spec but no corresponding authinfo (e.g. the secret is absent
so only partial spec data is present). mergeKubeconfig was returning a
fatal error for these, failing the entire sync.

Skip such contexts with a debug-level log instead. The authinfo check
is applied to both the mergeIdenticalUsers=true and =false paths.

Signed-off-by: onuryilmaz <onur.yilmaz@sap.com>
Closes #79.

--greenhouse-cluster-namespace no longer required as a flag. Resolution
order: explicit flag → ~/.cloudctl.yaml → namespace of the active
Greenhouse context (org name == namespace set by bootstrap).

When no context is specified, the current-context from the kubeconfig is
used, mirroring kubectl behaviour. Write target defaults to ~/.kube/config
when neither --remote-cluster-kubeconfig nor KUBECONFIG is set, so a plain
`cloudctl sync` works out of the box after bootstrap.

Every sync now prints a one-line connection summary (kubeconfig path,
context, namespace) before the cluster list, making it immediately clear
which Greenhouse org was synced and where the result was written.

Internal cleanups:
- resolveDisplayPath delegates to resolveWriteTarget (single path-resolution logic)
- resolveDisplayContext + namespaceFromKubeconfig merged into loadKubeconfigContext
  (single kubeconfig load instead of two)
- conn built after file-existence check so error messages always show
  the real context name instead of "(unknown)"

Signed-off-by: onuryilmaz <onur.yilmaz@sap.com>
@onuryilmaz
onuryilmaz requested review from a team as code owners November 13, 2026 19:35
Copilot AI balanced review requested due to automatic review settings November 13, 2026 19:35

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

With deduplication enabled, sync can delete existing managed credentials and contexts when incoming authinfo is missing, then persist those deletions and report success.

3 open issues
What changed in this PR

This PR simplifies cloudctl’s Greenhouse kubeconfig sync workflow, supporting zero-flag usage through persisted defaults and namespace inference from the selected context.

Changes:

  • Adds namespace auto-detection, setup guidance, and a default write target of ~/.kube/config.
  • Adds connection details to sync output and clearer configuration errors.
  • Changes missing-authinfo handling to skip affected contexts and adds tests for fallback behavior.
File Description
cmd/​sync.go Adds connection resolution, output metadata, write fallback, and missing-authinfo handling.
cmd/​sync_test.go Tests default write-target selection and context/namespace lookup; adds namespace-error assertions.
cmd/​root.go Includes configuration file paths in read errors.
cmd/​output/​types.go Adds connection and write-target fields to sync results.
cmd/​output/​plain_printer.go Renders connection details in plain-text sync output.
cmd/​output/​interactive_printer.go Renders styled connection details in interactive sync output.

Review effort: Balanced

Comment thread cmd/sync.go Resolved
Comment thread cmd/sync.go Resolved
Comment thread cmd/sync.go Resolved
resolveDisplayPath reported only the first file from a multi-file
KUBECONFIG, so the header could point to the wrong source when the
Greenhouse context lived in a later file.

Replace with resolveDisplaySources, which returns the full
colon-separated KUBECONFIG list verbatim.  The write target (first
entry, kubectl convention) is resolved separately and shown on the
"writing:" line only when it is not already represented in the source
list (checked via isWriteTargetInSources).

Signed-off-by: onuryilmaz <onur.yilmaz@sap.com>
When a ClusterKubeconfig had contexts but no matching authinfo (e.g.
a partially-ready cluster under --include-not-ready), the context was
already skipped in the merge loop. However the cleanup pass then saw
the context's authinfo as unmapped and deleted the existing local
managed context, leaving the kubeconfig in a worse state than before.

Track skipped server-side contexts in skippedServerContexts and check
this set in the cleanup pass before deleting a context as "unmapped
authinfo", preserving the existing local entry instead.

Also expand leading "~/" in kubeconfig paths read from ~/.cloudctl.yaml
(shell tilde-expansion does not apply inside config files). Add
expandTilde helper in root.go; update the help example to note that
tilde is supported.

Signed-off-by: onuryilmaz <onur.yilmaz@sap.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Skipped contexts can lose working access when cleanup deletes their credentials or clusters, and dry-run output still omits the selected context and namespace.

1 open issue
1 previously missed issue

In code that hasn't changed since last review

Medium severity Render connection metadata in sync dry-run output

cmd/​sync.go:196

The connection details are attached only to SyncResult, but sync --dry-run returns a separate SyncDryRunResult before reaching the final result construction. Neither dry-run printer renders this header, so previewing a zero-flag sync still gives no indication of the selected context or namespace. Carry the connection metadata into the dry-run result and render it in both text printers as well.

3 resolved since last review

Review effort: Balanced

Comment thread cmd/sync.go Resolved
The previous fix preserved the local managed context when its incoming
authinfo was missing, but the authinfo and cluster referenced by that
context were still deleted by the cleanup passes.

Introduce preservedAuthInfos and preservedClusters sets alongside
skippedServerContexts. When a context is skipped, read its current
local managed authinfo and cluster names and add them to the respective
sets. The cluster and authinfo cleanup loops skip any entry present in
these sets, keeping the full kubeconfig entry intact across syncs.

Signed-off-by: onuryilmaz <onur.yilmaz@sap.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

With --merge-identical-users=false, the final reference check can still delete an existing context whose incoming replacement was skipped because of a missing user.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Preserve skipped contexts when merging is disabled

cmd/​sync.go:962

The preservation check still runs only when mergeIdenticalUsers is true. With --merge-identical-users=false, if an existing context references cloudctl:old-user and the incomplete incoming context references a missing new-user, insertion is skipped and its dependencies are preserved, but the final mismatched-reference check (lines 978–980) deletes the context anyway. Check skippedServerContexts before either mode's reference validation so skipped contexts survive in both modes.

1 resolved since last review

Review effort: Balanced

@onuryilmaz
onuryilmaz merged commit d445ec5 into main Nov 14, 2026
8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEAT cloudctl] - Simplify sync command with persisted Greenhouse connection config

2 participants