From b2c7229b0ae238f78ea47a9214b94b648618aab7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=B3zes=20L=C3=A1szl=C3=B3=20M=C3=A1t=C3=A9?= Date: Wed, 6 May 2026 10:08:06 +0200 Subject: [PATCH 1/6] move internal/types and internal/pkg to pkg/lib MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mózes László Máté --- commands/live/init/cmdliveinit.go | 4 +- commands/live/init/cmdliveinit_test.go | 2 +- commands/live/migrate/migratecmd.go | 4 +- commands/live/migrate/migratecmd_test.go | 2 +- commands/pkg/diff/cmddiff.go | 2 +- commands/pkg/get/cmdget.go | 4 +- commands/pkg/init/cmdinit.go | 2 +- commands/pkg/update/cmdupdate.go | 4 +- internal/hook/executor.go | 4 +- internal/pkg/testing/helpers.go | 38 ------------------ internal/testutil/testutil.go | 16 ++++++++ internal/util/diff/diff.go | 2 +- internal/util/fetch/fetch.go | 4 +- internal/util/fetch/fetch_test.go | 29 +++++++------- internal/util/get/get.go | 4 +- internal/util/pkgutil/pkgutil.go | 2 +- internal/util/pkgutil/pkgutil_test.go | 2 +- internal/util/render/executor.go | 4 +- internal/util/render/executor_test.go | 4 +- internal/util/stack/stack.go | 2 +- internal/util/update/common.go | 2 +- internal/util/update/copy-merge.go | 6 +-- internal/util/update/fastforward.go | 6 +-- internal/util/update/replace.go | 6 +-- internal/util/update/resource-merge.go | 6 +-- internal/util/update/update.go | 6 +-- internal/util/update/update_test.go | 39 +++++++++---------- pkg/api/kptfile/v1/validation.go | 2 +- pkg/api/kptfile/v1/validation_test.go | 2 +- pkg/fn/runtime/runner.go | 4 +- pkg/fn/runtime/runner_test.go | 2 +- pkg/fn/runtime/utils.go | 2 +- pkg/kptfile/kptfileutil/util.go | 12 +++--- pkg/kptfile/kptfileutil/util_test.go | 2 +- pkg/kptpkg/init.go | 2 +- pkg/lib/errors/errors.go | 2 +- pkg/lib/errors/errors_test.go | 2 +- pkg/lib/errors/resolver/live.go | 2 +- pkg/lib/errors/resolver/pkg.go | 2 +- pkg/lib/kptops/render.go | 2 +- pkg/lib/kptops/render_test.go | 2 +- {internal => pkg/lib}/pkg/pkg.go | 2 +- {internal => pkg/lib}/pkg/pkg_test.go | 0 {internal => pkg/lib}/types/types.go | 0 pkg/live/load.go | 2 +- pkg/printer/fake/fake.go | 2 +- pkg/printer/printer.go | 4 +- pkg/printer/printer_test.go | 2 +- pkg/test/runner/config.go | 2 +- .../cmdconfig/commands/cmdsource/cmdsource.go | 2 +- thirdparty/kyaml/runfn/runfn.go | 4 +- 51 files changed, 121 insertions(+), 145 deletions(-) delete mode 100644 internal/pkg/testing/helpers.go rename {internal => pkg/lib}/pkg/pkg.go (99%) rename {internal => pkg/lib}/pkg/pkg_test.go (100%) rename {internal => pkg/lib}/types/types.go (100%) diff --git a/commands/live/init/cmdliveinit.go b/commands/live/init/cmdliveinit.go index 8d3ff2c943..37b3132ce7 100644 --- a/commands/live/init/cmdliveinit.go +++ b/commands/live/init/cmdliveinit.go @@ -26,14 +26,14 @@ import ( "time" "github.com/kptdev/kpt/internal/docs/generated/livedocs" - "github.com/kptdev/kpt/internal/pkg" - "github.com/kptdev/kpt/internal/types" "github.com/kptdev/kpt/internal/util/attribution" "github.com/kptdev/kpt/internal/util/pathutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" rgfilev1alpha1 "github.com/kptdev/kpt/pkg/api/resourcegroup/v1alpha1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" + "github.com/kptdev/kpt/pkg/lib/pkg" + "github.com/kptdev/kpt/pkg/lib/types" "github.com/kptdev/kpt/pkg/printer" "github.com/spf13/cobra" "k8s.io/cli-runtime/pkg/genericclioptions" diff --git a/commands/live/init/cmdliveinit_test.go b/commands/live/init/cmdliveinit_test.go index 49963969f4..7d79a68af7 100644 --- a/commands/live/init/cmdliveinit_test.go +++ b/commands/live/init/cmdliveinit_test.go @@ -21,11 +21,11 @@ import ( "testing" "time" - "github.com/kptdev/kpt/internal/pkg" "github.com/kptdev/kpt/internal/testutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" rgfilev1alpha1 "github.com/kptdev/kpt/pkg/api/resourcegroup/v1alpha1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" + "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/printer/fake" "github.com/stretchr/testify/assert" "k8s.io/cli-runtime/pkg/genericclioptions" diff --git a/commands/live/migrate/migratecmd.go b/commands/live/migrate/migratecmd.go index 616e228898..61eaada121 100644 --- a/commands/live/migrate/migratecmd.go +++ b/commands/live/migrate/migratecmd.go @@ -25,13 +25,13 @@ import ( initialization "github.com/kptdev/kpt/commands/live/init" "github.com/kptdev/kpt/internal/docs/generated/livedocs" - "github.com/kptdev/kpt/internal/pkg" - "github.com/kptdev/kpt/internal/types" "github.com/kptdev/kpt/internal/util/argutil" "github.com/kptdev/kpt/internal/util/pathutil" rgfilev1alpha1 "github.com/kptdev/kpt/pkg/api/resourcegroup/v1alpha1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" + "github.com/kptdev/kpt/pkg/lib/pkg" + "github.com/kptdev/kpt/pkg/lib/types" "github.com/kptdev/kpt/pkg/live" "github.com/spf13/cobra" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" diff --git a/commands/live/migrate/migratecmd_test.go b/commands/live/migrate/migratecmd_test.go index b6dbadda80..1448beaeed 100644 --- a/commands/live/migrate/migratecmd_test.go +++ b/commands/live/migrate/migratecmd_test.go @@ -20,9 +20,9 @@ import ( "strings" "testing" - "github.com/kptdev/kpt/internal/pkg" rgfilev1alpha1 "github.com/kptdev/kpt/pkg/api/resourcegroup/v1alpha1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" + "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/printer/fake" "github.com/stretchr/testify/assert" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" diff --git a/commands/pkg/diff/cmddiff.go b/commands/pkg/diff/cmddiff.go index f05fafd003..273a05701b 100644 --- a/commands/pkg/diff/cmddiff.go +++ b/commands/pkg/diff/cmddiff.go @@ -19,10 +19,10 @@ import ( "os" "github.com/kptdev/kpt/internal/docs/generated/pkgdocs" - "github.com/kptdev/kpt/internal/pkg" "github.com/kptdev/kpt/internal/util/argutil" "github.com/kptdev/kpt/internal/util/diff" "github.com/kptdev/kpt/internal/util/pathutil" + "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" "github.com/kptdev/kpt/pkg/printer" "github.com/spf13/cobra" diff --git a/commands/pkg/get/cmdget.go b/commands/pkg/get/cmdget.go index 03821c663e..4e65680d71 100644 --- a/commands/pkg/get/cmdget.go +++ b/commands/pkg/get/cmdget.go @@ -21,13 +21,13 @@ import ( "strings" docs "github.com/kptdev/kpt/internal/docs/generated/pkgdocs" - "github.com/kptdev/kpt/internal/pkg" - "github.com/kptdev/kpt/internal/types" "github.com/kptdev/kpt/internal/util/argutil" "github.com/kptdev/kpt/internal/util/get" "github.com/kptdev/kpt/internal/util/pathutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/lib/errors" + "github.com/kptdev/kpt/pkg/lib/pkg" + "github.com/kptdev/kpt/pkg/lib/types" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" "github.com/kptdev/kpt/pkg/lib/util/parse" "github.com/spf13/cobra" diff --git a/commands/pkg/init/cmdinit.go b/commands/pkg/init/cmdinit.go index 90b9afc38f..2135d5072c 100644 --- a/commands/pkg/init/cmdinit.go +++ b/commands/pkg/init/cmdinit.go @@ -18,9 +18,9 @@ import ( "context" docs "github.com/kptdev/kpt/internal/docs/generated/pkgdocs" - "github.com/kptdev/kpt/internal/pkg" "github.com/kptdev/kpt/internal/util/pathutil" "github.com/kptdev/kpt/pkg/kptpkg" + "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" "github.com/spf13/cobra" "sigs.k8s.io/kustomize/kyaml/filesys" diff --git a/commands/pkg/update/cmdupdate.go b/commands/pkg/update/cmdupdate.go index daa39b2f50..0042374781 100644 --- a/commands/pkg/update/cmdupdate.go +++ b/commands/pkg/update/cmdupdate.go @@ -22,13 +22,13 @@ import ( "strings" docs "github.com/kptdev/kpt/internal/docs/generated/pkgdocs" - "github.com/kptdev/kpt/internal/pkg" - "github.com/kptdev/kpt/internal/types" "github.com/kptdev/kpt/internal/util/argutil" "github.com/kptdev/kpt/internal/util/pathutil" "github.com/kptdev/kpt/internal/util/update" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/lib/errors" + "github.com/kptdev/kpt/pkg/lib/pkg" + "github.com/kptdev/kpt/pkg/lib/types" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" "github.com/spf13/cobra" "sigs.k8s.io/kustomize/kyaml/filesys" diff --git a/internal/hook/executor.go b/internal/hook/executor.go index 8595463b36..061978f764 100644 --- a/internal/hook/executor.go +++ b/internal/hook/executor.go @@ -19,13 +19,13 @@ import ( "fmt" "io" - "github.com/kptdev/kpt/internal/pkg" - "github.com/kptdev/kpt/internal/types" fnresult "github.com/kptdev/kpt/pkg/api/fnresult/v1" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/fn" fnruntime "github.com/kptdev/kpt/pkg/fn/runtime" + "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/runneroptions" + "github.com/kptdev/kpt/pkg/lib/types" "sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/kustomize/kyaml/kio" ) diff --git a/internal/pkg/testing/helpers.go b/internal/pkg/testing/helpers.go deleted file mode 100644 index d91358f3e7..0000000000 --- a/internal/pkg/testing/helpers.go +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2021 The kpt Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package testing - -import ( - "testing" - - "github.com/kptdev/kpt/internal/pkg" - "github.com/kptdev/kpt/internal/util/pathutil" - "github.com/stretchr/testify/assert" - "sigs.k8s.io/kustomize/kyaml/filesys" -) - -// CreatePkgOrFail creates a new package from the provided path. Unlike the -// pkg.New function, it fails the test instead of returning an error. -func CreatePkgOrFail(t *testing.T, path string) *pkg.Pkg { - absPath, _, err := pathutil.ResolveAbsAndRelPaths(path) - if !assert.NoError(t, err) { - t.FailNow() - } - p, err := pkg.New(filesys.FileSystemOrOnDisk{}, absPath) - if !assert.NoError(t, err) { - t.FailNow() - } - return p -} diff --git a/internal/testutil/testutil.go b/internal/testutil/testutil.go index 6d7040b7f4..ad24f4d19a 100644 --- a/internal/testutil/testutil.go +++ b/internal/testutil/testutil.go @@ -27,8 +27,10 @@ import ( "github.com/kptdev/kpt/internal/gitutil" "github.com/kptdev/kpt/internal/util/git" + "github.com/kptdev/kpt/internal/util/pathutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" + "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/util/addmergecomment" "github.com/kptdev/kpt/pkg/printer/fake" "github.com/philopon/go-toposort" @@ -908,3 +910,17 @@ func (ri *ReposInfo) ResolveCommitIndex(repoRef string, index int) (string, bool } return commits[index], true } + +// CreatePkgOrFail creates a new package from the provided path. Unlike the +// pkg.New function, it fails the test instead of returning an error. +func CreatePkgOrFail(t *testing.T, path string) *pkg.Pkg { + absPath, _, err := pathutil.ResolveAbsAndRelPaths(path) + if !assert.NoError(t, err) { + t.FailNow() + } + p, err := pkg.New(filesys.FileSystemOrOnDisk{}, absPath) + if !assert.NoError(t, err) { + t.FailNow() + } + return p +} diff --git a/internal/util/diff/diff.go b/internal/util/diff/diff.go index 864d6d2f4d..8ecae20bf2 100644 --- a/internal/util/diff/diff.go +++ b/internal/util/diff/diff.go @@ -25,11 +25,11 @@ import ( "strings" "github.com/kptdev/kpt/internal/gitutil" - "github.com/kptdev/kpt/internal/pkg" "github.com/kptdev/kpt/internal/util/fetch" "github.com/kptdev/kpt/internal/util/pkgutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" + "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/util/addmergecomment" "sigs.k8s.io/kustomize/kyaml/errors" "sigs.k8s.io/kustomize/kyaml/filesys" diff --git a/internal/util/fetch/fetch.go b/internal/util/fetch/fetch.go index 8a0636203e..0d15d7e2cc 100644 --- a/internal/util/fetch/fetch.go +++ b/internal/util/fetch/fetch.go @@ -22,17 +22,17 @@ import ( "path/filepath" "strings" - "github.com/kptdev/kpt/internal/types" "github.com/kptdev/kpt/pkg/lib/errors" + "github.com/kptdev/kpt/pkg/lib/types" "github.com/otiai10/copy" "sigs.k8s.io/kustomize/kyaml/filesys" "github.com/kptdev/kpt/internal/gitutil" - "github.com/kptdev/kpt/internal/pkg" "github.com/kptdev/kpt/internal/util/git" "github.com/kptdev/kpt/internal/util/pkgutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" + "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/printer" ) diff --git a/internal/util/fetch/fetch_test.go b/internal/util/fetch/fetch_test.go index 44458d903c..8da0067c10 100644 --- a/internal/util/fetch/fetch_test.go +++ b/internal/util/fetch/fetch_test.go @@ -19,7 +19,6 @@ import ( "path/filepath" "testing" - pkgtesting "github.com/kptdev/kpt/internal/pkg/testing" "github.com/kptdev/kpt/internal/testutil" "github.com/kptdev/kpt/internal/testutil/pkgbuilder" "github.com/kptdev/kpt/internal/util/fetch" @@ -85,7 +84,7 @@ func TestCommand_Run_failNoKptfile(t *testing.T) { } err = fetch.Command{ - Pkg: pkgtesting.CreatePkgOrFail(t, pkgPath), + Pkg: testutil.CreatePkgOrFail(t, pkgPath), }.Run(fake.CtxWithDefaultPrinter()) if !assert.Error(t, err) { t.FailNow() @@ -104,7 +103,7 @@ func TestCommand_Run_failNoGit(t *testing.T) { } err = fetch.Command{ - Pkg: pkgtesting.CreatePkgOrFail(t, w.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, w.FullPackagePath()), }.Run(fake.CtxWithDefaultPrinter()) if !assert.Error(t, err) { t.FailNow() @@ -127,7 +126,7 @@ func TestCommand_Run_failEmptyRepo(t *testing.T) { } err = fetch.Command{ - Pkg: pkgtesting.CreatePkgOrFail(t, w.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, w.FullPackagePath()), }.Run(fake.CtxWithDefaultPrinter()) if !assert.Error(t, err) { t.FailNow() @@ -150,7 +149,7 @@ func TestCommand_Run_failNoRevision(t *testing.T) { } err = fetch.Command{ - Pkg: pkgtesting.CreatePkgOrFail(t, w.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, w.FullPackagePath()), }.Run(fake.CtxWithDefaultPrinter()) if !assert.Error(t, err) { t.FailNow() @@ -177,7 +176,7 @@ func TestCommand_Run(t *testing.T) { absPath := filepath.Join(w.WorkspaceDirectory, g.RepoName) err = fetch.Command{ - Pkg: pkgtesting.CreatePkgOrFail(t, w.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, w.FullPackagePath()), }.Run(fake.CtxWithDefaultPrinter()) assert.NoError(t, err) @@ -240,7 +239,7 @@ func TestCommand_Run_subdir(t *testing.T) { absPath := filepath.Join(w.WorkspaceDirectory, g.RepoName) err = fetch.Command{ - Pkg: pkgtesting.CreatePkgOrFail(t, w.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, w.FullPackagePath()), }.Run(fake.CtxWithDefaultPrinter()) assert.NoError(t, err) @@ -318,7 +317,7 @@ func TestCommand_Run_branch(t *testing.T) { } err = fetch.Command{ - Pkg: pkgtesting.CreatePkgOrFail(t, w.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, w.FullPackagePath()), }.Run(fake.CtxWithDefaultPrinter()) assert.NoError(t, err) @@ -400,7 +399,7 @@ func TestCommand_Run_tag(t *testing.T) { } err = fetch.Command{ - Pkg: pkgtesting.CreatePkgOrFail(t, w.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, w.FullPackagePath()), }.Run(fake.CtxWithDefaultPrinter()) assert.NoError(t, err) @@ -508,7 +507,7 @@ func TestCommand_Run_subdir_at_tag(t *testing.T) { t.FailNow() } - actualPkg := pkgtesting.CreatePkgOrFail(t, rw.FullPackagePath()) + actualPkg := testutil.CreatePkgOrFail(t, rw.FullPackagePath()) err = fetch.Command{ Pkg: actualPkg, }.Run(fake.CtxWithDefaultPrinter()) @@ -566,7 +565,7 @@ func TestCommand_Run_no_subdir_at_valid_tag(t *testing.T) { t.FailNow() } - actualPkg := pkgtesting.CreatePkgOrFail(t, rw.FullPackagePath()) + actualPkg := testutil.CreatePkgOrFail(t, rw.FullPackagePath()) err = fetch.Command{ Pkg: actualPkg, }.Run(fake.CtxWithDefaultPrinter()) @@ -613,7 +612,7 @@ func TestCommand_Run_no_subdir_at_invalid_tag(t *testing.T) { t.FailNow() } - actualPkg := pkgtesting.CreatePkgOrFail(t, rw.FullPackagePath()) + actualPkg := testutil.CreatePkgOrFail(t, rw.FullPackagePath()) err = fetch.Command{ Pkg: actualPkg, }.Run(fake.CtxWithDefaultPrinter()) @@ -637,7 +636,7 @@ func TestCommand_Run_failInvalidRepo(t *testing.T) { } err = fetch.Command{ - Pkg: pkgtesting.CreatePkgOrFail(t, w.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, w.FullPackagePath()), }.Run(fake.CtxWithDefaultPrinter()) if !assert.Error(t, err) { t.FailNow() @@ -661,7 +660,7 @@ func TestCommand_Run_failInvalidBranch(t *testing.T) { } err = fetch.Command{ - Pkg: pkgtesting.CreatePkgOrFail(t, w.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, w.FullPackagePath()), }.Run(fake.CtxWithDefaultPrinter()) if !assert.Error(t, err) { t.FailNow() @@ -688,7 +687,7 @@ func TestCommand_Run_failInvalidTag(t *testing.T) { } err = fetch.Command{ - Pkg: pkgtesting.CreatePkgOrFail(t, w.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, w.FullPackagePath()), }.Run(fake.CtxWithDefaultPrinter()) if !assert.Error(t, err) { t.FailNow() diff --git a/internal/util/get/get.go b/internal/util/get/get.go index 1d74f85c40..ca32f1b76a 100644 --- a/internal/util/get/get.go +++ b/internal/util/get/get.go @@ -24,8 +24,6 @@ import ( "strings" "github.com/kptdev/kpt/internal/hook" - "github.com/kptdev/kpt/internal/pkg" - "github.com/kptdev/kpt/internal/types" "github.com/kptdev/kpt/internal/util/attribution" "github.com/kptdev/kpt/internal/util/fetch" "github.com/kptdev/kpt/internal/util/pathutil" @@ -33,7 +31,9 @@ import ( kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" + "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/runneroptions" + "github.com/kptdev/kpt/pkg/lib/types" "github.com/kptdev/kpt/pkg/lib/util/addmergecomment" "github.com/kptdev/kpt/pkg/printer" "sigs.k8s.io/kustomize/kyaml/filesys" diff --git a/internal/util/pkgutil/pkgutil.go b/internal/util/pkgutil/pkgutil.go index 5daedf9ab9..2e9ee73195 100644 --- a/internal/util/pkgutil/pkgutil.go +++ b/internal/util/pkgutil/pkgutil.go @@ -22,9 +22,9 @@ import ( "sort" "strings" - "github.com/kptdev/kpt/internal/pkg" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" + "github.com/kptdev/kpt/pkg/lib/pkg" "sigs.k8s.io/kustomize/kyaml/copyutil" "sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/kustomize/kyaml/kio" diff --git a/internal/util/pkgutil/pkgutil_test.go b/internal/util/pkgutil/pkgutil_test.go index 06d1dbaff8..cc18639b24 100644 --- a/internal/util/pkgutil/pkgutil_test.go +++ b/internal/util/pkgutil/pkgutil_test.go @@ -20,11 +20,11 @@ import ( "sort" "testing" - "github.com/kptdev/kpt/internal/pkg" "github.com/kptdev/kpt/internal/testutil" "github.com/kptdev/kpt/internal/testutil/pkgbuilder" "github.com/kptdev/kpt/internal/util/pkgutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" + "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/stretchr/testify/assert" ) diff --git a/internal/util/render/executor.go b/internal/util/render/executor.go index 3574ac0ccf..f2e0ece7de 100644 --- a/internal/util/render/executor.go +++ b/internal/util/render/executor.go @@ -23,8 +23,6 @@ import ( "slices" "strings" - "github.com/kptdev/kpt/internal/pkg" - "github.com/kptdev/kpt/internal/types" "github.com/kptdev/kpt/internal/util/attribution" "github.com/kptdev/kpt/internal/util/printerutil" fnresult "github.com/kptdev/kpt/pkg/api/fnresult/v1" @@ -33,7 +31,9 @@ import ( fnruntime "github.com/kptdev/kpt/pkg/fn/runtime" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" + "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/runneroptions" + "github.com/kptdev/kpt/pkg/lib/types" "github.com/kptdev/kpt/pkg/printer" "k8s.io/klog/v2" "sigs.k8s.io/kustomize/kyaml/filesys" diff --git a/internal/util/render/executor_test.go b/internal/util/render/executor_test.go index ac07c74af5..434ffb59d0 100644 --- a/internal/util/render/executor_test.go +++ b/internal/util/render/executor_test.go @@ -22,12 +22,12 @@ import ( "strings" "testing" - "github.com/kptdev/kpt/internal/pkg" - "github.com/kptdev/kpt/internal/types" fnresult "github.com/kptdev/kpt/pkg/api/fnresult/v1" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" fnruntime "github.com/kptdev/kpt/pkg/fn/runtime" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" + "github.com/kptdev/kpt/pkg/lib/pkg" + "github.com/kptdev/kpt/pkg/lib/types" "github.com/kptdev/kpt/pkg/printer" "github.com/stretchr/testify/assert" "sigs.k8s.io/kustomize/kyaml/filesys" diff --git a/internal/util/stack/stack.go b/internal/util/stack/stack.go index 7d3ec8ef93..9b08f494ba 100644 --- a/internal/util/stack/stack.go +++ b/internal/util/stack/stack.go @@ -17,7 +17,7 @@ package stack import ( "fmt" - "github.com/kptdev/kpt/internal/pkg" + "github.com/kptdev/kpt/pkg/lib/pkg" ) // New returns a new stack for elements of string type. diff --git a/internal/util/update/common.go b/internal/util/update/common.go index 9c854a6841..0de48039e2 100644 --- a/internal/util/update/common.go +++ b/internal/util/update/common.go @@ -17,9 +17,9 @@ package update import ( "reflect" - "github.com/kptdev/kpt/internal/types" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" + "github.com/kptdev/kpt/pkg/lib/types" "sigs.k8s.io/kustomize/kyaml/filesys" ) diff --git a/internal/util/update/copy-merge.go b/internal/util/update/copy-merge.go index 0c06d16bb5..392ab0ff64 100644 --- a/internal/util/update/copy-merge.go +++ b/internal/util/update/copy-merge.go @@ -15,12 +15,12 @@ package update import ( - "github.com/kptdev/kpt/internal/pkg" - "github.com/kptdev/kpt/internal/types" "github.com/kptdev/kpt/internal/util/pkgutil" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" - updatetypes "github.com/kptdev/kpt/pkg/lib/update/updatetypes" + "github.com/kptdev/kpt/pkg/lib/pkg" + "github.com/kptdev/kpt/pkg/lib/types" + "github.com/kptdev/kpt/pkg/lib/update/updatetypes" ) // CopyMergeUpdater is responsible for synchronizing the destination package diff --git a/internal/util/update/fastforward.go b/internal/util/update/fastforward.go index 5c379becc0..9d8193a2d7 100644 --- a/internal/util/update/fastforward.go +++ b/internal/util/update/fastforward.go @@ -20,14 +20,14 @@ import ( "path/filepath" "slices" - "github.com/kptdev/kpt/internal/pkg" - "github.com/kptdev/kpt/internal/types" pkgdiff "github.com/kptdev/kpt/internal/util/diff" "github.com/kptdev/kpt/internal/util/pkgutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" - updatetypes "github.com/kptdev/kpt/pkg/lib/update/updatetypes" + "github.com/kptdev/kpt/pkg/lib/pkg" + "github.com/kptdev/kpt/pkg/lib/types" + "github.com/kptdev/kpt/pkg/lib/update/updatetypes" "sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/kustomize/kyaml/sets" ) diff --git a/internal/util/update/replace.go b/internal/util/update/replace.go index a59e118214..9855d570a1 100644 --- a/internal/util/update/replace.go +++ b/internal/util/update/replace.go @@ -18,12 +18,12 @@ import ( "os" "path/filepath" - "github.com/kptdev/kpt/internal/pkg" - "github.com/kptdev/kpt/internal/types" "github.com/kptdev/kpt/internal/util/pkgutil" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" - updatetypes "github.com/kptdev/kpt/pkg/lib/update/updatetypes" + "github.com/kptdev/kpt/pkg/lib/pkg" + "github.com/kptdev/kpt/pkg/lib/types" + "github.com/kptdev/kpt/pkg/lib/update/updatetypes" ) // Updater updates a package to a new upstream version. diff --git a/internal/util/update/resource-merge.go b/internal/util/update/resource-merge.go index c9d0c05af1..78592a7c2c 100644 --- a/internal/util/update/resource-merge.go +++ b/internal/util/update/resource-merge.go @@ -21,10 +21,9 @@ import ( "path/filepath" "strings" + "github.com/kptdev/kpt/pkg/lib/types" "sigs.k8s.io/kustomize/kyaml/pathutil" - "github.com/kptdev/kpt/internal/pkg" - "github.com/kptdev/kpt/internal/types" pkgdiff "github.com/kptdev/kpt/internal/util/diff" "github.com/kptdev/kpt/internal/util/merge" "github.com/kptdev/kpt/internal/util/pkgutil" @@ -32,7 +31,8 @@ import ( kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" - updatetypes "github.com/kptdev/kpt/pkg/lib/update/updatetypes" + "github.com/kptdev/kpt/pkg/lib/pkg" + "github.com/kptdev/kpt/pkg/lib/update/updatetypes" "github.com/kptdev/krm-functions-sdk/go/fn" "gopkg.in/yaml.v2" "k8s.io/klog/v2" diff --git a/internal/util/update/update.go b/internal/util/update/update.go index 68badccc58..5cba10bd73 100644 --- a/internal/util/update/update.go +++ b/internal/util/update/update.go @@ -24,8 +24,6 @@ import ( "strings" "github.com/kptdev/kpt/internal/gitutil" - "github.com/kptdev/kpt/internal/pkg" - "github.com/kptdev/kpt/internal/types" "github.com/kptdev/kpt/internal/util/fetch" "github.com/kptdev/kpt/internal/util/git" "github.com/kptdev/kpt/internal/util/pkgutil" @@ -33,7 +31,9 @@ import ( kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" - updatetypes "github.com/kptdev/kpt/pkg/lib/update/updatetypes" + "github.com/kptdev/kpt/pkg/lib/pkg" + "github.com/kptdev/kpt/pkg/lib/types" + "github.com/kptdev/kpt/pkg/lib/update/updatetypes" "github.com/kptdev/kpt/pkg/lib/util/addmergecomment" "github.com/kptdev/kpt/pkg/printer" "sigs.k8s.io/kustomize/kyaml/copyutil" diff --git a/internal/util/update/update_test.go b/internal/util/update/update_test.go index c8bb6955f7..2a1c929099 100644 --- a/internal/util/update/update_test.go +++ b/internal/util/update/update_test.go @@ -23,7 +23,6 @@ import ( "slices" "testing" - pkgtest "github.com/kptdev/kpt/internal/pkg/testing" "github.com/kptdev/kpt/internal/testutil" "github.com/kptdev/kpt/internal/testutil/pkgbuilder" "github.com/kptdev/kpt/internal/util/update" @@ -76,7 +75,7 @@ func TestCommand_Run_noRefChanges(t *testing.T) { } upstreamRepo := g.Repos[testutil.Upstream] cmd := &update.Command{ - Pkg: pkgtest.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Strategy: strategy, } // Update the local package @@ -139,7 +138,7 @@ func TestCommand_Run_subDir(t *testing.T) { // Update the local package if !assert.NoError(t, (&update.Command{ - Pkg: pkgtest.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Ref: "v1.2", Strategy: strategy, }).Run(fake.CtxWithDefaultPrinter())) { @@ -195,7 +194,7 @@ func TestCommand_Run_noChanges(t *testing.T) { // Update the local package err := (&update.Command{ - Pkg: pkgtest.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Strategy: u.updater, }).Run(fake.CtxWithDefaultPrinter()) if u.err == "" { @@ -377,7 +376,7 @@ func TestCommand_Run_localPackageChanges(t *testing.T) { // run the command err = (&update.Command{ - Pkg: pkgtest.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Ref: masterBranch, Strategy: tc.strategy, }).Run(fake.CtxWithDefaultPrinter()) @@ -447,7 +446,7 @@ func TestCommand_Run_toBranchRef(t *testing.T) { // Update the local package if !assert.NoError(t, (&update.Command{ - Pkg: pkgtest.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Strategy: strategy, Ref: "exp", }).Run(fake.CtxWithDefaultPrinter())) { @@ -811,7 +810,7 @@ func TestCommand_Run_toBranchRefWithSubpkgs(t *testing.T) { // Update the local package if !assert.NoError(t, (&update.Command{ - Pkg: pkgtest.CreatePkgOrFail(t, path.Join(g.LocalWorkspace.FullPackagePath(), tc.updateSubPkg)), + Pkg: testutil.CreatePkgOrFail(t, path.Join(g.LocalWorkspace.FullPackagePath(), tc.updateSubPkg)), Strategy: tc.strategy, Ref: tc.updateRef, }).Run(fake.CtxWithDefaultPrinter())) { @@ -857,7 +856,7 @@ func TestCommand_Run_toTagRef(t *testing.T) { // Update the local package if !assert.NoError(t, (&update.Command{ - Pkg: pkgtest.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Strategy: strategy, Ref: "v1.0", }).Run(fake.CtxWithDefaultPrinter())) { @@ -914,7 +913,7 @@ func TestCommand_ResourceMerge_NonKRMUpdates(t *testing.T) { // Update the local package if !assert.NoError(t, (&update.Command{ - Pkg: pkgtest.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Strategy: strategy, Ref: "v1.0", }).Run(fake.CtxWithDefaultPrinter())) { @@ -957,7 +956,7 @@ func TestCommand_Run_noUpstreamReference(t *testing.T) { // Update the local package err := (&update.Command{ - Pkg: pkgtest.CreatePkgOrFail(t, w.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, w.FullPackagePath()), }).Run(fake.CtxWithDefaultPrinter()) assert.Contains(t, err.Error(), "must have an upstream reference") @@ -970,7 +969,7 @@ func TestCommand_Run_failInvalidPath(t *testing.T) { t.Run(string(strategy), func(t *testing.T) { path := filepath.Join("fake", "path") err := (&update.Command{ - Pkg: pkgtest.CreatePkgOrFail(t, path), + Pkg: testutil.CreatePkgOrFail(t, path), Strategy: strategy, }).Run(fake.CtxWithDefaultPrinter()) if assert.Error(t, err) { @@ -1035,7 +1034,7 @@ func TestCommand_Run_badUpstreamLock(t *testing.T) { // Update the local package. err = (&update.Command{ - Pkg: pkgtest.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), }).Run(fake.CtxWithDefaultPrinter()) if assert.Error(t, err) { @@ -1070,7 +1069,7 @@ func TestCommand_Run_failInvalidRef(t *testing.T) { } err := (&update.Command{ - Pkg: pkgtest.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Ref: "exp", Strategy: strategy, }).Run(fake.CtxWithDefaultPrinter()) @@ -1128,7 +1127,7 @@ func TestCommand_Run_manualChange(t *testing.T) { } err := (&update.Command{ - Pkg: pkgtest.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), }).Run(fake.CtxWithDefaultPrinter()) if !assert.NoError(t, err) { t.FailNow() @@ -1182,7 +1181,7 @@ func TestCommand_Run_symlinks(t *testing.T) { upstreamRepo := g.Repos[testutil.Upstream] err := (&update.Command{ - Pkg: pkgtest.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), }).Run(fake.CtxWithDefaultPrinter()) if !assert.NoError(t, err) { t.FailNow() @@ -1230,7 +1229,7 @@ func TestCommand_Run_badStrategy(t *testing.T) { // Update the local package err := (&update.Command{ - Pkg: pkgtest.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Strategy: strategy, }).Run(fake.CtxWithDefaultPrinter()) if !assert.Error(t, err, strategy) { @@ -2009,7 +2008,7 @@ func TestCommand_Run_local_subpackages(t *testing.T) { } err := (&update.Command{ - Pkg: pkgtest.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Strategy: strategy, }).Run(fake.CtxWithDefaultPrinter()) @@ -3545,7 +3544,7 @@ func TestRun_remote_subpackages(t *testing.T) { } err := (&update.Command{ - Pkg: pkgtest.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), }).Run(fake.CtxWithDefaultPrinter()) if tc.expectedErrMsg != "" { @@ -3690,7 +3689,7 @@ func TestRootPackageIsUnfetched(t *testing.T) { testutil.AddKptfileToWorkspace(t, w, kf) err := (&update.Command{ - Pkg: pkgtest.CreatePkgOrFail(t, w.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, w.FullPackagePath()), }).Run(fake.CtxWithDefaultPrinter()) if !assert.NoError(t, err) { t.FailNow() @@ -3761,7 +3760,7 @@ func TestMultiUpdateCache(t *testing.T) { } testutil.AddKptfileToWorkspace(t, w, kf) cmd := update.Command{ - Pkg: pkgtest.CreatePkgOrFail(t, w.FullPackagePath()), + Pkg: testutil.CreatePkgOrFail(t, w.FullPackagePath()), } err := cmd.Run(fake.CtxWithDefaultPrinter()) if !assert.NoError(t, err) { diff --git a/pkg/api/kptfile/v1/validation.go b/pkg/api/kptfile/v1/validation.go index be2778b2bd..2a1fb59838 100644 --- a/pkg/api/kptfile/v1/validation.go +++ b/pkg/api/kptfile/v1/validation.go @@ -21,7 +21,7 @@ import ( "slices" "strings" - "github.com/kptdev/kpt/internal/types" + "github.com/kptdev/kpt/pkg/lib/types" "sigs.k8s.io/kustomize/api/konfig" kustomizetypes "sigs.k8s.io/kustomize/api/types" "sigs.k8s.io/kustomize/kyaml/filesys" diff --git a/pkg/api/kptfile/v1/validation_test.go b/pkg/api/kptfile/v1/validation_test.go index 6d6598d0ab..08c5e23d1e 100644 --- a/pkg/api/kptfile/v1/validation_test.go +++ b/pkg/api/kptfile/v1/validation_test.go @@ -18,7 +18,7 @@ import ( "path/filepath" "testing" - "github.com/kptdev/kpt/internal/types" + "github.com/kptdev/kpt/pkg/lib/types" "github.com/stretchr/testify/assert" "sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/kustomize/kyaml/yaml" diff --git a/pkg/fn/runtime/runner.go b/pkg/fn/runtime/runner.go index ae2c023564..d23dfafaec 100644 --- a/pkg/fn/runtime/runner.go +++ b/pkg/fn/runtime/runner.go @@ -27,13 +27,13 @@ import ( "github.com/google/shlex" "github.com/kptdev/kpt/internal/builtins" - "github.com/kptdev/kpt/internal/pkg" - "github.com/kptdev/kpt/internal/types" fnresult "github.com/kptdev/kpt/pkg/api/fnresult/v1" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/fn" "github.com/kptdev/kpt/pkg/lib/errors" + "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/runneroptions" + "github.com/kptdev/kpt/pkg/lib/types" "github.com/kptdev/kpt/pkg/printer" "github.com/regclient/regclient" "sigs.k8s.io/kustomize/kyaml/filesys" diff --git a/pkg/fn/runtime/runner_test.go b/pkg/fn/runtime/runner_test.go index 74bdb5f786..e056fe551a 100644 --- a/pkg/fn/runtime/runner_test.go +++ b/pkg/fn/runtime/runner_test.go @@ -24,9 +24,9 @@ import ( "strings" "testing" - "github.com/kptdev/kpt/internal/types" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/lib/runneroptions" + "github.com/kptdev/kpt/pkg/lib/types" "github.com/kptdev/kpt/pkg/printer" "github.com/stretchr/testify/assert" "sigs.k8s.io/kustomize/kyaml/filesys" diff --git a/pkg/fn/runtime/utils.go b/pkg/fn/runtime/utils.go index 6d4289c6b5..d182b60e1d 100644 --- a/pkg/fn/runtime/utils.go +++ b/pkg/fn/runtime/utils.go @@ -19,9 +19,9 @@ import ( "fmt" "path/filepath" - "github.com/kptdev/kpt/internal/types" fnresult "github.com/kptdev/kpt/pkg/api/fnresult/v1" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" + "github.com/kptdev/kpt/pkg/lib/types" "sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/kustomize/kyaml/yaml" ) diff --git a/pkg/kptfile/kptfileutil/util.go b/pkg/kptfile/kptfileutil/util.go index 2070142470..f912a4e7e0 100644 --- a/pkg/kptfile/kptfileutil/util.go +++ b/pkg/kptfile/kptfileutil/util.go @@ -24,10 +24,10 @@ import ( "slices" "strings" - "github.com/kptdev/kpt/internal/types" "github.com/kptdev/kpt/internal/util/git" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/lib/errors" + "github.com/kptdev/kpt/pkg/lib/types" "k8s.io/apimachinery/pkg/runtime/schema" "sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/kustomize/kyaml/sets" @@ -194,7 +194,7 @@ func UpdateKptfileWithoutOrigin(localPath, updatedPath string, updateUpstream bo updatedKf = &kptfilev1.KptFile{} } - err = merge(localKf, updatedKf, &kptfilev1.KptFile{}) + err = MergeKptfiles(localKf, updatedKf, &kptfilev1.KptFile{}) if err != nil { return err } @@ -241,7 +241,7 @@ func UpdateKptfile(localPath, updatedPath, originPath string, updateUpstream boo originKf = &kptfilev1.KptFile{} } - err = merge(localKf, updatedKf, originKf) + err = MergeKptfiles(localKf, updatedKf, originKf) if err != nil { return err } @@ -379,10 +379,10 @@ func isSupportedKptfileVersion(gvk schema.GroupVersionKind) bool { return slices.Contains(SupportedKptfileVersions, gvk) } -// merge merges the Kptfiles from various sources and updates localKf with output +// MergeKptfiles merges the Kptfiles from various sources and updates localKf with output // please refer to https://github.com/kptdev/kpt/blob/main/docs/design-docs/03-pipeline-merge.md // for related design -func merge(localKf, updatedKf, originalKf *kptfilev1.KptFile) error { +func MergeKptfiles(localKf, updatedKf, originalKf *kptfilev1.KptFile) error { shouldAddSyntheticMergeName := shouldAddFnKey(localKf, updatedKf, originalKf) if shouldAddSyntheticMergeName { addNameForMerge(localKf, updatedKf, originalKf) @@ -471,7 +471,7 @@ func shouldAddFnKeyUtil(fns []kptfilev1.Function) bool { } // addNameForMerge adds name field for all the functions if empty -// name is primarily used as merge-key +// name is primarily used as MergeKptfiles-key func addNameForMerge(kfs ...*kptfilev1.KptFile) { for _, kf := range kfs { if kf == nil || kf.Pipeline == nil { diff --git a/pkg/kptfile/kptfileutil/util_test.go b/pkg/kptfile/kptfileutil/util_test.go index 424ef11624..9d31b348cf 100644 --- a/pkg/kptfile/kptfileutil/util_test.go +++ b/pkg/kptfile/kptfileutil/util_test.go @@ -1386,7 +1386,7 @@ pipeline: assert.NoError(t, err) originKf, err := DecodeKptfile(strings.NewReader(tc.origin)) assert.NoError(t, err) - err = merge(localKf, updatedKf, originKf) + err = MergeKptfiles(localKf, updatedKf, originKf) if tc.err == nil { if !assert.NoError(t, err) { t.FailNow() diff --git a/pkg/kptpkg/init.go b/pkg/kptpkg/init.go index eb6a7e07c2..4a02665310 100644 --- a/pkg/kptpkg/init.go +++ b/pkg/kptpkg/init.go @@ -22,10 +22,10 @@ import ( "strings" "github.com/kptdev/kpt/internal/builtins" - "github.com/kptdev/kpt/internal/pkg" "github.com/kptdev/kpt/internal/util/man" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" builtintypes "github.com/kptdev/kpt/pkg/lib/builtins/builtintypes" + "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/printer" "sigs.k8s.io/kustomize/kyaml/errors" "sigs.k8s.io/kustomize/kyaml/filesys" diff --git a/pkg/lib/errors/errors.go b/pkg/lib/errors/errors.go index a4a297b8ca..9feff62de4 100644 --- a/pkg/lib/errors/errors.go +++ b/pkg/lib/errors/errors.go @@ -22,7 +22,7 @@ import ( goerrors "errors" kyaml_errors "github.com/go-errors/errors" - "github.com/kptdev/kpt/internal/types" + "github.com/kptdev/kpt/pkg/lib/types" ) // Error is the type that implements error interface used in the kpt codebase. diff --git a/pkg/lib/errors/errors_test.go b/pkg/lib/errors/errors_test.go index c2e324e64f..75dd492c4c 100644 --- a/pkg/lib/errors/errors_test.go +++ b/pkg/lib/errors/errors_test.go @@ -19,7 +19,7 @@ import ( "strings" "testing" - "github.com/kptdev/kpt/internal/types" + "github.com/kptdev/kpt/pkg/lib/types" ) func TestErrorFormatting(t *testing.T) { diff --git a/pkg/lib/errors/resolver/live.go b/pkg/lib/errors/resolver/live.go index f57293809b..915abad922 100644 --- a/pkg/lib/errors/resolver/live.go +++ b/pkg/lib/errors/resolver/live.go @@ -20,7 +20,7 @@ import ( "strings" initialization "github.com/kptdev/kpt/commands/live/init" - "github.com/kptdev/kpt/internal/pkg" + "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" "github.com/kptdev/kpt/pkg/live" "sigs.k8s.io/cli-utils/pkg/inventory" diff --git a/pkg/lib/errors/resolver/pkg.go b/pkg/lib/errors/resolver/pkg.go index 6f2ab4ca9b..35eeaa0f68 100644 --- a/pkg/lib/errors/resolver/pkg.go +++ b/pkg/lib/errors/resolver/pkg.go @@ -18,10 +18,10 @@ import ( "fmt" "os" - "github.com/kptdev/kpt/internal/pkg" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" + "github.com/kptdev/kpt/pkg/lib/pkg" ) //nolint:gochecknoinits diff --git a/pkg/lib/kptops/render.go b/pkg/lib/kptops/render.go index 459f12ce5e..b21568ba5f 100644 --- a/pkg/lib/kptops/render.go +++ b/pkg/lib/kptops/render.go @@ -20,10 +20,10 @@ import ( "io" "os" - "github.com/kptdev/kpt/internal/pkg" "github.com/kptdev/kpt/internal/util/render" fnresult "github.com/kptdev/kpt/pkg/api/fnresult/v1" "github.com/kptdev/kpt/pkg/fn" + "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/runneroptions" "github.com/kptdev/kpt/pkg/printer" "k8s.io/klog/v2" diff --git a/pkg/lib/kptops/render_test.go b/pkg/lib/kptops/render_test.go index d4527504e9..63cdf524d7 100644 --- a/pkg/lib/kptops/render_test.go +++ b/pkg/lib/kptops/render_test.go @@ -23,7 +23,7 @@ import ( "strings" "testing" - "github.com/kptdev/kpt/internal/pkg" + "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/printer" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" diff --git a/internal/pkg/pkg.go b/pkg/lib/pkg/pkg.go similarity index 99% rename from internal/pkg/pkg.go rename to pkg/lib/pkg/pkg.go index ff10b27706..aebd52f682 100644 --- a/internal/pkg/pkg.go +++ b/pkg/lib/pkg/pkg.go @@ -24,13 +24,13 @@ import ( "sort" "strings" - "github.com/kptdev/kpt/internal/types" "github.com/kptdev/kpt/internal/util/git" "github.com/kptdev/kpt/internal/util/pathutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" rgfilev1alpha1 "github.com/kptdev/kpt/pkg/api/resourcegroup/v1alpha1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" + "github.com/kptdev/kpt/pkg/lib/types" "sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/kustomize/kyaml/kio" "sigs.k8s.io/kustomize/kyaml/kio/kioutil" diff --git a/internal/pkg/pkg_test.go b/pkg/lib/pkg/pkg_test.go similarity index 100% rename from internal/pkg/pkg_test.go rename to pkg/lib/pkg/pkg_test.go diff --git a/internal/types/types.go b/pkg/lib/types/types.go similarity index 100% rename from internal/types/types.go rename to pkg/lib/types/types.go diff --git a/pkg/live/load.go b/pkg/live/load.go index c08ff48e48..5ac4f15c30 100644 --- a/pkg/live/load.go +++ b/pkg/live/load.go @@ -19,13 +19,13 @@ import ( "fmt" "io" - "github.com/kptdev/kpt/internal/pkg" "github.com/kptdev/kpt/internal/util/pathutil" "github.com/kptdev/kpt/internal/util/strings" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" rgfilev1alpha1 "github.com/kptdev/kpt/pkg/api/resourcegroup/v1alpha1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" + "github.com/kptdev/kpt/pkg/lib/pkg" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/klog/v2" diff --git a/pkg/printer/fake/fake.go b/pkg/printer/fake/fake.go index dcbde829d8..0821398061 100644 --- a/pkg/printer/fake/fake.go +++ b/pkg/printer/fake/fake.go @@ -18,7 +18,7 @@ import ( "context" "io" - "github.com/kptdev/kpt/internal/pkg" + "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/printer" ) diff --git a/pkg/printer/printer.go b/pkg/printer/printer.go index 9d7475ca96..08f75cb26e 100644 --- a/pkg/printer/printer.go +++ b/pkg/printer/printer.go @@ -21,8 +21,8 @@ import ( "io" "os" - "github.com/kptdev/kpt/internal/pkg" - "github.com/kptdev/kpt/internal/types" + "github.com/kptdev/kpt/pkg/lib/pkg" + "github.com/kptdev/kpt/pkg/lib/types" ) // TruncateOutput defines should output be truncated diff --git a/pkg/printer/printer_test.go b/pkg/printer/printer_test.go index 165f705b37..fe685823b3 100644 --- a/pkg/printer/printer_test.go +++ b/pkg/printer/printer_test.go @@ -18,7 +18,7 @@ import ( "bytes" "testing" - "github.com/kptdev/kpt/internal/pkg" + "github.com/kptdev/kpt/pkg/lib/pkg" ) func TestOptPrintf_WithDisplayPath(t *testing.T) { diff --git a/pkg/test/runner/config.go b/pkg/test/runner/config.go index 7a4c9b57ca..0809e5c6c7 100644 --- a/pkg/test/runner/config.go +++ b/pkg/test/runner/config.go @@ -19,7 +19,7 @@ import ( "os" "path/filepath" - "github.com/kptdev/kpt/internal/types" + "github.com/kptdev/kpt/pkg/lib/types" "sigs.k8s.io/kustomize/kyaml/yaml" ) diff --git a/thirdparty/cmdconfig/commands/cmdsource/cmdsource.go b/thirdparty/cmdconfig/commands/cmdsource/cmdsource.go index 5cffb1a670..29d9f30871 100644 --- a/thirdparty/cmdconfig/commands/cmdsource/cmdsource.go +++ b/thirdparty/cmdconfig/commands/cmdsource/cmdsource.go @@ -9,9 +9,9 @@ import ( "path/filepath" "github.com/kptdev/kpt/internal/docs/generated/fndocs" - "github.com/kptdev/kpt/internal/pkg" "github.com/kptdev/kpt/internal/util/argutil" kptfile "github.com/kptdev/kpt/pkg/api/kptfile/v1" + "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" "github.com/kptdev/kpt/pkg/printer" "github.com/kptdev/kpt/thirdparty/cmdconfig/commands/runner" diff --git a/thirdparty/kyaml/runfn/runfn.go b/thirdparty/kyaml/runfn/runfn.go index a1264791ef..375449cc93 100644 --- a/thirdparty/kyaml/runfn/runfn.go +++ b/thirdparty/kyaml/runfn/runfn.go @@ -12,9 +12,9 @@ import ( "path/filepath" "strings" - "github.com/kptdev/kpt/internal/types" fnruntime "github.com/kptdev/kpt/pkg/fn/runtime" "github.com/kptdev/kpt/pkg/lib/runneroptions" + "github.com/kptdev/kpt/pkg/lib/types" "github.com/kptdev/kpt/pkg/printer" "sigs.k8s.io/kustomize/kyaml/errors" "sigs.k8s.io/kustomize/kyaml/filesys" @@ -22,10 +22,10 @@ import ( "sigs.k8s.io/kustomize/kyaml/kio" "sigs.k8s.io/kustomize/kyaml/yaml" - "github.com/kptdev/kpt/internal/pkg" "github.com/kptdev/kpt/internal/util/printerutil" fnresult "github.com/kptdev/kpt/pkg/api/fnresult/v1" kptfile "github.com/kptdev/kpt/pkg/api/kptfile/v1" + "github.com/kptdev/kpt/pkg/lib/pkg" ) // RunFns runs the set of configuration functions in a local directory against From 706f191d180f3b1813d1868e1b4d0ecdd750a5b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=B3zes=20L=C3=A1szl=C3=B3=20M=C3=A1t=C3=A9?= Date: Wed, 6 May 2026 14:39:37 +0200 Subject: [PATCH 2/6] move alpha/printers, builtins, util/update, merge util/merge into util/update/merge3 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mózes László Máté --- commands/live/apply/cmdapply.go | 2 +- commands/pkg/init/cmdinit_test.go | 4 +- commands/pkg/update/cmdupdate.go | 2 +- internal/util/merge/merge3.go | 369 ---------- internal/util/merge/merge3_test.go | 635 ------------------ internal/util/update/update.go | 493 -------------- pkg/fn/runtime/runner.go | 2 +- pkg/kptpkg/init.go | 4 +- pkg/lib/builtins/builtins.go | 5 +- {internal => pkg/lib}/builtins/pkg_context.go | 2 +- .../lib}/builtins/pkg_context_test.go | 0 .../testdata/pkg-with-existing-ctx/in.yaml | 0 .../testdata/pkg-with-existing-ctx/out.yaml | 0 .../testdata/pkg-with-nesting/in.yaml | 0 .../testdata/pkg-with-nesting/out.yaml | 0 .../builtins/testdata/pkg-wo-nesting/in.yaml | 0 .../builtins/testdata/pkg-wo-nesting/out.yaml | 0 pkg/lib/errors/resolver/update.go | 2 +- pkg/lib/kptops/pkgupdate.go | 2 +- {internal/util => pkg/lib}/update/common.go | 0 .../util => pkg/lib}/update/copy-merge.go | 0 .../lib}/update/copy-merge_test.go | 4 +- .../util => pkg/lib}/update/fastforward.go | 0 .../lib}/update/fastforward_test.go | 2 +- .../lib}/update/merge3/MergeTestResources.go | 0 .../util => pkg/lib}/update/merge3/merge3.go | 0 .../lib}/update/merge3/merge3_test.go | 0 .../lib}/update/merge3/merge3_util_test.go | 0 .../lib}/update/merge3/resource_matcher.go | 0 .../util => pkg/lib}/update/merge3/schema.go | 0 .../lib}/update/merge3/strategy.go | 0 .../lib}/update/merge3/strategy_test.go | 0 .../infer-crd-empty-dest/destination.yaml | 0 .../infer-crd-empty-dest/original.yaml | 0 .../infer-crd-empty-dest/updated.yaml | 0 .../infer-crd-empty-orig/destination.yaml | 0 .../infer-crd-empty-orig/original.yaml | 0 .../infer-crd-empty-orig/updated.yaml | 0 .../infer-crd-empty-updated/destination.yaml | 0 .../infer-crd-empty-updated/original.yaml | 0 .../infer-crd-empty-updated/updated.yaml | 0 .../testdata/infer-crd/destination.yaml | 0 .../merge3/testdata/infer-crd/original.yaml | 0 .../merge3/testdata/infer-crd/updated.yaml | 0 .../one-key-crd-empty-dest/destination.yaml | 0 .../fruitstore.crd.yaml | 0 .../one-key-crd-empty-dest/original.yaml | 0 .../one-key-crd-empty-dest/updated.yaml | 0 .../one-key-crd-empty-orig/destination.yaml | 0 .../fruitstore.crd.yaml | 0 .../one-key-crd-empty-orig/original.yaml | 0 .../one-key-crd-empty-orig/updated.yaml | 0 .../destination.yaml | 0 .../fruitstore.crd.yaml | 0 .../one-key-crd-empty-updated/original.yaml | 0 .../one-key-crd-empty-updated/updated.yaml | 0 .../testdata/one-key-crd/destination.yaml | 0 .../testdata/one-key-crd/fruitstore.crd.yaml | 0 .../merge3/testdata/one-key-crd/original.yaml | 0 .../merge3/testdata/one-key-crd/updated.yaml | 0 .../testdata/simple-conflict/destination.yaml | 0 .../testdata/simple-conflict/original.yaml | 0 .../testdata/simple-conflict/updated.yaml | 0 .../destination.yaml | 0 .../simple-subpackage-conflict/original.yaml | 0 .../simple-subpackage-conflict/updated.yaml | 0 .../util => pkg/lib}/update/merge3/tuple.go | 0 pkg/lib/update/pruninglocalpackagereader.go | 51 ++ {internal/util => pkg/lib}/update/replace.go | 0 .../util => pkg/lib}/update/replace_test.go | 4 +- .../util => pkg/lib}/update/resource-merge.go | 11 +- .../lib}/update/resource-merge_test.go | 4 +- pkg/lib/update/update.go | 478 ++++++++++++- .../util => pkg/lib}/update/update_test.go | 42 +- .../util/addmergecomment/addmergecomment.go | 28 +- .../printer}/table/collector.go | 0 .../printers => pkg/printer}/table/printer.go | 4 +- 77 files changed, 594 insertions(+), 1556 deletions(-) delete mode 100644 internal/util/merge/merge3.go delete mode 100644 internal/util/merge/merge3_test.go delete mode 100644 internal/util/update/update.go rename {internal => pkg/lib}/builtins/pkg_context.go (98%) rename {internal => pkg/lib}/builtins/pkg_context_test.go (100%) rename {internal => pkg/lib}/builtins/testdata/pkg-with-existing-ctx/in.yaml (100%) rename {internal => pkg/lib}/builtins/testdata/pkg-with-existing-ctx/out.yaml (100%) rename {internal => pkg/lib}/builtins/testdata/pkg-with-nesting/in.yaml (100%) rename {internal => pkg/lib}/builtins/testdata/pkg-with-nesting/out.yaml (100%) rename {internal => pkg/lib}/builtins/testdata/pkg-wo-nesting/in.yaml (100%) rename {internal => pkg/lib}/builtins/testdata/pkg-wo-nesting/out.yaml (100%) rename {internal/util => pkg/lib}/update/common.go (100%) rename {internal/util => pkg/lib}/update/copy-merge.go (100%) rename {internal/util => pkg/lib}/update/copy-merge_test.go (99%) rename {internal/util => pkg/lib}/update/fastforward.go (100%) rename {internal/util => pkg/lib}/update/fastforward_test.go (99%) rename {internal/util => pkg/lib}/update/merge3/MergeTestResources.go (100%) rename {internal/util => pkg/lib}/update/merge3/merge3.go (100%) rename {internal/util => pkg/lib}/update/merge3/merge3_test.go (100%) rename {internal/util => pkg/lib}/update/merge3/merge3_util_test.go (100%) rename {internal/util => pkg/lib}/update/merge3/resource_matcher.go (100%) rename {internal/util => pkg/lib}/update/merge3/schema.go (100%) rename {internal/util => pkg/lib}/update/merge3/strategy.go (100%) rename {internal/util => pkg/lib}/update/merge3/strategy_test.go (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/infer-crd-empty-dest/destination.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/infer-crd-empty-dest/original.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/infer-crd-empty-dest/updated.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/infer-crd-empty-orig/destination.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/infer-crd-empty-orig/original.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/infer-crd-empty-orig/updated.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/infer-crd-empty-updated/destination.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/infer-crd-empty-updated/original.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/infer-crd-empty-updated/updated.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/infer-crd/destination.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/infer-crd/original.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/infer-crd/updated.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/one-key-crd-empty-dest/destination.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/one-key-crd-empty-dest/fruitstore.crd.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/one-key-crd-empty-dest/original.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/one-key-crd-empty-dest/updated.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/one-key-crd-empty-orig/destination.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/one-key-crd-empty-orig/fruitstore.crd.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/one-key-crd-empty-orig/original.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/one-key-crd-empty-orig/updated.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/one-key-crd-empty-updated/destination.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/one-key-crd-empty-updated/fruitstore.crd.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/one-key-crd-empty-updated/original.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/one-key-crd-empty-updated/updated.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/one-key-crd/destination.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/one-key-crd/fruitstore.crd.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/one-key-crd/original.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/one-key-crd/updated.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/simple-conflict/destination.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/simple-conflict/original.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/simple-conflict/updated.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/simple-subpackage-conflict/destination.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/simple-subpackage-conflict/original.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/testdata/simple-subpackage-conflict/updated.yaml (100%) rename {internal/util => pkg/lib}/update/merge3/tuple.go (100%) create mode 100644 pkg/lib/update/pruninglocalpackagereader.go rename {internal/util => pkg/lib}/update/replace.go (100%) rename {internal/util => pkg/lib}/update/replace_test.go (98%) rename {internal/util => pkg/lib}/update/resource-merge.go (97%) rename {internal/util => pkg/lib}/update/resource-merge_test.go (98%) rename {internal/util => pkg/lib}/update/update_test.go (99%) rename {internal/alpha/printers => pkg/printer}/table/collector.go (100%) rename {internal/alpha/printers => pkg/printer}/table/printer.go (98%) diff --git a/commands/live/apply/cmdapply.go b/commands/live/apply/cmdapply.go index 2c5ea3e0c1..4b2c97ebcb 100644 --- a/commands/live/apply/cmdapply.go +++ b/commands/live/apply/cmdapply.go @@ -20,12 +20,12 @@ import ( "os" "time" - alphaprinterstable "github.com/kptdev/kpt/internal/alpha/printers/table" "github.com/kptdev/kpt/internal/docs/generated/livedocs" "github.com/kptdev/kpt/internal/util/argutil" "github.com/kptdev/kpt/internal/util/strings" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" "github.com/kptdev/kpt/pkg/live" + alphaprinterstable "github.com/kptdev/kpt/pkg/printer/table" "github.com/kptdev/kpt/pkg/status" "github.com/spf13/cobra" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" diff --git a/commands/pkg/init/cmdinit_test.go b/commands/pkg/init/cmdinit_test.go index 9b6ee69611..c1ffc1d7b9 100644 --- a/commands/pkg/init/cmdinit_test.go +++ b/commands/pkg/init/cmdinit_test.go @@ -21,10 +21,10 @@ import ( "testing" initialization "github.com/kptdev/kpt/commands/pkg/init" - "github.com/kptdev/kpt/internal/builtins" "github.com/kptdev/kpt/internal/testutil" "github.com/kptdev/kpt/internal/util/man" - builtintypes "github.com/kptdev/kpt/pkg/lib/builtins/builtintypes" + "github.com/kptdev/kpt/pkg/lib/builtins" + "github.com/kptdev/kpt/pkg/lib/builtins/builtintypes" "github.com/kptdev/kpt/pkg/printer/fake" "github.com/stretchr/testify/assert" ) diff --git a/commands/pkg/update/cmdupdate.go b/commands/pkg/update/cmdupdate.go index 0042374781..38276fb0f5 100644 --- a/commands/pkg/update/cmdupdate.go +++ b/commands/pkg/update/cmdupdate.go @@ -24,11 +24,11 @@ import ( docs "github.com/kptdev/kpt/internal/docs/generated/pkgdocs" "github.com/kptdev/kpt/internal/util/argutil" "github.com/kptdev/kpt/internal/util/pathutil" - "github.com/kptdev/kpt/internal/util/update" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/lib/errors" "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/types" + "github.com/kptdev/kpt/pkg/lib/update" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" "github.com/spf13/cobra" "sigs.k8s.io/kustomize/kyaml/filesys" diff --git a/internal/util/merge/merge3.go b/internal/util/merge/merge3.go deleted file mode 100644 index 4d7c2d9237..0000000000 --- a/internal/util/merge/merge3.go +++ /dev/null @@ -1,369 +0,0 @@ -// Copyright 2020 The kpt Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package merge - -import ( - "path/filepath" - "strings" - - "github.com/kptdev/kpt/internal/util/attribution" - kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" - "sigs.k8s.io/kustomize/kyaml/kio" - "sigs.k8s.io/kustomize/kyaml/kio/filters" - "sigs.k8s.io/kustomize/kyaml/kio/kioutil" - "sigs.k8s.io/kustomize/kyaml/pathutil" - "sigs.k8s.io/kustomize/kyaml/resid" - "sigs.k8s.io/kustomize/kyaml/yaml" -) - -const ( - mergeSourceAnnotation = "config.kubernetes.io/merge-source" - mergeSourceOriginal = "original" - mergeSourceUpdated = "updated" - mergeSourceDest = "dest" - MergeCommentPrefix = "kpt-merge:" -) - -// Merge3 performs a 3-way merge on the original, upstream and -// destination packages. It provides support for doing this only for -// the parent package and ignore any subpackages. Whenever the boundaries -// of a package differs between original, upstream and destination, the -// boundaries in destination will be used. -type Merge3 struct { - OriginalPath string - UpdatedPath string - DestPath string - MatchFilesGlob []string - MergeOnPath bool - IncludeSubPackages bool -} - -func (m Merge3) Merge() error { - // If subpackages are not included when doing the merge, first - // look up the known subpackages in destination so we can make sure - // those are ignored when reading files from original and updated. - var relPaths []string - if !m.IncludeSubPackages { - var err error - relPaths, err = m.findExclusions() - if err != nil { - return err - } - } - - var inputs []kio.Reader - dest := &kio.LocalPackageReadWriter{ - PackagePath: m.DestPath, - MatchFilesGlob: m.MatchFilesGlob, - SetAnnotations: map[string]string{mergeSourceAnnotation: mergeSourceDest}, - IncludeSubpackages: m.IncludeSubPackages, - PackageFileName: kptfilev1.KptFileName, - PreserveSeqIndent: true, - WrapBareSeqNode: true, - } - inputs = append(inputs, dest) - - // Read the original package - inputs = append(inputs, PruningLocalPackageReader{ - LocalPackageReader: kio.LocalPackageReader{ - PackagePath: m.OriginalPath, - MatchFilesGlob: m.MatchFilesGlob, - SetAnnotations: map[string]string{mergeSourceAnnotation: mergeSourceOriginal}, - IncludeSubpackages: m.IncludeSubPackages, - PackageFileName: kptfilev1.KptFileName, - PreserveSeqIndent: true, - WrapBareSeqNode: true, - }, - Exclusions: relPaths, - }) - - // Read the updated package - inputs = append(inputs, PruningLocalPackageReader{ - LocalPackageReader: kio.LocalPackageReader{ - PackagePath: m.UpdatedPath, - MatchFilesGlob: m.MatchFilesGlob, - SetAnnotations: map[string]string{mergeSourceAnnotation: mergeSourceUpdated}, - IncludeSubpackages: m.IncludeSubPackages, - PackageFileName: kptfilev1.KptFileName, - PreserveSeqIndent: true, - WrapBareSeqNode: true, - }, - Exclusions: relPaths, - }) - - rmMatcher := ResourceMergeMatcher{MergeOnPath: m.MergeOnPath} - resourceHandler := resourceHandler{} - kyamlMerge := filters.Merge3{ - Matcher: &rmMatcher, - Handler: &resourceHandler, - } - - return kio.Pipeline{ - Inputs: inputs, - Filters: []kio.Filter{kyamlMerge}, - Outputs: []kio.Writer{dest}, - }.Execute() -} - -func (m Merge3) findExclusions() ([]string, error) { - var relPaths []string - paths, err := pathutil.DirsWithFile(m.DestPath, kptfilev1.KptFileName, true) - if err != nil { - return relPaths, err - } - - for _, p := range paths { - rel, err := filepath.Rel(m.DestPath, p) - if err != nil { - return relPaths, err - } - if rel == "." { - continue - } - relPaths = append(relPaths, rel) - } - return relPaths, nil -} - -// PruningLocalPackageReader implements the Reader interface. It is similar -// to the LocalPackageReader but allows for exclusion of subdirectories. -type PruningLocalPackageReader struct { - LocalPackageReader kio.LocalPackageReader - Exclusions []string -} - -func (p PruningLocalPackageReader) Read() ([]*yaml.RNode, error) { - // Delegate reading the resources to the LocalPackageReader. - nodes, err := p.LocalPackageReader.Read() - if err != nil { - return nil, err - } - - // Exclude any resources that exist underneath an excluded path. - var filteredNodes []*yaml.RNode - for _, node := range nodes { - if err := kioutil.CopyLegacyAnnotations(node); err != nil { - return nil, err - } - n, err := node.Pipe(yaml.GetAnnotation(kioutil.PathAnnotation)) - if err != nil { - return nil, err - } - path := n.YNode().Value - if p.isExcluded(path) { - continue - } - filteredNodes = append(filteredNodes, node) - } - return filteredNodes, nil -} - -func (p PruningLocalPackageReader) isExcluded(path string) bool { - for _, e := range p.Exclusions { - if strings.HasPrefix(path, e) { - return true - } - } - return false -} - -type ResourceMergeMatcher struct { - MergeOnPath bool -} - -// IsSameResource determines if 2 resources are same to be merged by matching GKNN+filepath -// Group, Kind are derived from resource metadata directly, Namespace and Name are derived -// from merge comment which is of format "kpt-merge: namespace/name", if the merge comment -// is not present, then it falls back to Namespace and Name on the resource meta -func (rm *ResourceMergeMatcher) IsSameResource(node1, node2 *yaml.RNode) bool { - if node1 == nil || node2 == nil { - return false - } - - if err := kioutil.CopyLegacyAnnotations(node1); err != nil { - return false - } - if err := kioutil.CopyLegacyAnnotations(node2); err != nil { - return false - } - - meta1, err := node1.GetMeta() - if err != nil { - return false - } - - meta2, err := node2.GetMeta() - if err != nil { - return false - } - - if resolveGroup(meta1) != resolveGroup(meta2) { - return false - } - - if meta1.Kind != meta2.Kind { - return false - } - - if resolveName(meta1, metadataComment(node1)) != resolveName(meta2, metadataComment(node2)) { - return false - } - - if resolveNamespace(meta1, metadataComment(node1)) != resolveNamespace(meta2, metadataComment(node2)) { - return false - } - - if rm.MergeOnPath { - // directories may contain multiple copies of a resource with the same - // name, namespace, apiVersion and kind -- e.g. kustomize patches, or - // multiple environments - // mergeOnPath configures the merge logic to use the path as part of the - // resource key - if meta1.Annotations[kioutil.PathAnnotation] != meta2.Annotations[kioutil.PathAnnotation] { - return false - } - } - return true -} - -// resolveGroup resolves the group of a resource from ResourceMeta -func resolveGroup(meta yaml.ResourceMeta) string { - group, _ := resid.ParseGroupVersion(meta.APIVersion) - return group -} - -// resolveNamespace resolves the namespace which should be used for merging resources -// uses namespace from comment on metadata field if present, falls back to resource namespace -func resolveNamespace(meta yaml.ResourceMeta, metadataComment string) string { - nsName := NsAndNameForMerge(metadataComment) - if nsName == nil { - return meta.Namespace - } - return nsName[0] -} - -// resolveName resolves the name which should be used for merging resources -// uses name from comment on metadata field if present, falls back to resource name -func resolveName(meta yaml.ResourceMeta, metadataComment string) string { - nsName := NsAndNameForMerge(metadataComment) - if nsName == nil { - return meta.Name - } - return nsName[1] -} - -// NsAndNameForMerge returns the namespace and name for merge -// from the line comment on the metadata field -// e.g. metadata: # kpt-merge: default/foo returns [default, foo] -func NsAndNameForMerge(metadataComment string) []string { - comment := strings.TrimPrefix(metadataComment, "#") - comment = strings.TrimSpace(comment) - if !strings.HasPrefix(comment, MergeCommentPrefix) { - return nil - } - comment = strings.TrimPrefix(comment, MergeCommentPrefix) - nsAndName := strings.SplitN(strings.TrimSpace(comment), "/", 2) - if len(nsAndName) != 2 { - return nil - } - return nsAndName -} - -// metadataComment returns the line comment on the metadata field of input RNode -func metadataComment(node *yaml.RNode) string { - mf := node.Field(yaml.MetadataField) - if mf.IsNilOrEmpty() { - return "" - } - return mf.Key.YNode().LineComment -} - -// resourceHandler is an implementation of the ResourceHandler interface from -// kyaml. It is used to decide how a resource should be handled during the -// 3-way merge. This differs from the default implementation in that if a -// resource is deleted from upstream, it will only be deleted from local if -// there is no diff between origin and local. -type resourceHandler struct { - keptResources []*yaml.RNode -} - -func (r *resourceHandler) Handle(origin, upstream, local *yaml.RNode) (filters.ResourceMergeStrategy, error) { - var strategy filters.ResourceMergeStrategy - switch { - // Keep the resource if added locally. - case origin == nil && upstream == nil && local != nil: - strategy = filters.KeepDest - // Add the resource if added in upstream. - case origin == nil && upstream != nil && local == nil: - strategy = filters.KeepUpdated - // Do not re-add the resource if deleted from both upstream and local - case upstream == nil && local == nil: - strategy = filters.Skip - // If deleted from upstream, only delete if local fork does not have changes. - case origin != nil && upstream == nil: - equal, err := r.equals(origin, local) - if err != nil { - return strategy, err - } - if equal { - strategy = filters.Skip - } else { - r.keptResources = append(r.keptResources, local) - strategy = filters.KeepDest - } - // Do not re-add if deleted from local. - case origin != nil && local == nil: - strategy = filters.Skip - default: - strategy = filters.Merge - } - return strategy, nil -} - -func (*resourceHandler) equals(r1, r2 *yaml.RNode) (bool, error) { - // We need to create new copies of the resources since we need to - // mutate them before comparing them. - r1Clone, err := yaml.Parse(r1.MustString()) - if err != nil { - return false, err - } - r2Clone, err := yaml.Parse(r2.MustString()) - if err != nil { - return false, err - } - - // The resources include annotations with information used during the merge - // process. We need to remove those before comparing the resources. - if err := stripKyamlAnnos(r1Clone); err != nil { - return false, err - } - if err := stripKyamlAnnos(r2Clone); err != nil { - return false, err - } - - return r1Clone.MustString() == r2Clone.MustString(), nil -} - -func stripKyamlAnnos(n *yaml.RNode) error { - for _, a := range []string{mergeSourceAnnotation, kioutil.PathAnnotation, kioutil.IndexAnnotation, - kioutil.LegacyPathAnnotation, kioutil.LegacyIndexAnnotation, // nolint:staticcheck - kioutil.InternalAnnotationsMigrationResourceIDAnnotation, attribution.CNRMMetricsAnnotation} { - err := n.PipeE(yaml.ClearAnnotation(a)) - if err != nil { - return err - } - } - return nil -} diff --git a/internal/util/merge/merge3_test.go b/internal/util/merge/merge3_test.go deleted file mode 100644 index 8bf2c4a056..0000000000 --- a/internal/util/merge/merge3_test.go +++ /dev/null @@ -1,635 +0,0 @@ -// Copyright 2020 The kpt Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package merge_test - -import ( - "os" - "path/filepath" - "strings" - "testing" - - "github.com/kptdev/kpt/internal/testutil" - "github.com/kptdev/kpt/internal/testutil/pkgbuilder" - "github.com/kptdev/kpt/internal/util/merge" - "github.com/stretchr/testify/assert" - "sigs.k8s.io/kustomize/kyaml/copyutil" - "sigs.k8s.io/kustomize/kyaml/yaml" -) - -func TestMerge3_Nested_packages(t *testing.T) { - annotationSetter := yaml.SetAnnotation("foo", "bar") - labelSetter := yaml.SetLabel("bar", "foo") - - testCases := []struct { - name string - includeSubPackages bool - original *pkgbuilder.RootPkg - upstream *pkgbuilder.RootPkg - local *pkgbuilder.RootPkg - expected *pkgbuilder.RootPkg - }{ - { - name: "subpackages are merged if included", - includeSubPackages: true, - original: createPkg(), - upstream: createPkg(annotationSetter), - local: createPkg(labelSetter), - expected: createPkg(labelSetter, annotationSetter), - }, - { - name: "subpackages are not merged if not included", - includeSubPackages: false, - original: createPkg(), - upstream: createPkg(annotationSetter), - local: createPkg(labelSetter), - expected: createPkgMultipleMutators( - []yaml.Filter{ - labelSetter, - annotationSetter, - }, - []yaml.Filter{ - labelSetter, - }, - ), - }, - { - name: "local copy defines the package boundaries if different from upstream", - includeSubPackages: false, - original: pkgbuilder.NewRootPkg(). - WithKptfile(). - WithResource(pkgbuilder.DeploymentResource). - WithSubPackages( - pkgbuilder.NewSubPkg("a"). - WithKptfile(). - WithResource(pkgbuilder.DeploymentResource), - ), - upstream: pkgbuilder.NewRootPkg(). - WithKptfile(). - WithResource(pkgbuilder.DeploymentResource, annotationSetter). - WithSubPackages( - pkgbuilder.NewSubPkg("a"). - WithResource(pkgbuilder.DeploymentResource, annotationSetter), - ), - local: pkgbuilder.NewRootPkg(). - WithKptfile(). - WithResource(pkgbuilder.DeploymentResource, labelSetter). - WithSubPackages( - pkgbuilder.NewSubPkg("a"). - WithKptfile(). - WithResource(pkgbuilder.DeploymentResource, labelSetter), - ), - expected: pkgbuilder.NewRootPkg(). - WithKptfile(). - WithResource(pkgbuilder.DeploymentResource, labelSetter, annotationSetter). - WithSubPackages( - pkgbuilder.NewSubPkg("a"). - WithKptfile(). - WithResource(pkgbuilder.DeploymentResource, labelSetter), - ), - }, - { - name: "upstream changes not included if in a different package", - includeSubPackages: false, - original: pkgbuilder.NewRootPkg(). - WithKptfile(). - WithResource(pkgbuilder.DeploymentResource). - WithSubPackages( - pkgbuilder.NewSubPkg("a"). - WithKptfile(). - WithResource(pkgbuilder.DeploymentResource), - ), - upstream: pkgbuilder.NewRootPkg(). - WithKptfile(). - WithResource(pkgbuilder.DeploymentResource, annotationSetter). - WithSubPackages( - pkgbuilder.NewSubPkg("a"). - WithKptfile(). - WithResource(pkgbuilder.DeploymentResource, annotationSetter), - ), - local: pkgbuilder.NewRootPkg(). - WithKptfile(). - WithResource(pkgbuilder.DeploymentResource, labelSetter). - WithSubPackages( - pkgbuilder.NewSubPkg("a"). // No Kptfile - WithResource(pkgbuilder.DeploymentResource, labelSetter), - ), - expected: pkgbuilder.NewRootPkg(). - WithKptfile(). - WithResource(pkgbuilder.DeploymentResource, labelSetter, annotationSetter). - WithSubPackages( - pkgbuilder.NewSubPkg("a"). - WithResource(pkgbuilder.DeploymentResource, labelSetter), - ), - }, - } - - for i := range testCases { - test := testCases[i] - t.Run(test.name, func(t *testing.T) { - original := test.original.ExpandPkg(t, testutil.EmptyReposInfo) - updated := test.upstream.ExpandPkg(t, testutil.EmptyReposInfo) - local := test.local.ExpandPkg(t, testutil.EmptyReposInfo) - expected := test.expected.ExpandPkg(t, testutil.EmptyReposInfo) - err := merge.Merge3{ - OriginalPath: original, - UpdatedPath: updated, - DestPath: local, - MergeOnPath: true, - IncludeSubPackages: test.includeSubPackages, - }.Merge() - if !assert.NoError(t, err) { - t.FailNow() - } - - diffs, err := copyutil.Diff(local, expected) - if !assert.NoError(t, err) { - t.FailNow() - } - - if !assert.Empty(t, diffs.List()) { - t.FailNow() - } - }) - } -} - -func createPkg(mutators ...yaml.Filter) *pkgbuilder.RootPkg { - return createPkgMultipleMutators(mutators, mutators) -} - -func createPkgMultipleMutators(packageMutators, subPackageMutators []yaml.Filter) *pkgbuilder.RootPkg { - return pkgbuilder.NewRootPkg(). - WithKptfile(). - WithResource(pkgbuilder.DeploymentResource, packageMutators...). - WithSubPackages( - pkgbuilder.NewSubPkg("a"). - WithKptfile(). - WithResource(pkgbuilder.DeploymentResource, subPackageMutators...), - pkgbuilder.NewSubPkg("b"). - WithResource(pkgbuilder.DeploymentResource, packageMutators...). - WithSubPackages( - pkgbuilder.NewSubPkg("c"). - WithKptfile(). - WithResource(pkgbuilder.DeploymentResource, subPackageMutators...), - ), - ) -} - -func TestMerge3_Merge_path(t *testing.T) { - testCases := map[string]struct { - origin string - update string - local string - expected string - errMsg string - }{ - `Most common: add namespace and name-prefix on local, merge upstream changes`: { - origin: ` -apiVersion: apps/v1 -kind: Deployment -metadata: - name: nginx-deployment -spec: - replicas: 3`, - update: ` -apiVersion: apps/v1 -kind: Deployment -metadata: - name: nginx-deployment -spec: - replicas: 4`, - local: ` -apiVersion: apps/v1 -kind: Deployment -metadata: # kpt-merge: /nginx-deployment - name: dev-nginx-deployment - namespace: my-space -spec: - replicas: 3 -`, - expected: ` -apiVersion: apps/v1 -kind: Deployment -metadata: # kpt-merge: /nginx-deployment - name: dev-nginx-deployment - namespace: my-space -spec: - replicas: 4 -`}, - - `Add namespace and name-prefix on local manually without adding annotations, adds new resource`: { - origin: ` -apiVersion: apps/v1 -kind: Deployment -metadata: - name: nginx-deployment -spec: - replicas: 3`, - update: ` -apiVersion: apps/v1 -kind: Deployment -metadata: - name: nginx-deployment -spec: - replicas: 4`, - local: ` -apiVersion: apps/v1 -kind: Deployment -metadata: - name: dev-nginx-deployment - namespace: my-space -spec: - replicas: 3 -`, - expected: ` -apiVersion: apps/v1 -kind: Deployment -metadata: - name: dev-nginx-deployment - namespace: my-space -spec: - replicas: 3 -`}, - - `Conflict: User fetches package, copies a resource in same file, adds different name suffix`: { - origin: ` -apiVersion: apps/v1 -kind: Deployment -metadata: - name: nginx-deployment -spec: - replicas: 3`, - update: ` -apiVersion: apps/v1 -kind: Deployment -metadata: - name: nginx-deployment -spec: - replicas: 4`, - local: ` -apiVersion: apps/v1 -kind: Deployment -metadata: # kpt-merge: default/nginx-deployment - name: nginx-deployment-1 - namespace: my-space -spec: - replicas: 3 ---- -apiVersion: apps/v1 -kind: Deployment -metadata: # kpt-merge: default/nginx-deployment - name: nginx-deployment-2 - namespace: my-space -spec: - replicas: 3 -`, - errMsg: `found duplicate "local" resources in file "f1.yaml"`}, - - `Publisher changes name in upstream but want to maintain original identity, no local customizations, fetch upstream changes`: { - origin: ` -apiVersion: apps/v1 -kind: Deployment -metadata: - name: nginx-deployment -spec: - replicas: 3`, - update: ` -apiVersion: apps/v1 -kind: Deployment -metadata: # kpt-merge: /nginx-deployment - name: nginx-deployment-new -spec: - replicas: 4`, - local: ` -apiVersion: apps/v1 -kind: Deployment -metadata: - name: nginx-deployment -spec: - replicas: 3 -`, - expected: ` -apiVersion: apps/v1 -kind: Deployment -metadata: # kpt-merge: /nginx-deployment - name: nginx-deployment-new -spec: - replicas: 4 -`}, - - `Publisher changes name in upstream but want to maintain original identity, consumer adds name-prefix on local, fetch upstream changes`: { - origin: ` -apiVersion: apps/v1 -kind: Deployment -metadata: - name: nginx-deployment -spec: - replicas: 3`, - update: ` -apiVersion: apps/v1 -kind: Deployment -metadata: # kpt-merge: default/nginx-deployment - name: nginx-deployment-new -spec: - replicas: 4`, - local: ` -apiVersion: apps/v1 -kind: Deployment -metadata: # kpt-merge: default/nginx-deployment - name: dev-nginx-deployment - namespace: my-space -spec: - replicas: 3 -`, - expected: ` -apiVersion: apps/v1 -kind: Deployment -metadata: # kpt-merge: default/nginx-deployment - name: nginx-deployment-new - namespace: my-space -spec: - replicas: 4 -`}, - - `Publisher changes name in upstream but don't want to maintain original identity which is equivalent -to delete existing resource and add new one, consumer adds name-prefix on local`: { - origin: ` -apiVersion: apps/v1 -kind: Deployment -metadata: - name: nginx-deployment -spec: - replicas: 3`, - update: ` -apiVersion: apps/v1 -kind: Deployment -metadata: - name: nginx-deployment-new -spec: - replicas: 4`, - local: ` -apiVersion: apps/v1 -kind: Deployment -metadata: # kpt-merge: /nginx-deployment - name: dev-nginx-deployment - namespace: my-space -spec: - replicas: 3 -`, - expected: ` -apiVersion: apps/v1 -kind: Deployment -metadata: # kpt-merge: /nginx-deployment - name: dev-nginx-deployment - namespace: my-space -spec: - replicas: 3 ---- -apiVersion: apps/v1 -kind: Deployment -metadata: - name: nginx-deployment-new -spec: - replicas: 4 -`}, - - `Publisher changes name multiple times in upstream but maintains original identity, no local customizations, -fetch upstream changes`: { - origin: ` -apiVersion: apps/v1 -kind: Deployment -metadata: # kpt-merge: default/nginx-deployment - name: nginx-deployment-new -spec: - replicas: 4`, - update: ` -apiVersion: apps/v1 -kind: Deployment -metadata: # kpt-merge: default/nginx-deployment - name: nginx-deployment-new-again -spec: - replicas: 5`, - local: ` -apiVersion: apps/v1 -kind: Deployment -metadata: # kpt-merge: default/nginx-deployment - name: nginx-deployment-new -spec: - replicas: 5 -`, - expected: ` -apiVersion: apps/v1 -kind: Deployment -metadata: # kpt-merge: default/nginx-deployment - name: nginx-deployment-new-again -spec: - replicas: 5 -`}, - - `Publisher changes name multiple times in upstream but maintains original identity, consumer adds name-prefix -on local, fetch upstream changes`: { - origin: ` -apiVersion: apps/v1 -kind: Deployment -metadata: # kpt-merge: default/nginx-deployment - name: nginx-deployment-new -spec: - replicas: 4`, - update: ` -apiVersion: apps/v1 -kind: Deployment -metadata: # kpt-merge: default/nginx-deployment - name: nginx-deployment-new-again -spec: - replicas: 5`, - local: ` -apiVersion: apps/v1 -kind: Deployment -metadata: # kpt-merge: default/nginx-deployment - name: dev-nginx-deployment - namespace: my-space -spec: - replicas: 5 -`, - expected: ` -apiVersion: apps/v1 -kind: Deployment -metadata: # kpt-merge: default/nginx-deployment - name: nginx-deployment-new-again - namespace: my-space -spec: - replicas: 5 -`}, - `Publisher adds metadata.annotations in upstream in a non-identity kustomization resource, consumer adds changes to resource body -on local, fetch upstream changes`: { - origin: ` -apiVersion: kustomize.config.k8s.io/v1beta1 -metadata: - labels: - color: blue -commonLabels: - app: dev`, - update: ` -apiVersion: kustomize.config.k8s.io/v1beta1 -kind: Kustomization -metadata: - labels: - color: blue - annotations: - id.example.org: abcd -commonLabels: - app: dev`, - local: ` -apiVersion: kustomize.config.k8s.io/v1beta1 -kind: Kustomization -metadata: - labels: - color: blue -commonLabels: - app: dev - tier: backend -`, - expected: ` -apiVersion: kustomize.config.k8s.io/v1beta1 -kind: Kustomization -metadata: - labels: - color: blue - annotations: - id.example.org: abcd -commonLabels: - app: dev - tier: backend -`}, - `Publisher adds commonLabels in upstream in a non-identity kustomization resource, consumer adds changes to resource body -on local, fetch upstream changes`: { - origin: ` -commonLabels: - app: dev`, - update: ` -commonLabels: - tier: backend - app: dev`, - local: ` -commonLabels: - app: dev - tier: backend - db: mysql -`, - expected: ` -commonLabels: - app: dev - tier: backend - db: mysql -`}, - - `Version changes are just like any other changes`: { - origin: ` -apiVersion: apps/v1 -kind: Deployment -metadata: - name: nginx-deployment -spec: - replicas: 3`, - update: ` -apiVersion: apps/v2 -kind: Deployment -metadata: - name: nginx-deployment -spec: - replicas: 4`, - local: ` -apiVersion: apps/v1 -kind: Deployment -metadata: - name: nginx-deployment -spec: - replicas: 3 -`, - expected: ` -apiVersion: apps/v2 -kind: Deployment -metadata: - name: nginx-deployment -spec: - replicas: 4 -`}, - } - - for tn, tc := range testCases { - t.Run(tn, func(t *testing.T) { - // setup the local directory - dir := t.TempDir() - - err := os.MkdirAll(filepath.Join(dir, "localDir"), 0700) - if !assert.NoError(t, err) { - t.FailNow() - } - - err = os.MkdirAll(filepath.Join(dir, "updatedDir"), 0700) - if !assert.NoError(t, err) { - t.FailNow() - } - - err = os.MkdirAll(filepath.Join(dir, "originalDir"), 0700) - if !assert.NoError(t, err) { - t.FailNow() - } - - err = os.WriteFile(filepath.Join(dir, "originalDir", "f1.yaml"), []byte(strings.TrimSpace(tc.origin)), 0700) - if !assert.NoError(t, err) { - t.FailNow() - } - - err = os.WriteFile(filepath.Join(dir, "updatedDir", "f1.yaml"), []byte(strings.TrimSpace(tc.update)), 0700) - if !assert.NoError(t, err) { - t.FailNow() - } - - err = os.WriteFile(filepath.Join(dir, "localDir", "f1.yaml"), []byte(strings.TrimSpace(tc.local)), 0700) - if !assert.NoError(t, err) { - t.FailNow() - } - - err = merge.Merge3{ - OriginalPath: filepath.Join(dir, "originalDir"), - UpdatedPath: filepath.Join(dir, "updatedDir"), - DestPath: filepath.Join(dir, "localDir"), - MergeOnPath: true, - }.Merge() - if tc.errMsg == "" { - if !assert.NoError(t, err) { - t.FailNow() - } - } else { - if !assert.Error(t, err) { - t.FailNow() - } - if !assert.Contains(t, err.Error(), tc.errMsg) { - t.FailNow() - } - return - } - - b, err := os.ReadFile(filepath.Join(dir, "localDir", "f1.yaml")) - if !assert.NoError(t, err) { - t.FailNow() - } - if !assert.Equal(t, strings.TrimSpace(tc.expected), strings.TrimSpace(string(b))) { - t.FailNow() - } - }) - } -} diff --git a/internal/util/update/update.go b/internal/util/update/update.go deleted file mode 100644 index 5cba10bd73..0000000000 --- a/internal/util/update/update.go +++ /dev/null @@ -1,493 +0,0 @@ -// Copyright 2019,2026 The kpt Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -// Package update contains libraries for updating packages. -package update - -import ( - "context" - "fmt" - "os" - "path" - "path/filepath" - "strings" - - "github.com/kptdev/kpt/internal/gitutil" - "github.com/kptdev/kpt/internal/util/fetch" - "github.com/kptdev/kpt/internal/util/git" - "github.com/kptdev/kpt/internal/util/pkgutil" - "github.com/kptdev/kpt/internal/util/stack" - kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" - "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" - "github.com/kptdev/kpt/pkg/lib/errors" - "github.com/kptdev/kpt/pkg/lib/pkg" - "github.com/kptdev/kpt/pkg/lib/types" - "github.com/kptdev/kpt/pkg/lib/update/updatetypes" - "github.com/kptdev/kpt/pkg/lib/util/addmergecomment" - "github.com/kptdev/kpt/pkg/printer" - "sigs.k8s.io/kustomize/kyaml/copyutil" - "sigs.k8s.io/kustomize/kyaml/filesys" -) - -// PkgNotGitRepoError is the error type returned if the package being updated is not inside -// a git repository. -type PkgNotGitRepoError struct { - Path types.UniquePath -} - -func (p *PkgNotGitRepoError) Error() string { - return fmt.Sprintf("package %q is not a git repository", p.Path.String()) -} - -// PkgRepoDirtyError is the error type returned if the package being updated contains -// uncommitted changes. -type PkgRepoDirtyError struct { - Path types.UniquePath -} - -func (p *PkgRepoDirtyError) Error() string { - return fmt.Sprintf("package %q contains uncommitted changes", p.Path.String()) -} - -type Options struct { - // RelPackagePath is the relative path of a subpackage to the root. If the - // package is root, the value here will be ".". - RelPackagePath string - - // LocalPath is the absolute path to the package on the local fork. - LocalPath string - - // OriginPath is the absolute path to the package in the on-disk clone - // of the origin ref of the repo. - OriginPath string - - // UpdatedPath is the absolute path to the package in the on-disk clone - // of the updated ref of the repo. - UpdatedPath string - - // IsRoot is true if the package is the root, i.e. the clones of - // updated and origin were fetched based on the information in the - // Kptfile from this package. - IsRoot bool -} - -// Updater updates a local package -var strategies = map[kptfilev1.UpdateStrategyType]func() updatetypes.Updater{ - kptfilev1.FastForward: func() updatetypes.Updater { return FastForwardUpdater{} }, - kptfilev1.ForceDeleteReplace: func() updatetypes.Updater { return ReplaceUpdater{} }, - kptfilev1.ResourceMerge: func() updatetypes.Updater { return ResourceMergeUpdater{} }, - kptfilev1.CopyMerge: func() updatetypes.Updater { return CopyMergeUpdater{} }, -} - -// Command updates the contents of a local package to a different version. -type Command struct { - // Pkg captures information about the package that should be updated. - Pkg *pkg.Pkg - - // Ref is the ref to update to - Ref string - - // Strategy is the update strategy to use - Strategy kptfilev1.UpdateStrategyType - - // cachedUpstreamRepos is an upstream repo already fetched for a given repoSpec CloneRef - cachedUpstreamRepos map[string]*gitutil.GitUpstreamRepo -} - -func GetUpdater(strategy string) updatetypes.Updater { - switch strategy { - case "fast-forward": - return FastForwardUpdater{} - case "force-delete-replace": - return ReplaceUpdater{} - case "copy-merge": - return CopyMergeUpdater{} - default: - return ResourceMergeUpdater{} - } -} - -// Run runs the Command. -func (u *Command) Run(ctx context.Context) error { - const op errors.Op = "update.Run" - pr := printer.FromContextOrDie(ctx) - - if u.Pkg == nil { - return errors.E(op, errors.MissingParam, "pkg must be provided") - } - - rootKf, err := u.Pkg.Kptfile() - if err != nil { - return errors.E(op, u.Pkg.UniquePath, err) - } - - if rootKf.Upstream == nil || rootKf.Upstream.Git == nil { - return errors.E(op, u.Pkg.UniquePath, - fmt.Errorf("package must have an upstream reference")) - } - originalRootKfRef := rootKf.Upstream.Git.Ref - if u.Ref != "" { - rootKf.Upstream.Git.Ref = u.Ref - } - if u.Strategy != "" { - rootKf.Upstream.UpdateStrategy = u.Strategy - } - err = kptfileutil.WriteFile(u.Pkg.UniquePath.String(), rootKf) - if err != nil { - return errors.E(op, u.Pkg.UniquePath, err) - } - if u.cachedUpstreamRepos == nil { - u.cachedUpstreamRepos = make(map[string]*gitutil.GitUpstreamRepo) - } - packageCount := 0 - - // Use stack to keep track of paths with a Kptfile that might contain - // information about remote subpackages. - s := stack.NewPkgStack() - s.Push(u.Pkg) - - for s.Len() > 0 { - p := s.Pop() - packageCount++ - - if err := u.updateRootPackage(ctx, p); err != nil { - return errors.E(op, p.UniquePath, err) - } - - subPkgs, err := p.DirectSubpackages() - if err != nil { - return errors.E(op, p.UniquePath, err) - } - for _, subPkg := range subPkgs { - subKf, err := subPkg.Kptfile() - if err != nil { - return errors.E(op, p.UniquePath, err) - } - - if subKf.Upstream != nil && subKf.Upstream.Git != nil { - // update subpackage kf ref/strategy if current pkg is a subpkg of root pkg or is root pkg - // and if original root pkg ref matches the subpkg ref - if shouldUpdateSubPkgRef(subKf, rootKf, originalRootKfRef) { - updateSubKf(subKf, u.Ref, u.Strategy) - err = kptfileutil.WriteFile(subPkg.UniquePath.String(), subKf) - if err != nil { - return errors.E(op, subPkg.UniquePath, err) - } - } - s.Push(subPkg) - } - } - } - pr.Printf("\nUpdated %d package(s).\n", packageCount) - - // finally, make sure that the merge comments are added to all resources in the updated package - if err := addmergecomment.Process(string(u.Pkg.UniquePath)); err != nil { - return errors.E(op, u.Pkg.UniquePath, err) - } - return nil -} - -// GetCachedUpstreamRepos returns repos cached during update -func (u Command) GetCachedUpstreamRepos() map[string]*gitutil.GitUpstreamRepo { - return u.cachedUpstreamRepos -} - -// updateSubKf updates subpackage with given ref and update strategy -func updateSubKf(subKf *kptfilev1.KptFile, ref string, strategy kptfilev1.UpdateStrategyType) { - // check if explicit ref provided - if ref != "" { - subKf.Upstream.Git.Ref = ref - } - if strategy != "" { - subKf.Upstream.UpdateStrategy = strategy - } -} - -// shouldUpdateSubPkgRef checks if subpkg ref should be updated. -// This is true if pkg has the same upstream repo, upstream directory is within or equal to root pkg directory and original root pkg ref matches the subpkg ref. -func shouldUpdateSubPkgRef(subKf, rootKf *kptfilev1.KptFile, originalRootKfRef string) bool { - return subKf.Upstream.Git.Repo == rootKf.Upstream.Git.Repo && - subKf.Upstream.Git.Ref == originalRootKfRef && - strings.HasPrefix(path.Clean(subKf.Upstream.Git.Directory), path.Clean(rootKf.Upstream.Git.Directory)) -} - -// repoClone is an interface that represents a clone of a repo on the local -// disk. -type repoClone interface { - AbsPath() string -} - -// newNilRepoClone creates a new nilRepoClone that implements the repoClone -// interface -func newNilRepoClone() (*nilRepoClone, error) { - const op errors.Op = "update.newNilRepoClone" - dir, err := os.MkdirTemp("", "kpt-empty-") - if err != nil { - return nil, errors.E(op, errors.IO, fmt.Errorf("errors creating a temporary directory: %w", err)) - } - return &nilRepoClone{ - dir: dir, - }, nil -} - -// nilRepoClone is an implementation of the repoClone interface, but that -// just represents an empty directory. This simplifies the logic for update -// since we don't have to special case situations where we don't have -// upstream and/or origin. -type nilRepoClone struct { - dir string -} - -// AbsPath returns the absolute path to the local directory for the repo. For -// the nilRepoClone, this will always be an empty directory. -func (nrc *nilRepoClone) AbsPath() string { - return nrc.dir -} - -// updateRootPackage updates a local package. It will use the information -// about upstream in the Kptfile to fetch upstream and origin, and then -// recursively traverse the hierarchy to add/update/delete packages. -func (u Command) updateRootPackage(ctx context.Context, p *pkg.Pkg) error { - const op errors.Op = "update.updateRootPackage" - kf, err := p.Kptfile() - if err != nil { - return errors.E(op, p.UniquePath, err) - } - - pr := printer.FromContextOrDie(ctx) - pr.PrintPackage(p, p != u.Pkg) - - g := kf.Upstream.Git - updated := &git.RepoSpec{OrgRepo: g.Repo, Path: g.Directory, Ref: g.Ref} - pr.Printf("Fetching upstream from %s@%s\n", kf.Upstream.Git.Repo, kf.Upstream.Git.Ref) - cloner := fetch.NewCloner(updated, fetch.WithCachedRepo(u.cachedUpstreamRepos)) - if err := cloner.ClonerUsingGitExec(ctx); err != nil { - return errors.E(op, p.UniquePath, err) - } - defer os.RemoveAll(updated.AbsPath()) - - var origin repoClone - if kf.UpstreamLock != nil { - gLock := kf.UpstreamLock.Git - originRepoSpec := &git.RepoSpec{OrgRepo: gLock.Repo, Path: gLock.Directory, Ref: gLock.Commit} - pr.Printf("Fetching origin from %s@%s\n", kf.Upstream.Git.Repo, kf.Upstream.Git.Ref) - if err := fetch.NewCloner(originRepoSpec, fetch.WithCachedRepo(u.cachedUpstreamRepos)).ClonerUsingGitExec(ctx); err != nil { - return errors.E(op, p.UniquePath, err) - } - origin = originRepoSpec - } else { - origin, err = newNilRepoClone() - if err != nil { - return errors.E(op, p.UniquePath, err) - } - } - defer os.RemoveAll(origin.AbsPath()) - - s := stack.New() - s.Push(".") - - for s.Len() > 0 { - relPath := s.Pop() - localPath := filepath.Join(p.UniquePath.String(), relPath) - updatedPath := filepath.Join(updated.AbsPath(), relPath) - originPath := filepath.Join(origin.AbsPath(), relPath) - - isRoot := false - if relPath == "." { - isRoot = true - } - - if err := u.updatePackage(ctx, relPath, localPath, updatedPath, originPath, isRoot); err != nil { - return errors.E(op, p.UniquePath, err) - } - - paths, err := pkgutil.FindSubpackagesForPaths(pkg.Remote, false, - localPath, updatedPath, originPath) - if err != nil { - return errors.E(op, p.UniquePath, err) - } - for _, path := range paths { - s.Push(filepath.Join(relPath, path)) - } - } - - if err := kptfileutil.UpdateUpstreamLockFromGit(p.UniquePath.String(), updated); err != nil { - return errors.E(op, p.UniquePath, err) - } - return nil -} - -// updatePackage takes care of updating a single package. The absolute paths to -// the local, updated and origin packages are provided, as well as the path to the -// package relative to the root. -// The last parameter tells if this package is the root, i.e. the package -// from which we got the information about upstream and origin. -// -//nolint:gocyclo -func (u Command) updatePackage(ctx context.Context, subPkgPath, localPath, updatedPath, originPath string, isRootPkg bool) error { - const op errors.Op = "update.updatePackage" - pr := printer.FromContextOrDie(ctx) - - localExists, err := pkg.IsPackageDir(filesys.FileSystemOrOnDisk{}, localPath) - if err != nil { - return errors.E(op, types.UniquePath(localPath), err) - } - - // We need to handle the root package special here, since the copies - // from updated and origin might not have a Kptfile at the root. - updatedExists := isRootPkg - if !isRootPkg { - updatedExists, err = pkg.IsPackageDir(filesys.FileSystemOrOnDisk{}, updatedPath) - if err != nil { - return errors.E(op, types.UniquePath(localPath), err) - } - } - - originExists := isRootPkg - if !isRootPkg { - originExists, err = pkg.IsPackageDir(filesys.FileSystemOrOnDisk{}, originPath) - if err != nil { - return errors.E(op, types.UniquePath(localPath), err) - } - } - - switch { - case !originExists && !localExists && !updatedExists: - break - // Check if subpackage has been added both in upstream and in local. We - // can't make a sane merge here, so we treat it as an error. - case !originExists && localExists && updatedExists: - pr.Printf("Package %q added in both local and upstream.\n", packageName(localPath)) - return errors.E(op, types.UniquePath(localPath), - fmt.Errorf("subpackage %q added in both upstream and local", subPkgPath)) - - // Package added in upstream. We just copy the package. If the package - // contains any unfetched subpackages, those will be handled when we traverse - // the package hierarchy and that package is the root. - case !originExists && !localExists && updatedExists: - pr.Printf("Adding package %q from upstream.\n", packageName(localPath)) - if err := pkgutil.CopyPackage(updatedPath, localPath, !isRootPkg, pkg.None); err != nil { - return errors.E(op, types.UniquePath(localPath), err) - } - - // Package added locally, so no action needed. - case !originExists && localExists && !updatedExists: - break - - // Package deleted from both upstream and local, so no action needed. - case originExists && !localExists && !updatedExists: - break - - // Package deleted from local - // In this case we assume the user knows what they are doing, so - // we don't re-add the updated package from upstream. - case originExists && !localExists && updatedExists: - pr.Printf("Ignoring package %q in upstream since it is deleted from local.\n", packageName(localPath)) - - // Package deleted from upstream - case originExists && localExists && !updatedExists: - // Check the diff. If there are local changes, we keep the subpackage. - diff, err := copyutil.Diff(originPath, localPath) - if err != nil { - return errors.E(op, types.UniquePath(localPath), err) - } - if diff.Len() == 0 { - pr.Printf("Deleting package %q from local since it is removed in upstream.\n", packageName(localPath)) - if err := os.RemoveAll(localPath); err != nil { - return errors.E(op, types.UniquePath(localPath), err) - } - } else { - pr.Printf("Package %q deleted from upstream, but keeping local since it has changes.\n", packageName(localPath)) - } - default: - if err := u.mergePackage(ctx, localPath, updatedPath, originPath, subPkgPath, isRootPkg); err != nil { - return errors.E(op, types.UniquePath(localPath), err) - } - } - return nil -} - -func (u Command) mergePackage(ctx context.Context, localPath, updatedPath, originPath, relPath string, isRootPkg bool) error { - const op errors.Op = "update.mergePackage" - pr := printer.FromContextOrDie(ctx) - // at this point, the localPath, updatedPath and originPath exists and are about to be merged - // make sure that the merge comments are added to all of them so that they are merged accurately - if err := addmergecomment.Process(localPath, updatedPath, originPath); err != nil { - return errors.E(op, types.UniquePath(localPath), - fmt.Errorf("failed to add merge comments %q", err.Error())) - } - updatedUnfetched, err := pkg.IsPackageUnfetched(updatedPath) - if err != nil { - if !errors.Is(err, os.ErrNotExist) || !isRootPkg { - return errors.E(op, types.UniquePath(localPath), err) - } - // For root packages, there might not be a Kptfile in the upstream repo. - updatedUnfetched = false - } - - originUnfetched, err := pkg.IsPackageUnfetched(originPath) - if err != nil { - if !errors.Is(err, os.ErrNotExist) || !isRootPkg { - return errors.E(op, types.UniquePath(localPath), err) - } - // For root packages, there might not be a Kptfile in origin. - originUnfetched = false - } - - switch { - case updatedUnfetched && originUnfetched: - fallthrough - case updatedUnfetched && !originUnfetched: - // updated is unfetched, so can't have changes except for Kptfile. - // we can just merge that one. - return kptfileutil.UpdateKptfile(localPath, updatedPath, originPath, true) - case !updatedUnfetched && originUnfetched: - // This means that the package was unfetched when local forked from upstream, - // so the local fork and upstream might have fetched different versions of - // the package. We just return an error here. - // We might be able to compare the commit SHAs from local and updated - // to determine if they share the common upstream and then fetch origin - // using the common commit SHA. But this is a very advanced scenario, - // so we just return the error for now. - return errors.E(op, types.UniquePath(localPath), fmt.Errorf("no origin available for package")) - default: - // Both exists, so just go ahead as normal. - } - - pkgKf, err := kptfileutil.ReadKptfile(filesys.FileSystemOrOnDisk{}, localPath) - if err != nil { - return errors.E(op, types.UniquePath(localPath), err) - } - updater, found := strategies[pkgKf.Upstream.UpdateStrategy] - if !found { - return errors.E(op, types.UniquePath(localPath), - fmt.Errorf("unrecognized update strategy %s", u.Strategy)) - } - pr.Printf("Updating package %q with strategy %q.\n", packageName(localPath), pkgKf.Upstream.UpdateStrategy) - if err := updater().Update(updatetypes.Options{ - RelPackagePath: relPath, - LocalPath: localPath, - UpdatedPath: updatedPath, - OriginPath: originPath, - IsRoot: isRootPkg, - }); err != nil { - return errors.E(op, types.UniquePath(localPath), err) - } - - return nil -} - -func packageName(path string) string { - return filepath.Base(path) -} diff --git a/pkg/fn/runtime/runner.go b/pkg/fn/runtime/runner.go index d23dfafaec..4c193207e6 100644 --- a/pkg/fn/runtime/runner.go +++ b/pkg/fn/runtime/runner.go @@ -26,10 +26,10 @@ import ( "time" "github.com/google/shlex" - "github.com/kptdev/kpt/internal/builtins" fnresult "github.com/kptdev/kpt/pkg/api/fnresult/v1" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/fn" + "github.com/kptdev/kpt/pkg/lib/builtins" "github.com/kptdev/kpt/pkg/lib/errors" "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/runneroptions" diff --git a/pkg/kptpkg/init.go b/pkg/kptpkg/init.go index 4a02665310..118642936e 100644 --- a/pkg/kptpkg/init.go +++ b/pkg/kptpkg/init.go @@ -21,10 +21,10 @@ import ( "path/filepath" "strings" - "github.com/kptdev/kpt/internal/builtins" "github.com/kptdev/kpt/internal/util/man" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" - builtintypes "github.com/kptdev/kpt/pkg/lib/builtins/builtintypes" + "github.com/kptdev/kpt/pkg/lib/builtins" + "github.com/kptdev/kpt/pkg/lib/builtins/builtintypes" "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/printer" "sigs.k8s.io/kustomize/kyaml/errors" diff --git a/pkg/lib/builtins/builtins.go b/pkg/lib/builtins/builtins.go index 484dd56c0b..c05cf78a14 100644 --- a/pkg/lib/builtins/builtins.go +++ b/pkg/lib/builtins/builtins.go @@ -19,14 +19,13 @@ import ( "fmt" "reflect" - "github.com/kptdev/kpt/internal/builtins" - builtintypes "github.com/kptdev/kpt/pkg/lib/builtins/builtintypes" + "github.com/kptdev/kpt/pkg/lib/builtins/builtintypes" ) func GetBuiltinFn(config any) builtintypes.BuiltinFunction { if reflect.TypeOf(config) == reflect.TypeFor[*builtintypes.PackageConfig]() { packageConfig := config.(*builtintypes.PackageConfig) - return &builtins.PackageContextGenerator{ + return &PackageContextGenerator{ PackageConfig: packageConfig, } } diff --git a/internal/builtins/pkg_context.go b/pkg/lib/builtins/pkg_context.go similarity index 98% rename from internal/builtins/pkg_context.go rename to pkg/lib/builtins/pkg_context.go index 80d74df606..3f0680d3af 100644 --- a/internal/builtins/pkg_context.go +++ b/pkg/lib/builtins/pkg_context.go @@ -26,7 +26,7 @@ import ( "sigs.k8s.io/kustomize/kyaml/yaml" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" - builtintypes "github.com/kptdev/kpt/pkg/lib/builtins/builtintypes" + "github.com/kptdev/kpt/pkg/lib/builtins/builtintypes" ) var ( diff --git a/internal/builtins/pkg_context_test.go b/pkg/lib/builtins/pkg_context_test.go similarity index 100% rename from internal/builtins/pkg_context_test.go rename to pkg/lib/builtins/pkg_context_test.go diff --git a/internal/builtins/testdata/pkg-with-existing-ctx/in.yaml b/pkg/lib/builtins/testdata/pkg-with-existing-ctx/in.yaml similarity index 100% rename from internal/builtins/testdata/pkg-with-existing-ctx/in.yaml rename to pkg/lib/builtins/testdata/pkg-with-existing-ctx/in.yaml diff --git a/internal/builtins/testdata/pkg-with-existing-ctx/out.yaml b/pkg/lib/builtins/testdata/pkg-with-existing-ctx/out.yaml similarity index 100% rename from internal/builtins/testdata/pkg-with-existing-ctx/out.yaml rename to pkg/lib/builtins/testdata/pkg-with-existing-ctx/out.yaml diff --git a/internal/builtins/testdata/pkg-with-nesting/in.yaml b/pkg/lib/builtins/testdata/pkg-with-nesting/in.yaml similarity index 100% rename from internal/builtins/testdata/pkg-with-nesting/in.yaml rename to pkg/lib/builtins/testdata/pkg-with-nesting/in.yaml diff --git a/internal/builtins/testdata/pkg-with-nesting/out.yaml b/pkg/lib/builtins/testdata/pkg-with-nesting/out.yaml similarity index 100% rename from internal/builtins/testdata/pkg-with-nesting/out.yaml rename to pkg/lib/builtins/testdata/pkg-with-nesting/out.yaml diff --git a/internal/builtins/testdata/pkg-wo-nesting/in.yaml b/pkg/lib/builtins/testdata/pkg-wo-nesting/in.yaml similarity index 100% rename from internal/builtins/testdata/pkg-wo-nesting/in.yaml rename to pkg/lib/builtins/testdata/pkg-wo-nesting/in.yaml diff --git a/internal/builtins/testdata/pkg-wo-nesting/out.yaml b/pkg/lib/builtins/testdata/pkg-wo-nesting/out.yaml similarity index 100% rename from internal/builtins/testdata/pkg-wo-nesting/out.yaml rename to pkg/lib/builtins/testdata/pkg-wo-nesting/out.yaml diff --git a/pkg/lib/errors/resolver/update.go b/pkg/lib/errors/resolver/update.go index a94c8f3e96..3730767756 100644 --- a/pkg/lib/errors/resolver/update.go +++ b/pkg/lib/errors/resolver/update.go @@ -17,8 +17,8 @@ package resolver import ( "fmt" - "github.com/kptdev/kpt/internal/util/update" "github.com/kptdev/kpt/pkg/lib/errors" + "github.com/kptdev/kpt/pkg/lib/update" ) //nolint:gochecknoinits diff --git a/pkg/lib/kptops/pkgupdate.go b/pkg/lib/kptops/pkgupdate.go index 642cdedf39..c28c1964ad 100644 --- a/pkg/lib/kptops/pkgupdate.go +++ b/pkg/lib/kptops/pkgupdate.go @@ -22,9 +22,9 @@ import ( "github.com/kptdev/kpt/internal/util/fetch" "github.com/kptdev/kpt/internal/util/git" - "github.com/kptdev/kpt/internal/util/update" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" + "github.com/kptdev/kpt/pkg/lib/update" "github.com/kptdev/kpt/pkg/lib/update/updatetypes" "github.com/kptdev/kpt/pkg/printer" "k8s.io/klog/v2" diff --git a/internal/util/update/common.go b/pkg/lib/update/common.go similarity index 100% rename from internal/util/update/common.go rename to pkg/lib/update/common.go diff --git a/internal/util/update/copy-merge.go b/pkg/lib/update/copy-merge.go similarity index 100% rename from internal/util/update/copy-merge.go rename to pkg/lib/update/copy-merge.go diff --git a/internal/util/update/copy-merge_test.go b/pkg/lib/update/copy-merge_test.go similarity index 99% rename from internal/util/update/copy-merge_test.go rename to pkg/lib/update/copy-merge_test.go index edbcd1d473..63eeca0060 100644 --- a/internal/util/update/copy-merge_test.go +++ b/pkg/lib/update/copy-merge_test.go @@ -21,8 +21,8 @@ import ( "github.com/kptdev/kpt/internal/testutil" "github.com/kptdev/kpt/internal/testutil/pkgbuilder" - "github.com/kptdev/kpt/internal/util/update" - updatetypes "github.com/kptdev/kpt/pkg/lib/update/updatetypes" + "github.com/kptdev/kpt/pkg/lib/update" + "github.com/kptdev/kpt/pkg/lib/update/updatetypes" "github.com/stretchr/testify/assert" ) diff --git a/internal/util/update/fastforward.go b/pkg/lib/update/fastforward.go similarity index 100% rename from internal/util/update/fastforward.go rename to pkg/lib/update/fastforward.go diff --git a/internal/util/update/fastforward_test.go b/pkg/lib/update/fastforward_test.go similarity index 99% rename from internal/util/update/fastforward_test.go rename to pkg/lib/update/fastforward_test.go index f37a4847fa..079a2540eb 100644 --- a/internal/util/update/fastforward_test.go +++ b/pkg/lib/update/fastforward_test.go @@ -21,8 +21,8 @@ import ( "github.com/kptdev/kpt/internal/testutil" "github.com/kptdev/kpt/internal/testutil/pkgbuilder" - "github.com/kptdev/kpt/internal/util/update" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" + "github.com/kptdev/kpt/pkg/lib/update" "github.com/kptdev/kpt/pkg/lib/update/updatetypes" "github.com/stretchr/testify/assert" ) diff --git a/internal/util/update/merge3/MergeTestResources.go b/pkg/lib/update/merge3/MergeTestResources.go similarity index 100% rename from internal/util/update/merge3/MergeTestResources.go rename to pkg/lib/update/merge3/MergeTestResources.go diff --git a/internal/util/update/merge3/merge3.go b/pkg/lib/update/merge3/merge3.go similarity index 100% rename from internal/util/update/merge3/merge3.go rename to pkg/lib/update/merge3/merge3.go diff --git a/internal/util/update/merge3/merge3_test.go b/pkg/lib/update/merge3/merge3_test.go similarity index 100% rename from internal/util/update/merge3/merge3_test.go rename to pkg/lib/update/merge3/merge3_test.go diff --git a/internal/util/update/merge3/merge3_util_test.go b/pkg/lib/update/merge3/merge3_util_test.go similarity index 100% rename from internal/util/update/merge3/merge3_util_test.go rename to pkg/lib/update/merge3/merge3_util_test.go diff --git a/internal/util/update/merge3/resource_matcher.go b/pkg/lib/update/merge3/resource_matcher.go similarity index 100% rename from internal/util/update/merge3/resource_matcher.go rename to pkg/lib/update/merge3/resource_matcher.go diff --git a/internal/util/update/merge3/schema.go b/pkg/lib/update/merge3/schema.go similarity index 100% rename from internal/util/update/merge3/schema.go rename to pkg/lib/update/merge3/schema.go diff --git a/internal/util/update/merge3/strategy.go b/pkg/lib/update/merge3/strategy.go similarity index 100% rename from internal/util/update/merge3/strategy.go rename to pkg/lib/update/merge3/strategy.go diff --git a/internal/util/update/merge3/strategy_test.go b/pkg/lib/update/merge3/strategy_test.go similarity index 100% rename from internal/util/update/merge3/strategy_test.go rename to pkg/lib/update/merge3/strategy_test.go diff --git a/internal/util/update/merge3/testdata/infer-crd-empty-dest/destination.yaml b/pkg/lib/update/merge3/testdata/infer-crd-empty-dest/destination.yaml similarity index 100% rename from internal/util/update/merge3/testdata/infer-crd-empty-dest/destination.yaml rename to pkg/lib/update/merge3/testdata/infer-crd-empty-dest/destination.yaml diff --git a/internal/util/update/merge3/testdata/infer-crd-empty-dest/original.yaml b/pkg/lib/update/merge3/testdata/infer-crd-empty-dest/original.yaml similarity index 100% rename from internal/util/update/merge3/testdata/infer-crd-empty-dest/original.yaml rename to pkg/lib/update/merge3/testdata/infer-crd-empty-dest/original.yaml diff --git a/internal/util/update/merge3/testdata/infer-crd-empty-dest/updated.yaml b/pkg/lib/update/merge3/testdata/infer-crd-empty-dest/updated.yaml similarity index 100% rename from internal/util/update/merge3/testdata/infer-crd-empty-dest/updated.yaml rename to pkg/lib/update/merge3/testdata/infer-crd-empty-dest/updated.yaml diff --git a/internal/util/update/merge3/testdata/infer-crd-empty-orig/destination.yaml b/pkg/lib/update/merge3/testdata/infer-crd-empty-orig/destination.yaml similarity index 100% rename from internal/util/update/merge3/testdata/infer-crd-empty-orig/destination.yaml rename to pkg/lib/update/merge3/testdata/infer-crd-empty-orig/destination.yaml diff --git a/internal/util/update/merge3/testdata/infer-crd-empty-orig/original.yaml b/pkg/lib/update/merge3/testdata/infer-crd-empty-orig/original.yaml similarity index 100% rename from internal/util/update/merge3/testdata/infer-crd-empty-orig/original.yaml rename to pkg/lib/update/merge3/testdata/infer-crd-empty-orig/original.yaml diff --git a/internal/util/update/merge3/testdata/infer-crd-empty-orig/updated.yaml b/pkg/lib/update/merge3/testdata/infer-crd-empty-orig/updated.yaml similarity index 100% rename from internal/util/update/merge3/testdata/infer-crd-empty-orig/updated.yaml rename to pkg/lib/update/merge3/testdata/infer-crd-empty-orig/updated.yaml diff --git a/internal/util/update/merge3/testdata/infer-crd-empty-updated/destination.yaml b/pkg/lib/update/merge3/testdata/infer-crd-empty-updated/destination.yaml similarity index 100% rename from internal/util/update/merge3/testdata/infer-crd-empty-updated/destination.yaml rename to pkg/lib/update/merge3/testdata/infer-crd-empty-updated/destination.yaml diff --git a/internal/util/update/merge3/testdata/infer-crd-empty-updated/original.yaml b/pkg/lib/update/merge3/testdata/infer-crd-empty-updated/original.yaml similarity index 100% rename from internal/util/update/merge3/testdata/infer-crd-empty-updated/original.yaml rename to pkg/lib/update/merge3/testdata/infer-crd-empty-updated/original.yaml diff --git a/internal/util/update/merge3/testdata/infer-crd-empty-updated/updated.yaml b/pkg/lib/update/merge3/testdata/infer-crd-empty-updated/updated.yaml similarity index 100% rename from internal/util/update/merge3/testdata/infer-crd-empty-updated/updated.yaml rename to pkg/lib/update/merge3/testdata/infer-crd-empty-updated/updated.yaml diff --git a/internal/util/update/merge3/testdata/infer-crd/destination.yaml b/pkg/lib/update/merge3/testdata/infer-crd/destination.yaml similarity index 100% rename from internal/util/update/merge3/testdata/infer-crd/destination.yaml rename to pkg/lib/update/merge3/testdata/infer-crd/destination.yaml diff --git a/internal/util/update/merge3/testdata/infer-crd/original.yaml b/pkg/lib/update/merge3/testdata/infer-crd/original.yaml similarity index 100% rename from internal/util/update/merge3/testdata/infer-crd/original.yaml rename to pkg/lib/update/merge3/testdata/infer-crd/original.yaml diff --git a/internal/util/update/merge3/testdata/infer-crd/updated.yaml b/pkg/lib/update/merge3/testdata/infer-crd/updated.yaml similarity index 100% rename from internal/util/update/merge3/testdata/infer-crd/updated.yaml rename to pkg/lib/update/merge3/testdata/infer-crd/updated.yaml diff --git a/internal/util/update/merge3/testdata/one-key-crd-empty-dest/destination.yaml b/pkg/lib/update/merge3/testdata/one-key-crd-empty-dest/destination.yaml similarity index 100% rename from internal/util/update/merge3/testdata/one-key-crd-empty-dest/destination.yaml rename to pkg/lib/update/merge3/testdata/one-key-crd-empty-dest/destination.yaml diff --git a/internal/util/update/merge3/testdata/one-key-crd-empty-dest/fruitstore.crd.yaml b/pkg/lib/update/merge3/testdata/one-key-crd-empty-dest/fruitstore.crd.yaml similarity index 100% rename from internal/util/update/merge3/testdata/one-key-crd-empty-dest/fruitstore.crd.yaml rename to pkg/lib/update/merge3/testdata/one-key-crd-empty-dest/fruitstore.crd.yaml diff --git a/internal/util/update/merge3/testdata/one-key-crd-empty-dest/original.yaml b/pkg/lib/update/merge3/testdata/one-key-crd-empty-dest/original.yaml similarity index 100% rename from internal/util/update/merge3/testdata/one-key-crd-empty-dest/original.yaml rename to pkg/lib/update/merge3/testdata/one-key-crd-empty-dest/original.yaml diff --git a/internal/util/update/merge3/testdata/one-key-crd-empty-dest/updated.yaml b/pkg/lib/update/merge3/testdata/one-key-crd-empty-dest/updated.yaml similarity index 100% rename from internal/util/update/merge3/testdata/one-key-crd-empty-dest/updated.yaml rename to pkg/lib/update/merge3/testdata/one-key-crd-empty-dest/updated.yaml diff --git a/internal/util/update/merge3/testdata/one-key-crd-empty-orig/destination.yaml b/pkg/lib/update/merge3/testdata/one-key-crd-empty-orig/destination.yaml similarity index 100% rename from internal/util/update/merge3/testdata/one-key-crd-empty-orig/destination.yaml rename to pkg/lib/update/merge3/testdata/one-key-crd-empty-orig/destination.yaml diff --git a/internal/util/update/merge3/testdata/one-key-crd-empty-orig/fruitstore.crd.yaml b/pkg/lib/update/merge3/testdata/one-key-crd-empty-orig/fruitstore.crd.yaml similarity index 100% rename from internal/util/update/merge3/testdata/one-key-crd-empty-orig/fruitstore.crd.yaml rename to pkg/lib/update/merge3/testdata/one-key-crd-empty-orig/fruitstore.crd.yaml diff --git a/internal/util/update/merge3/testdata/one-key-crd-empty-orig/original.yaml b/pkg/lib/update/merge3/testdata/one-key-crd-empty-orig/original.yaml similarity index 100% rename from internal/util/update/merge3/testdata/one-key-crd-empty-orig/original.yaml rename to pkg/lib/update/merge3/testdata/one-key-crd-empty-orig/original.yaml diff --git a/internal/util/update/merge3/testdata/one-key-crd-empty-orig/updated.yaml b/pkg/lib/update/merge3/testdata/one-key-crd-empty-orig/updated.yaml similarity index 100% rename from internal/util/update/merge3/testdata/one-key-crd-empty-orig/updated.yaml rename to pkg/lib/update/merge3/testdata/one-key-crd-empty-orig/updated.yaml diff --git a/internal/util/update/merge3/testdata/one-key-crd-empty-updated/destination.yaml b/pkg/lib/update/merge3/testdata/one-key-crd-empty-updated/destination.yaml similarity index 100% rename from internal/util/update/merge3/testdata/one-key-crd-empty-updated/destination.yaml rename to pkg/lib/update/merge3/testdata/one-key-crd-empty-updated/destination.yaml diff --git a/internal/util/update/merge3/testdata/one-key-crd-empty-updated/fruitstore.crd.yaml b/pkg/lib/update/merge3/testdata/one-key-crd-empty-updated/fruitstore.crd.yaml similarity index 100% rename from internal/util/update/merge3/testdata/one-key-crd-empty-updated/fruitstore.crd.yaml rename to pkg/lib/update/merge3/testdata/one-key-crd-empty-updated/fruitstore.crd.yaml diff --git a/internal/util/update/merge3/testdata/one-key-crd-empty-updated/original.yaml b/pkg/lib/update/merge3/testdata/one-key-crd-empty-updated/original.yaml similarity index 100% rename from internal/util/update/merge3/testdata/one-key-crd-empty-updated/original.yaml rename to pkg/lib/update/merge3/testdata/one-key-crd-empty-updated/original.yaml diff --git a/internal/util/update/merge3/testdata/one-key-crd-empty-updated/updated.yaml b/pkg/lib/update/merge3/testdata/one-key-crd-empty-updated/updated.yaml similarity index 100% rename from internal/util/update/merge3/testdata/one-key-crd-empty-updated/updated.yaml rename to pkg/lib/update/merge3/testdata/one-key-crd-empty-updated/updated.yaml diff --git a/internal/util/update/merge3/testdata/one-key-crd/destination.yaml b/pkg/lib/update/merge3/testdata/one-key-crd/destination.yaml similarity index 100% rename from internal/util/update/merge3/testdata/one-key-crd/destination.yaml rename to pkg/lib/update/merge3/testdata/one-key-crd/destination.yaml diff --git a/internal/util/update/merge3/testdata/one-key-crd/fruitstore.crd.yaml b/pkg/lib/update/merge3/testdata/one-key-crd/fruitstore.crd.yaml similarity index 100% rename from internal/util/update/merge3/testdata/one-key-crd/fruitstore.crd.yaml rename to pkg/lib/update/merge3/testdata/one-key-crd/fruitstore.crd.yaml diff --git a/internal/util/update/merge3/testdata/one-key-crd/original.yaml b/pkg/lib/update/merge3/testdata/one-key-crd/original.yaml similarity index 100% rename from internal/util/update/merge3/testdata/one-key-crd/original.yaml rename to pkg/lib/update/merge3/testdata/one-key-crd/original.yaml diff --git a/internal/util/update/merge3/testdata/one-key-crd/updated.yaml b/pkg/lib/update/merge3/testdata/one-key-crd/updated.yaml similarity index 100% rename from internal/util/update/merge3/testdata/one-key-crd/updated.yaml rename to pkg/lib/update/merge3/testdata/one-key-crd/updated.yaml diff --git a/internal/util/update/merge3/testdata/simple-conflict/destination.yaml b/pkg/lib/update/merge3/testdata/simple-conflict/destination.yaml similarity index 100% rename from internal/util/update/merge3/testdata/simple-conflict/destination.yaml rename to pkg/lib/update/merge3/testdata/simple-conflict/destination.yaml diff --git a/internal/util/update/merge3/testdata/simple-conflict/original.yaml b/pkg/lib/update/merge3/testdata/simple-conflict/original.yaml similarity index 100% rename from internal/util/update/merge3/testdata/simple-conflict/original.yaml rename to pkg/lib/update/merge3/testdata/simple-conflict/original.yaml diff --git a/internal/util/update/merge3/testdata/simple-conflict/updated.yaml b/pkg/lib/update/merge3/testdata/simple-conflict/updated.yaml similarity index 100% rename from internal/util/update/merge3/testdata/simple-conflict/updated.yaml rename to pkg/lib/update/merge3/testdata/simple-conflict/updated.yaml diff --git a/internal/util/update/merge3/testdata/simple-subpackage-conflict/destination.yaml b/pkg/lib/update/merge3/testdata/simple-subpackage-conflict/destination.yaml similarity index 100% rename from internal/util/update/merge3/testdata/simple-subpackage-conflict/destination.yaml rename to pkg/lib/update/merge3/testdata/simple-subpackage-conflict/destination.yaml diff --git a/internal/util/update/merge3/testdata/simple-subpackage-conflict/original.yaml b/pkg/lib/update/merge3/testdata/simple-subpackage-conflict/original.yaml similarity index 100% rename from internal/util/update/merge3/testdata/simple-subpackage-conflict/original.yaml rename to pkg/lib/update/merge3/testdata/simple-subpackage-conflict/original.yaml diff --git a/internal/util/update/merge3/testdata/simple-subpackage-conflict/updated.yaml b/pkg/lib/update/merge3/testdata/simple-subpackage-conflict/updated.yaml similarity index 100% rename from internal/util/update/merge3/testdata/simple-subpackage-conflict/updated.yaml rename to pkg/lib/update/merge3/testdata/simple-subpackage-conflict/updated.yaml diff --git a/internal/util/update/merge3/tuple.go b/pkg/lib/update/merge3/tuple.go similarity index 100% rename from internal/util/update/merge3/tuple.go rename to pkg/lib/update/merge3/tuple.go diff --git a/pkg/lib/update/pruninglocalpackagereader.go b/pkg/lib/update/pruninglocalpackagereader.go new file mode 100644 index 0000000000..1358e000e8 --- /dev/null +++ b/pkg/lib/update/pruninglocalpackagereader.go @@ -0,0 +1,51 @@ +package update + +import ( + "strings" + + "sigs.k8s.io/kustomize/kyaml/kio" + "sigs.k8s.io/kustomize/kyaml/kio/kioutil" + "sigs.k8s.io/kustomize/kyaml/yaml" +) + +// PruningLocalPackageReader implements the Reader interface. It is similar +// to the LocalPackageReader but allows for exclusion of subdirectories. +type PruningLocalPackageReader struct { + LocalPackageReader kio.LocalPackageReader + Exclusions []string +} + +func (p PruningLocalPackageReader) Read() ([]*yaml.RNode, error) { + // Delegate reading the resources to the LocalPackageReader. + nodes, err := p.LocalPackageReader.Read() + if err != nil { + return nil, err + } + + // Exclude any resources that exist underneath an excluded path. + var filteredNodes []*yaml.RNode + for _, node := range nodes { + if err := kioutil.CopyLegacyAnnotations(node); err != nil { + return nil, err + } + n, err := node.Pipe(yaml.GetAnnotation(kioutil.PathAnnotation)) + if err != nil { + return nil, err + } + path := n.YNode().Value + if p.isExcluded(path) { + continue + } + filteredNodes = append(filteredNodes, node) + } + return filteredNodes, nil +} + +func (p PruningLocalPackageReader) isExcluded(path string) bool { + for _, e := range p.Exclusions { + if strings.HasPrefix(path, e) { + return true + } + } + return false +} diff --git a/internal/util/update/replace.go b/pkg/lib/update/replace.go similarity index 100% rename from internal/util/update/replace.go rename to pkg/lib/update/replace.go diff --git a/internal/util/update/replace_test.go b/pkg/lib/update/replace_test.go similarity index 98% rename from internal/util/update/replace_test.go rename to pkg/lib/update/replace_test.go index 64b320a4e5..10733fe222 100644 --- a/internal/util/update/replace_test.go +++ b/pkg/lib/update/replace_test.go @@ -21,8 +21,8 @@ import ( "github.com/kptdev/kpt/internal/testutil" "github.com/kptdev/kpt/internal/testutil/pkgbuilder" - "github.com/kptdev/kpt/internal/util/update" - updatetypes "github.com/kptdev/kpt/pkg/lib/update/updatetypes" + "github.com/kptdev/kpt/pkg/lib/update" + "github.com/kptdev/kpt/pkg/lib/update/updatetypes" "github.com/stretchr/testify/assert" ) diff --git a/internal/util/update/resource-merge.go b/pkg/lib/update/resource-merge.go similarity index 97% rename from internal/util/update/resource-merge.go rename to pkg/lib/update/resource-merge.go index 78592a7c2c..426f0c2859 100644 --- a/internal/util/update/resource-merge.go +++ b/pkg/lib/update/resource-merge.go @@ -22,12 +22,11 @@ import ( "strings" "github.com/kptdev/kpt/pkg/lib/types" + merge4 "github.com/kptdev/kpt/pkg/lib/update/merge3" "sigs.k8s.io/kustomize/kyaml/pathutil" pkgdiff "github.com/kptdev/kpt/internal/util/diff" - "github.com/kptdev/kpt/internal/util/merge" "github.com/kptdev/kpt/internal/util/pkgutil" - "github.com/kptdev/kpt/internal/util/update/merge3" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" @@ -168,7 +167,7 @@ func (u ResourceMergeUpdater) mergePackage(localPath, updatedPath, originalPath, return err } - mergedKos, err := merge3.Merge( + mergedKos, err := merge4.Merge( originalKos, updatedKos, destinationKos, crdSchemas, ) if err != nil { @@ -372,8 +371,8 @@ func getCrdSchemas(updated fn.KubeObjects, destination fn.KubeObjects) ([]byte, var kubeobjects fn.KubeObjects copy(kubeobjects, updated) kubeobjects = append(kubeobjects, destination...) - _, crdObjects := merge3.FilterCrds(kubeobjects) - crdSchemas, err := merge3.SchemasFromCrdKubeObjects(crdObjects) + _, crdObjects := merge4.FilterCrds(kubeobjects) + crdSchemas, err := merge4.SchemasFromCrdKubeObjects(crdObjects) if err != nil { klog.Error("An error occurred during CRD extraction: %w", err) return nil, nil @@ -392,7 +391,7 @@ func loadResourcesFromDirectory(directoryPath string, mergeSourceAnnotation stri if err != nil { return nil, err } - reader := merge.PruningLocalPackageReader{ + reader := PruningLocalPackageReader{ LocalPackageReader: kio.LocalPackageReader{ PackagePath: directoryPath, IncludeSubpackages: false, diff --git a/internal/util/update/resource-merge_test.go b/pkg/lib/update/resource-merge_test.go similarity index 98% rename from internal/util/update/resource-merge_test.go rename to pkg/lib/update/resource-merge_test.go index 011f92ce63..b68072d607 100644 --- a/internal/util/update/resource-merge_test.go +++ b/pkg/lib/update/resource-merge_test.go @@ -21,8 +21,8 @@ import ( "github.com/kptdev/kpt/internal/testutil" "github.com/kptdev/kpt/internal/testutil/pkgbuilder" - "github.com/kptdev/kpt/internal/util/update" - updatetypes "github.com/kptdev/kpt/pkg/lib/update/updatetypes" + "github.com/kptdev/kpt/pkg/lib/update" + "github.com/kptdev/kpt/pkg/lib/update/updatetypes" "github.com/stretchr/testify/assert" ) diff --git a/pkg/lib/update/update.go b/pkg/lib/update/update.go index dfd14e26f6..5cba10bd73 100644 --- a/pkg/lib/update/update.go +++ b/pkg/lib/update/update.go @@ -1,4 +1,4 @@ -// Copyright 2026 The kpt Authors +// Copyright 2019,2026 The kpt Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -12,14 +12,482 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package update returns a kpt updating function to a user +// Package update contains libraries for updating packages. package update import ( - internalupdate "github.com/kptdev/kpt/internal/util/update" - updatetypes "github.com/kptdev/kpt/pkg/lib/update/updatetypes" + "context" + "fmt" + "os" + "path" + "path/filepath" + "strings" + + "github.com/kptdev/kpt/internal/gitutil" + "github.com/kptdev/kpt/internal/util/fetch" + "github.com/kptdev/kpt/internal/util/git" + "github.com/kptdev/kpt/internal/util/pkgutil" + "github.com/kptdev/kpt/internal/util/stack" + kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" + "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" + "github.com/kptdev/kpt/pkg/lib/errors" + "github.com/kptdev/kpt/pkg/lib/pkg" + "github.com/kptdev/kpt/pkg/lib/types" + "github.com/kptdev/kpt/pkg/lib/update/updatetypes" + "github.com/kptdev/kpt/pkg/lib/util/addmergecomment" + "github.com/kptdev/kpt/pkg/printer" + "sigs.k8s.io/kustomize/kyaml/copyutil" + "sigs.k8s.io/kustomize/kyaml/filesys" ) +// PkgNotGitRepoError is the error type returned if the package being updated is not inside +// a git repository. +type PkgNotGitRepoError struct { + Path types.UniquePath +} + +func (p *PkgNotGitRepoError) Error() string { + return fmt.Sprintf("package %q is not a git repository", p.Path.String()) +} + +// PkgRepoDirtyError is the error type returned if the package being updated contains +// uncommitted changes. +type PkgRepoDirtyError struct { + Path types.UniquePath +} + +func (p *PkgRepoDirtyError) Error() string { + return fmt.Sprintf("package %q contains uncommitted changes", p.Path.String()) +} + +type Options struct { + // RelPackagePath is the relative path of a subpackage to the root. If the + // package is root, the value here will be ".". + RelPackagePath string + + // LocalPath is the absolute path to the package on the local fork. + LocalPath string + + // OriginPath is the absolute path to the package in the on-disk clone + // of the origin ref of the repo. + OriginPath string + + // UpdatedPath is the absolute path to the package in the on-disk clone + // of the updated ref of the repo. + UpdatedPath string + + // IsRoot is true if the package is the root, i.e. the clones of + // updated and origin were fetched based on the information in the + // Kptfile from this package. + IsRoot bool +} + +// Updater updates a local package +var strategies = map[kptfilev1.UpdateStrategyType]func() updatetypes.Updater{ + kptfilev1.FastForward: func() updatetypes.Updater { return FastForwardUpdater{} }, + kptfilev1.ForceDeleteReplace: func() updatetypes.Updater { return ReplaceUpdater{} }, + kptfilev1.ResourceMerge: func() updatetypes.Updater { return ResourceMergeUpdater{} }, + kptfilev1.CopyMerge: func() updatetypes.Updater { return CopyMergeUpdater{} }, +} + +// Command updates the contents of a local package to a different version. +type Command struct { + // Pkg captures information about the package that should be updated. + Pkg *pkg.Pkg + + // Ref is the ref to update to + Ref string + + // Strategy is the update strategy to use + Strategy kptfilev1.UpdateStrategyType + + // cachedUpstreamRepos is an upstream repo already fetched for a given repoSpec CloneRef + cachedUpstreamRepos map[string]*gitutil.GitUpstreamRepo +} + func GetUpdater(strategy string) updatetypes.Updater { - return internalupdate.GetUpdater(strategy) + switch strategy { + case "fast-forward": + return FastForwardUpdater{} + case "force-delete-replace": + return ReplaceUpdater{} + case "copy-merge": + return CopyMergeUpdater{} + default: + return ResourceMergeUpdater{} + } +} + +// Run runs the Command. +func (u *Command) Run(ctx context.Context) error { + const op errors.Op = "update.Run" + pr := printer.FromContextOrDie(ctx) + + if u.Pkg == nil { + return errors.E(op, errors.MissingParam, "pkg must be provided") + } + + rootKf, err := u.Pkg.Kptfile() + if err != nil { + return errors.E(op, u.Pkg.UniquePath, err) + } + + if rootKf.Upstream == nil || rootKf.Upstream.Git == nil { + return errors.E(op, u.Pkg.UniquePath, + fmt.Errorf("package must have an upstream reference")) + } + originalRootKfRef := rootKf.Upstream.Git.Ref + if u.Ref != "" { + rootKf.Upstream.Git.Ref = u.Ref + } + if u.Strategy != "" { + rootKf.Upstream.UpdateStrategy = u.Strategy + } + err = kptfileutil.WriteFile(u.Pkg.UniquePath.String(), rootKf) + if err != nil { + return errors.E(op, u.Pkg.UniquePath, err) + } + if u.cachedUpstreamRepos == nil { + u.cachedUpstreamRepos = make(map[string]*gitutil.GitUpstreamRepo) + } + packageCount := 0 + + // Use stack to keep track of paths with a Kptfile that might contain + // information about remote subpackages. + s := stack.NewPkgStack() + s.Push(u.Pkg) + + for s.Len() > 0 { + p := s.Pop() + packageCount++ + + if err := u.updateRootPackage(ctx, p); err != nil { + return errors.E(op, p.UniquePath, err) + } + + subPkgs, err := p.DirectSubpackages() + if err != nil { + return errors.E(op, p.UniquePath, err) + } + for _, subPkg := range subPkgs { + subKf, err := subPkg.Kptfile() + if err != nil { + return errors.E(op, p.UniquePath, err) + } + + if subKf.Upstream != nil && subKf.Upstream.Git != nil { + // update subpackage kf ref/strategy if current pkg is a subpkg of root pkg or is root pkg + // and if original root pkg ref matches the subpkg ref + if shouldUpdateSubPkgRef(subKf, rootKf, originalRootKfRef) { + updateSubKf(subKf, u.Ref, u.Strategy) + err = kptfileutil.WriteFile(subPkg.UniquePath.String(), subKf) + if err != nil { + return errors.E(op, subPkg.UniquePath, err) + } + } + s.Push(subPkg) + } + } + } + pr.Printf("\nUpdated %d package(s).\n", packageCount) + + // finally, make sure that the merge comments are added to all resources in the updated package + if err := addmergecomment.Process(string(u.Pkg.UniquePath)); err != nil { + return errors.E(op, u.Pkg.UniquePath, err) + } + return nil +} + +// GetCachedUpstreamRepos returns repos cached during update +func (u Command) GetCachedUpstreamRepos() map[string]*gitutil.GitUpstreamRepo { + return u.cachedUpstreamRepos +} + +// updateSubKf updates subpackage with given ref and update strategy +func updateSubKf(subKf *kptfilev1.KptFile, ref string, strategy kptfilev1.UpdateStrategyType) { + // check if explicit ref provided + if ref != "" { + subKf.Upstream.Git.Ref = ref + } + if strategy != "" { + subKf.Upstream.UpdateStrategy = strategy + } +} + +// shouldUpdateSubPkgRef checks if subpkg ref should be updated. +// This is true if pkg has the same upstream repo, upstream directory is within or equal to root pkg directory and original root pkg ref matches the subpkg ref. +func shouldUpdateSubPkgRef(subKf, rootKf *kptfilev1.KptFile, originalRootKfRef string) bool { + return subKf.Upstream.Git.Repo == rootKf.Upstream.Git.Repo && + subKf.Upstream.Git.Ref == originalRootKfRef && + strings.HasPrefix(path.Clean(subKf.Upstream.Git.Directory), path.Clean(rootKf.Upstream.Git.Directory)) +} + +// repoClone is an interface that represents a clone of a repo on the local +// disk. +type repoClone interface { + AbsPath() string +} + +// newNilRepoClone creates a new nilRepoClone that implements the repoClone +// interface +func newNilRepoClone() (*nilRepoClone, error) { + const op errors.Op = "update.newNilRepoClone" + dir, err := os.MkdirTemp("", "kpt-empty-") + if err != nil { + return nil, errors.E(op, errors.IO, fmt.Errorf("errors creating a temporary directory: %w", err)) + } + return &nilRepoClone{ + dir: dir, + }, nil +} + +// nilRepoClone is an implementation of the repoClone interface, but that +// just represents an empty directory. This simplifies the logic for update +// since we don't have to special case situations where we don't have +// upstream and/or origin. +type nilRepoClone struct { + dir string +} + +// AbsPath returns the absolute path to the local directory for the repo. For +// the nilRepoClone, this will always be an empty directory. +func (nrc *nilRepoClone) AbsPath() string { + return nrc.dir +} + +// updateRootPackage updates a local package. It will use the information +// about upstream in the Kptfile to fetch upstream and origin, and then +// recursively traverse the hierarchy to add/update/delete packages. +func (u Command) updateRootPackage(ctx context.Context, p *pkg.Pkg) error { + const op errors.Op = "update.updateRootPackage" + kf, err := p.Kptfile() + if err != nil { + return errors.E(op, p.UniquePath, err) + } + + pr := printer.FromContextOrDie(ctx) + pr.PrintPackage(p, p != u.Pkg) + + g := kf.Upstream.Git + updated := &git.RepoSpec{OrgRepo: g.Repo, Path: g.Directory, Ref: g.Ref} + pr.Printf("Fetching upstream from %s@%s\n", kf.Upstream.Git.Repo, kf.Upstream.Git.Ref) + cloner := fetch.NewCloner(updated, fetch.WithCachedRepo(u.cachedUpstreamRepos)) + if err := cloner.ClonerUsingGitExec(ctx); err != nil { + return errors.E(op, p.UniquePath, err) + } + defer os.RemoveAll(updated.AbsPath()) + + var origin repoClone + if kf.UpstreamLock != nil { + gLock := kf.UpstreamLock.Git + originRepoSpec := &git.RepoSpec{OrgRepo: gLock.Repo, Path: gLock.Directory, Ref: gLock.Commit} + pr.Printf("Fetching origin from %s@%s\n", kf.Upstream.Git.Repo, kf.Upstream.Git.Ref) + if err := fetch.NewCloner(originRepoSpec, fetch.WithCachedRepo(u.cachedUpstreamRepos)).ClonerUsingGitExec(ctx); err != nil { + return errors.E(op, p.UniquePath, err) + } + origin = originRepoSpec + } else { + origin, err = newNilRepoClone() + if err != nil { + return errors.E(op, p.UniquePath, err) + } + } + defer os.RemoveAll(origin.AbsPath()) + + s := stack.New() + s.Push(".") + + for s.Len() > 0 { + relPath := s.Pop() + localPath := filepath.Join(p.UniquePath.String(), relPath) + updatedPath := filepath.Join(updated.AbsPath(), relPath) + originPath := filepath.Join(origin.AbsPath(), relPath) + + isRoot := false + if relPath == "." { + isRoot = true + } + + if err := u.updatePackage(ctx, relPath, localPath, updatedPath, originPath, isRoot); err != nil { + return errors.E(op, p.UniquePath, err) + } + + paths, err := pkgutil.FindSubpackagesForPaths(pkg.Remote, false, + localPath, updatedPath, originPath) + if err != nil { + return errors.E(op, p.UniquePath, err) + } + for _, path := range paths { + s.Push(filepath.Join(relPath, path)) + } + } + + if err := kptfileutil.UpdateUpstreamLockFromGit(p.UniquePath.String(), updated); err != nil { + return errors.E(op, p.UniquePath, err) + } + return nil +} + +// updatePackage takes care of updating a single package. The absolute paths to +// the local, updated and origin packages are provided, as well as the path to the +// package relative to the root. +// The last parameter tells if this package is the root, i.e. the package +// from which we got the information about upstream and origin. +// +//nolint:gocyclo +func (u Command) updatePackage(ctx context.Context, subPkgPath, localPath, updatedPath, originPath string, isRootPkg bool) error { + const op errors.Op = "update.updatePackage" + pr := printer.FromContextOrDie(ctx) + + localExists, err := pkg.IsPackageDir(filesys.FileSystemOrOnDisk{}, localPath) + if err != nil { + return errors.E(op, types.UniquePath(localPath), err) + } + + // We need to handle the root package special here, since the copies + // from updated and origin might not have a Kptfile at the root. + updatedExists := isRootPkg + if !isRootPkg { + updatedExists, err = pkg.IsPackageDir(filesys.FileSystemOrOnDisk{}, updatedPath) + if err != nil { + return errors.E(op, types.UniquePath(localPath), err) + } + } + + originExists := isRootPkg + if !isRootPkg { + originExists, err = pkg.IsPackageDir(filesys.FileSystemOrOnDisk{}, originPath) + if err != nil { + return errors.E(op, types.UniquePath(localPath), err) + } + } + + switch { + case !originExists && !localExists && !updatedExists: + break + // Check if subpackage has been added both in upstream and in local. We + // can't make a sane merge here, so we treat it as an error. + case !originExists && localExists && updatedExists: + pr.Printf("Package %q added in both local and upstream.\n", packageName(localPath)) + return errors.E(op, types.UniquePath(localPath), + fmt.Errorf("subpackage %q added in both upstream and local", subPkgPath)) + + // Package added in upstream. We just copy the package. If the package + // contains any unfetched subpackages, those will be handled when we traverse + // the package hierarchy and that package is the root. + case !originExists && !localExists && updatedExists: + pr.Printf("Adding package %q from upstream.\n", packageName(localPath)) + if err := pkgutil.CopyPackage(updatedPath, localPath, !isRootPkg, pkg.None); err != nil { + return errors.E(op, types.UniquePath(localPath), err) + } + + // Package added locally, so no action needed. + case !originExists && localExists && !updatedExists: + break + + // Package deleted from both upstream and local, so no action needed. + case originExists && !localExists && !updatedExists: + break + + // Package deleted from local + // In this case we assume the user knows what they are doing, so + // we don't re-add the updated package from upstream. + case originExists && !localExists && updatedExists: + pr.Printf("Ignoring package %q in upstream since it is deleted from local.\n", packageName(localPath)) + + // Package deleted from upstream + case originExists && localExists && !updatedExists: + // Check the diff. If there are local changes, we keep the subpackage. + diff, err := copyutil.Diff(originPath, localPath) + if err != nil { + return errors.E(op, types.UniquePath(localPath), err) + } + if diff.Len() == 0 { + pr.Printf("Deleting package %q from local since it is removed in upstream.\n", packageName(localPath)) + if err := os.RemoveAll(localPath); err != nil { + return errors.E(op, types.UniquePath(localPath), err) + } + } else { + pr.Printf("Package %q deleted from upstream, but keeping local since it has changes.\n", packageName(localPath)) + } + default: + if err := u.mergePackage(ctx, localPath, updatedPath, originPath, subPkgPath, isRootPkg); err != nil { + return errors.E(op, types.UniquePath(localPath), err) + } + } + return nil +} + +func (u Command) mergePackage(ctx context.Context, localPath, updatedPath, originPath, relPath string, isRootPkg bool) error { + const op errors.Op = "update.mergePackage" + pr := printer.FromContextOrDie(ctx) + // at this point, the localPath, updatedPath and originPath exists and are about to be merged + // make sure that the merge comments are added to all of them so that they are merged accurately + if err := addmergecomment.Process(localPath, updatedPath, originPath); err != nil { + return errors.E(op, types.UniquePath(localPath), + fmt.Errorf("failed to add merge comments %q", err.Error())) + } + updatedUnfetched, err := pkg.IsPackageUnfetched(updatedPath) + if err != nil { + if !errors.Is(err, os.ErrNotExist) || !isRootPkg { + return errors.E(op, types.UniquePath(localPath), err) + } + // For root packages, there might not be a Kptfile in the upstream repo. + updatedUnfetched = false + } + + originUnfetched, err := pkg.IsPackageUnfetched(originPath) + if err != nil { + if !errors.Is(err, os.ErrNotExist) || !isRootPkg { + return errors.E(op, types.UniquePath(localPath), err) + } + // For root packages, there might not be a Kptfile in origin. + originUnfetched = false + } + + switch { + case updatedUnfetched && originUnfetched: + fallthrough + case updatedUnfetched && !originUnfetched: + // updated is unfetched, so can't have changes except for Kptfile. + // we can just merge that one. + return kptfileutil.UpdateKptfile(localPath, updatedPath, originPath, true) + case !updatedUnfetched && originUnfetched: + // This means that the package was unfetched when local forked from upstream, + // so the local fork and upstream might have fetched different versions of + // the package. We just return an error here. + // We might be able to compare the commit SHAs from local and updated + // to determine if they share the common upstream and then fetch origin + // using the common commit SHA. But this is a very advanced scenario, + // so we just return the error for now. + return errors.E(op, types.UniquePath(localPath), fmt.Errorf("no origin available for package")) + default: + // Both exists, so just go ahead as normal. + } + + pkgKf, err := kptfileutil.ReadKptfile(filesys.FileSystemOrOnDisk{}, localPath) + if err != nil { + return errors.E(op, types.UniquePath(localPath), err) + } + updater, found := strategies[pkgKf.Upstream.UpdateStrategy] + if !found { + return errors.E(op, types.UniquePath(localPath), + fmt.Errorf("unrecognized update strategy %s", u.Strategy)) + } + pr.Printf("Updating package %q with strategy %q.\n", packageName(localPath), pkgKf.Upstream.UpdateStrategy) + if err := updater().Update(updatetypes.Options{ + RelPackagePath: relPath, + LocalPath: localPath, + UpdatedPath: updatedPath, + OriginPath: originPath, + IsRoot: isRootPkg, + }); err != nil { + return errors.E(op, types.UniquePath(localPath), err) + } + + return nil +} + +func packageName(path string) string { + return filepath.Base(path) } diff --git a/internal/util/update/update_test.go b/pkg/lib/update/update_test.go similarity index 99% rename from internal/util/update/update_test.go rename to pkg/lib/update/update_test.go index 2a1c929099..230ad6951a 100644 --- a/internal/util/update/update_test.go +++ b/pkg/lib/update/update_test.go @@ -25,9 +25,9 @@ import ( "github.com/kptdev/kpt/internal/testutil" "github.com/kptdev/kpt/internal/testutil/pkgbuilder" - "github.com/kptdev/kpt/internal/util/update" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" + update2 "github.com/kptdev/kpt/pkg/lib/update" "github.com/kptdev/kpt/pkg/printer/fake" "github.com/stretchr/testify/assert" "sigs.k8s.io/kustomize/kyaml/copyutil" @@ -74,7 +74,7 @@ func TestCommand_Run_noRefChanges(t *testing.T) { return } upstreamRepo := g.Repos[testutil.Upstream] - cmd := &update.Command{ + cmd := &update2.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Strategy: strategy, } @@ -137,7 +137,7 @@ func TestCommand_Run_subDir(t *testing.T) { upstreamRepo := g.Repos[testutil.Upstream] // Update the local package - if !assert.NoError(t, (&update.Command{ + if !assert.NoError(t, (&update2.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Ref: "v1.2", Strategy: strategy, @@ -193,7 +193,7 @@ func TestCommand_Run_noChanges(t *testing.T) { upstreamRepo := g.Repos[testutil.Upstream] // Update the local package - err := (&update.Command{ + err := (&update2.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Strategy: u.updater, }).Run(fake.CtxWithDefaultPrinter()) @@ -375,7 +375,7 @@ func TestCommand_Run_localPackageChanges(t *testing.T) { } // run the command - err = (&update.Command{ + err = (&update2.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Ref: masterBranch, Strategy: tc.strategy, @@ -445,7 +445,7 @@ func TestCommand_Run_toBranchRef(t *testing.T) { upstreamRepo := g.Repos[testutil.Upstream] // Update the local package - if !assert.NoError(t, (&update.Command{ + if !assert.NoError(t, (&update2.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Strategy: strategy, Ref: "exp", @@ -809,7 +809,7 @@ func TestCommand_Run_toBranchRefWithSubpkgs(t *testing.T) { } // Update the local package - if !assert.NoError(t, (&update.Command{ + if !assert.NoError(t, (&update2.Command{ Pkg: testutil.CreatePkgOrFail(t, path.Join(g.LocalWorkspace.FullPackagePath(), tc.updateSubPkg)), Strategy: tc.strategy, Ref: tc.updateRef, @@ -855,7 +855,7 @@ func TestCommand_Run_toTagRef(t *testing.T) { upstreamRepo := g.Repos[testutil.Upstream] // Update the local package - if !assert.NoError(t, (&update.Command{ + if !assert.NoError(t, (&update2.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Strategy: strategy, Ref: "v1.0", @@ -912,7 +912,7 @@ func TestCommand_ResourceMerge_NonKRMUpdates(t *testing.T) { upstreamRepo := g.Repos[testutil.Upstream] // Update the local package - if !assert.NoError(t, (&update.Command{ + if !assert.NoError(t, (&update2.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Strategy: strategy, Ref: "v1.0", @@ -955,7 +955,7 @@ func TestCommand_Run_noUpstreamReference(t *testing.T) { testutil.AddKptfileToWorkspace(t, w, kf) // Update the local package - err := (&update.Command{ + err := (&update2.Command{ Pkg: testutil.CreatePkgOrFail(t, w.FullPackagePath()), }).Run(fake.CtxWithDefaultPrinter()) @@ -968,7 +968,7 @@ func TestCommand_Run_failInvalidPath(t *testing.T) { strategy := kptfilev1.UpdateStrategies[i] t.Run(string(strategy), func(t *testing.T) { path := filepath.Join("fake", "path") - err := (&update.Command{ + err := (&update2.Command{ Pkg: testutil.CreatePkgOrFail(t, path), Strategy: strategy, }).Run(fake.CtxWithDefaultPrinter()) @@ -1033,7 +1033,7 @@ func TestCommand_Run_badUpstreamLock(t *testing.T) { } // Update the local package. - err = (&update.Command{ + err = (&update2.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), }).Run(fake.CtxWithDefaultPrinter()) @@ -1068,7 +1068,7 @@ func TestCommand_Run_failInvalidRef(t *testing.T) { return } - err := (&update.Command{ + err := (&update2.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Ref: "exp", Strategy: strategy, @@ -1126,7 +1126,7 @@ func TestCommand_Run_manualChange(t *testing.T) { return } - err := (&update.Command{ + err := (&update2.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), }).Run(fake.CtxWithDefaultPrinter()) if !assert.NoError(t, err) { @@ -1180,7 +1180,7 @@ func TestCommand_Run_symlinks(t *testing.T) { } upstreamRepo := g.Repos[testutil.Upstream] - err := (&update.Command{ + err := (&update2.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), }).Run(fake.CtxWithDefaultPrinter()) if !assert.NoError(t, err) { @@ -1228,7 +1228,7 @@ func TestCommand_Run_badStrategy(t *testing.T) { } // Update the local package - err := (&update.Command{ + err := (&update2.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Strategy: strategy, }).Run(fake.CtxWithDefaultPrinter()) @@ -2007,7 +2007,7 @@ func TestCommand_Run_local_subpackages(t *testing.T) { return } - err := (&update.Command{ + err := (&update2.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Strategy: strategy, }).Run(fake.CtxWithDefaultPrinter()) @@ -3543,7 +3543,7 @@ func TestRun_remote_subpackages(t *testing.T) { return } - err := (&update.Command{ + err := (&update2.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), }).Run(fake.CtxWithDefaultPrinter()) @@ -3688,7 +3688,7 @@ func TestRootPackageIsUnfetched(t *testing.T) { } testutil.AddKptfileToWorkspace(t, w, kf) - err := (&update.Command{ + err := (&update2.Command{ Pkg: testutil.CreatePkgOrFail(t, w.FullPackagePath()), }).Run(fake.CtxWithDefaultPrinter()) if !assert.NoError(t, err) { @@ -3759,7 +3759,7 @@ func TestMultiUpdateCache(t *testing.T) { UpdateStrategy: kptfilev1.ResourceMerge, } testutil.AddKptfileToWorkspace(t, w, kf) - cmd := update.Command{ + cmd := update2.Command{ Pkg: testutil.CreatePkgOrFail(t, w.FullPackagePath()), } err := cmd.Run(fake.CtxWithDefaultPrinter()) @@ -3864,7 +3864,7 @@ func TestReplaceNonKRMFiles(t *testing.T) { // expectedLocal. err = os.WriteFile(filepath.Join(updated, "new.yaml"), []byte("a: b"), 0600) assert.NoError(t, err) - err = update.ReplaceNonKRMFiles(updated, original, local) + err = update2.ReplaceNonKRMFiles(updated, original, local) assert.NoError(t, err) tg := testutil.TestGitRepo{} tg.AssertEqual(t, local, expectedLocal, false) diff --git a/pkg/lib/util/addmergecomment/addmergecomment.go b/pkg/lib/util/addmergecomment/addmergecomment.go index 0ac5c9b2b2..92f7f5fd31 100644 --- a/pkg/lib/util/addmergecomment/addmergecomment.go +++ b/pkg/lib/util/addmergecomment/addmergecomment.go @@ -19,7 +19,6 @@ import ( "os" "strings" - "github.com/kptdev/kpt/internal/util/merge" "sigs.k8s.io/kustomize/kyaml/copyutil" "sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/kustomize/kyaml/kio" @@ -37,6 +36,8 @@ const ( upstreamIdentifier = "internal.kpt.dev/upstream-identifier" unknownNamespace = "~C" defaultNamespace = "default" + + MergeCommentPrefix = "kpt-merge:" ) // AddMergeComment adds merge comments with format "kpt-merge: namespace/name" @@ -73,8 +74,8 @@ func Process(paths ...string) error { func addUpstreamAnnotation(object *kyaml.RNode, mergeComment string) error { group, _ := resid.ParseGroupVersion(object.GetApiVersion()) var name, namespace string - if strings.Contains(mergeComment, merge.MergeCommentPrefix) { - nsAndName := merge.NsAndNameForMerge(mergeComment) + if strings.Contains(mergeComment, MergeCommentPrefix) { + nsAndName := NsAndNameForMerge(mergeComment) namespace = nsAndName[0] name = nsAndName[1] } else { @@ -93,6 +94,23 @@ func addUpstreamAnnotation(object *kyaml.RNode, mergeComment string) error { return object.PipeE(kyaml.SetAnnotation(upstreamIdentifier, upstreamIdentifierValue)) } +// NsAndNameForMerge returns the namespace and name for merge +// from the line comment on the metadata field +// e.g. metadata: # kpt-merge: default/foo returns [default, foo] +func NsAndNameForMerge(metadataComment string) []string { + comment := strings.TrimPrefix(metadataComment, "#") + comment = strings.TrimSpace(comment) + if !strings.HasPrefix(comment, MergeCommentPrefix) { + return nil + } + comment = strings.TrimPrefix(comment, MergeCommentPrefix) + nsAndName := strings.SplitN(strings.TrimSpace(comment), "/", 2) + if len(nsAndName) != 2 { + return nil + } + return nsAndName +} + // Filter implements kyaml.Filter // this filter adds merge comment with format "kpt-merge: namespace/name" to // the input resource, if the namespace field doesn't exist on the resource, @@ -123,8 +141,8 @@ func (amc *AddMergeComment) Filter(object *kyaml.RNode) (*kyaml.RNode, error) { } // Only add merge comment if merge comment does not present - if !strings.Contains(mf.Key.YNode().LineComment, merge.MergeCommentPrefix) { - mf.Key.YNode().LineComment = fmt.Sprintf("%s %s/%s", merge.MergeCommentPrefix, rm.Namespace, rm.Name) + if !strings.Contains(mf.Key.YNode().LineComment, MergeCommentPrefix) { + mf.Key.YNode().LineComment = fmt.Sprintf("%s %s/%s", MergeCommentPrefix, rm.Namespace, rm.Name) } // We will migrate kpt-merge comment to upstream-identifier annotation. As an intermediate stage, this filter // preserves the mergeComment behavior to guarantee the backward compatibility. diff --git a/internal/alpha/printers/table/collector.go b/pkg/printer/table/collector.go similarity index 100% rename from internal/alpha/printers/table/collector.go rename to pkg/printer/table/collector.go diff --git a/internal/alpha/printers/table/printer.go b/pkg/printer/table/printer.go similarity index 98% rename from internal/alpha/printers/table/printer.go rename to pkg/printer/table/printer.go index 39eda062a7..e295458798 100644 --- a/internal/alpha/printers/table/printer.go +++ b/pkg/printer/table/printer.go @@ -10,7 +10,7 @@ import ( "time" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" - "k8s.io/cli-runtime/pkg/genericclioptions" + "k8s.io/cli-runtime/pkg/genericiooptions" "k8s.io/klog/v2" "sigs.k8s.io/cli-utils/pkg/apply/event" "sigs.k8s.io/cli-utils/pkg/common" @@ -21,7 +21,7 @@ import ( ) type Printer struct { - IOStreams genericclioptions.IOStreams + IOStreams genericiooptions.IOStreams } func (t *Printer) Print(ch <-chan event.Event, _ common.DryRunStrategy, _ bool) error { From 73a391f98cc3486cfe87b56b9ea597b3954c7033 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=B3zes=20L=C3=A1szl=C3=B3=20M=C3=A1t=C3=A9?= Date: Thu, 7 May 2026 10:41:16 +0200 Subject: [PATCH 3/6] move more things from internal/util MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mózes László Máté --- commands/fn/render/cmdrender.go | 4 +- commands/live/apply/cmdapply.go | 2 +- commands/live/destroy/cmddestroy.go | 2 +- commands/live/init/cmdliveinit.go | 2 +- commands/pkg/diff/cmddiff.go | 2 +- internal/util/fetch/fetch.go | 3 +- internal/util/get/get.go | 4 +- internal/util/httputil/httputil.go | 38 ------------------- pkg/lib/errors/validate.go | 2 +- .../render => pkg/lib/kptops}/executor.go | 7 ++-- .../lib/kptops}/executor_test.go | 2 +- pkg/lib/kptops/fs_test.go | 5 +-- pkg/lib/kptops/render.go | 3 +- {internal/util => pkg/lib/pkg}/diff/diff.go | 3 +- .../util => pkg/lib/pkg}/diff/diff_test.go | 2 +- .../util => pkg/lib/pkg}/diff/pkgdiff.go | 6 +-- .../util => pkg/lib/pkg}/diff/pkgdiff_test.go | 0 .../pkgutil/pkgutil.go => pkg/lib/pkg/util.go | 15 ++++---- .../lib/pkg/util_test.go | 37 +++++++++--------- pkg/lib/update/copy-merge.go | 5 +-- pkg/lib/update/fastforward.go | 13 +++---- pkg/lib/update/merge3/strategy.go | 2 +- pkg/lib/update/replace.go | 7 ++-- pkg/lib/update/resource-merge.go | 17 ++++----- pkg/lib/update/update.go | 7 ++-- .../lib}/util/attribution/attribution.go | 0 .../lib}/util/attribution/attribution_test.go | 0 {internal => pkg/lib}/util/stack/stack.go | 0 {internal => pkg/lib}/util/strings/strings.go | 0 .../lib}/util/strings/strings_test.go | 0 pkg/live/load.go | 2 +- .../printerutil.go => pkg/printer/util.go | 6 +-- thirdparty/kyaml/runfn/runfn.go | 3 +- 33 files changed, 74 insertions(+), 127 deletions(-) delete mode 100644 internal/util/httputil/httputil.go rename {internal/util/render => pkg/lib/kptops}/executor.go (99%) rename {internal/util/render => pkg/lib/kptops}/executor_test.go (99%) rename {internal/util => pkg/lib/pkg}/diff/diff.go (99%) rename {internal/util => pkg/lib/pkg}/diff/diff_test.go (99%) rename {internal/util => pkg/lib/pkg}/diff/pkgdiff.go (95%) rename {internal/util => pkg/lib/pkg}/diff/pkgdiff_test.go (100%) rename internal/util/pkgutil/pkgutil.go => pkg/lib/pkg/util.go (95%) rename internal/util/pkgutil/pkgutil_test.go => pkg/lib/pkg/util_test.go (94%) rename {internal => pkg/lib}/util/attribution/attribution.go (100%) rename {internal => pkg/lib}/util/attribution/attribution_test.go (100%) rename {internal => pkg/lib}/util/stack/stack.go (100%) rename {internal => pkg/lib}/util/strings/strings.go (100%) rename {internal => pkg/lib}/util/strings/strings_test.go (100%) rename internal/util/printerutil/printerutil.go => pkg/printer/util.go (90%) diff --git a/commands/fn/render/cmdrender.go b/commands/fn/render/cmdrender.go index 43426ebef9..507eeb0ab1 100644 --- a/commands/fn/render/cmdrender.go +++ b/commands/fn/render/cmdrender.go @@ -25,7 +25,7 @@ import ( docs "github.com/kptdev/kpt/internal/docs/generated/fndocs" "github.com/kptdev/kpt/internal/util/argutil" "github.com/kptdev/kpt/internal/util/pathutil" - "github.com/kptdev/kpt/internal/util/render" + "github.com/kptdev/kpt/pkg/lib/kptops" "github.com/kptdev/kpt/pkg/lib/runneroptions" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" "github.com/kptdev/kpt/pkg/printer" @@ -130,7 +130,7 @@ func (r *Runner) runE(_ *cobra.Command, _ []string) error { if err != nil { return err } - executor := render.Renderer{ + executor := kptops.Renderer{ PkgPath: absPkgPath, ResultsDirPath: r.resultsDirPath, Output: output, diff --git a/commands/live/apply/cmdapply.go b/commands/live/apply/cmdapply.go index 4b2c97ebcb..db20cde1c6 100644 --- a/commands/live/apply/cmdapply.go +++ b/commands/live/apply/cmdapply.go @@ -22,8 +22,8 @@ import ( "github.com/kptdev/kpt/internal/docs/generated/livedocs" "github.com/kptdev/kpt/internal/util/argutil" - "github.com/kptdev/kpt/internal/util/strings" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" + "github.com/kptdev/kpt/pkg/lib/util/strings" "github.com/kptdev/kpt/pkg/live" alphaprinterstable "github.com/kptdev/kpt/pkg/printer/table" "github.com/kptdev/kpt/pkg/status" diff --git a/commands/live/destroy/cmddestroy.go b/commands/live/destroy/cmddestroy.go index bc274f2578..09b710cc35 100644 --- a/commands/live/destroy/cmddestroy.go +++ b/commands/live/destroy/cmddestroy.go @@ -21,7 +21,7 @@ import ( "github.com/kptdev/kpt/internal/docs/generated/livedocs" "github.com/kptdev/kpt/internal/util/argutil" - "github.com/kptdev/kpt/internal/util/strings" + "github.com/kptdev/kpt/pkg/lib/util/strings" "github.com/kptdev/kpt/pkg/live" "github.com/kptdev/kpt/pkg/status" "github.com/spf13/cobra" diff --git a/commands/live/init/cmdliveinit.go b/commands/live/init/cmdliveinit.go index 37b3132ce7..e0e9da0aa8 100644 --- a/commands/live/init/cmdliveinit.go +++ b/commands/live/init/cmdliveinit.go @@ -26,7 +26,6 @@ import ( "time" "github.com/kptdev/kpt/internal/docs/generated/livedocs" - "github.com/kptdev/kpt/internal/util/attribution" "github.com/kptdev/kpt/internal/util/pathutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" rgfilev1alpha1 "github.com/kptdev/kpt/pkg/api/resourcegroup/v1alpha1" @@ -34,6 +33,7 @@ import ( "github.com/kptdev/kpt/pkg/lib/errors" "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/types" + "github.com/kptdev/kpt/pkg/lib/util/attribution" "github.com/kptdev/kpt/pkg/printer" "github.com/spf13/cobra" "k8s.io/cli-runtime/pkg/genericclioptions" diff --git a/commands/pkg/diff/cmddiff.go b/commands/pkg/diff/cmddiff.go index 273a05701b..8d731a6a6c 100644 --- a/commands/pkg/diff/cmddiff.go +++ b/commands/pkg/diff/cmddiff.go @@ -20,9 +20,9 @@ import ( "github.com/kptdev/kpt/internal/docs/generated/pkgdocs" "github.com/kptdev/kpt/internal/util/argutil" - "github.com/kptdev/kpt/internal/util/diff" "github.com/kptdev/kpt/internal/util/pathutil" "github.com/kptdev/kpt/pkg/lib/pkg" + "github.com/kptdev/kpt/pkg/lib/pkg/diff" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" "github.com/kptdev/kpt/pkg/printer" "github.com/spf13/cobra" diff --git a/internal/util/fetch/fetch.go b/internal/util/fetch/fetch.go index 0d15d7e2cc..893f3ec23c 100644 --- a/internal/util/fetch/fetch.go +++ b/internal/util/fetch/fetch.go @@ -29,7 +29,6 @@ import ( "github.com/kptdev/kpt/internal/gitutil" "github.com/kptdev/kpt/internal/util/git" - "github.com/kptdev/kpt/internal/util/pkgutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/pkg" @@ -142,7 +141,7 @@ func (c *Cloner) cloneAndCopy(ctx context.Context, dest string) error { sourcePath := filepath.Join(c.repoSpec.Dir, c.repoSpec.Path) pr.Printf("Adding package %q.\n", strings.TrimPrefix(c.repoSpec.Path, "/")) - if err := pkgutil.CopyPackage(sourcePath, dest, true, pkg.All); err != nil { + if err := pkg.CopyPackage(sourcePath, dest, true, pkg.All); err != nil { return errors.E(op, types.UniquePath(dest), err) } diff --git a/internal/util/get/get.go b/internal/util/get/get.go index ca32f1b76a..12bc024b37 100644 --- a/internal/util/get/get.go +++ b/internal/util/get/get.go @@ -24,10 +24,8 @@ import ( "strings" "github.com/kptdev/kpt/internal/hook" - "github.com/kptdev/kpt/internal/util/attribution" "github.com/kptdev/kpt/internal/util/fetch" "github.com/kptdev/kpt/internal/util/pathutil" - "github.com/kptdev/kpt/internal/util/stack" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" @@ -35,6 +33,8 @@ import ( "github.com/kptdev/kpt/pkg/lib/runneroptions" "github.com/kptdev/kpt/pkg/lib/types" "github.com/kptdev/kpt/pkg/lib/util/addmergecomment" + "github.com/kptdev/kpt/pkg/lib/util/attribution" + "github.com/kptdev/kpt/pkg/lib/util/stack" "github.com/kptdev/kpt/pkg/printer" "sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/kustomize/kyaml/kio" diff --git a/internal/util/httputil/httputil.go b/internal/util/httputil/httputil.go deleted file mode 100644 index e59674d680..0000000000 --- a/internal/util/httputil/httputil.go +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2021 The kpt Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package httputil - -import ( - "io" - "net/http" -) - -// FetchContent fetches the content from the input url -func FetchContent(url string) (string, error) { - req, err := http.NewRequest("GET", url, nil) - if err != nil { - return "", err - } - res, err := http.DefaultClient.Do(req) - if err != nil { - return "", err - } - defer res.Body.Close() - body, err := io.ReadAll(res.Body) - if err != nil { - return "", err - } - return string(body), nil -} diff --git a/pkg/lib/errors/validate.go b/pkg/lib/errors/validate.go index c098f1763d..ca9f7515c0 100644 --- a/pkg/lib/errors/validate.go +++ b/pkg/lib/errors/validate.go @@ -17,7 +17,7 @@ package errors import ( "fmt" - "github.com/kptdev/kpt/internal/util/strings" + "github.com/kptdev/kpt/pkg/lib/util/strings" ) // ValidationError is an error type used when validation fails. diff --git a/internal/util/render/executor.go b/pkg/lib/kptops/executor.go similarity index 99% rename from internal/util/render/executor.go rename to pkg/lib/kptops/executor.go index f2e0ece7de..044a9fb0e7 100644 --- a/internal/util/render/executor.go +++ b/pkg/lib/kptops/executor.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package render +package kptops import ( "context" @@ -23,8 +23,6 @@ import ( "slices" "strings" - "github.com/kptdev/kpt/internal/util/attribution" - "github.com/kptdev/kpt/internal/util/printerutil" fnresult "github.com/kptdev/kpt/pkg/api/fnresult/v1" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/fn" @@ -34,6 +32,7 @@ import ( "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/runneroptions" "github.com/kptdev/kpt/pkg/lib/types" + "github.com/kptdev/kpt/pkg/lib/util/attribution" "github.com/kptdev/kpt/pkg/printer" "k8s.io/klog/v2" "sigs.k8s.io/kustomize/kyaml/filesys" @@ -308,7 +307,7 @@ func (e *Renderer) saveFnResults(ctx context.Context, fnResults *fnresult.Result return fmt.Errorf("failed to save function results: %w", err) } - printerutil.PrintFnResultInfo(ctx, resultsFile, false) + printer.PrintFnResultInfo(ctx, resultsFile, false) return nil } diff --git a/internal/util/render/executor_test.go b/pkg/lib/kptops/executor_test.go similarity index 99% rename from internal/util/render/executor_test.go rename to pkg/lib/kptops/executor_test.go index 434ffb59d0..a208d39a4a 100644 --- a/internal/util/render/executor_test.go +++ b/pkg/lib/kptops/executor_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package render +package kptops import ( "bytes" diff --git a/pkg/lib/kptops/fs_test.go b/pkg/lib/kptops/fs_test.go index d989f2fe15..1a03caf791 100644 --- a/pkg/lib/kptops/fs_test.go +++ b/pkg/lib/kptops/fs_test.go @@ -17,7 +17,6 @@ package kptops import ( "testing" - "github.com/kptdev/kpt/internal/util/render" "github.com/kptdev/kpt/pkg/lib/runneroptions" "github.com/kptdev/kpt/pkg/printer/fake" "sigs.k8s.io/kustomize/kyaml/filesys" @@ -101,7 +100,7 @@ spec: if err := fs.WriteFile("/a/b/c/Kptfile", []byte(kptfile)); err != nil { t.Errorf("Failed to write file: %v", err) } - r := render.Renderer{ + r := Renderer{ PkgPath: "/a/b/c", FileSystem: fs, Runtime: &runtime{}, @@ -215,7 +214,7 @@ spec: t.Errorf("Failed to write file: %v", err) } - r := render.Renderer{ + r := Renderer{ PkgPath: "/app", FileSystem: fs, Runtime: &runtime{}, diff --git a/pkg/lib/kptops/render.go b/pkg/lib/kptops/render.go index b21568ba5f..260c619c8a 100644 --- a/pkg/lib/kptops/render.go +++ b/pkg/lib/kptops/render.go @@ -20,7 +20,6 @@ import ( "io" "os" - "github.com/kptdev/kpt/internal/util/render" fnresult "github.com/kptdev/kpt/pkg/api/fnresult/v1" "github.com/kptdev/kpt/pkg/fn" "github.com/kptdev/kpt/pkg/lib/pkg" @@ -41,7 +40,7 @@ type renderer struct { var _ fn.Renderer = &renderer{} func (r *renderer) Render(ctx context.Context, pkg filesys.FileSystem, opts fn.RenderOptions) (*fnresult.ResultList, error) { - rr := render.Renderer{ + rr := Renderer{ PkgPath: opts.PkgPath, Runtime: opts.Runtime, DisplayName: opts.DisplayName, diff --git a/internal/util/diff/diff.go b/pkg/lib/pkg/diff/diff.go similarity index 99% rename from internal/util/diff/diff.go rename to pkg/lib/pkg/diff/diff.go index 8ecae20bf2..e108c6a6db 100644 --- a/internal/util/diff/diff.go +++ b/pkg/lib/pkg/diff/diff.go @@ -26,7 +26,6 @@ import ( "github.com/kptdev/kpt/internal/gitutil" "github.com/kptdev/kpt/internal/util/fetch" - "github.com/kptdev/kpt/internal/util/pkgutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/pkg" @@ -149,7 +148,7 @@ func (c *Command) Run(ctx context.Context) error { return errors.Errorf("failed to create stage dir for current package: %v", err) } - err = pkgutil.CopyPackage(c.Path, currPkg, true, pkg.Local) + err = pkg.CopyPackage(c.Path, currPkg, true, pkg.Local) if err != nil { return errors.Errorf("failed to stage current package: %v", err) } diff --git a/internal/util/diff/diff_test.go b/pkg/lib/pkg/diff/diff_test.go similarity index 99% rename from internal/util/diff/diff_test.go rename to pkg/lib/pkg/diff/diff_test.go index faea661428..dc037a1cd5 100644 --- a/internal/util/diff/diff_test.go +++ b/pkg/lib/pkg/diff/diff_test.go @@ -28,7 +28,7 @@ import ( "github.com/kptdev/kpt/internal/testutil" "github.com/kptdev/kpt/internal/testutil/pkgbuilder" - "github.com/kptdev/kpt/internal/util/diff" + "github.com/kptdev/kpt/pkg/lib/pkg/diff" "github.com/kptdev/kpt/pkg/printer/fake" "github.com/stretchr/testify/assert" ) diff --git a/internal/util/diff/pkgdiff.go b/pkg/lib/pkg/diff/pkgdiff.go similarity index 95% rename from internal/util/diff/pkgdiff.go rename to pkg/lib/pkg/diff/pkgdiff.go index e41681ed33..5049921c56 100644 --- a/internal/util/diff/pkgdiff.go +++ b/pkg/lib/pkg/diff/pkgdiff.go @@ -19,10 +19,10 @@ import ( "os" "path/filepath" - "github.com/kptdev/kpt/internal/util/attribution" - "github.com/kptdev/kpt/internal/util/pkgutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" + "github.com/kptdev/kpt/pkg/lib/pkg" + "github.com/kptdev/kpt/pkg/lib/util/attribution" "sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/kustomize/kyaml/kio" "sigs.k8s.io/kustomize/kyaml/sets" @@ -107,7 +107,7 @@ func kptfilesEqual(pkg1, pkg2, filePath string) (bool, error) { func pkgSet(pkgPath string) (sets.String, error) { pkgFiles := sets.String{} - if err := pkgutil.WalkPackage(pkgPath, func(path string, _ os.FileInfo, err error) error { + if err := pkg.WalkPackage(pkgPath, func(path string, _ os.FileInfo, err error) error { if err != nil { return err } diff --git a/internal/util/diff/pkgdiff_test.go b/pkg/lib/pkg/diff/pkgdiff_test.go similarity index 100% rename from internal/util/diff/pkgdiff_test.go rename to pkg/lib/pkg/diff/pkgdiff_test.go diff --git a/internal/util/pkgutil/pkgutil.go b/pkg/lib/pkg/util.go similarity index 95% rename from internal/util/pkgutil/pkgutil.go rename to pkg/lib/pkg/util.go index 2e9ee73195..5a05cf2f28 100644 --- a/internal/util/pkgutil/pkgutil.go +++ b/pkg/lib/pkg/util.go @@ -13,7 +13,7 @@ // limitations under the License. // Package pkgutil contains utility functions for packages -package pkgutil +package pkg import ( "io" @@ -24,7 +24,6 @@ import ( kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" - "github.com/kptdev/kpt/pkg/lib/pkg" "sigs.k8s.io/kustomize/kyaml/copyutil" "sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/kustomize/kyaml/kio" @@ -69,8 +68,8 @@ func WalkPackage(src string, c func(string, os.FileInfo, error) error) error { // CopyPackage copies the content of a single package from src to dst. If includeSubpackages // is true, it will copy resources belonging to any subpackages. -func CopyPackage(src, dst string, copyRootKptfile bool, matcher pkg.SubpackageMatcher) error { - subpackagesToCopy, err := pkg.Subpackages(filesys.FileSystemOrOnDisk{}, src, matcher, true) +func CopyPackage(src, dst string, copyRootKptfile bool, matcher SubpackageMatcher) error { + subpackagesToCopy, err := Subpackages(filesys.FileSystemOrOnDisk{}, src, matcher, true) if err != nil { return err } @@ -172,7 +171,7 @@ func CopyPackage(src, dst string, copyRootKptfile bool, matcher pkg.SubpackageMa // RemoveStaleItems removes files and directories from the dst package that were present in the org package, // but are not present in the src package. It does not remove the root Kptfile of the dst package. -func RemoveStaleItems(org, src, dst string, _ bool, _ pkg.SubpackageMatcher) error { +func RemoveStaleItems(org, src, dst string, _ bool, _ SubpackageMatcher) error { var dirsToDelete []string walkErr := filepath.Walk(dst, func(path string, info os.FileInfo, err error) error { if err != nil { @@ -319,10 +318,10 @@ func SubPkgFirstSorter(paths []string) func(i, j int) bool { // FindSubpackagesForPaths traverses the provided package paths // and finds all subpackages using the provided pkgLocatorFunc -func FindSubpackagesForPaths(matcher pkg.SubpackageMatcher, recurse bool, pkgPaths ...string) ([]string, error) { +func FindSubpackagesForPaths(matcher SubpackageMatcher, recurse bool, pkgPaths ...string) ([]string, error) { uniquePaths := make(map[string]bool) for _, path := range pkgPaths { - paths, err := pkg.Subpackages(filesys.FileSystemOrOnDisk{}, path, matcher, recurse) + paths, err := Subpackages(filesys.FileSystemOrOnDisk{}, path, matcher, recurse) if err != nil { return []string{}, err } @@ -369,7 +368,7 @@ func FormatPackage(pkgPath string) { // subpackages. This is used to format Kptfiles in the order of go structures // TODO: phanimarupaka remove this method after addressing https://github.com/kptdev/kpt/issues/2052 func RoundTripKptfilesInPkg(pkgPath string) error { - paths, err := pkg.Subpackages(filesys.FileSystemOrOnDisk{}, pkgPath, pkg.All, true) + paths, err := Subpackages(filesys.FileSystemOrOnDisk{}, pkgPath, All, true) if err != nil { return err } diff --git a/internal/util/pkgutil/pkgutil_test.go b/pkg/lib/pkg/util_test.go similarity index 94% rename from internal/util/pkgutil/pkgutil_test.go rename to pkg/lib/pkg/util_test.go index cc18639b24..35802ab0a7 100644 --- a/internal/util/pkgutil/pkgutil_test.go +++ b/pkg/lib/pkg/util_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package pkgutil_test +package pkg_test import ( "os" @@ -22,9 +22,8 @@ import ( "github.com/kptdev/kpt/internal/testutil" "github.com/kptdev/kpt/internal/testutil/pkgbuilder" - "github.com/kptdev/kpt/internal/util/pkgutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" - "github.com/kptdev/kpt/pkg/lib/pkg" + . "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/stretchr/testify/assert" ) @@ -83,7 +82,7 @@ func TestWalkPackage(t *testing.T) { pkgPath := tc.pkg.ExpandPkg(t, testutil.EmptyReposInfo) var visited []string - if err := pkgutil.WalkPackage(pkgPath, func(s string, _ os.FileInfo, err error) error { + if err := WalkPackage(pkgPath, func(s string, _ os.FileInfo, err error) error { if err != nil { return err } @@ -109,7 +108,7 @@ func TestCopyPackage(t *testing.T) { testCases := map[string]struct { pkg *pkgbuilder.RootPkg copyRootKptfile bool - subpackageMatcher pkg.SubpackageMatcher + subpackageMatcher SubpackageMatcher expected []string }{ "subpackages without root kptfile": { @@ -122,7 +121,7 @@ func TestCopyPackage(t *testing.T) { WithFile("def.yaml", "123"), ), copyRootKptfile: false, - subpackageMatcher: pkg.Local, + subpackageMatcher: Local, expected: []string{ ".", "abc.yaml", @@ -139,7 +138,7 @@ func TestCopyPackage(t *testing.T) { pkgbuilder.NewSubPkg(".git"). WithFile("INDEX", "ABC123"), ), - subpackageMatcher: pkg.None, + subpackageMatcher: None, expected: []string{ ".", "abc.yaml", @@ -156,7 +155,7 @@ func TestCopyPackage(t *testing.T) { WithFile("def.yaml", "123"), ), copyRootKptfile: true, - subpackageMatcher: pkg.None, + subpackageMatcher: None, expected: []string{ ".", "Kptfile", @@ -178,7 +177,7 @@ func TestCopyPackage(t *testing.T) { WithFile("def.yaml", "123"), ), copyRootKptfile: true, - subpackageMatcher: pkg.All, + subpackageMatcher: All, expected: []string{ ".", "Kptfile", @@ -206,7 +205,7 @@ func TestCopyPackage(t *testing.T) { WithFile("def.yaml", "123"), ), copyRootKptfile: true, - subpackageMatcher: pkg.Local, + subpackageMatcher: Local, expected: []string{ ".", "Kptfile", @@ -231,7 +230,7 @@ func TestCopyPackage(t *testing.T) { WithFile("def.yaml", "123"), ), copyRootKptfile: true, - subpackageMatcher: pkg.Remote, + subpackageMatcher: Remote, expected: []string{ ".", "Kptfile", @@ -257,7 +256,7 @@ func TestCopyPackage(t *testing.T) { ), ), copyRootKptfile: true, - subpackageMatcher: pkg.Local, + subpackageMatcher: Local, expected: []string{ ".", "Kptfile", @@ -286,7 +285,7 @@ func TestCopyPackage(t *testing.T) { ), ), copyRootKptfile: true, - subpackageMatcher: pkg.Local, + subpackageMatcher: Local, expected: []string{ ".", "Kptfile", @@ -315,7 +314,7 @@ func TestCopyPackage(t *testing.T) { ), ), copyRootKptfile: true, - subpackageMatcher: pkg.Remote, + subpackageMatcher: Remote, expected: []string{ ".", "Kptfile", @@ -344,7 +343,7 @@ func TestCopyPackage(t *testing.T) { ), ), copyRootKptfile: true, - subpackageMatcher: pkg.Remote, + subpackageMatcher: Remote, expected: []string{ ".", "Kptfile", @@ -365,7 +364,7 @@ func TestCopyPackage(t *testing.T) { pkgPath := tc.pkg.ExpandPkg(t, testutil.EmptyReposInfo) dest := t.TempDir() - err := pkgutil.CopyPackage(pkgPath, dest, tc.copyRootKptfile, tc.subpackageMatcher) + err := CopyPackage(pkgPath, dest, tc.copyRootKptfile, tc.subpackageMatcher) if !assert.NoError(t, err) { t.FailNow() } @@ -538,7 +537,7 @@ func TestFindLocalRecursiveSubpackagesForPaths(t *testing.T) { pkgPaths = append(pkgPaths, p.ExpandPkg(t, testutil.EmptyReposInfo)) } - paths, err := pkgutil.FindSubpackagesForPaths(pkg.Local, true, pkgPaths...) + paths, err := FindSubpackagesForPaths(Local, true, pkgPaths...) if !assert.NoError(t, err) { t.FailNow() } @@ -561,7 +560,7 @@ func TestRemoveStaleItems_RemovesFile(t *testing.T) { assert.NoError(t, os.WriteFile(filepath.Join(dst, fileName), []byte("content"), 0644)) // Should remove file.txt from dst - err := pkgutil.RemoveStaleItems(org, src, dst, true, pkg.All) + err := RemoveStaleItems(org, src, dst, true, All) assert.NoError(t, err) _, err = os.Stat(filepath.Join(dst, fileName)) assert.True(t, os.IsNotExist(err)) @@ -584,7 +583,7 @@ func TestRemoveStaleItems_ErrorOnRemove(t *testing.T) { assert.NoError(t, os.Mkdir(filePathDst, 0755)) assert.NoError(t, os.WriteFile(filepath.Join(filePathDst, "dummy"), []byte("x"), 0644)) - err := pkgutil.RemoveStaleItems(org, src, dst, true, pkg.All) + err := RemoveStaleItems(org, src, dst, true, All) assert.Error(t, err) assert.Contains(t, err.Error(), "directory not empty") } diff --git a/pkg/lib/update/copy-merge.go b/pkg/lib/update/copy-merge.go index 392ab0ff64..c7d6e5b783 100644 --- a/pkg/lib/update/copy-merge.go +++ b/pkg/lib/update/copy-merge.go @@ -15,7 +15,6 @@ package update import ( - "github.com/kptdev/kpt/internal/util/pkgutil" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" "github.com/kptdev/kpt/pkg/lib/pkg" @@ -44,10 +43,10 @@ func (u CopyMergeUpdater) Update(options updatetypes.Options) error { if err := kptfileutil.UpdateKptfile(dst, src, options.OriginPath, true); err != nil { return errors.E(op, types.UniquePath(dst), err) } - if err := pkgutil.CopyPackage(src, dst, options.IsRoot, pkg.All); err != nil { + if err := pkg.CopyPackage(src, dst, options.IsRoot, pkg.All); err != nil { return errors.E(op, types.UniquePath(dst), err) } - if err := pkgutil.RemoveStaleItems(org, src, dst, options.IsRoot, pkg.All); err != nil { + if err := pkg.RemoveStaleItems(org, src, dst, options.IsRoot, pkg.All); err != nil { return errors.E(op, types.UniquePath(dst), err) } return nil diff --git a/pkg/lib/update/fastforward.go b/pkg/lib/update/fastforward.go index 9d8193a2d7..fd43a24cec 100644 --- a/pkg/lib/update/fastforward.go +++ b/pkg/lib/update/fastforward.go @@ -20,12 +20,11 @@ import ( "path/filepath" "slices" - pkgdiff "github.com/kptdev/kpt/internal/util/diff" - "github.com/kptdev/kpt/internal/util/pkgutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" "github.com/kptdev/kpt/pkg/lib/pkg" + "github.com/kptdev/kpt/pkg/lib/pkg/diff" "github.com/kptdev/kpt/pkg/lib/types" "github.com/kptdev/kpt/pkg/lib/update/updatetypes" "sigs.k8s.io/kustomize/kyaml/filesys" @@ -73,7 +72,7 @@ func (u FastForwardUpdater) Update(options updatetypes.Options) error { func (u FastForwardUpdater) checkForLocalChanges(localPath, originalPath string) error { const op errors.Op = "update.checkForLocalChanges" - found, err := pkgutil.Exists(originalPath) + found, err := pkg.Exists(originalPath) if err != nil { return errors.E(op, types.UniquePath(localPath), err) } @@ -81,7 +80,7 @@ func (u FastForwardUpdater) checkForLocalChanges(localPath, originalPath string) return nil } - subPkgPaths, err := pkgutil.FindSubpackagesForPaths(pkg.Local, true, localPath, originalPath) + subPkgPaths, err := pkg.FindSubpackagesForPaths(pkg.Local, true, localPath, originalPath) if err != nil { return errors.E(op, types.UniquePath(localPath), err) } @@ -90,11 +89,11 @@ func (u FastForwardUpdater) checkForLocalChanges(localPath, originalPath string) localSubPkgPath := filepath.Join(localPath, subPkgPath) originalSubPkgPath := filepath.Join(originalPath, subPkgPath) - localExists, err := pkgutil.Exists(localSubPkgPath) + localExists, err := pkg.Exists(localSubPkgPath) if err != nil { return errors.E(op, types.UniquePath(localSubPkgPath), err) } - originalExists, err := pkgutil.Exists(originalSubPkgPath) + originalExists, err := pkg.Exists(originalSubPkgPath) if err != nil { return errors.E(op, types.UniquePath(localSubPkgPath), err) } @@ -102,7 +101,7 @@ func (u FastForwardUpdater) checkForLocalChanges(localPath, originalPath string) aggDiff.Insert("%s (Package)", subPkgPath) continue } - d, err := pkgdiff.PkgDiff(localSubPkgPath, originalSubPkgPath) + d, err := diff.PkgDiff(localSubPkgPath, originalSubPkgPath) if err != nil { return errors.E(op, types.UniquePath(localSubPkgPath), err) } diff --git a/pkg/lib/update/merge3/strategy.go b/pkg/lib/update/merge3/strategy.go index 854da15f0c..786bf962d2 100644 --- a/pkg/lib/update/merge3/strategy.go +++ b/pkg/lib/update/merge3/strategy.go @@ -16,7 +16,7 @@ package merge3 import ( - "github.com/kptdev/kpt/internal/util/attribution" + "github.com/kptdev/kpt/pkg/lib/util/attribution" "sigs.k8s.io/kustomize/kyaml/kio/filters" "sigs.k8s.io/kustomize/kyaml/kio/kioutil" "sigs.k8s.io/kustomize/kyaml/yaml" diff --git a/pkg/lib/update/replace.go b/pkg/lib/update/replace.go index 9855d570a1..85fbed98e0 100644 --- a/pkg/lib/update/replace.go +++ b/pkg/lib/update/replace.go @@ -18,7 +18,6 @@ import ( "os" "path/filepath" - "github.com/kptdev/kpt/internal/util/pkgutil" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" "github.com/kptdev/kpt/pkg/lib/pkg" @@ -42,7 +41,7 @@ func (u ReplaceUpdater) Update(options updatetypes.Options) error { return errors.E(op, types.UniquePath(options.LocalPath), err) } - paths, err := pkgutil.FindSubpackagesForPaths(pkg.Local, true, options.LocalPath, options.UpdatedPath) + paths, err := pkg.FindSubpackagesForPaths(pkg.Local, true, options.LocalPath, options.UpdatedPath) if err != nil { return errors.E(op, types.UniquePath(options.LocalPath), err) } @@ -54,7 +53,7 @@ func (u ReplaceUpdater) Update(options updatetypes.Options) error { } localSubPkgPath := filepath.Join(options.LocalPath, p) updatedSubPkgPath := filepath.Join(options.UpdatedPath, p) - err = pkgutil.RemovePackageContent(localSubPkgPath, !isRootPkg) + err = pkg.RemovePackageContent(localSubPkgPath, !isRootPkg) if err != nil { return errors.E(op, types.UniquePath(localSubPkgPath), err) } @@ -71,7 +70,7 @@ func (u ReplaceUpdater) Update(options updatetypes.Options) error { return errors.E(op, types.UniquePath(localSubPkgPath), err) } } else { - if err = pkgutil.CopyPackage(updatedSubPkgPath, localSubPkgPath, !isRootPkg, pkg.None); err != nil { + if err = pkg.CopyPackage(updatedSubPkgPath, localSubPkgPath, !isRootPkg, pkg.None); err != nil { return errors.E(op, types.UniquePath(localSubPkgPath), err) } } diff --git a/pkg/lib/update/resource-merge.go b/pkg/lib/update/resource-merge.go index 426f0c2859..b54e229ecf 100644 --- a/pkg/lib/update/resource-merge.go +++ b/pkg/lib/update/resource-merge.go @@ -21,12 +21,11 @@ import ( "path/filepath" "strings" + "github.com/kptdev/kpt/pkg/lib/pkg/diff" "github.com/kptdev/kpt/pkg/lib/types" merge4 "github.com/kptdev/kpt/pkg/lib/update/merge3" "sigs.k8s.io/kustomize/kyaml/pathutil" - pkgdiff "github.com/kptdev/kpt/internal/util/diff" - "github.com/kptdev/kpt/internal/util/pkgutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" @@ -65,7 +64,7 @@ func (u ResourceMergeUpdater) Update(options updatetypes.Options) error { // Find all subpackages in local, upstream and original. They are sorted // in increasing order based on the depth of the subpackage relative to the // root package. - subPkgPaths, err := pkgutil.FindSubpackagesForPaths(pkg.Local, true, + subPkgPaths, err := pkg.FindSubpackagesForPaths(pkg.Local, true, options.LocalPath, options.UpdatedPath, options.OriginPath) if err != nil { return errors.E(op, types.UniquePath(options.LocalPath), err) @@ -95,17 +94,17 @@ func (u ResourceMergeUpdater) Update(options updatetypes.Options) error { // original version of the package. func (u ResourceMergeUpdater) updatePackage(subPkgPath, localPath, updatedPath, originalPath string, isRootPkg bool) error { const op errors.Op = "update.updatePackage" - localExists, err := pkgutil.Exists(localPath) + localExists, err := pkg.Exists(localPath) if err != nil { return errors.E(op, types.UniquePath(localPath), err) } - updatedExists, err := pkgutil.Exists(updatedPath) + updatedExists, err := pkg.Exists(updatedPath) if err != nil { return errors.E(op, types.UniquePath(localPath), err) } - originalExists, err := pkgutil.Exists(originalPath) + originalExists, err := pkg.Exists(originalPath) if err != nil { return errors.E(op, types.UniquePath(localPath), err) } @@ -117,7 +116,7 @@ func (u ResourceMergeUpdater) updatePackage(subPkgPath, localPath, updatedPath, fmt.Errorf("subpackage %q added in both upstream and local", subPkgPath)) // Package added in upstream case !originalExists && !localExists && updatedExists: - if err := pkgutil.CopyPackage(updatedPath, localPath, !isRootPkg, pkg.None); err != nil { + if err := pkg.CopyPackage(updatedPath, localPath, !isRootPkg, pkg.None); err != nil { return errors.E(op, types.UniquePath(localPath), err) } // Package added locally @@ -133,7 +132,7 @@ func (u ResourceMergeUpdater) updatePackage(subPkgPath, localPath, updatedPath, // Package deleted from upstream case originalExists && localExists && !updatedExists: // Check the diff. If there are local changes, we keep the subpackage. - diff, err := pkgdiff.PkgDiff(originalPath, localPath) + diff, err := diff.PkgDiff(originalPath, localPath) if err != nil { return errors.E(op, types.UniquePath(localPath), err) } @@ -286,7 +285,7 @@ func getSubDirsAndNonKrmFiles(root string) (sets.String, sets.String, error) { const op errors.Op = "update.getSubDirsAndNonKrmFiles" files := sets.String{} dirs := sets.String{} - err := pkgutil.WalkPackage(root, func(path string, info os.FileInfo, err error) error { + err := pkg.WalkPackage(root, func(path string, info os.FileInfo, err error) error { if err != nil { return errors.E(op, errors.IO, err) } diff --git a/pkg/lib/update/update.go b/pkg/lib/update/update.go index 5cba10bd73..9c90d2a726 100644 --- a/pkg/lib/update/update.go +++ b/pkg/lib/update/update.go @@ -26,8 +26,6 @@ import ( "github.com/kptdev/kpt/internal/gitutil" "github.com/kptdev/kpt/internal/util/fetch" "github.com/kptdev/kpt/internal/util/git" - "github.com/kptdev/kpt/internal/util/pkgutil" - "github.com/kptdev/kpt/internal/util/stack" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" @@ -35,6 +33,7 @@ import ( "github.com/kptdev/kpt/pkg/lib/types" "github.com/kptdev/kpt/pkg/lib/update/updatetypes" "github.com/kptdev/kpt/pkg/lib/util/addmergecomment" + "github.com/kptdev/kpt/pkg/lib/util/stack" "github.com/kptdev/kpt/pkg/printer" "sigs.k8s.io/kustomize/kyaml/copyutil" "sigs.k8s.io/kustomize/kyaml/filesys" @@ -312,7 +311,7 @@ func (u Command) updateRootPackage(ctx context.Context, p *pkg.Pkg) error { return errors.E(op, p.UniquePath, err) } - paths, err := pkgutil.FindSubpackagesForPaths(pkg.Remote, false, + paths, err := pkg.FindSubpackagesForPaths(pkg.Remote, false, localPath, updatedPath, originPath) if err != nil { return errors.E(op, p.UniquePath, err) @@ -377,7 +376,7 @@ func (u Command) updatePackage(ctx context.Context, subPkgPath, localPath, updat // the package hierarchy and that package is the root. case !originExists && !localExists && updatedExists: pr.Printf("Adding package %q from upstream.\n", packageName(localPath)) - if err := pkgutil.CopyPackage(updatedPath, localPath, !isRootPkg, pkg.None); err != nil { + if err := pkg.CopyPackage(updatedPath, localPath, !isRootPkg, pkg.None); err != nil { return errors.E(op, types.UniquePath(localPath), err) } diff --git a/internal/util/attribution/attribution.go b/pkg/lib/util/attribution/attribution.go similarity index 100% rename from internal/util/attribution/attribution.go rename to pkg/lib/util/attribution/attribution.go diff --git a/internal/util/attribution/attribution_test.go b/pkg/lib/util/attribution/attribution_test.go similarity index 100% rename from internal/util/attribution/attribution_test.go rename to pkg/lib/util/attribution/attribution_test.go diff --git a/internal/util/stack/stack.go b/pkg/lib/util/stack/stack.go similarity index 100% rename from internal/util/stack/stack.go rename to pkg/lib/util/stack/stack.go diff --git a/internal/util/strings/strings.go b/pkg/lib/util/strings/strings.go similarity index 100% rename from internal/util/strings/strings.go rename to pkg/lib/util/strings/strings.go diff --git a/internal/util/strings/strings_test.go b/pkg/lib/util/strings/strings_test.go similarity index 100% rename from internal/util/strings/strings_test.go rename to pkg/lib/util/strings/strings_test.go diff --git a/pkg/live/load.go b/pkg/live/load.go index 5ac4f15c30..f1837782bd 100644 --- a/pkg/live/load.go +++ b/pkg/live/load.go @@ -20,12 +20,12 @@ import ( "io" "github.com/kptdev/kpt/internal/util/pathutil" - "github.com/kptdev/kpt/internal/util/strings" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" rgfilev1alpha1 "github.com/kptdev/kpt/pkg/api/resourcegroup/v1alpha1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" "github.com/kptdev/kpt/pkg/lib/pkg" + "github.com/kptdev/kpt/pkg/lib/util/strings" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/schema" "k8s.io/klog/v2" diff --git a/internal/util/printerutil/printerutil.go b/pkg/printer/util.go similarity index 90% rename from internal/util/printerutil/printerutil.go rename to pkg/printer/util.go index 6e1fac468f..044b4e335f 100644 --- a/internal/util/printerutil/printerutil.go +++ b/pkg/printer/util.go @@ -12,17 +12,15 @@ // See the License for the specific language governing permissions and // limitations under the License. -package printerutil +package printer import ( "context" - - "github.com/kptdev/kpt/pkg/printer" ) // PrintFnResultInfo displays information about the function results file. func PrintFnResultInfo(ctx context.Context, resultsFile string, withNewLine bool) { - pr := printer.FromContextOrDie(ctx) + pr := FromContextOrDie(ctx) if resultsFile != "" { if withNewLine { pr.Printf("\n") diff --git a/thirdparty/kyaml/runfn/runfn.go b/thirdparty/kyaml/runfn/runfn.go index 375449cc93..5306156b36 100644 --- a/thirdparty/kyaml/runfn/runfn.go +++ b/thirdparty/kyaml/runfn/runfn.go @@ -22,7 +22,6 @@ import ( "sigs.k8s.io/kustomize/kyaml/kio" "sigs.k8s.io/kustomize/kyaml/yaml" - "github.com/kptdev/kpt/internal/util/printerutil" fnresult "github.com/kptdev/kpt/pkg/api/fnresult/v1" kptfile "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/lib/pkg" @@ -249,7 +248,7 @@ func (r RunFns) runFunctions(input kio.Reader, output kio.Writer, fltrs []kio.Fi } func (r RunFns) printFnResultsStatus(resultsFile string) { - printerutil.PrintFnResultInfo(r.Ctx, resultsFile, true) + printer.PrintFnResultInfo(r.Ctx, resultsFile, true) } // mergeContainerEnv will merge the envs specified by command line (imperative) and config From 5f6d1c62747e79afee9490d0e4e36bdd918eeba4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=B3zes=20L=C3=A1szl=C3=B3=20M=C3=A1t=C3=A9?= Date: Thu, 7 May 2026 14:48:07 +0200 Subject: [PATCH 4/6] eliminate internal/util completely, move some other packages as well MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mózes László Máté --- commands/alpha/live/plan/command.go | 4 ++-- commands/fn/render/cmdrender.go | 8 ++++---- commands/live/apply/cmdapply.go | 4 ++-- commands/live/destroy/cmddestroy.go | 4 ++-- commands/live/init/cmdliveinit.go | 4 ++-- commands/live/migrate/migratecmd.go | 10 +++++----- commands/live/status/cmdstatus.go | 4 ++-- commands/live/status/fake-loader.go | 4 ++-- commands/pkg/diff/cmddiff.go | 10 +++++----- commands/pkg/get/cmdget.go | 10 +++++----- commands/pkg/init/cmdinit.go | 4 ++-- commands/pkg/init/cmdinit_test.go | 2 +- commands/pkg/update/cmdupdate.go | 8 ++++---- internal/testutil/setup_manager.go | 2 +- internal/testutil/testutil.go | 12 +++++------ pkg/kptfile/kptfileutil/util.go | 2 +- pkg/kptpkg/init.go | 2 +- .../lib/kptops/hook_executor.go | 2 +- pkg/lib/kptops/pkgupdate.go | 4 ++-- .../{executor.go => render_executor.go} | 0 ...ecutor_test.go => render_executor_test.go} | 0 pkg/lib/pkg/diff/diff.go | 2 +- pkg/lib/pkg/diff/pkgdiff_test.go | 5 +++-- pkg/lib/pkg/pkg.go | 6 +++--- pkg/lib/pkg/pkg_test.go | 12 +++++------ pkg/lib/update/update.go | 12 +++++------ .../argutil.go => pkg/lib/util/args/args.go | 4 ++-- .../lib/util/args/args_test.go | 20 +++++++++---------- {internal => pkg/lib}/util/fetch/fetch.go | 4 ++-- .../lib}/util/fetch/fetch_test.go | 2 +- .../lib}/util/get/example_test.go | 2 +- {internal => pkg/lib}/util/get/get.go | 10 +++++----- {internal => pkg/lib}/util/get/get_test.go | 2 +- {internal => pkg/lib}/util/git/git.go | 4 ++-- {internal => pkg/lib}/util/man/man.go | 0 {internal => pkg/lib}/util/man/man_test.go | 2 +- .../pathutil.go => pkg/lib/util/path/path.go | 2 +- pkg/live/load.go | 2 +- pkg/live/rgpath.go | 4 ++-- .../cmdconfig/commands/cmdeval/cmdeval.go | 11 +++++----- .../cmdconfig/commands/cmdsource/cmdsource.go | 4 ++-- .../cmdconfig/commands/cmdtree/cmdtree.go | 4 ++-- 42 files changed, 108 insertions(+), 106 deletions(-) rename internal/hook/executor.go => pkg/lib/kptops/hook_executor.go (99%) rename pkg/lib/kptops/{executor.go => render_executor.go} (100%) rename pkg/lib/kptops/{executor_test.go => render_executor_test.go} (100%) rename internal/util/argutil/argutil.go => pkg/lib/util/args/args.go (97%) rename internal/util/argutil/argutil_test.go => pkg/lib/util/args/args_test.go (83%) rename {internal => pkg/lib}/util/fetch/fetch.go (99%) rename {internal => pkg/lib}/util/fetch/fetch_test.go (99%) rename {internal => pkg/lib}/util/get/example_test.go (98%) rename {internal => pkg/lib}/util/get/get.go (97%) rename {internal => pkg/lib}/util/get/get_test.go (99%) rename {internal => pkg/lib}/util/git/git.go (96%) rename {internal => pkg/lib}/util/man/man.go (100%) rename {internal => pkg/lib}/util/man/man_test.go (99%) rename internal/util/pathutil/pathutil.go => pkg/lib/util/path/path.go (98%) diff --git a/commands/alpha/live/plan/command.go b/commands/alpha/live/plan/command.go index 886face464..e1b6b51687 100644 --- a/commands/alpha/live/plan/command.go +++ b/commands/alpha/live/plan/command.go @@ -21,7 +21,7 @@ import ( "os" "strings" - "github.com/kptdev/kpt/internal/util/argutil" + argsutil "github.com/kptdev/kpt/pkg/lib/util/args" "github.com/kptdev/kpt/pkg/live" kptplanner "github.com/kptdev/kpt/pkg/live/planner" "github.com/spf13/cobra" @@ -112,7 +112,7 @@ func (r *Runner) RunE(c *cobra.Command, args []string) error { path := args[0] var err error if args[0] != "-" { - path, err = argutil.ResolveSymlink(r.ctx, path) + path, err = argsutil.ResolveSymlink(r.ctx, path) if err != nil { return err } diff --git a/commands/fn/render/cmdrender.go b/commands/fn/render/cmdrender.go index 507eeb0ab1..f1bdb03f52 100644 --- a/commands/fn/render/cmdrender.go +++ b/commands/fn/render/cmdrender.go @@ -23,11 +23,11 @@ import ( "os" docs "github.com/kptdev/kpt/internal/docs/generated/fndocs" - "github.com/kptdev/kpt/internal/util/argutil" - "github.com/kptdev/kpt/internal/util/pathutil" "github.com/kptdev/kpt/pkg/lib/kptops" "github.com/kptdev/kpt/pkg/lib/runneroptions" + argsutil "github.com/kptdev/kpt/pkg/lib/util/args" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" + "github.com/kptdev/kpt/pkg/lib/util/path" "github.com/kptdev/kpt/pkg/printer" "github.com/spf13/cobra" "sigs.k8s.io/kustomize/kyaml/filesys" @@ -100,7 +100,7 @@ func (r *Runner) preRunE(_ *cobra.Command, args []string) error { r.pkgPath = args[0] } var err error - r.pkgPath, err = argutil.ResolveSymlink(r.ctx, r.pkgPath) + r.pkgPath, err = argsutil.ResolveSymlink(r.ctx, r.pkgPath) if err != nil { return err } @@ -126,7 +126,7 @@ func (r *Runner) runE(_ *cobra.Command, _ []string) error { // capture the content to be written output = &outContent } - absPkgPath, _, err := pathutil.ResolveAbsAndRelPaths(r.pkgPath) + absPkgPath, _, err := path.ResolveAbsAndRelPaths(r.pkgPath) if err != nil { return err } diff --git a/commands/live/apply/cmdapply.go b/commands/live/apply/cmdapply.go index db20cde1c6..1dd81fd15a 100644 --- a/commands/live/apply/cmdapply.go +++ b/commands/live/apply/cmdapply.go @@ -21,7 +21,7 @@ import ( "time" "github.com/kptdev/kpt/internal/docs/generated/livedocs" - "github.com/kptdev/kpt/internal/util/argutil" + argsutil "github.com/kptdev/kpt/pkg/lib/util/args" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" "github.com/kptdev/kpt/pkg/lib/util/strings" "github.com/kptdev/kpt/pkg/live" @@ -174,7 +174,7 @@ func (r *Runner) runE(c *cobra.Command, args []string) error { path := args[0] var err error if args[0] != "-" { - path, err = argutil.ResolveSymlink(r.ctx, path) + path, err = argsutil.ResolveSymlink(r.ctx, path) if err != nil { return err } diff --git a/commands/live/destroy/cmddestroy.go b/commands/live/destroy/cmddestroy.go index 09b710cc35..1381e217dc 100644 --- a/commands/live/destroy/cmddestroy.go +++ b/commands/live/destroy/cmddestroy.go @@ -20,7 +20,7 @@ import ( "os" "github.com/kptdev/kpt/internal/docs/generated/livedocs" - "github.com/kptdev/kpt/internal/util/argutil" + argsutil "github.com/kptdev/kpt/pkg/lib/util/args" "github.com/kptdev/kpt/pkg/lib/util/strings" "github.com/kptdev/kpt/pkg/live" "github.com/kptdev/kpt/pkg/status" @@ -133,7 +133,7 @@ func (r *Runner) runE(c *cobra.Command, args []string) error { path := args[0] var err error if args[0] != "-" { - path, err = argutil.ResolveSymlink(r.ctx, path) + path, err = argsutil.ResolveSymlink(r.ctx, path) if err != nil { return err } diff --git a/commands/live/init/cmdliveinit.go b/commands/live/init/cmdliveinit.go index e0e9da0aa8..8ad6737d6b 100644 --- a/commands/live/init/cmdliveinit.go +++ b/commands/live/init/cmdliveinit.go @@ -26,7 +26,6 @@ import ( "time" "github.com/kptdev/kpt/internal/docs/generated/livedocs" - "github.com/kptdev/kpt/internal/util/pathutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" rgfilev1alpha1 "github.com/kptdev/kpt/pkg/api/resourcegroup/v1alpha1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" @@ -34,6 +33,7 @@ import ( "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/types" "github.com/kptdev/kpt/pkg/lib/util/attribution" + "github.com/kptdev/kpt/pkg/lib/util/path" "github.com/kptdev/kpt/pkg/printer" "github.com/spf13/cobra" "k8s.io/cli-runtime/pkg/genericclioptions" @@ -140,7 +140,7 @@ func (r *Runner) runE(_ *cobra.Command, args []string) error { return errors.E(op, err) } - absPath, _, err := pathutil.ResolveAbsAndRelPaths(dir) + absPath, _, err := path.ResolveAbsAndRelPaths(dir) if err != nil { return err } diff --git a/commands/live/migrate/migratecmd.go b/commands/live/migrate/migratecmd.go index 61eaada121..7e2af23bb7 100644 --- a/commands/live/migrate/migratecmd.go +++ b/commands/live/migrate/migratecmd.go @@ -25,13 +25,13 @@ import ( initialization "github.com/kptdev/kpt/commands/live/init" "github.com/kptdev/kpt/internal/docs/generated/livedocs" - "github.com/kptdev/kpt/internal/util/argutil" - "github.com/kptdev/kpt/internal/util/pathutil" rgfilev1alpha1 "github.com/kptdev/kpt/pkg/api/resourcegroup/v1alpha1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/types" + argsutil "github.com/kptdev/kpt/pkg/lib/util/args" + "github.com/kptdev/kpt/pkg/lib/util/path" "github.com/kptdev/kpt/pkg/live" "github.com/spf13/cobra" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -251,7 +251,7 @@ func (mr *Runner) migrateObjs(rgInvClient inventory.Client, path := args[0] var err error if args[0] != "-" { - path, err = argutil.ResolveSymlink(mr.ctx, path) + path, err = argsutil.ResolveSymlink(mr.ctx, path) if err != nil { return err } @@ -363,7 +363,7 @@ func (mr *Runner) migrateKptfileToRG(args []string) error { klog.V(4).Infoln("attempting to migrate from Kptfile inventory") fmt.Fprint(mr.ioStreams.Out, " reading existing Kptfile...") if !mr.dryRun { - dir, _, err := pathutil.ResolveAbsAndRelPaths(args[0]) + dir, _, err := path.ResolveAbsAndRelPaths(args[0]) if err != nil { return err } @@ -459,7 +459,7 @@ func (mr *Runner) migrateCMToRG(stdinBytes []byte, args []string) error { func (mr *Runner) createRGfile(ctx context.Context, args []string, prevID string) error { fmt.Fprint(mr.ioStreams.Out, " creating ResourceGroup object file...") if !mr.dryRun { - dir, _, err := pathutil.ResolveAbsAndRelPaths(args[0]) + dir, _, err := path.ResolveAbsAndRelPaths(args[0]) if err != nil { return err } diff --git a/commands/live/status/cmdstatus.go b/commands/live/status/cmdstatus.go index c3e73c1b3e..ecb61bc4a6 100644 --- a/commands/live/status/cmdstatus.go +++ b/commands/live/status/cmdstatus.go @@ -19,7 +19,7 @@ import ( "os" "github.com/kptdev/kpt/internal/docs/generated/livedocs" - "github.com/kptdev/kpt/internal/util/argutil" + argsutil "github.com/kptdev/kpt/pkg/lib/util/args" "github.com/kptdev/kpt/pkg/live" kptstatus "github.com/kptdev/kpt/pkg/status" "github.com/spf13/cobra" @@ -74,7 +74,7 @@ func (rir *RGInventoryLoader) GetInvInfo(cmd *cobra.Command, args []string) (inv path := args[0] var err error if args[0] != "-" { - path, err = argutil.ResolveSymlink(rir.ctx, path) + path, err = argsutil.ResolveSymlink(rir.ctx, path) if err != nil { return nil, err } diff --git a/commands/live/status/fake-loader.go b/commands/live/status/fake-loader.go index 74c3b8c82d..90db0edca8 100644 --- a/commands/live/status/fake-loader.go +++ b/commands/live/status/fake-loader.go @@ -18,7 +18,7 @@ import ( "context" "os" - "github.com/kptdev/kpt/internal/util/argutil" + argsutil "github.com/kptdev/kpt/pkg/lib/util/args" "github.com/kptdev/kpt/pkg/live" "github.com/spf13/cobra" "k8s.io/kubectl/pkg/cmd/util" @@ -56,7 +56,7 @@ func (r *FakeLoader) GetInvInfo(cmd *cobra.Command, args []string) (inventory.In path := args[0] var err error if args[0] != "-" { - path, err = argutil.ResolveSymlink(r.ctx, path) + path, err = argsutil.ResolveSymlink(r.ctx, path) if err != nil { return nil, err } diff --git a/commands/pkg/diff/cmddiff.go b/commands/pkg/diff/cmddiff.go index 8d731a6a6c..1d38397c49 100644 --- a/commands/pkg/diff/cmddiff.go +++ b/commands/pkg/diff/cmddiff.go @@ -19,11 +19,11 @@ import ( "os" "github.com/kptdev/kpt/internal/docs/generated/pkgdocs" - "github.com/kptdev/kpt/internal/util/argutil" - "github.com/kptdev/kpt/internal/util/pathutil" "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/pkg/diff" + argsutil "github.com/kptdev/kpt/pkg/lib/util/args" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" + "github.com/kptdev/kpt/pkg/lib/util/path" "github.com/kptdev/kpt/pkg/printer" "github.com/spf13/cobra" "sigs.k8s.io/kustomize/kyaml/filesys" @@ -80,7 +80,7 @@ func (r *Runner) preRunE(_ *cobra.Command, args []string) error { args = append(args, pkg.CurDir) } dirVer := args[0] - dir, version, err := argutil.ParseDirVersion(dirVer) + dir, version, err := argsutil.ParseDirVersion(dirVer) if err != nil { return err } @@ -96,12 +96,12 @@ func (r *Runner) preRunE(_ *cobra.Command, args []string) error { r.DiffType = diff.Type(r.diffType) } - resolvedPath, err := argutil.ResolveSymlink(r.ctx, dir) + resolvedPath, err := argsutil.ResolveSymlink(r.ctx, dir) if err != nil { return err } - absResolvedPath, _, err := pathutil.ResolveAbsAndRelPaths(resolvedPath) + absResolvedPath, _, err := path.ResolveAbsAndRelPaths(resolvedPath) if err != nil { return err } diff --git a/commands/pkg/get/cmdget.go b/commands/pkg/get/cmdget.go index 4e65680d71..0f9baea157 100644 --- a/commands/pkg/get/cmdget.go +++ b/commands/pkg/get/cmdget.go @@ -21,15 +21,15 @@ import ( "strings" docs "github.com/kptdev/kpt/internal/docs/generated/pkgdocs" - "github.com/kptdev/kpt/internal/util/argutil" - "github.com/kptdev/kpt/internal/util/get" - "github.com/kptdev/kpt/internal/util/pathutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/lib/errors" "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/types" + argsutil "github.com/kptdev/kpt/pkg/lib/util/args" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" + "github.com/kptdev/kpt/pkg/lib/util/get" "github.com/kptdev/kpt/pkg/lib/util/parse" + "github.com/kptdev/kpt/pkg/lib/util/path" "github.com/spf13/cobra" "sigs.k8s.io/kustomize/kyaml/filesys" ) @@ -84,7 +84,7 @@ func (r *Runner) preRunE(_ *cobra.Command, args []string) error { } else if filepath.Clean(args[1]) != "." { _, err := os.Lstat(args[1]) if err == nil || os.IsExist(err) { - resolvedPath, err := argutil.ResolveSymlink(r.ctx, args[1]) + resolvedPath, err := argsutil.ResolveSymlink(r.ctx, args[1]) if err != nil { return errors.E(op, err) } @@ -97,7 +97,7 @@ func (r *Runner) preRunE(_ *cobra.Command, args []string) error { } r.Get.Git = &t.Git - absDestPath, _, err := pathutil.ResolveAbsAndRelPaths(t.Destination) + absDestPath, _, err := path.ResolveAbsAndRelPaths(t.Destination) if err != nil { return err } diff --git a/commands/pkg/init/cmdinit.go b/commands/pkg/init/cmdinit.go index 2135d5072c..232b6b4da3 100644 --- a/commands/pkg/init/cmdinit.go +++ b/commands/pkg/init/cmdinit.go @@ -18,10 +18,10 @@ import ( "context" docs "github.com/kptdev/kpt/internal/docs/generated/pkgdocs" - "github.com/kptdev/kpt/internal/util/pathutil" "github.com/kptdev/kpt/pkg/kptpkg" "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" + "github.com/kptdev/kpt/pkg/lib/util/path" "github.com/spf13/cobra" "sigs.k8s.io/kustomize/kyaml/filesys" ) @@ -67,7 +67,7 @@ func (r *Runner) runE(_ *cobra.Command, args []string) error { args = append(args, pkg.CurDir) } - absPath, _, err := pathutil.ResolveAbsAndRelPaths(args[0]) + absPath, _, err := path.ResolveAbsAndRelPaths(args[0]) if err != nil { return err } diff --git a/commands/pkg/init/cmdinit_test.go b/commands/pkg/init/cmdinit_test.go index c1ffc1d7b9..8c1641c73f 100644 --- a/commands/pkg/init/cmdinit_test.go +++ b/commands/pkg/init/cmdinit_test.go @@ -22,9 +22,9 @@ import ( initialization "github.com/kptdev/kpt/commands/pkg/init" "github.com/kptdev/kpt/internal/testutil" - "github.com/kptdev/kpt/internal/util/man" "github.com/kptdev/kpt/pkg/lib/builtins" "github.com/kptdev/kpt/pkg/lib/builtins/builtintypes" + "github.com/kptdev/kpt/pkg/lib/util/man" "github.com/kptdev/kpt/pkg/printer/fake" "github.com/stretchr/testify/assert" ) diff --git a/commands/pkg/update/cmdupdate.go b/commands/pkg/update/cmdupdate.go index 38276fb0f5..133101fc25 100644 --- a/commands/pkg/update/cmdupdate.go +++ b/commands/pkg/update/cmdupdate.go @@ -22,14 +22,14 @@ import ( "strings" docs "github.com/kptdev/kpt/internal/docs/generated/pkgdocs" - "github.com/kptdev/kpt/internal/util/argutil" - "github.com/kptdev/kpt/internal/util/pathutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/lib/errors" "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/types" "github.com/kptdev/kpt/pkg/lib/update" + argsutil "github.com/kptdev/kpt/pkg/lib/util/args" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" + "github.com/kptdev/kpt/pkg/lib/util/path" "github.com/spf13/cobra" "sigs.k8s.io/kustomize/kyaml/filesys" ) @@ -91,11 +91,11 @@ func (r *Runner) preRunE(_ *cobra.Command, args []string) error { return errors.E(op, errors.InvalidParam, fmt.Errorf("at most 1 version permitted")) } - resolvedPath, err := argutil.ResolveSymlink(r.ctx, parts[0]) + resolvedPath, err := argsutil.ResolveSymlink(r.ctx, parts[0]) if err != nil { return err } - absResolvedPath, _, err := pathutil.ResolveAbsAndRelPaths(resolvedPath) + absResolvedPath, _, err := path.ResolveAbsAndRelPaths(resolvedPath) if err != nil { return err } diff --git a/internal/testutil/setup_manager.go b/internal/testutil/setup_manager.go index 27a546852c..66031304b0 100644 --- a/internal/testutil/setup_manager.go +++ b/internal/testutil/setup_manager.go @@ -22,8 +22,8 @@ import ( "github.com/kptdev/kpt/internal/gitutil" "github.com/kptdev/kpt/internal/testutil/pkgbuilder" - "github.com/kptdev/kpt/internal/util/get" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" + "github.com/kptdev/kpt/pkg/lib/util/get" "github.com/kptdev/kpt/pkg/printer/fake" "github.com/stretchr/testify/assert" "sigs.k8s.io/kustomize/kyaml/yaml" diff --git a/internal/testutil/testutil.go b/internal/testutil/testutil.go index ad24f4d19a..c15c81ecf0 100644 --- a/internal/testutil/testutil.go +++ b/internal/testutil/testutil.go @@ -25,13 +25,13 @@ import ( "strings" "testing" - "github.com/kptdev/kpt/internal/gitutil" - "github.com/kptdev/kpt/internal/util/git" - "github.com/kptdev/kpt/internal/util/pathutil" + git2 "github.com/kptdev/kpt/internal/gitutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/util/addmergecomment" + "github.com/kptdev/kpt/pkg/lib/util/git" + pathutil "github.com/kptdev/kpt/pkg/lib/util/path" "github.com/kptdev/kpt/pkg/printer/fake" "github.com/philopon/go-toposort" "github.com/stretchr/testify/assert" @@ -436,7 +436,7 @@ func SetupWorkspace(t *testing.T) (*TestWorkspace, func()) { err := w.SetupTestWorkspace() assert.NoError(t, err) - gr, err := gitutil.NewLocalGitRunner(w.WorkspaceDirectory) + gr, err := git2.NewLocalGitRunner(w.WorkspaceDirectory) if !assert.NoError(t, err) { t.FailNow() } @@ -463,7 +463,7 @@ func AddKptfileToWorkspace(t *testing.T, w *TestWorkspace, kf *kptfilev1.KptFile t.FailNow() } - gitRunner, err := gitutil.NewLocalGitRunner(w.WorkspaceDirectory) + gitRunner, err := git2.NewLocalGitRunner(w.WorkspaceDirectory) if !assert.NoError(t, err) { t.FailNow() } @@ -873,7 +873,7 @@ func ConfigureTestKptCache(m *testing.M) int { defer func() { _ = os.RemoveAll(cacheDir) }() - if err := os.Setenv(gitutil.RepoCacheDirEnv, cacheDir); err != nil { + if err := os.Setenv(git2.RepoCacheDirEnv, cacheDir); err != nil { panic(fmt.Errorf("error setting repo cache env variable: %w", err)) } return m.Run() diff --git a/pkg/kptfile/kptfileutil/util.go b/pkg/kptfile/kptfileutil/util.go index f912a4e7e0..f6cf55b1a5 100644 --- a/pkg/kptfile/kptfileutil/util.go +++ b/pkg/kptfile/kptfileutil/util.go @@ -24,10 +24,10 @@ import ( "slices" "strings" - "github.com/kptdev/kpt/internal/util/git" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/lib/errors" "github.com/kptdev/kpt/pkg/lib/types" + "github.com/kptdev/kpt/pkg/lib/util/git" "k8s.io/apimachinery/pkg/runtime/schema" "sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/kustomize/kyaml/sets" diff --git a/pkg/kptpkg/init.go b/pkg/kptpkg/init.go index 118642936e..e4a4c41f7c 100644 --- a/pkg/kptpkg/init.go +++ b/pkg/kptpkg/init.go @@ -21,11 +21,11 @@ import ( "path/filepath" "strings" - "github.com/kptdev/kpt/internal/util/man" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/lib/builtins" "github.com/kptdev/kpt/pkg/lib/builtins/builtintypes" "github.com/kptdev/kpt/pkg/lib/pkg" + "github.com/kptdev/kpt/pkg/lib/util/man" "github.com/kptdev/kpt/pkg/printer" "sigs.k8s.io/kustomize/kyaml/errors" "sigs.k8s.io/kustomize/kyaml/filesys" diff --git a/internal/hook/executor.go b/pkg/lib/kptops/hook_executor.go similarity index 99% rename from internal/hook/executor.go rename to pkg/lib/kptops/hook_executor.go index 061978f764..e01dcadb86 100644 --- a/internal/hook/executor.go +++ b/pkg/lib/kptops/hook_executor.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package hook +package kptops import ( "context" diff --git a/pkg/lib/kptops/pkgupdate.go b/pkg/lib/kptops/pkgupdate.go index c28c1964ad..63cb678a0a 100644 --- a/pkg/lib/kptops/pkgupdate.go +++ b/pkg/lib/kptops/pkgupdate.go @@ -20,12 +20,12 @@ import ( "os" "path/filepath" - "github.com/kptdev/kpt/internal/util/fetch" - "github.com/kptdev/kpt/internal/util/git" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/update" "github.com/kptdev/kpt/pkg/lib/update/updatetypes" + "github.com/kptdev/kpt/pkg/lib/util/fetch" + "github.com/kptdev/kpt/pkg/lib/util/git" "github.com/kptdev/kpt/pkg/printer" "k8s.io/klog/v2" ) diff --git a/pkg/lib/kptops/executor.go b/pkg/lib/kptops/render_executor.go similarity index 100% rename from pkg/lib/kptops/executor.go rename to pkg/lib/kptops/render_executor.go diff --git a/pkg/lib/kptops/executor_test.go b/pkg/lib/kptops/render_executor_test.go similarity index 100% rename from pkg/lib/kptops/executor_test.go rename to pkg/lib/kptops/render_executor_test.go diff --git a/pkg/lib/pkg/diff/diff.go b/pkg/lib/pkg/diff/diff.go index e108c6a6db..0f34ee7a56 100644 --- a/pkg/lib/pkg/diff/diff.go +++ b/pkg/lib/pkg/diff/diff.go @@ -25,11 +25,11 @@ import ( "strings" "github.com/kptdev/kpt/internal/gitutil" - "github.com/kptdev/kpt/internal/util/fetch" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/util/addmergecomment" + "github.com/kptdev/kpt/pkg/lib/util/fetch" "sigs.k8s.io/kustomize/kyaml/errors" "sigs.k8s.io/kustomize/kyaml/filesys" ) diff --git a/pkg/lib/pkg/diff/pkgdiff_test.go b/pkg/lib/pkg/diff/pkgdiff_test.go index a5400164fb..6f4d53928d 100644 --- a/pkg/lib/pkg/diff/pkgdiff_test.go +++ b/pkg/lib/pkg/diff/pkgdiff_test.go @@ -12,13 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -package diff +package diff_test import ( "testing" "github.com/kptdev/kpt/internal/testutil" "github.com/kptdev/kpt/internal/testutil/pkgbuilder" + "github.com/kptdev/kpt/pkg/lib/pkg/diff" "github.com/stretchr/testify/assert" "sigs.k8s.io/kustomize/kyaml/sets" ) @@ -141,7 +142,7 @@ func TestPkgDiff(t *testing.T) { t.Run(test.name, func(t *testing.T) { pkg1Dir := test.pkg1.ExpandPkg(t, testutil.EmptyReposInfo) pkg2Dir := test.pkg2.ExpandPkg(t, testutil.EmptyReposInfo) - diff, err := PkgDiff(pkg1Dir, pkg2Dir) + diff, err := diff.PkgDiff(pkg1Dir, pkg2Dir) if !assert.NoError(t, err) { t.FailNow() } diff --git a/pkg/lib/pkg/pkg.go b/pkg/lib/pkg/pkg.go index aebd52f682..46c3a904de 100644 --- a/pkg/lib/pkg/pkg.go +++ b/pkg/lib/pkg/pkg.go @@ -24,13 +24,13 @@ import ( "sort" "strings" - "github.com/kptdev/kpt/internal/util/git" - "github.com/kptdev/kpt/internal/util/pathutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" rgfilev1alpha1 "github.com/kptdev/kpt/pkg/api/resourcegroup/v1alpha1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" "github.com/kptdev/kpt/pkg/lib/types" + "github.com/kptdev/kpt/pkg/lib/util/git" + "github.com/kptdev/kpt/pkg/lib/util/path" "sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/kustomize/kyaml/kio" "sigs.k8s.io/kustomize/kyaml/kio/kioutil" @@ -503,7 +503,7 @@ func ReadRGFile(pkgPath, rgfile string) (*rgfilev1alpha1.ResourceGroup, error) { if filepath.Base(rgfile) == rgfile { absPath = filepath.Join(pkgPath, rgfile) } else { - rgFilePath, _, err := pathutil.ResolveAbsAndRelPaths(rgfile) + rgFilePath, _, err := path.ResolveAbsAndRelPaths(rgfile) if err != nil { return nil, &RGError{ Path: types.UniquePath(rgfile), diff --git a/pkg/lib/pkg/pkg_test.go b/pkg/lib/pkg/pkg_test.go index c70f0f4f51..1e39fc6527 100644 --- a/pkg/lib/pkg/pkg_test.go +++ b/pkg/lib/pkg/pkg_test.go @@ -23,8 +23,8 @@ import ( "testing" "github.com/kptdev/kpt/internal/testutil/pkgbuilder" - "github.com/kptdev/kpt/internal/util/pathutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" + "github.com/kptdev/kpt/pkg/lib/util/path" "github.com/stretchr/testify/assert" "sigs.k8s.io/kustomize/kyaml/filesys" ) @@ -79,7 +79,7 @@ func TestNewPkg(t *testing.T) { assert.NoError(t, err) revert := Chdir(t, filepath.Join(dir, test.workingDir)) defer revert() - absInputPath, _, err := pathutil.ResolveAbsAndRelPaths(test.inputPath) + absInputPath, _, err := path.ResolveAbsAndRelPaths(test.inputPath) assert.NoError(t, err) p, err := New(filesys.FileSystemOrOnDisk{}, absInputPath) assert.NoError(t, err) @@ -163,18 +163,18 @@ func TestAdjustDisplayPathForSubpkg(t *testing.T) { assert.NoError(t, err) revert := Chdir(t, filepath.Join(dir, "rootPkgParentDir", test.workingDir)) defer revert() - absPkgPath, _, err := pathutil.ResolveAbsAndRelPaths(test.pkgPath) + absPkgPath, _, err := path.ResolveAbsAndRelPaths(test.pkgPath) assert.NoError(t, err) parent, err := New(filesys.FileSystemOrOnDisk{}, absPkgPath) assert.NoError(t, err) if test.rootPkgParentDirPath != "" { - absRootPkgPath, _, err := pathutil.ResolveAbsAndRelPaths(test.rootPkgParentDirPath) + absRootPkgPath, _, err := path.ResolveAbsAndRelPaths(test.rootPkgParentDirPath) assert.NoError(t, err) rootPkg, err := New(filesys.FileSystemOrOnDisk{}, absRootPkgPath) assert.NoError(t, err) parent.rootPkgParentDirPath = string(rootPkg.UniquePath) } - absSubPkgPath, _, err := pathutil.ResolveAbsAndRelPaths(test.subPkgPath) + absSubPkgPath, _, err := path.ResolveAbsAndRelPaths(test.subPkgPath) assert.NoError(t, err) subPkg, err := New(filesys.FileSystemOrOnDisk{}, absSubPkgPath) assert.NoError(t, err) @@ -278,7 +278,7 @@ func TestDirectSubpackages(t *testing.T) { t.Run(tn, func(t *testing.T) { pkgPath := tc.pkg.ExpandPkg(t, nil) defer os.RemoveAll(pkgPath) - absPkgPath, _, err := pathutil.ResolveAbsAndRelPaths(pkgPath) + absPkgPath, _, err := path.ResolveAbsAndRelPaths(pkgPath) if !assert.NoError(t, err) { t.FailNow() } diff --git a/pkg/lib/update/update.go b/pkg/lib/update/update.go index 9c90d2a726..b5c5483935 100644 --- a/pkg/lib/update/update.go +++ b/pkg/lib/update/update.go @@ -23,9 +23,7 @@ import ( "path/filepath" "strings" - "github.com/kptdev/kpt/internal/gitutil" - "github.com/kptdev/kpt/internal/util/fetch" - "github.com/kptdev/kpt/internal/util/git" + git2 "github.com/kptdev/kpt/internal/gitutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" @@ -33,6 +31,8 @@ import ( "github.com/kptdev/kpt/pkg/lib/types" "github.com/kptdev/kpt/pkg/lib/update/updatetypes" "github.com/kptdev/kpt/pkg/lib/util/addmergecomment" + "github.com/kptdev/kpt/pkg/lib/util/fetch" + "github.com/kptdev/kpt/pkg/lib/util/git" "github.com/kptdev/kpt/pkg/lib/util/stack" "github.com/kptdev/kpt/pkg/printer" "sigs.k8s.io/kustomize/kyaml/copyutil" @@ -101,7 +101,7 @@ type Command struct { Strategy kptfilev1.UpdateStrategyType // cachedUpstreamRepos is an upstream repo already fetched for a given repoSpec CloneRef - cachedUpstreamRepos map[string]*gitutil.GitUpstreamRepo + cachedUpstreamRepos map[string]*git2.GitUpstreamRepo } func GetUpdater(strategy string) updatetypes.Updater { @@ -147,7 +147,7 @@ func (u *Command) Run(ctx context.Context) error { return errors.E(op, u.Pkg.UniquePath, err) } if u.cachedUpstreamRepos == nil { - u.cachedUpstreamRepos = make(map[string]*gitutil.GitUpstreamRepo) + u.cachedUpstreamRepos = make(map[string]*git2.GitUpstreamRepo) } packageCount := 0 @@ -198,7 +198,7 @@ func (u *Command) Run(ctx context.Context) error { } // GetCachedUpstreamRepos returns repos cached during update -func (u Command) GetCachedUpstreamRepos() map[string]*gitutil.GitUpstreamRepo { +func (u Command) GetCachedUpstreamRepos() map[string]*git2.GitUpstreamRepo { return u.cachedUpstreamRepos } diff --git a/internal/util/argutil/argutil.go b/pkg/lib/util/args/args.go similarity index 97% rename from internal/util/argutil/argutil.go rename to pkg/lib/util/args/args.go index dfcaa2a082..9401d21860 100644 --- a/internal/util/argutil/argutil.go +++ b/pkg/lib/util/args/args.go @@ -12,8 +12,8 @@ // See the License for the specific language governing permissions and // limitations under the License. -// The argutil package contains libraries for parsing commandline args. -package argutil +// Package args contains libraries for parsing commandline args. +package args import ( "context" diff --git a/internal/util/argutil/argutil_test.go b/pkg/lib/util/args/args_test.go similarity index 83% rename from internal/util/argutil/argutil_test.go rename to pkg/lib/util/args/args_test.go index d8b9230c66..7295fae5ab 100644 --- a/internal/util/argutil/argutil_test.go +++ b/pkg/lib/util/args/args_test.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package argutil_test +package args_test import ( "os" @@ -20,7 +20,7 @@ import ( "testing" "github.com/kptdev/kpt/internal/testutil" - "github.com/kptdev/kpt/internal/util/argutil" + "github.com/kptdev/kpt/pkg/lib/util/args" "github.com/kptdev/kpt/pkg/printer/fake" "github.com/stretchr/testify/assert" ) @@ -54,7 +54,7 @@ func TestParseDirVersion(t *testing.T) { in: "/some@dir2@ver1", expDir: "", expVer: "", - expErr: argutil.ErrMultiVersion, + expErr: args.ErrMultiVersion, }, { // empty in: "", @@ -65,7 +65,7 @@ func TestParseDirVersion(t *testing.T) { } for _, test := range tests { - gotDir, gotVer, gotErr := argutil.ParseDirVersion(test.in) + gotDir, gotVer, gotErr := args.ParseDirVersion(test.in) assert.Equal(t, gotErr, test.expErr) assert.Equal(t, gotDir, test.expDir) @@ -102,7 +102,7 @@ func TestParseDirVersionWithDefaults(t *testing.T) { in: "/some@dir2@ver1", expDir: "", expVer: "", - expErr: argutil.ErrMultiVersion, + expErr: args.ErrMultiVersion, }, { // empty in: "", @@ -113,7 +113,7 @@ func TestParseDirVersionWithDefaults(t *testing.T) { } for _, test := range tests { - gotDir, gotVer, gotErr := argutil.ParseDirVersionWithDefaults(test.in) + gotDir, gotVer, gotErr := args.ParseDirVersionWithDefaults(test.in) assert.Equal(t, gotErr, test.expErr) assert.Equal(t, gotDir, test.expDir) @@ -131,19 +131,19 @@ func TestResolveSymlink(t *testing.T) { err = os.Symlink("foo-link", "link-to-foo-link") assert.NoError(t, err) - actual1, err := argutil.ResolveSymlink(fake.CtxWithDefaultPrinter(), "foo-link") + actual1, err := args.ResolveSymlink(fake.CtxWithDefaultPrinter(), "foo-link") assert.NoError(t, err) assert.Equal(t, "foo", actual1) - actual2, err := argutil.ResolveSymlink(fake.CtxWithDefaultPrinter(), "link-to-foo-link") + actual2, err := args.ResolveSymlink(fake.CtxWithDefaultPrinter(), "link-to-foo-link") assert.NoError(t, err) assert.Equal(t, "foo", actual2) - actual3, err := argutil.ResolveSymlink(fake.CtxWithDefaultPrinter(), ".") + actual3, err := args.ResolveSymlink(fake.CtxWithDefaultPrinter(), ".") assert.NoError(t, err) assert.Equal(t, ".", actual3) - _, err = argutil.ResolveSymlink(fake.CtxWithDefaultPrinter(), "baz") + _, err = args.ResolveSymlink(fake.CtxWithDefaultPrinter(), "baz") assert.Error(t, err) assert.Equal(t, "lstat baz: no such file or directory", err.Error()) } diff --git a/internal/util/fetch/fetch.go b/pkg/lib/util/fetch/fetch.go similarity index 99% rename from internal/util/fetch/fetch.go rename to pkg/lib/util/fetch/fetch.go index 893f3ec23c..5aabd0064c 100644 --- a/internal/util/fetch/fetch.go +++ b/pkg/lib/util/fetch/fetch.go @@ -22,16 +22,16 @@ import ( "path/filepath" "strings" + "github.com/kptdev/kpt/internal/gitutil" "github.com/kptdev/kpt/pkg/lib/errors" "github.com/kptdev/kpt/pkg/lib/types" "github.com/otiai10/copy" "sigs.k8s.io/kustomize/kyaml/filesys" - "github.com/kptdev/kpt/internal/gitutil" - "github.com/kptdev/kpt/internal/util/git" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/pkg" + "github.com/kptdev/kpt/pkg/lib/util/git" "github.com/kptdev/kpt/pkg/printer" ) diff --git a/internal/util/fetch/fetch_test.go b/pkg/lib/util/fetch/fetch_test.go similarity index 99% rename from internal/util/fetch/fetch_test.go rename to pkg/lib/util/fetch/fetch_test.go index 8da0067c10..cd1537c638 100644 --- a/internal/util/fetch/fetch_test.go +++ b/pkg/lib/util/fetch/fetch_test.go @@ -21,9 +21,9 @@ import ( "github.com/kptdev/kpt/internal/testutil" "github.com/kptdev/kpt/internal/testutil/pkgbuilder" - "github.com/kptdev/kpt/internal/util/fetch" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" + "github.com/kptdev/kpt/pkg/lib/util/fetch" "github.com/kptdev/kpt/pkg/printer/fake" "github.com/stretchr/testify/assert" "sigs.k8s.io/kustomize/kyaml/filesys" diff --git a/internal/util/get/example_test.go b/pkg/lib/util/get/example_test.go similarity index 98% rename from internal/util/get/example_test.go rename to pkg/lib/util/get/example_test.go index ed5f11268a..52ee77fe2c 100644 --- a/internal/util/get/example_test.go +++ b/pkg/lib/util/get/example_test.go @@ -18,8 +18,8 @@ import ( "fmt" "path/filepath" - "github.com/kptdev/kpt/internal/util/get" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" + "github.com/kptdev/kpt/pkg/lib/util/get" "github.com/kptdev/kpt/pkg/printer/fake" ) diff --git a/internal/util/get/get.go b/pkg/lib/util/get/get.go similarity index 97% rename from internal/util/get/get.go rename to pkg/lib/util/get/get.go index 12bc024b37..fd20085c70 100644 --- a/internal/util/get/get.go +++ b/pkg/lib/util/get/get.go @@ -23,17 +23,17 @@ import ( "path/filepath" "strings" - "github.com/kptdev/kpt/internal/hook" - "github.com/kptdev/kpt/internal/util/fetch" - "github.com/kptdev/kpt/internal/util/pathutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" + "github.com/kptdev/kpt/pkg/lib/kptops" "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/runneroptions" "github.com/kptdev/kpt/pkg/lib/types" "github.com/kptdev/kpt/pkg/lib/util/addmergecomment" "github.com/kptdev/kpt/pkg/lib/util/attribution" + "github.com/kptdev/kpt/pkg/lib/util/fetch" + "github.com/kptdev/kpt/pkg/lib/util/path" "github.com/kptdev/kpt/pkg/lib/util/stack" "github.com/kptdev/kpt/pkg/printer" "sigs.k8s.io/kustomize/kyaml/filesys" @@ -122,7 +122,7 @@ func (c Command) Run(ctx context.Context) error { return cleanUpDirAndError(c.Destination, err) } - absDestPath, _, err := pathutil.ResolveAbsAndRelPaths(c.Destination) + absDestPath, _, err := path.ResolveAbsAndRelPaths(c.Destination) if err != nil { return err } @@ -148,7 +148,7 @@ func (c Command) Run(ctx context.Context) error { if c.IsDeploymentInstance { pr := printer.FromContextOrDie(ctx) pr.Printf("\nCustomizing package for deployment.\n") - hookCmd := hook.Executor{} + hookCmd := kptops.Executor{} hookCmd.RunnerOptions.InitDefaults(c.DefaultKrmFunctionImagePrefix) hookCmd.PkgPath = c.Destination diff --git a/internal/util/get/get_test.go b/pkg/lib/util/get/get_test.go similarity index 99% rename from internal/util/get/get_test.go rename to pkg/lib/util/get/get_test.go index ab965599b1..fe8ce0815a 100644 --- a/internal/util/get/get_test.go +++ b/pkg/lib/util/get/get_test.go @@ -22,8 +22,8 @@ import ( "github.com/kptdev/kpt/internal/testutil" "github.com/kptdev/kpt/internal/testutil/pkgbuilder" - "github.com/kptdev/kpt/internal/util/get" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" + "github.com/kptdev/kpt/pkg/lib/util/get" "github.com/kptdev/kpt/pkg/printer/fake" "github.com/stretchr/testify/assert" "sigs.k8s.io/kustomize/kyaml/kio" diff --git a/internal/util/git/git.go b/pkg/lib/util/git/git.go similarity index 96% rename from internal/util/git/git.go rename to pkg/lib/util/git/git.go index e8c7b1741c..c484f37b57 100644 --- a/internal/util/git/git.go +++ b/pkg/lib/util/git/git.go @@ -53,7 +53,7 @@ type RepoSpec struct { } // AbsPath is the absolute path to the subdirectory -func (rs RepoSpec) AbsPath() string { +func (rs *RepoSpec) AbsPath() string { return filepath.Join(rs.Dir, rs.Path) } @@ -80,7 +80,7 @@ func isAWSHost(host string) bool { return strings.Contains(host, "amazonaws.com") } -// lookupCommit looks up the sha of the current commit on the repo at the +// LookupCommit looks up the sha of the current commit on the repo at the // provided path. func LookupCommit(repoPath string) (string, error) { const op errors.Op = "git.LookupCommit" diff --git a/internal/util/man/man.go b/pkg/lib/util/man/man.go similarity index 100% rename from internal/util/man/man.go rename to pkg/lib/util/man/man.go diff --git a/internal/util/man/man_test.go b/pkg/lib/util/man/man_test.go similarity index 99% rename from internal/util/man/man_test.go rename to pkg/lib/util/man/man_test.go index e6d5c3cafe..2b2b0ada2c 100644 --- a/internal/util/man/man_test.go +++ b/pkg/lib/util/man/man_test.go @@ -21,8 +21,8 @@ import ( "path/filepath" "testing" - man "github.com/kptdev/kpt/internal/util/man" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" + "github.com/kptdev/kpt/pkg/lib/util/man" "github.com/stretchr/testify/assert" ) diff --git a/internal/util/pathutil/pathutil.go b/pkg/lib/util/path/path.go similarity index 98% rename from internal/util/pathutil/pathutil.go rename to pkg/lib/util/path/path.go index 5270ace41e..42a837f3fc 100644 --- a/internal/util/pathutil/pathutil.go +++ b/pkg/lib/util/path/path.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package pathutil +package path import ( "os" diff --git a/pkg/live/load.go b/pkg/live/load.go index f1837782bd..18282a3637 100644 --- a/pkg/live/load.go +++ b/pkg/live/load.go @@ -19,12 +19,12 @@ import ( "fmt" "io" - "github.com/kptdev/kpt/internal/util/pathutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" rgfilev1alpha1 "github.com/kptdev/kpt/pkg/api/resourcegroup/v1alpha1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" "github.com/kptdev/kpt/pkg/lib/pkg" + pathutil "github.com/kptdev/kpt/pkg/lib/util/path" "github.com/kptdev/kpt/pkg/lib/util/strings" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/schema" diff --git a/pkg/live/rgpath.go b/pkg/live/rgpath.go index 1093528f5a..56b98e0205 100644 --- a/pkg/live/rgpath.go +++ b/pkg/live/rgpath.go @@ -18,8 +18,8 @@ import ( "encoding/json" "fmt" - "github.com/kptdev/kpt/internal/util/pathutil" rgfilev1alpha1 "github.com/kptdev/kpt/pkg/api/resourcegroup/v1alpha1" + "github.com/kptdev/kpt/pkg/lib/util/path" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "sigs.k8s.io/cli-utils/pkg/manifestreader" "sigs.k8s.io/kustomize/kyaml/kio" @@ -41,7 +41,7 @@ type ResourceGroupPathManifestReader struct { // Kptfile data. If unable to generate the ResourceGroup inventory // object from the Kptfile, it is NOT an error. func (r *ResourceGroupPathManifestReader) Read() ([]*unstructured.Unstructured, error) { - absPkgPath, _, err := pathutil.ResolveAbsAndRelPaths(r.PkgPath) + absPkgPath, _, err := path.ResolveAbsAndRelPaths(r.PkgPath) if err != nil { return nil, err } diff --git a/thirdparty/cmdconfig/commands/cmdeval/cmdeval.go b/thirdparty/cmdconfig/commands/cmdeval/cmdeval.go index f4ba0fbe46..4958914873 100644 --- a/thirdparty/cmdconfig/commands/cmdeval/cmdeval.go +++ b/thirdparty/cmdconfig/commands/cmdeval/cmdeval.go @@ -14,12 +14,13 @@ import ( "github.com/google/shlex" docs "github.com/kptdev/kpt/internal/docs/generated/fndocs" - "github.com/kptdev/kpt/internal/util/argutil" - "github.com/kptdev/kpt/internal/util/pathutil" kptfile "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/runneroptions" + argsutil "github.com/kptdev/kpt/pkg/lib/util/args" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" + "github.com/kptdev/kpt/pkg/lib/util/path" + pathutil "github.com/kptdev/kpt/pkg/lib/util/path" "github.com/kptdev/kpt/pkg/printer" "github.com/kptdev/kpt/thirdparty/cmdconfig/commands/runner" "github.com/kptdev/kpt/thirdparty/kyaml/runfn" @@ -200,8 +201,8 @@ func (r *EvalFnRunner) NewFunction() *kptfile.Function { newFn.Exclusions = []kptfile.Selector{r.Exclusion} } if r.FnConfigPath != "" { - fnConfigAbsPath, _, _ := pathutil.ResolveAbsAndRelPaths(r.FnConfigPath) - pkgAbsPath, _, _ := pathutil.ResolveAbsAndRelPaths(r.runFns.Path) + fnConfigAbsPath, _, _ := path.ResolveAbsAndRelPaths(r.FnConfigPath) + pkgAbsPath, _, _ := path.ResolveAbsAndRelPaths(r.runFns.Path) newFn.ConfigPath, _ = filepath.Rel(pkgAbsPath, fnConfigAbsPath) } else { data := map[string]string{} @@ -519,7 +520,7 @@ func (r *EvalFnRunner) preRunE(c *cobra.Command, args []string) error { } if path != "" { - path, err = argutil.ResolveSymlink(r.Ctx, path) + path, err = argsutil.ResolveSymlink(r.Ctx, path) if err != nil { return err } diff --git a/thirdparty/cmdconfig/commands/cmdsource/cmdsource.go b/thirdparty/cmdconfig/commands/cmdsource/cmdsource.go index 29d9f30871..7fba739a4a 100644 --- a/thirdparty/cmdconfig/commands/cmdsource/cmdsource.go +++ b/thirdparty/cmdconfig/commands/cmdsource/cmdsource.go @@ -9,9 +9,9 @@ import ( "path/filepath" "github.com/kptdev/kpt/internal/docs/generated/fndocs" - "github.com/kptdev/kpt/internal/util/argutil" kptfile "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/lib/pkg" + argsutil "github.com/kptdev/kpt/pkg/lib/util/args" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" "github.com/kptdev/kpt/pkg/printer" "github.com/kptdev/kpt/thirdparty/cmdconfig/commands/runner" @@ -102,7 +102,7 @@ func (r *SourceRunner) runE(c *cobra.Command, args []string) error { if err != nil { return fmt.Errorf("cannot convert input path %q to absolute path: %w", a, err) } - resolvedPath, err := argutil.ResolveSymlink(r.Ctx, pkgPath) + resolvedPath, err := argsutil.ResolveSymlink(r.Ctx, pkgPath) if err != nil { return err } diff --git a/thirdparty/cmdconfig/commands/cmdtree/cmdtree.go b/thirdparty/cmdconfig/commands/cmdtree/cmdtree.go index d81d910258..dbec01da9b 100644 --- a/thirdparty/cmdconfig/commands/cmdtree/cmdtree.go +++ b/thirdparty/cmdconfig/commands/cmdtree/cmdtree.go @@ -8,8 +8,8 @@ import ( "path/filepath" "github.com/kptdev/kpt/internal/docs/generated/pkgdocs" - "github.com/kptdev/kpt/internal/util/argutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" + argsutil "github.com/kptdev/kpt/pkg/lib/util/args" "github.com/kptdev/kpt/pkg/printer" "github.com/kptdev/kpt/thirdparty/cmdconfig/commands/runner" "github.com/spf13/cobra" @@ -51,7 +51,7 @@ func (r *TreeRunner) runE(c *cobra.Command, args []string) error { args = append(args, root) } root = filepath.Clean(args[0]) - resolvedPath, err := argutil.ResolveSymlink(r.Ctx, args[0]) + resolvedPath, err := argsutil.ResolveSymlink(r.Ctx, args[0]) if err != nil { return err } From ce4a1a82a420aaa4b5a0b8a53c5399476eb60720 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=B3zes=20L=C3=A1szl=C3=B3=20M=C3=A1t=C3=A9?= Date: Thu, 14 May 2026 15:16:00 +0200 Subject: [PATCH 5/6] address copilot review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mózes László Máté --- commands/pkg/update/cmdupdate_test.go | 4 +- internal/gitutil/gitutil_test.go | 36 ++++++++-------- internal/testutil/setup_manager.go | 8 ++-- internal/testutil/testutil.go | 12 +++--- pkg/kptfile/kptfileutil/util.go | 4 +- pkg/lib/errors/resolver/git.go | 14 +++---- pkg/lib/kptops/pkgupdate.go | 8 ++-- pkg/lib/pkg/diff/diff.go | 4 +- pkg/lib/pkg/pkg.go | 4 +- pkg/lib/pkg/util.go | 2 +- pkg/lib/update/pruninglocalpackagereader.go | 14 +++++++ pkg/lib/update/resource-merge.go | 12 +++--- pkg/lib/update/update.go | 14 +++---- pkg/lib/update/update_test.go | 42 +++++++++---------- pkg/lib/util/fetch/fetch.go | 22 +++++----- pkg/lib/util/parse/parse.go | 8 ++-- .../cmdconfig/commands/cmdeval/cmdeval.go | 5 +-- 17 files changed, 113 insertions(+), 100 deletions(-) diff --git a/commands/pkg/update/cmdupdate_test.go b/commands/pkg/update/cmdupdate_test.go index 5dfa5bbb14..4909799d65 100644 --- a/commands/pkg/update/cmdupdate_test.go +++ b/commands/pkg/update/cmdupdate_test.go @@ -26,7 +26,7 @@ import ( "github.com/kptdev/kpt/commands/pkg/get" "github.com/kptdev/kpt/commands/pkg/update" - "github.com/kptdev/kpt/internal/gitutil" + internalgitutil "github.com/kptdev/kpt/internal/gitutil" "github.com/kptdev/kpt/internal/testutil" "github.com/kptdev/kpt/internal/testutil/pkgbuilder" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" @@ -62,7 +62,7 @@ func TestCmd_execute(t *testing.T) { if !g.AssertEqual(t, filepath.Join(g.DatasetDirectory, testutil.Dataset1), dest, true) { return } - gitRunner, err := gitutil.NewLocalGitRunner(w.WorkspaceDirectory) + gitRunner, err := internalgitutil.NewLocalGitRunner(w.WorkspaceDirectory) if !assert.NoError(t, err) { t.FailNow() } diff --git a/internal/gitutil/gitutil_test.go b/internal/gitutil/gitutil_test.go index 3198e3aca7..0ae5ebb4fa 100644 --- a/internal/gitutil/gitutil_test.go +++ b/internal/gitutil/gitutil_test.go @@ -21,7 +21,7 @@ import ( "strings" "testing" - "github.com/kptdev/kpt/internal/gitutil" + internalgitutil "github.com/kptdev/kpt/internal/gitutil" "github.com/kptdev/kpt/internal/testutil" "github.com/kptdev/kpt/internal/testutil/pkgbuilder" "github.com/kptdev/kpt/pkg/lib/errors" @@ -38,7 +38,7 @@ func TestLocalGitRunner(t *testing.T) { command string args []string expectedStdout string - expectedErr *gitutil.GitExecError + expectedErr *internalgitutil.GitExecError }{ "successful command with output to stdout": { command: "branch", @@ -48,7 +48,7 @@ func TestLocalGitRunner(t *testing.T) { "failed command with output to stderr": { command: "checkout", args: []string{"does-not-exist"}, - expectedErr: &gitutil.GitExecError{ + expectedErr: &internalgitutil.GitExecError{ StdOut: "", StdErr: "error: pathspec 'does-not-exist' did not match any file(s) known to git", }, @@ -59,7 +59,7 @@ func TestLocalGitRunner(t *testing.T) { t.Run(tn, func(t *testing.T) { dir := t.TempDir() - runner, err := gitutil.NewLocalGitRunner(dir) + runner, err := internalgitutil.NewLocalGitRunner(dir) if !assert.NoError(t, err) { t.FailNow() } @@ -70,7 +70,7 @@ func TestLocalGitRunner(t *testing.T) { rr, err := runner.Run(fake.CtxWithDefaultPrinter(), tc.command, tc.args...) if tc.expectedErr != nil { - var gitExecError *gitutil.GitExecError + var gitExecError *internalgitutil.GitExecError if !errors.As(err, &gitExecError) { t.Error("expected error of type *GitExecError") t.FailNow() @@ -92,7 +92,7 @@ func TestLocalGitRunner(t *testing.T) { func TestNewGitUpstreamRepo_noRepo(t *testing.T) { dir := t.TempDir() - _, err := gitutil.NewGitUpstreamRepo(fake.CtxWithDefaultPrinter(), dir) + _, err := internalgitutil.NewGitUpstreamRepo(fake.CtxWithDefaultPrinter(), dir) if !assert.Error(t, err) { t.FailNow() } @@ -102,7 +102,7 @@ func TestNewGitUpstreamRepo_noRepo(t *testing.T) { func TestNewGitUpstreamRepo_noRefs(t *testing.T) { dir := t.TempDir() - runner, err := gitutil.NewLocalGitRunner(dir) + runner, err := internalgitutil.NewLocalGitRunner(dir) if !assert.NoError(t, err) { t.FailNow() } @@ -111,7 +111,7 @@ func TestNewGitUpstreamRepo_noRefs(t *testing.T) { t.FailNow() } - gur, err := gitutil.NewGitUpstreamRepo(fake.CtxWithDefaultPrinter(), dir) + gur, err := internalgitutil.NewGitUpstreamRepo(fake.CtxWithDefaultPrinter(), dir) if !assert.NoError(t, err) { t.FailNow() } @@ -168,7 +168,7 @@ func TestNewGitUpstreamRepo(t *testing.T) { t.FailNow() } - gur, err := gitutil.NewGitUpstreamRepo(fake.CtxWithDefaultPrinter(), g[testutil.Upstream].RepoDirectory) + gur, err := internalgitutil.NewGitUpstreamRepo(fake.CtxWithDefaultPrinter(), g[testutil.Upstream].RepoDirectory) if !assert.NoError(t, err) { t.FailNow() } @@ -181,7 +181,7 @@ func TestNewGitUpstreamRepo(t *testing.T) { func TestGitUpstreamRepo_GetDefaultBranch_noRefs(t *testing.T) { dir := t.TempDir() - runner, err := gitutil.NewLocalGitRunner(dir) + runner, err := internalgitutil.NewLocalGitRunner(dir) if !assert.NoError(t, err) { t.FailNow() } @@ -190,7 +190,7 @@ func TestGitUpstreamRepo_GetDefaultBranch_noRefs(t *testing.T) { t.FailNow() } - gur, err := gitutil.NewGitUpstreamRepo(fake.CtxWithDefaultPrinter(), dir) + gur, err := internalgitutil.NewGitUpstreamRepo(fake.CtxWithDefaultPrinter(), dir) if !assert.NoError(t, err) { t.FailNow() } @@ -241,7 +241,7 @@ func TestGitUpstreamRepo_GetDefaultBranch(t *testing.T) { }) defer clean() - gur, err := gitutil.NewGitUpstreamRepo(fake.CtxWithDefaultPrinter(), g[testutil.Upstream].RepoDirectory) + gur, err := internalgitutil.NewGitUpstreamRepo(fake.CtxWithDefaultPrinter(), g[testutil.Upstream].RepoDirectory) if !assert.NoError(t, err) { t.FailNow() } @@ -298,7 +298,7 @@ func TestGitUpstreamRepo_GetRepo(t *testing.T) { }, }, refsFunc: func(t *testing.T, upstreamPath string) []string { - runner, err := gitutil.NewLocalGitRunner(upstreamPath) + runner, err := internalgitutil.NewLocalGitRunner(upstreamPath) if !assert.NoError(t, err) { t.FailNow() } @@ -319,7 +319,7 @@ func TestGitUpstreamRepo_GetRepo(t *testing.T) { }, }, refsFunc: func(t *testing.T, upstreamPath string) []string { - runner, err := gitutil.NewLocalGitRunner(upstreamPath) + runner, err := internalgitutil.NewLocalGitRunner(upstreamPath) if !assert.NoError(t, err) { t.FailNow() } @@ -344,7 +344,7 @@ func TestGitUpstreamRepo_GetRepo(t *testing.T) { }) defer clean() - gur, err := gitutil.NewGitUpstreamRepo(fake.CtxWithDefaultPrinter(), g[testutil.Upstream].RepoDirectory) + gur, err := internalgitutil.NewGitUpstreamRepo(fake.CtxWithDefaultPrinter(), g[testutil.Upstream].RepoDirectory) if !assert.NoError(t, err) { t.FailNow() } @@ -354,7 +354,7 @@ func TestGitUpstreamRepo_GetRepo(t *testing.T) { t.FailNow() } - runner, err := gitutil.NewLocalGitRunner(dir) + runner, err := internalgitutil.NewLocalGitRunner(dir) if !assert.NoError(t, err) { t.FailNow() } @@ -412,7 +412,7 @@ func TestGitUpstreamRepo_GetRepo_multipleUpdates(t *testing.T) { } func getRepoAndVerify(t *testing.T, repo, branchName string) string { - gur, err := gitutil.NewGitUpstreamRepo(fake.CtxWithDefaultPrinter(), repo) + gur, err := internalgitutil.NewGitUpstreamRepo(fake.CtxWithDefaultPrinter(), repo) if !assert.NoError(t, err) { t.FailNow() } @@ -421,7 +421,7 @@ func getRepoAndVerify(t *testing.T, repo, branchName string) string { t.FailNow() } - runner, err := gitutil.NewLocalGitRunner(dir) + runner, err := internalgitutil.NewLocalGitRunner(dir) if !assert.NoError(t, err) { t.FailNow() } diff --git a/internal/testutil/setup_manager.go b/internal/testutil/setup_manager.go index 66031304b0..febfdfe304 100644 --- a/internal/testutil/setup_manager.go +++ b/internal/testutil/setup_manager.go @@ -20,7 +20,7 @@ import ( "path/filepath" "testing" - "github.com/kptdev/kpt/internal/gitutil" + internalgitutil "github.com/kptdev/kpt/internal/gitutil" "github.com/kptdev/kpt/internal/testutil/pkgbuilder" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/lib/util/get" @@ -92,7 +92,7 @@ func (g *TestSetupManager) Init() bool { return false } g.cacheDir = cacheDir - os.Setenv(gitutil.RepoCacheDirEnv, g.cacheDir) + os.Setenv(internalgitutil.RepoCacheDirEnv, g.cacheDir) g.Repos, g.LocalWorkspace, g.cleanTestRepos = SetupReposAndWorkspace(g.T, g.ReposChanges) if g.GetSubDirectory == "/" { @@ -112,7 +112,7 @@ func (g *TestSetupManager) Init() bool { }}.Run(fake.CtxWithDefaultPrinter())) { return false } - localGit, err := gitutil.NewLocalGitRunner(g.LocalWorkspace.WorkspaceDirectory) + localGit, err := internalgitutil.NewLocalGitRunner(g.LocalWorkspace.WorkspaceDirectory) if !assert.NoError(g.T, err) { return false } @@ -207,7 +207,7 @@ func (g *TestSetupManager) GetSubPkg(dest, repo, upstreamDir string) { Directory: path.Join(g.GetSubDirectory, upstreamDir), }}.Run(fake.CtxWithDefaultPrinter())) - localGit, err := gitutil.NewLocalGitRunner(g.LocalWorkspace.WorkspaceDirectory) + localGit, err := internalgitutil.NewLocalGitRunner(g.LocalWorkspace.WorkspaceDirectory) assert.NoError(g.T, err) _, err = localGit.Run(fake.CtxWithDefaultPrinter(), "add", ".") assert.NoError(g.T, err) diff --git a/internal/testutil/testutil.go b/internal/testutil/testutil.go index c15c81ecf0..cba750f7b0 100644 --- a/internal/testutil/testutil.go +++ b/internal/testutil/testutil.go @@ -25,12 +25,12 @@ import ( "strings" "testing" - git2 "github.com/kptdev/kpt/internal/gitutil" + internalgitutil "github.com/kptdev/kpt/internal/gitutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/util/addmergecomment" - "github.com/kptdev/kpt/pkg/lib/util/git" + gitutil "github.com/kptdev/kpt/pkg/lib/util/git" pathutil "github.com/kptdev/kpt/pkg/lib/util/path" "github.com/kptdev/kpt/pkg/printer/fake" "github.com/philopon/go-toposort" @@ -436,7 +436,7 @@ func SetupWorkspace(t *testing.T) (*TestWorkspace, func()) { err := w.SetupTestWorkspace() assert.NoError(t, err) - gr, err := git2.NewLocalGitRunner(w.WorkspaceDirectory) + gr, err := internalgitutil.NewLocalGitRunner(w.WorkspaceDirectory) if !assert.NoError(t, err) { t.FailNow() } @@ -463,7 +463,7 @@ func AddKptfileToWorkspace(t *testing.T, w *TestWorkspace, kf *kptfilev1.KptFile t.FailNow() } - gitRunner, err := git2.NewLocalGitRunner(w.WorkspaceDirectory) + gitRunner, err := internalgitutil.NewLocalGitRunner(w.WorkspaceDirectory) if !assert.NoError(t, err) { t.FailNow() } @@ -748,7 +748,7 @@ func commit(repo, message string) (string, error) { return "", err } - sha, err := git.LookupCommit(repo) + sha, err := gitutil.LookupCommit(repo) if err != nil { return "", err } @@ -873,7 +873,7 @@ func ConfigureTestKptCache(m *testing.M) int { defer func() { _ = os.RemoveAll(cacheDir) }() - if err := os.Setenv(git2.RepoCacheDirEnv, cacheDir); err != nil { + if err := os.Setenv(internalgitutil.RepoCacheDirEnv, cacheDir); err != nil { panic(fmt.Errorf("error setting repo cache env variable: %w", err)) } return m.Run() diff --git a/pkg/kptfile/kptfileutil/util.go b/pkg/kptfile/kptfileutil/util.go index f6cf55b1a5..d9e2216e90 100644 --- a/pkg/kptfile/kptfileutil/util.go +++ b/pkg/kptfile/kptfileutil/util.go @@ -27,7 +27,7 @@ import ( kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/lib/errors" "github.com/kptdev/kpt/pkg/lib/types" - "github.com/kptdev/kpt/pkg/lib/util/git" + gitutil "github.com/kptdev/kpt/pkg/lib/util/git" "k8s.io/apimachinery/pkg/runtime/schema" "sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/kustomize/kyaml/sets" @@ -261,7 +261,7 @@ func UpdateKptfile(localPath, updatedPath, originPath string, updateUpstream boo // by path by using the values from spec. It will also populate the commit // field in upstreamLock using the latest commit of the git repo given // by spec. -func UpdateUpstreamLockFromGit(path string, spec *git.RepoSpec) error { +func UpdateUpstreamLockFromGit(path string, spec *gitutil.RepoSpec) error { const op errors.Op = "kptfileutil.UpdateUpstreamLockFromGit" // read KptFile cloned with the package if it exists kpgfile, err := ReadKptfile(filesys.FileSystemOrOnDisk{}, path) diff --git a/pkg/lib/errors/resolver/git.go b/pkg/lib/errors/resolver/git.go index bc939c723d..80894c4f0c 100644 --- a/pkg/lib/errors/resolver/git.go +++ b/pkg/lib/errors/resolver/git.go @@ -19,7 +19,7 @@ import ( "fmt" "strings" - "github.com/kptdev/kpt/internal/gitutil" + internalgitutil "github.com/kptdev/kpt/internal/gitutil" ) //nolint:gochecknoinits @@ -32,7 +32,7 @@ func init() { type gitExecErrorResolver struct{} func (*gitExecErrorResolver) Resolve(err error) (ResolvedResult, bool) { - gitExecErr, ok := goerrors.AsType[*gitutil.GitExecError](err) + gitExecErr, ok := goerrors.AsType[*internalgitutil.GitExecError](err) if !ok { return ResolvedResult{}, false } @@ -41,25 +41,25 @@ func (*gitExecErrorResolver) Resolve(err error) (ResolvedResult, bool) { var msg string switch gitExecErr.Type { - case gitutil.UnknownReference: + case internalgitutil.UnknownReference: msg = fmt.Sprintf("Error: Unknown ref %q. Please verify that the reference exists in upstream repo %q.", gitExecErr.Ref, gitExecErr.Repo) msg = msg + "\n" + BuildOutputDetails(gitExecErr.StdOut, gitExecErr.StdErr) - case gitutil.GitExecutableNotFound: + case internalgitutil.GitExecutableNotFound: msg = "Error: No git executable found. kpt requires git to be installed and available in the path." msg = msg + "\n" + BuildOutputDetails(gitExecErr.StdOut, gitExecErr.StdErr) - case gitutil.HTTPSAuthRequired: + case internalgitutil.HTTPSAuthRequired: msg = fmt.Sprintf("Error: Repository %q requires authentication.", gitExecErr.Repo) msg += " kpt does not support this for the 'https' protocol." msg += " Please use the 'git' protocol instead." msg = msg + "\n" + BuildOutputDetails(gitExecErr.StdOut, gitExecErr.StdErr) - case gitutil.RepositoryUnavailable: + case internalgitutil.RepositoryUnavailable: msg = fmt.Sprintf("Error: Unable to access repository %q.", gitExecErr.Repo) msg = msg + "\n" + BuildOutputDetails(gitExecErr.StdOut, gitExecErr.StdErr) - case gitutil.RepositoryNotFound: + case internalgitutil.RepositoryNotFound: msg = fmt.Sprintf("Error: Repository %q not found.", gitExecErr.Repo) msg = msg + "\n" + BuildOutputDetails(gitExecErr.StdOut, gitExecErr.StdErr) default: diff --git a/pkg/lib/kptops/pkgupdate.go b/pkg/lib/kptops/pkgupdate.go index 63cb678a0a..9f8aa18aec 100644 --- a/pkg/lib/kptops/pkgupdate.go +++ b/pkg/lib/kptops/pkgupdate.go @@ -25,7 +25,7 @@ import ( "github.com/kptdev/kpt/pkg/lib/update" "github.com/kptdev/kpt/pkg/lib/update/updatetypes" "github.com/kptdev/kpt/pkg/lib/util/fetch" - "github.com/kptdev/kpt/pkg/lib/util/git" + gitutil "github.com/kptdev/kpt/pkg/lib/util/git" "github.com/kptdev/kpt/pkg/printer" "k8s.io/klog/v2" ) @@ -72,7 +72,7 @@ func PkgUpdate(ctx context.Context, ref string, packageDir string, _ PkgUpdateOp } // var updatedDigest string - var updatedRepoSpec git.RepoSpec + var updatedRepoSpec gitutil.RepoSpec var updatedDir string var originDir string @@ -80,7 +80,7 @@ func PkgUpdate(ctx context.Context, ref string, packageDir string, _ PkgUpdateOp switch kf.Upstream.Type { case kptfilev1.GitOrigin: g := kf.Upstream.Git - upstream := &git.RepoSpec{OrgRepo: g.Repo, Path: g.Directory, Ref: g.Ref} + upstream := &gitutil.RepoSpec{OrgRepo: g.Repo, Path: g.Directory, Ref: g.Ref} klog.Infof("Fetching upstream from %s@%s\n", upstream.OrgRepo, upstream.Ref) // pr.Printf("Fetching upstream from %s@%s\n", kf.Upstream.Git.Repo, kf.Upstream.Git.Ref) // if err := fetch.ClonerUsingGitExec(ctx, updated); err != nil { @@ -97,7 +97,7 @@ func PkgUpdate(ctx context.Context, ref string, packageDir string, _ PkgUpdateOp // var origin repoClone if kf.UpstreamLock != nil { gLock := kf.UpstreamLock.Git - originRepoSpec := &git.RepoSpec{OrgRepo: gLock.Repo, Path: gLock.Directory, Ref: gLock.Commit} + originRepoSpec := &gitutil.RepoSpec{OrgRepo: gLock.Repo, Path: gLock.Directory, Ref: gLock.Commit} klog.Infof("Fetching origin from %s@%s\n", originRepoSpec.OrgRepo, originRepoSpec.Ref) // pr.Printf("Fetching origin from %s@%s\n", kf.Upstream.Git.Repo, kf.Upstream.Git.Ref) // if err := fetch.ClonerUsingGitExec(ctx, originRepoSpec); err != nil { diff --git a/pkg/lib/pkg/diff/diff.go b/pkg/lib/pkg/diff/diff.go index 0f34ee7a56..c61b12825f 100644 --- a/pkg/lib/pkg/diff/diff.go +++ b/pkg/lib/pkg/diff/diff.go @@ -24,7 +24,7 @@ import ( "path/filepath" "strings" - "github.com/kptdev/kpt/internal/gitutil" + internalgitutil "github.com/kptdev/kpt/internal/gitutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/pkg" @@ -169,7 +169,7 @@ func (c *Command) Run(ctx context.Context) error { var upstreamTargetPkg string if c.Ref == "" { - gur, err := gitutil.NewGitUpstreamRepo(ctx, kptFile.UpstreamLock.Git.Repo) + gur, err := internalgitutil.NewGitUpstreamRepo(ctx, kptFile.UpstreamLock.Git.Repo) if err != nil { return err } diff --git a/pkg/lib/pkg/pkg.go b/pkg/lib/pkg/pkg.go index 11957ed176..7b8b7ce3cf 100644 --- a/pkg/lib/pkg/pkg.go +++ b/pkg/lib/pkg/pkg.go @@ -30,7 +30,7 @@ import ( "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" "github.com/kptdev/kpt/pkg/lib/types" - "github.com/kptdev/kpt/pkg/lib/util/git" + gitutil "github.com/kptdev/kpt/pkg/lib/util/git" "github.com/kptdev/kpt/pkg/lib/util/path" regclientref "github.com/regclient/regclient/types/ref" "sigs.k8s.io/kustomize/kyaml/filesys" @@ -54,7 +54,7 @@ var MatchAllKRM = append([]string{kptfilev1.KptFileName}, kio.MatchAll...) // RemoteKptfileError records errors regarding reading or parsing of a Kptfile // in a remote repo. type RemoteKptfileError struct { - RepoSpec *git.RepoSpec + RepoSpec *gitutil.RepoSpec Err error } diff --git a/pkg/lib/pkg/util.go b/pkg/lib/pkg/util.go index 5a05cf2f28..dd7fb28d73 100644 --- a/pkg/lib/pkg/util.go +++ b/pkg/lib/pkg/util.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -// Package pkgutil contains utility functions for packages +// Package pkg contains utility functions for packages package pkg import ( diff --git a/pkg/lib/update/pruninglocalpackagereader.go b/pkg/lib/update/pruninglocalpackagereader.go index 1358e000e8..78b7173ec7 100644 --- a/pkg/lib/update/pruninglocalpackagereader.go +++ b/pkg/lib/update/pruninglocalpackagereader.go @@ -1,3 +1,17 @@ +// Copyright 2026 The kpt Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + package update import ( diff --git a/pkg/lib/update/resource-merge.go b/pkg/lib/update/resource-merge.go index b54e229ecf..85dbeeb426 100644 --- a/pkg/lib/update/resource-merge.go +++ b/pkg/lib/update/resource-merge.go @@ -21,9 +21,9 @@ import ( "path/filepath" "strings" - "github.com/kptdev/kpt/pkg/lib/pkg/diff" + pkgdiff "github.com/kptdev/kpt/pkg/lib/pkg/diff" "github.com/kptdev/kpt/pkg/lib/types" - merge4 "github.com/kptdev/kpt/pkg/lib/update/merge3" + "github.com/kptdev/kpt/pkg/lib/update/merge3" "sigs.k8s.io/kustomize/kyaml/pathutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" @@ -132,7 +132,7 @@ func (u ResourceMergeUpdater) updatePackage(subPkgPath, localPath, updatedPath, // Package deleted from upstream case originalExists && localExists && !updatedExists: // Check the diff. If there are local changes, we keep the subpackage. - diff, err := diff.PkgDiff(originalPath, localPath) + diff, err := pkgdiff.PkgDiff(originalPath, localPath) if err != nil { return errors.E(op, types.UniquePath(localPath), err) } @@ -166,7 +166,7 @@ func (u ResourceMergeUpdater) mergePackage(localPath, updatedPath, originalPath, return err } - mergedKos, err := merge4.Merge( + mergedKos, err := merge3.Merge( originalKos, updatedKos, destinationKos, crdSchemas, ) if err != nil { @@ -370,8 +370,8 @@ func getCrdSchemas(updated fn.KubeObjects, destination fn.KubeObjects) ([]byte, var kubeobjects fn.KubeObjects copy(kubeobjects, updated) kubeobjects = append(kubeobjects, destination...) - _, crdObjects := merge4.FilterCrds(kubeobjects) - crdSchemas, err := merge4.SchemasFromCrdKubeObjects(crdObjects) + _, crdObjects := merge3.FilterCrds(kubeobjects) + crdSchemas, err := merge3.SchemasFromCrdKubeObjects(crdObjects) if err != nil { klog.Error("An error occurred during CRD extraction: %w", err) return nil, nil diff --git a/pkg/lib/update/update.go b/pkg/lib/update/update.go index b5c5483935..b3e59b5f5a 100644 --- a/pkg/lib/update/update.go +++ b/pkg/lib/update/update.go @@ -23,7 +23,7 @@ import ( "path/filepath" "strings" - git2 "github.com/kptdev/kpt/internal/gitutil" + internalgitutil "github.com/kptdev/kpt/internal/gitutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/errors" @@ -32,7 +32,7 @@ import ( "github.com/kptdev/kpt/pkg/lib/update/updatetypes" "github.com/kptdev/kpt/pkg/lib/util/addmergecomment" "github.com/kptdev/kpt/pkg/lib/util/fetch" - "github.com/kptdev/kpt/pkg/lib/util/git" + gitutil "github.com/kptdev/kpt/pkg/lib/util/git" "github.com/kptdev/kpt/pkg/lib/util/stack" "github.com/kptdev/kpt/pkg/printer" "sigs.k8s.io/kustomize/kyaml/copyutil" @@ -101,7 +101,7 @@ type Command struct { Strategy kptfilev1.UpdateStrategyType // cachedUpstreamRepos is an upstream repo already fetched for a given repoSpec CloneRef - cachedUpstreamRepos map[string]*git2.GitUpstreamRepo + cachedUpstreamRepos map[string]*internalgitutil.GitUpstreamRepo } func GetUpdater(strategy string) updatetypes.Updater { @@ -147,7 +147,7 @@ func (u *Command) Run(ctx context.Context) error { return errors.E(op, u.Pkg.UniquePath, err) } if u.cachedUpstreamRepos == nil { - u.cachedUpstreamRepos = make(map[string]*git2.GitUpstreamRepo) + u.cachedUpstreamRepos = make(map[string]*internalgitutil.GitUpstreamRepo) } packageCount := 0 @@ -198,7 +198,7 @@ func (u *Command) Run(ctx context.Context) error { } // GetCachedUpstreamRepos returns repos cached during update -func (u Command) GetCachedUpstreamRepos() map[string]*git2.GitUpstreamRepo { +func (u Command) GetCachedUpstreamRepos() map[string]*internalgitutil.GitUpstreamRepo { return u.cachedUpstreamRepos } @@ -268,7 +268,7 @@ func (u Command) updateRootPackage(ctx context.Context, p *pkg.Pkg) error { pr.PrintPackage(p, p != u.Pkg) g := kf.Upstream.Git - updated := &git.RepoSpec{OrgRepo: g.Repo, Path: g.Directory, Ref: g.Ref} + updated := &gitutil.RepoSpec{OrgRepo: g.Repo, Path: g.Directory, Ref: g.Ref} pr.Printf("Fetching upstream from %s@%s\n", kf.Upstream.Git.Repo, kf.Upstream.Git.Ref) cloner := fetch.NewCloner(updated, fetch.WithCachedRepo(u.cachedUpstreamRepos)) if err := cloner.ClonerUsingGitExec(ctx); err != nil { @@ -279,7 +279,7 @@ func (u Command) updateRootPackage(ctx context.Context, p *pkg.Pkg) error { var origin repoClone if kf.UpstreamLock != nil { gLock := kf.UpstreamLock.Git - originRepoSpec := &git.RepoSpec{OrgRepo: gLock.Repo, Path: gLock.Directory, Ref: gLock.Commit} + originRepoSpec := &gitutil.RepoSpec{OrgRepo: gLock.Repo, Path: gLock.Directory, Ref: gLock.Commit} pr.Printf("Fetching origin from %s@%s\n", kf.Upstream.Git.Repo, kf.Upstream.Git.Ref) if err := fetch.NewCloner(originRepoSpec, fetch.WithCachedRepo(u.cachedUpstreamRepos)).ClonerUsingGitExec(ctx); err != nil { return errors.E(op, p.UniquePath, err) diff --git a/pkg/lib/update/update_test.go b/pkg/lib/update/update_test.go index 230ad6951a..5997a4c8ee 100644 --- a/pkg/lib/update/update_test.go +++ b/pkg/lib/update/update_test.go @@ -27,7 +27,7 @@ import ( "github.com/kptdev/kpt/internal/testutil/pkgbuilder" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" - update2 "github.com/kptdev/kpt/pkg/lib/update" + "github.com/kptdev/kpt/pkg/lib/update" "github.com/kptdev/kpt/pkg/printer/fake" "github.com/stretchr/testify/assert" "sigs.k8s.io/kustomize/kyaml/copyutil" @@ -74,7 +74,7 @@ func TestCommand_Run_noRefChanges(t *testing.T) { return } upstreamRepo := g.Repos[testutil.Upstream] - cmd := &update2.Command{ + cmd := &update.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Strategy: strategy, } @@ -137,7 +137,7 @@ func TestCommand_Run_subDir(t *testing.T) { upstreamRepo := g.Repos[testutil.Upstream] // Update the local package - if !assert.NoError(t, (&update2.Command{ + if !assert.NoError(t, (&update.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Ref: "v1.2", Strategy: strategy, @@ -193,7 +193,7 @@ func TestCommand_Run_noChanges(t *testing.T) { upstreamRepo := g.Repos[testutil.Upstream] // Update the local package - err := (&update2.Command{ + err := (&update.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Strategy: u.updater, }).Run(fake.CtxWithDefaultPrinter()) @@ -375,7 +375,7 @@ func TestCommand_Run_localPackageChanges(t *testing.T) { } // run the command - err = (&update2.Command{ + err = (&update.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Ref: masterBranch, Strategy: tc.strategy, @@ -445,7 +445,7 @@ func TestCommand_Run_toBranchRef(t *testing.T) { upstreamRepo := g.Repos[testutil.Upstream] // Update the local package - if !assert.NoError(t, (&update2.Command{ + if !assert.NoError(t, (&update.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Strategy: strategy, Ref: "exp", @@ -809,7 +809,7 @@ func TestCommand_Run_toBranchRefWithSubpkgs(t *testing.T) { } // Update the local package - if !assert.NoError(t, (&update2.Command{ + if !assert.NoError(t, (&update.Command{ Pkg: testutil.CreatePkgOrFail(t, path.Join(g.LocalWorkspace.FullPackagePath(), tc.updateSubPkg)), Strategy: tc.strategy, Ref: tc.updateRef, @@ -855,7 +855,7 @@ func TestCommand_Run_toTagRef(t *testing.T) { upstreamRepo := g.Repos[testutil.Upstream] // Update the local package - if !assert.NoError(t, (&update2.Command{ + if !assert.NoError(t, (&update.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Strategy: strategy, Ref: "v1.0", @@ -912,7 +912,7 @@ func TestCommand_ResourceMerge_NonKRMUpdates(t *testing.T) { upstreamRepo := g.Repos[testutil.Upstream] // Update the local package - if !assert.NoError(t, (&update2.Command{ + if !assert.NoError(t, (&update.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Strategy: strategy, Ref: "v1.0", @@ -955,7 +955,7 @@ func TestCommand_Run_noUpstreamReference(t *testing.T) { testutil.AddKptfileToWorkspace(t, w, kf) // Update the local package - err := (&update2.Command{ + err := (&update.Command{ Pkg: testutil.CreatePkgOrFail(t, w.FullPackagePath()), }).Run(fake.CtxWithDefaultPrinter()) @@ -968,7 +968,7 @@ func TestCommand_Run_failInvalidPath(t *testing.T) { strategy := kptfilev1.UpdateStrategies[i] t.Run(string(strategy), func(t *testing.T) { path := filepath.Join("fake", "path") - err := (&update2.Command{ + err := (&update.Command{ Pkg: testutil.CreatePkgOrFail(t, path), Strategy: strategy, }).Run(fake.CtxWithDefaultPrinter()) @@ -1033,7 +1033,7 @@ func TestCommand_Run_badUpstreamLock(t *testing.T) { } // Update the local package. - err = (&update2.Command{ + err = (&update.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), }).Run(fake.CtxWithDefaultPrinter()) @@ -1068,7 +1068,7 @@ func TestCommand_Run_failInvalidRef(t *testing.T) { return } - err := (&update2.Command{ + err := (&update.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Ref: "exp", Strategy: strategy, @@ -1126,7 +1126,7 @@ func TestCommand_Run_manualChange(t *testing.T) { return } - err := (&update2.Command{ + err := (&update.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), }).Run(fake.CtxWithDefaultPrinter()) if !assert.NoError(t, err) { @@ -1180,7 +1180,7 @@ func TestCommand_Run_symlinks(t *testing.T) { } upstreamRepo := g.Repos[testutil.Upstream] - err := (&update2.Command{ + err := (&update.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), }).Run(fake.CtxWithDefaultPrinter()) if !assert.NoError(t, err) { @@ -1228,7 +1228,7 @@ func TestCommand_Run_badStrategy(t *testing.T) { } // Update the local package - err := (&update2.Command{ + err := (&update.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Strategy: strategy, }).Run(fake.CtxWithDefaultPrinter()) @@ -2007,7 +2007,7 @@ func TestCommand_Run_local_subpackages(t *testing.T) { return } - err := (&update2.Command{ + err := (&update.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), Strategy: strategy, }).Run(fake.CtxWithDefaultPrinter()) @@ -3543,7 +3543,7 @@ func TestRun_remote_subpackages(t *testing.T) { return } - err := (&update2.Command{ + err := (&update.Command{ Pkg: testutil.CreatePkgOrFail(t, g.LocalWorkspace.FullPackagePath()), }).Run(fake.CtxWithDefaultPrinter()) @@ -3688,7 +3688,7 @@ func TestRootPackageIsUnfetched(t *testing.T) { } testutil.AddKptfileToWorkspace(t, w, kf) - err := (&update2.Command{ + err := (&update.Command{ Pkg: testutil.CreatePkgOrFail(t, w.FullPackagePath()), }).Run(fake.CtxWithDefaultPrinter()) if !assert.NoError(t, err) { @@ -3759,7 +3759,7 @@ func TestMultiUpdateCache(t *testing.T) { UpdateStrategy: kptfilev1.ResourceMerge, } testutil.AddKptfileToWorkspace(t, w, kf) - cmd := update2.Command{ + cmd := update.Command{ Pkg: testutil.CreatePkgOrFail(t, w.FullPackagePath()), } err := cmd.Run(fake.CtxWithDefaultPrinter()) @@ -3864,7 +3864,7 @@ func TestReplaceNonKRMFiles(t *testing.T) { // expectedLocal. err = os.WriteFile(filepath.Join(updated, "new.yaml"), []byte("a: b"), 0600) assert.NoError(t, err) - err = update2.ReplaceNonKRMFiles(updated, original, local) + err = update.ReplaceNonKRMFiles(updated, original, local) assert.NoError(t, err) tg := testutil.TestGitRepo{} tg.AssertEqual(t, local, expectedLocal, false) diff --git a/pkg/lib/util/fetch/fetch.go b/pkg/lib/util/fetch/fetch.go index 5aabd0064c..b3a966c488 100644 --- a/pkg/lib/util/fetch/fetch.go +++ b/pkg/lib/util/fetch/fetch.go @@ -22,7 +22,7 @@ import ( "path/filepath" "strings" - "github.com/kptdev/kpt/internal/gitutil" + internalgitutil "github.com/kptdev/kpt/internal/gitutil" "github.com/kptdev/kpt/pkg/lib/errors" "github.com/kptdev/kpt/pkg/lib/types" "github.com/otiai10/copy" @@ -31,7 +31,7 @@ import ( kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "github.com/kptdev/kpt/pkg/kptfile/kptfileutil" "github.com/kptdev/kpt/pkg/lib/pkg" - "github.com/kptdev/kpt/pkg/lib/util/git" + gitutil "github.com/kptdev/kpt/pkg/lib/util/git" "github.com/kptdev/kpt/pkg/printer" ) @@ -55,7 +55,7 @@ func (c Command) Run(ctx context.Context) error { } g := kf.Upstream.Git - repoSpec := &git.RepoSpec{ + repoSpec := &gitutil.RepoSpec{ OrgRepo: g.Repo, Path: g.Directory, Ref: g.Ref, @@ -97,21 +97,21 @@ func (c Command) validate(kf *kptfilev1.KptFile) error { // rather than recloning them each time. type Cloner struct { // repoSpec spec to clone - repoSpec *git.RepoSpec + repoSpec *gitutil.RepoSpec // cachedRepos - cachedRepo map[string]*gitutil.GitUpstreamRepo + cachedRepo map[string]*internalgitutil.GitUpstreamRepo } type NewClonerOption func(*Cloner) -func WithCachedRepo(r map[string]*gitutil.GitUpstreamRepo) NewClonerOption { +func WithCachedRepo(r map[string]*internalgitutil.GitUpstreamRepo) NewClonerOption { return func(c *Cloner) { c.cachedRepo = r } } -func NewCloner(r *git.RepoSpec, opts ...NewClonerOption) *Cloner { +func NewCloner(r *gitutil.RepoSpec, opts ...NewClonerOption) *Cloner { c := &Cloner{ repoSpec: r, } @@ -119,7 +119,7 @@ func NewCloner(r *git.RepoSpec, opts ...NewClonerOption) *Cloner { opt(c) } if c.cachedRepo == nil { - c.cachedRepo = make(map[string]*gitutil.GitUpstreamRepo) + c.cachedRepo = make(map[string]*internalgitutil.GitUpstreamRepo) } return c } @@ -169,7 +169,7 @@ func (c *Cloner) ClonerUsingGitExec(ctx context.Context) error { // fetches and caches all tag and branch refs from the upstream repo. upstreamRepo, exists := c.cachedRepo[c.repoSpec.CloneSpec()] if !exists { - newUpstreamRemp, err := gitutil.NewGitUpstreamRepo(ctx, c.repoSpec.CloneSpec()) + newUpstreamRemp, err := internalgitutil.NewGitUpstreamRepo(ctx, c.repoSpec.CloneSpec()) if err != nil { return errors.E(op, errors.Git, errors.Repo(c.repoSpec.CloneSpec()), err) } @@ -196,7 +196,7 @@ func (c *Cloner) ClonerUsingGitExec(ctx context.Context) error { return errors.E(op, errors.Git, errors.Repo(c.repoSpec.CloneSpec()), err) } - gitRunner, err := gitutil.NewLocalGitRunner(dir) + gitRunner, err := internalgitutil.NewLocalGitRunner(dir) if err != nil { return errors.E(op, errors.Git, errors.Repo(c.repoSpec.CloneSpec()), err) } @@ -214,7 +214,7 @@ func (c *Cloner) ClonerUsingGitExec(ctx context.Context) error { // sure that any changes in the local worktree are cleaned out. _, err = gitRunner.Run(ctx, "reset", "--hard", commit) if err != nil { - gitutil.AmendGitExecError(err, func(e *gitutil.GitExecError) { + internalgitutil.AmendGitExecError(err, func(e *internalgitutil.GitExecError) { e.Repo = c.repoSpec.CloneSpec() e.Ref = commit }) diff --git a/pkg/lib/util/parse/parse.go b/pkg/lib/util/parse/parse.go index 16c85c6e38..3f5fe78a0a 100644 --- a/pkg/lib/util/parse/parse.go +++ b/pkg/lib/util/parse/parse.go @@ -23,7 +23,7 @@ import ( "regexp" "strings" - "github.com/kptdev/kpt/internal/gitutil" + internalgitutil "github.com/kptdev/kpt/internal/gitutil" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" "sigs.k8s.io/kustomize/kyaml/errors" ) @@ -64,7 +64,7 @@ func GitParseArgs(ctx context.Context, args []string, explicitDest bool) (Target return g, err } if version == "" { - gur, err := gitutil.NewGitUpstreamRepo(ctx, repo) + gur, err := internalgitutil.NewGitUpstreamRepo(ctx, repo) if err != nil { return g, err } @@ -97,7 +97,7 @@ func targetFromPkgURL(ctx context.Context, pkgURL string, dest string, explicitD dir = "/" } if ref == "" { - gur, err := gitutil.NewGitUpstreamRepo(ctx, repo) + gur, err := internalgitutil.NewGitUpstreamRepo(ctx, repo) if err != nil { return g, err } @@ -196,7 +196,7 @@ func pkgURLFromGHURL(ctx context.Context, v string, findRepoBranches func(contex // getRepoBranches returns a slice of branches in upstream repo func getRepoBranches(ctx context.Context, repo string) ([]string, error) { - gur, err := gitutil.NewGitUpstreamRepo(ctx, repo) + gur, err := internalgitutil.NewGitUpstreamRepo(ctx, repo) if err != nil { return nil, err } diff --git a/thirdparty/cmdconfig/commands/cmdeval/cmdeval.go b/thirdparty/cmdconfig/commands/cmdeval/cmdeval.go index 4958914873..11928339f8 100644 --- a/thirdparty/cmdconfig/commands/cmdeval/cmdeval.go +++ b/thirdparty/cmdconfig/commands/cmdeval/cmdeval.go @@ -19,7 +19,6 @@ import ( "github.com/kptdev/kpt/pkg/lib/runneroptions" argsutil "github.com/kptdev/kpt/pkg/lib/util/args" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" - "github.com/kptdev/kpt/pkg/lib/util/path" pathutil "github.com/kptdev/kpt/pkg/lib/util/path" "github.com/kptdev/kpt/pkg/printer" "github.com/kptdev/kpt/thirdparty/cmdconfig/commands/runner" @@ -201,8 +200,8 @@ func (r *EvalFnRunner) NewFunction() *kptfile.Function { newFn.Exclusions = []kptfile.Selector{r.Exclusion} } if r.FnConfigPath != "" { - fnConfigAbsPath, _, _ := path.ResolveAbsAndRelPaths(r.FnConfigPath) - pkgAbsPath, _, _ := path.ResolveAbsAndRelPaths(r.runFns.Path) + fnConfigAbsPath, _, _ := pathutil.ResolveAbsAndRelPaths(r.FnConfigPath) + pkgAbsPath, _, _ := pathutil.ResolveAbsAndRelPaths(r.runFns.Path) newFn.ConfigPath, _ = filepath.Rel(pkgAbsPath, fnConfigAbsPath) } else { data := map[string]string{} From 594b4aaba43279b324f1c8c24908f2522c5960aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=B3zes=20L=C3=A1szl=C3=B3=20M=C3=A1t=C3=A9?= Date: Mon, 18 May 2026 09:58:20 +0200 Subject: [PATCH 6/6] alias pathutil everywhere MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mózes László Máté --- commands/fn/render/cmdrender.go | 4 ++-- commands/live/init/cmdliveinit.go | 4 ++-- commands/live/migrate/migratecmd.go | 6 +++--- commands/pkg/diff/cmddiff.go | 4 ++-- commands/pkg/get/cmdget.go | 4 ++-- commands/pkg/init/cmdinit.go | 4 ++-- commands/pkg/update/cmdupdate.go | 4 ++-- pkg/lib/pkg/pkg.go | 4 ++-- pkg/lib/pkg/pkg_test.go | 12 ++++++------ pkg/lib/util/get/get.go | 4 ++-- pkg/live/rgpath.go | 4 ++-- 11 files changed, 27 insertions(+), 27 deletions(-) diff --git a/commands/fn/render/cmdrender.go b/commands/fn/render/cmdrender.go index f1bdb03f52..ecbfa83d60 100644 --- a/commands/fn/render/cmdrender.go +++ b/commands/fn/render/cmdrender.go @@ -27,7 +27,7 @@ import ( "github.com/kptdev/kpt/pkg/lib/runneroptions" argsutil "github.com/kptdev/kpt/pkg/lib/util/args" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" - "github.com/kptdev/kpt/pkg/lib/util/path" + pathutil "github.com/kptdev/kpt/pkg/lib/util/path" "github.com/kptdev/kpt/pkg/printer" "github.com/spf13/cobra" "sigs.k8s.io/kustomize/kyaml/filesys" @@ -126,7 +126,7 @@ func (r *Runner) runE(_ *cobra.Command, _ []string) error { // capture the content to be written output = &outContent } - absPkgPath, _, err := path.ResolveAbsAndRelPaths(r.pkgPath) + absPkgPath, _, err := pathutil.ResolveAbsAndRelPaths(r.pkgPath) if err != nil { return err } diff --git a/commands/live/init/cmdliveinit.go b/commands/live/init/cmdliveinit.go index 8ad6737d6b..90d44f2c95 100644 --- a/commands/live/init/cmdliveinit.go +++ b/commands/live/init/cmdliveinit.go @@ -33,7 +33,7 @@ import ( "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/types" "github.com/kptdev/kpt/pkg/lib/util/attribution" - "github.com/kptdev/kpt/pkg/lib/util/path" + pathutil "github.com/kptdev/kpt/pkg/lib/util/path" "github.com/kptdev/kpt/pkg/printer" "github.com/spf13/cobra" "k8s.io/cli-runtime/pkg/genericclioptions" @@ -140,7 +140,7 @@ func (r *Runner) runE(_ *cobra.Command, args []string) error { return errors.E(op, err) } - absPath, _, err := path.ResolveAbsAndRelPaths(dir) + absPath, _, err := pathutil.ResolveAbsAndRelPaths(dir) if err != nil { return err } diff --git a/commands/live/migrate/migratecmd.go b/commands/live/migrate/migratecmd.go index 7e2af23bb7..03f5f1a44d 100644 --- a/commands/live/migrate/migratecmd.go +++ b/commands/live/migrate/migratecmd.go @@ -31,7 +31,7 @@ import ( "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/types" argsutil "github.com/kptdev/kpt/pkg/lib/util/args" - "github.com/kptdev/kpt/pkg/lib/util/path" + pathutil "github.com/kptdev/kpt/pkg/lib/util/path" "github.com/kptdev/kpt/pkg/live" "github.com/spf13/cobra" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -363,7 +363,7 @@ func (mr *Runner) migrateKptfileToRG(args []string) error { klog.V(4).Infoln("attempting to migrate from Kptfile inventory") fmt.Fprint(mr.ioStreams.Out, " reading existing Kptfile...") if !mr.dryRun { - dir, _, err := path.ResolveAbsAndRelPaths(args[0]) + dir, _, err := pathutil.ResolveAbsAndRelPaths(args[0]) if err != nil { return err } @@ -459,7 +459,7 @@ func (mr *Runner) migrateCMToRG(stdinBytes []byte, args []string) error { func (mr *Runner) createRGfile(ctx context.Context, args []string, prevID string) error { fmt.Fprint(mr.ioStreams.Out, " creating ResourceGroup object file...") if !mr.dryRun { - dir, _, err := path.ResolveAbsAndRelPaths(args[0]) + dir, _, err := pathutil.ResolveAbsAndRelPaths(args[0]) if err != nil { return err } diff --git a/commands/pkg/diff/cmddiff.go b/commands/pkg/diff/cmddiff.go index 1d38397c49..ed2ef95885 100644 --- a/commands/pkg/diff/cmddiff.go +++ b/commands/pkg/diff/cmddiff.go @@ -23,7 +23,7 @@ import ( "github.com/kptdev/kpt/pkg/lib/pkg/diff" argsutil "github.com/kptdev/kpt/pkg/lib/util/args" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" - "github.com/kptdev/kpt/pkg/lib/util/path" + pathutil "github.com/kptdev/kpt/pkg/lib/util/path" "github.com/kptdev/kpt/pkg/printer" "github.com/spf13/cobra" "sigs.k8s.io/kustomize/kyaml/filesys" @@ -101,7 +101,7 @@ func (r *Runner) preRunE(_ *cobra.Command, args []string) error { return err } - absResolvedPath, _, err := path.ResolveAbsAndRelPaths(resolvedPath) + absResolvedPath, _, err := pathutil.ResolveAbsAndRelPaths(resolvedPath) if err != nil { return err } diff --git a/commands/pkg/get/cmdget.go b/commands/pkg/get/cmdget.go index 0f9baea157..d3b3e61a02 100644 --- a/commands/pkg/get/cmdget.go +++ b/commands/pkg/get/cmdget.go @@ -29,7 +29,7 @@ import ( "github.com/kptdev/kpt/pkg/lib/util/cmdutil" "github.com/kptdev/kpt/pkg/lib/util/get" "github.com/kptdev/kpt/pkg/lib/util/parse" - "github.com/kptdev/kpt/pkg/lib/util/path" + pathutil "github.com/kptdev/kpt/pkg/lib/util/path" "github.com/spf13/cobra" "sigs.k8s.io/kustomize/kyaml/filesys" ) @@ -97,7 +97,7 @@ func (r *Runner) preRunE(_ *cobra.Command, args []string) error { } r.Get.Git = &t.Git - absDestPath, _, err := path.ResolveAbsAndRelPaths(t.Destination) + absDestPath, _, err := pathutil.ResolveAbsAndRelPaths(t.Destination) if err != nil { return err } diff --git a/commands/pkg/init/cmdinit.go b/commands/pkg/init/cmdinit.go index 232b6b4da3..f9ad1ca014 100644 --- a/commands/pkg/init/cmdinit.go +++ b/commands/pkg/init/cmdinit.go @@ -21,7 +21,7 @@ import ( "github.com/kptdev/kpt/pkg/kptpkg" "github.com/kptdev/kpt/pkg/lib/pkg" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" - "github.com/kptdev/kpt/pkg/lib/util/path" + pathutil "github.com/kptdev/kpt/pkg/lib/util/path" "github.com/spf13/cobra" "sigs.k8s.io/kustomize/kyaml/filesys" ) @@ -67,7 +67,7 @@ func (r *Runner) runE(_ *cobra.Command, args []string) error { args = append(args, pkg.CurDir) } - absPath, _, err := path.ResolveAbsAndRelPaths(args[0]) + absPath, _, err := pathutil.ResolveAbsAndRelPaths(args[0]) if err != nil { return err } diff --git a/commands/pkg/update/cmdupdate.go b/commands/pkg/update/cmdupdate.go index 133101fc25..63939b7409 100644 --- a/commands/pkg/update/cmdupdate.go +++ b/commands/pkg/update/cmdupdate.go @@ -29,7 +29,7 @@ import ( "github.com/kptdev/kpt/pkg/lib/update" argsutil "github.com/kptdev/kpt/pkg/lib/util/args" "github.com/kptdev/kpt/pkg/lib/util/cmdutil" - "github.com/kptdev/kpt/pkg/lib/util/path" + pathutil "github.com/kptdev/kpt/pkg/lib/util/path" "github.com/spf13/cobra" "sigs.k8s.io/kustomize/kyaml/filesys" ) @@ -95,7 +95,7 @@ func (r *Runner) preRunE(_ *cobra.Command, args []string) error { if err != nil { return err } - absResolvedPath, _, err := path.ResolveAbsAndRelPaths(resolvedPath) + absResolvedPath, _, err := pathutil.ResolveAbsAndRelPaths(resolvedPath) if err != nil { return err } diff --git a/pkg/lib/pkg/pkg.go b/pkg/lib/pkg/pkg.go index 7b8b7ce3cf..27710ec2f3 100644 --- a/pkg/lib/pkg/pkg.go +++ b/pkg/lib/pkg/pkg.go @@ -31,7 +31,7 @@ import ( "github.com/kptdev/kpt/pkg/lib/errors" "github.com/kptdev/kpt/pkg/lib/types" gitutil "github.com/kptdev/kpt/pkg/lib/util/git" - "github.com/kptdev/kpt/pkg/lib/util/path" + pathutil "github.com/kptdev/kpt/pkg/lib/util/path" regclientref "github.com/regclient/regclient/types/ref" "sigs.k8s.io/kustomize/kyaml/filesys" "sigs.k8s.io/kustomize/kyaml/kio" @@ -538,7 +538,7 @@ func ReadRGFile(pkgPath, rgfile string) (*rgfilev1alpha1.ResourceGroup, error) { if filepath.Base(rgfile) == rgfile { absPath = filepath.Join(pkgPath, rgfile) } else { - rgFilePath, _, err := path.ResolveAbsAndRelPaths(rgfile) + rgFilePath, _, err := pathutil.ResolveAbsAndRelPaths(rgfile) if err != nil { return nil, &RGError{ Path: types.UniquePath(rgfile), diff --git a/pkg/lib/pkg/pkg_test.go b/pkg/lib/pkg/pkg_test.go index 999309a146..f5a4b70843 100644 --- a/pkg/lib/pkg/pkg_test.go +++ b/pkg/lib/pkg/pkg_test.go @@ -24,7 +24,7 @@ import ( "github.com/kptdev/kpt/internal/testutil/pkgbuilder" kptfilev1 "github.com/kptdev/kpt/pkg/api/kptfile/v1" - "github.com/kptdev/kpt/pkg/lib/util/path" + pathutil "github.com/kptdev/kpt/pkg/lib/util/path" "github.com/stretchr/testify/assert" "sigs.k8s.io/kustomize/kyaml/filesys" ) @@ -79,7 +79,7 @@ func TestNewPkg(t *testing.T) { assert.NoError(t, err) revert := Chdir(t, filepath.Join(dir, test.workingDir)) defer revert() - absInputPath, _, err := path.ResolveAbsAndRelPaths(test.inputPath) + absInputPath, _, err := pathutil.ResolveAbsAndRelPaths(test.inputPath) assert.NoError(t, err) p, err := New(filesys.FileSystemOrOnDisk{}, absInputPath) assert.NoError(t, err) @@ -163,18 +163,18 @@ func TestAdjustDisplayPathForSubpkg(t *testing.T) { assert.NoError(t, err) revert := Chdir(t, filepath.Join(dir, "rootPkgParentDir", test.workingDir)) defer revert() - absPkgPath, _, err := path.ResolveAbsAndRelPaths(test.pkgPath) + absPkgPath, _, err := pathutil.ResolveAbsAndRelPaths(test.pkgPath) assert.NoError(t, err) parent, err := New(filesys.FileSystemOrOnDisk{}, absPkgPath) assert.NoError(t, err) if test.rootPkgParentDirPath != "" { - absRootPkgPath, _, err := path.ResolveAbsAndRelPaths(test.rootPkgParentDirPath) + absRootPkgPath, _, err := pathutil.ResolveAbsAndRelPaths(test.rootPkgParentDirPath) assert.NoError(t, err) rootPkg, err := New(filesys.FileSystemOrOnDisk{}, absRootPkgPath) assert.NoError(t, err) parent.rootPkgParentDirPath = string(rootPkg.UniquePath) } - absSubPkgPath, _, err := path.ResolveAbsAndRelPaths(test.subPkgPath) + absSubPkgPath, _, err := pathutil.ResolveAbsAndRelPaths(test.subPkgPath) assert.NoError(t, err) subPkg, err := New(filesys.FileSystemOrOnDisk{}, absSubPkgPath) assert.NoError(t, err) @@ -278,7 +278,7 @@ func TestDirectSubpackages(t *testing.T) { t.Run(tn, func(t *testing.T) { pkgPath := tc.pkg.ExpandPkg(t, nil) defer os.RemoveAll(pkgPath) - absPkgPath, _, err := path.ResolveAbsAndRelPaths(pkgPath) + absPkgPath, _, err := pathutil.ResolveAbsAndRelPaths(pkgPath) if !assert.NoError(t, err) { t.FailNow() } diff --git a/pkg/lib/util/get/get.go b/pkg/lib/util/get/get.go index fd20085c70..d100cd0d54 100644 --- a/pkg/lib/util/get/get.go +++ b/pkg/lib/util/get/get.go @@ -33,7 +33,7 @@ import ( "github.com/kptdev/kpt/pkg/lib/util/addmergecomment" "github.com/kptdev/kpt/pkg/lib/util/attribution" "github.com/kptdev/kpt/pkg/lib/util/fetch" - "github.com/kptdev/kpt/pkg/lib/util/path" + pathutil "github.com/kptdev/kpt/pkg/lib/util/path" "github.com/kptdev/kpt/pkg/lib/util/stack" "github.com/kptdev/kpt/pkg/printer" "sigs.k8s.io/kustomize/kyaml/filesys" @@ -122,7 +122,7 @@ func (c Command) Run(ctx context.Context) error { return cleanUpDirAndError(c.Destination, err) } - absDestPath, _, err := path.ResolveAbsAndRelPaths(c.Destination) + absDestPath, _, err := pathutil.ResolveAbsAndRelPaths(c.Destination) if err != nil { return err } diff --git a/pkg/live/rgpath.go b/pkg/live/rgpath.go index 56b98e0205..15bcf539f3 100644 --- a/pkg/live/rgpath.go +++ b/pkg/live/rgpath.go @@ -19,7 +19,7 @@ import ( "fmt" rgfilev1alpha1 "github.com/kptdev/kpt/pkg/api/resourcegroup/v1alpha1" - "github.com/kptdev/kpt/pkg/lib/util/path" + pathutil "github.com/kptdev/kpt/pkg/lib/util/path" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "sigs.k8s.io/cli-utils/pkg/manifestreader" "sigs.k8s.io/kustomize/kyaml/kio" @@ -41,7 +41,7 @@ type ResourceGroupPathManifestReader struct { // Kptfile data. If unable to generate the ResourceGroup inventory // object from the Kptfile, it is NOT an error. func (r *ResourceGroupPathManifestReader) Read() ([]*unstructured.Unstructured, error) { - absPkgPath, _, err := path.ResolveAbsAndRelPaths(r.PkgPath) + absPkgPath, _, err := pathutil.ResolveAbsAndRelPaths(r.PkgPath) if err != nil { return nil, err }