Skip to content

Commit

Permalink
address comments
Browse files Browse the repository at this point in the history
  • Loading branch information
natasha41575 committed Oct 14, 2022
1 parent 1bff276 commit ba12df0
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 39 deletions.
26 changes: 7 additions & 19 deletions commands/alpha/rpkg/clone/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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://"):
Expand Down Expand Up @@ -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):
Expand Down
29 changes: 10 additions & 19 deletions commands/alpha/rpkg/init/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
39 changes: 39 additions & 0 deletions commands/alpha/rpkg/util/common.go
Original file line number Diff line number Diff line change
@@ -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
}
1 change: 0 additions & 1 deletion porch/pkg/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,5 @@ func reverseSlice(s []v1alpha1.Task) {
s[first], s[last] = s[last], s[first]
first++
last--

}
}

0 comments on commit ba12df0

Please sign in to comment.