Skip to content

Commit

Permalink
Add kpt command for package edit
Browse files Browse the repository at this point in the history
  • Loading branch information
mortent committed Apr 27, 2022
1 parent 4db5feb commit 2b02059
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 3 deletions.
2 changes: 2 additions & 0 deletions commands/rpkgcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/GoogleContainerTools/kpt/internal/cmdrpkgapprove"
"github.com/GoogleContainerTools/kpt/internal/cmdrpkgclone"
"github.com/GoogleContainerTools/kpt/internal/cmdrpkgdel"
"github.com/GoogleContainerTools/kpt/internal/cmdrpkgedit"
"github.com/GoogleContainerTools/kpt/internal/cmdrpkgget"
"github.com/GoogleContainerTools/kpt/internal/cmdrpkginit"
"github.com/GoogleContainerTools/kpt/internal/cmdrpkgpropose"
Expand Down Expand Up @@ -75,6 +76,7 @@ func NewRpkgCommand(ctx context.Context, version string) *cobra.Command {
cmdrpkgapprove.NewCommand(ctx, kubeflags),
cmdrpkgreject.NewCommand(ctx, kubeflags),
cmdrpkgdel.NewCommand(ctx, kubeflags),
cmdrpkgedit.NewCommand(ctx, kubeflags),
)

return repo
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/GoogleContainerTools/kpt
go 1.17

require (
github.com/GoogleContainerTools/kpt/porch/api v0.0.0-20220419065917-49bc6c977530
github.com/GoogleContainerTools/kpt/porch/api v0.0.0-20220426215627-4db5feb3a360
github.com/cpuguy83/go-md2man/v2 v2.0.1
github.com/go-errors/errors v1.4.2
github.com/google/go-cmp v0.5.7
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ github.com/Azure/go-autorest/tracing v0.6.0/go.mod h1:+vhtPC754Xsa23ID7GlGsrdKBp
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo=
github.com/DataDog/datadog-go v3.2.0+incompatible/go.mod h1:LButxg5PwREeZtORoXG3tL4fMGNddJ+vMq1mwgfaqoQ=
github.com/GoogleContainerTools/kpt/porch/api v0.0.0-20220419065917-49bc6c977530 h1:fTWATexPCxotu/KoGF4wVWS9LbAnA8SVazGOnFXgyz0=
github.com/GoogleContainerTools/kpt/porch/api v0.0.0-20220419065917-49bc6c977530/go.mod h1:51Vk7QZ+XUzHCvQBi7t9tiWqTXvy6T13cv/inUXJJ0s=
github.com/GoogleContainerTools/kpt/porch/api v0.0.0-20220426215627-4db5feb3a360 h1:ajgEZIm6i8Eh2OYe91OdMk3cQdbxdPvtF5kG2+DqIYA=
github.com/GoogleContainerTools/kpt/porch/api v0.0.0-20220426215627-4db5feb3a360/go.mod h1:51Vk7QZ+XUzHCvQBi7t9tiWqTXvy6T13cv/inUXJJ0s=
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd h1:sjQovDkwrZp8u+gxLtPgKGjk5hCxuy2hrRejBTA9xFU=
github.com/MakeNowJust/heredoc v0.0.0-20170808103936-bb23615498cd/go.mod h1:64YHyfSL2R96J44Nlwm39UHepQbyR5q10x7iYa1ks2E=
github.com/NYTimes/gziphandler v0.0.0-20170623195520-56545f4a5d46/go.mod h1:3wb06e3pkSAbeQ52E9H9iFoQsEEwGN64994WTCIhntQ=
Expand Down
152 changes: 152 additions & 0 deletions internal/cmdrpkgedit/command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// 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 cmdrpkgedit

import (
"context"
"fmt"

"github.com/GoogleContainerTools/kpt/internal/errors"
"github.com/GoogleContainerTools/kpt/internal/util/porch"
porchapi "github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1"
"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/cli-runtime/pkg/genericclioptions"
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
command = "cmdrpkgedit"
longMsg = `
kpt alpha rpkg edit SOURCE_PACKAGE TARGET
Creates a new copy of a source package in the target repository.
Args:
SOURCE_PACKAGE:
Source package. Must be a package resource name: <repository>-<hash>
NAME:
Target package revision name
Example: package-name
Flags:
--repository
Repository to which package will be copied.
--revision
Revision of the downstream package.
`
)

func NewCommand(ctx context.Context, rcg *genericclioptions.ConfigFlags) *cobra.Command {
return newRunner(ctx, rcg).Command
}

func newRunner(ctx context.Context, rcg *genericclioptions.ConfigFlags) *runner {
r := &runner{
ctx: ctx,
cfg: rcg,
}
c := &cobra.Command{
Use: "edit SOURCE_PACKAGE NAME",
Short: "Creates a copy of a source package in the target repository.",
Long: longMsg,
Example: "kpt alpha rpkg edit upstream-package-name target-package-name --repository target-repository --revision v1",
PreRunE: r.preRunE,
RunE: r.runE,
Hidden: porch.HidePorchCommands,
}
r.Command = c

c.Flags().StringVar(&r.repository, "repository", "", "Repository in which the copy will be created.")
c.Flags().StringVar(&r.revision, "revision", "v1", "Revision of the copied package.")

return r
}

type runner struct {
ctx context.Context
cfg *genericclioptions.ConfigFlags
client client.Client
Command *cobra.Command

edit porchapi.PackageEditTaskSpec

repository string // Target repository
revision string // Target package revision
target string // Target package name
}

func (r *runner) preRunE(cmd *cobra.Command, args []string) error {
const op errors.Op = command + ".preRunE"
client, err := porch.CreateClient(r.cfg)
if err != nil {
return errors.E(op, err)
}
r.client = client

if len(args) < 2 {
return errors.E(op, fmt.Errorf("SOURCE_PACKAGE and NAME are required positional arguments; %d provided", len(args)))
}

if r.repository == "" {
return errors.E(op, fmt.Errorf("--repository is required to specify target repository"))
}

source := args[0]
target := args[1]

r.edit.Source = &porchapi.PackageRevisionRef{
Name: source,
}

r.target = target

return nil
}

func (r *runner) runE(cmd *cobra.Command, args []string) error {
const op errors.Op = command + ".runE"

pr := &porchapi.PackageRevision{
TypeMeta: metav1.TypeMeta{
Kind: "PackageRevision",
APIVersion: porchapi.SchemeGroupVersion.Identifier(),
},
ObjectMeta: metav1.ObjectMeta{
Namespace: *r.cfg.Namespace,
},
Spec: porchapi.PackageRevisionSpec{
PackageName: r.target,
Revision: r.revision,
RepositoryName: r.repository,
Tasks: []porchapi.Task{
{
Type: porchapi.TaskTypeEdit,
Edit: &r.edit,
},
},
},
}
if err := r.client.Create(r.ctx, pr); err != nil {
return errors.E(op, err)
}

fmt.Fprintf(cmd.OutOrStdout(), "%s created", pr.Name)
return nil
}

0 comments on commit 2b02059

Please sign in to comment.