diff --git a/commands/alpha/rpkg/clone/command.go b/commands/alpha/rpkg/clone/command.go index 4aa3d05844..01bfe7feb2 100644 --- a/commands/alpha/rpkg/clone/command.go +++ b/commands/alpha/rpkg/clone/command.go @@ -19,6 +19,7 @@ import ( "fmt" "strings" + "github.com/GoogleContainerTools/kpt/commands/alpha/rpkg/util" "github.com/GoogleContainerTools/kpt/internal/docs/generated/rpkgdocs" "github.com/GoogleContainerTools/kpt/internal/errors" "github.com/GoogleContainerTools/kpt/internal/util/parse" @@ -114,9 +115,14 @@ func (r *runner) preRunE(cmd *cobra.Command, args []string) error { source := args[0] target := args[1] - if err := r.packageAlreadyExists(target); err != nil { + pkgExists, err := util.PackageAlreadyExists(r.ctx, r.client, r.repository, target, *r.cfg.Namespace) + if err != nil { return err } + if pkgExists { + return fmt.Errorf("`clone` cannot create a new revision for package %q that already exists in repo %q; make subsequent revisions using `copy`", + r.target, r.repository) + } switch { case strings.HasPrefix(source, "oci://"): @@ -204,24 +210,6 @@ func (r *runner) runE(cmd *cobra.Command, args []string) error { return nil } -func (r *runner) packageAlreadyExists(packageName string) error { - // only the first package revision can be created from init or clone, so - // we need to check that the package doesn't already exist. - packageRevisionList := porchapi.PackageRevisionList{} - if err := r.client.List(r.ctx, &packageRevisionList, &client.ListOptions{ - Namespace: *r.cfg.Namespace, - }); err != nil { - return err - } - for _, pr := range packageRevisionList.Items { - if pr.Spec.RepositoryName == r.repository && pr.Spec.PackageName == packageName { - return fmt.Errorf("`clone` cannot create a new revision for package %q that already exists in repo %q; make subsequent revisions using `copy`", - packageName, r.repository) - } - } - return nil -} - func toMergeStrategy(strategy string) (porchapi.PackageMergeStrategy, error) { switch strategy { case string(porchapi.ResourceMerge): diff --git a/commands/alpha/rpkg/init/command.go b/commands/alpha/rpkg/init/command.go index bc3203ef31..5c524e39a0 100644 --- a/commands/alpha/rpkg/init/command.go +++ b/commands/alpha/rpkg/init/command.go @@ -18,6 +18,7 @@ import ( "context" "fmt" + "github.com/GoogleContainerTools/kpt/commands/alpha/rpkg/util" "github.com/GoogleContainerTools/kpt/internal/docs/generated/rpkgdocs" "github.com/GoogleContainerTools/kpt/internal/errors" "github.com/GoogleContainerTools/kpt/internal/util/porch" @@ -90,7 +91,15 @@ func (r *runner) preRunE(cmd *cobra.Command, args []string) error { } r.name = args[0] - return r.packageAlreadyExists(r.name) + pkgExists, err := util.PackageAlreadyExists(r.ctx, r.client, r.repository, r.name, *r.cfg.Namespace) + if err != nil { + return err + } + if pkgExists { + return fmt.Errorf("`init` cannot create a new revision for package %q that already exists in repo %q; make subsequent revisions using `copy`", + r.name, r.repository) + } + return nil } func (r *runner) runE(cmd *cobra.Command, args []string) error { @@ -128,21 +137,3 @@ func (r *runner) runE(cmd *cobra.Command, args []string) error { fmt.Fprintf(cmd.OutOrStdout(), "%s created\n", pr.Name) return nil } - -func (r *runner) packageAlreadyExists(packageName string) error { - // only the first package revision can be created from init or clone, so - // we need to check that the package doesn't already exist. - packageRevisionList := porchapi.PackageRevisionList{} - if err := r.client.List(r.ctx, &packageRevisionList, &client.ListOptions{ - Namespace: *r.cfg.Namespace, - }); err != nil { - return err - } - for _, pr := range packageRevisionList.Items { - if pr.Spec.RepositoryName == r.repository && pr.Spec.PackageName == packageName { - return fmt.Errorf("`init` cannot create a new revision for package %q that already exists in repo %q; make subsequent revisions using `copy`", - packageName, r.repository) - } - } - return nil -} diff --git a/commands/alpha/rpkg/util/common.go b/commands/alpha/rpkg/util/common.go new file mode 100644 index 0000000000..4a89d557b3 --- /dev/null +++ b/commands/alpha/rpkg/util/common.go @@ -0,0 +1,39 @@ +// Copyright 2022 Google LLC +// +// 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 util + +import ( + "context" + + porchapi "github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +func PackageAlreadyExists(ctx context.Context, c client.Client, repository, packageName, namespace string) (bool, error) { + // only the first package revision can be created from init or clone, so + // we need to check that the package doesn't already exist. + packageRevisionList := porchapi.PackageRevisionList{} + if err := c.List(ctx, &packageRevisionList, &client.ListOptions{ + Namespace: namespace, + }); err != nil { + return false, err + } + for _, pr := range packageRevisionList.Items { + if pr.Spec.RepositoryName == repository && pr.Spec.PackageName == packageName { + return true, nil + } + } + return false, nil +} diff --git a/porch/pkg/git/git.go b/porch/pkg/git/git.go index 148fc96e32..aaaf38ab45 100644 --- a/porch/pkg/git/git.go +++ b/porch/pkg/git/git.go @@ -1053,6 +1053,5 @@ func reverseSlice(s []v1alpha1.Task) { s[first], s[last] = s[last], s[first] first++ last-- - } }