You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Reduce code duplication across rpkg CLI commands (#541)
* Reduce code duplication across rpkg CLI commands
Extract shared helpers to pkg/cli/commands/rpkg/util/common.go and apply
them across the seven rpkg lifecycle commands (approve, propose,
proposedelete, reject, del, pull, push):
- InitClient: wraps cliutils.CreateClientWithFlags with namespace flag
validation. Previously only the get command checked for an empty
--namespace flag; this now applies to approve, propose,
proposedelete, reject and del as well. Also rejects a nil cfg up
front so callers cannot trigger a panic inside ToRESTConfig.
- CreateScheme: consolidates the identical local createScheme()
function that was duplicated in both pull and push, registering
porchapi, porchconfig, corev1 and metav1 so callers see the same
kinds as cliutils.CreateClientWithFlags.
- MakePreRunE: returns a cobra PreRunE closure that validates
namespace and creates the client. Eliminates duplicate preRunE
methods from approve, propose, proposedelete and del.
- RunForEachPackage: extracts the retry-with-error-collection loop
shared by approve, propose, proposedelete and reject. Each command
now provides only its lifecycle-specific logic as a callback. The
lastErr workaround for k8s retry library behaviour is included in
the shared helper. Per-iteration body is further split into
fetchAndAct and reportResult helpers for clarity. Behavioural
options are grouped into RunForEachOpts; CmdName is required and
rejected up front if empty.
- Runner struct + NewTestRunner: shared fields (Ctx, Cfg, Client,
Command) embedded into each command's runner. NewTestRunner builds
a Runner pre-wired for table-driven CLI tests. Ctx is intentionally
stored on the struct, matching the existing per-command runner
convention (see SonarQube godre:S8242 -- documented rather than
refactored).
The seven lifecycle command files (approve, propose, proposedelete,
reject, del, pull, push) all embed rpkgutil.Runner. pull and push
retain their printer field on top of the embed.
Behavioural improvements that surfaced during the refactor:
- approve and propose-delete now reject empty argument invocations
with "PACKAGE is a required positional argument", matching reject's
existing behaviour. The reject error message itself is aligned to
say PACKAGE rather than PACKAGE_REVISION, matching the Use string.
- reject's DeletionProposed -> Published path now uses
UpdatePackageRevisionApproval like the Proposed -> Draft path,
resolving the inconsistency kispaljr's NOTE comment had pointed at.
- pull, push and reject preRunE gain explicit unit tests that
exercise the cfg.ToRESTConfig + CreateScheme + client.New wiring
through a kubeconfig fixture, raising coverage on those files from
0% to 76.9% (pull/push) and 87.5% (reject).
Fixes#870Fixes#900
Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
* Address @mozesl-nokia post-approval review feedback
Two cosmetic items raised after @liamfallon's approval are addressed
in this commit.
1. approve/command_test.go used "nephio.org.Specializer.specialize"
as the ReadinessGate fixture string, which lingered from the
branch state before #981 swept the rest of the repository. The
string is pure test data: the test only verifies that the
ConditionType on a ReadinessGate matches a Condition.Type with
status False. Aligned to "kpt.dev.Specializer.specialize" so it
matches what #981 settled on for the rest of the codebase.
2. writeTempKubeconfig was duplicated across pull, push and reject
test files. Extracted as the exported rpkgutil.WriteTempKubeconfig
in a new pkg/cli/commands/rpkg/util/testhelpers.go, matching the
existing testhelpers_v1alpha2.go pattern. The three test files
now call the shared helper, and the now-unused "os" import is
dropped from pull/command_test.go.
Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
---------
Signed-off-by: thc1006 <84045975+thc1006@users.noreply.github.com>
Co-authored-by: Liam Fallon <35595825+liamfallon@users.noreply.github.com>
0 commit comments