Skip to content

Commit

Permalink
oci-support feature branch (kptdev#2666)
Browse files Browse the repository at this point in the history
Rebased on main and squashed

Co-authored-by: Louis DeJardin <[email protected]>

OCI: Fix Porch Build
  • Loading branch information
loudej authored and martinmaly committed Feb 18, 2022
1 parent 67aa8c6 commit 2bc4bd9
Show file tree
Hide file tree
Showing 36 changed files with 3,101 additions and 466 deletions.
5 changes: 4 additions & 1 deletion commands/pkgcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ import (
"github.com/GoogleContainerTools/kpt/internal/cmddiff"
"github.com/GoogleContainerTools/kpt/internal/cmdget"
"github.com/GoogleContainerTools/kpt/internal/cmdinit"
"github.com/GoogleContainerTools/kpt/internal/cmdpull"
"github.com/GoogleContainerTools/kpt/internal/cmdpush"
"github.com/GoogleContainerTools/kpt/internal/cmdupdate"
"github.com/GoogleContainerTools/kpt/internal/docs/generated/pkgdocs"
"github.com/GoogleContainerTools/kpt/thirdparty/cmdconfig/commands/cmdtree"
Expand Down Expand Up @@ -47,7 +49,8 @@ func GetPkgCommand(ctx context.Context, name string) *cobra.Command {
pkg.AddCommand(
cmdget.NewCommand(ctx, name), cmdinit.NewCommand(ctx, name),
cmdupdate.NewCommand(ctx, name), cmddiff.NewCommand(ctx, name),
cmdtree.NewCommand(ctx, name),
cmdtree.NewCommand(ctx, name), cmdpull.NewCommand(ctx, name),
cmdpush.NewCommand(ctx, name),
)
return pkg
}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ module github.com/GoogleContainerTools/kpt
go 1.16

require (
github.com/Masterminds/semver v1.5.0
github.com/cpuguy83/go-md2man/v2 v2.0.1
github.com/go-errors/errors v1.4.0
github.com/google/go-containerregistry v0.6.0
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510
github.com/igorsobreira/titlecase v0.0.0-20140109233139-4156b5b858ac
github.com/otiai10/copy v1.6.0
Expand Down
393 changes: 393 additions & 0 deletions go.sum

Large diffs are not rendered by default.

22 changes: 16 additions & 6 deletions internal/cmdget/cmdget.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/GoogleContainerTools/kpt/internal/util/cmdutil"
"github.com/GoogleContainerTools/kpt/internal/util/get"
"github.com/GoogleContainerTools/kpt/internal/util/parse"
"github.com/GoogleContainerTools/kpt/internal/util/remote"
kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
"github.com/spf13/cobra"
)
Expand All @@ -38,7 +39,7 @@ func NewRunner(ctx context.Context, parent string) *Runner {
ctx: ctx,
}
c := &cobra.Command{
Use: "get REPO_URI[.git]/PKG_PATH[@VERSION] [LOCAL_DEST_DIRECTORY]",
Use: "get {REPO_URI[.git]/PKG_PATH[@VERSION]|IMAGE:TAG} [LOCAL_DEST_DIRECTORY]",
Args: cobra.MinimumNArgs(1),
Short: docs.GetShort,
Long: docs.GetShort + "\n" + docs.GetLong,
Expand Down Expand Up @@ -84,15 +85,24 @@ func (r *Runner) preRunE(_ *cobra.Command, args []string) error {
args[1] = resolvedPath
}
}
t, err := parse.GitParseArgs(r.ctx, args)
destination, err := parse.ParseArgs(r.ctx, args, parse.Options{
SetGit: func(git *kptfilev1.Git) error {
r.Get.Git = git
r.Get.Upstream = remote.NewGitUpstream(git)
return nil
},
SetOci: func(oci *kptfilev1.Oci) error {
r.Get.Upstream = remote.NewOciUpstream(oci)
return nil
},
})
if err != nil {
return errors.E(op, err)
return err
}

r.Get.Git = &t.Git
p, err := pkg.New(t.Destination)
p, err := pkg.New(destination)
if err != nil {
return errors.E(op, types.UniquePath(t.Destination), err)
return errors.E(op, types.UniquePath(destination), err)
}
r.Get.Destination = string(p.UniquePath)

Expand Down
1 change: 1 addition & 0 deletions internal/cmdget/cmdget_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ func TestCmd_Execute_flagAndArgParsing(t *testing.T) {
validations: func(repo, dir string, r *cmdget.Runner, err error) {
assert.Error(t, err)
assert.Contains(t, err.Error(), "specify '.git'")
assert.Contains(t, err.Error(), "specify 'oci://'")
},
},
"valid strategy provided": {
Expand Down
108 changes: 108 additions & 0 deletions internal/cmdpull/cmdpull.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
// Copyright 2021 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 cmdpull

import (
"context"
"os"

docs "github.com/GoogleContainerTools/kpt/internal/docs/generated/pkgdocs"
"github.com/GoogleContainerTools/kpt/internal/errors"
"github.com/GoogleContainerTools/kpt/internal/pkg"
"github.com/GoogleContainerTools/kpt/internal/types"
"github.com/GoogleContainerTools/kpt/internal/util/argutil"
"github.com/GoogleContainerTools/kpt/internal/util/cmdutil"
"github.com/GoogleContainerTools/kpt/internal/util/parse"
"github.com/GoogleContainerTools/kpt/internal/util/pull"
"github.com/GoogleContainerTools/kpt/internal/util/remote"
kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
"github.com/spf13/cobra"
)

// NewRunner returns a command runner
func NewRunner(ctx context.Context, parent string) *Runner {
r := &Runner{
ctx: ctx,
}
c := &cobra.Command{
Use: "pull {REPO_URI[.git]/PKG_PATH[@VERSION]|IMAGE:TAG} [LOCAL_DEST_DIRECTORY]",
Args: cobra.MinimumNArgs(1),
Short: docs.PullShort,
Long: docs.PullShort + "\n" + docs.PullLong,
Example: docs.PullExamples,
RunE: r.runE,
PreRunE: r.preRunE,
}
cmdutil.FixDocs("kpt", parent, c)
r.Command = c
return r
}

func NewCommand(ctx context.Context, parent string) *cobra.Command {
return NewRunner(ctx, parent).Command
}

// Runner contains the run function
type Runner struct {
ctx context.Context
Pull pull.Command
Command *cobra.Command
}

func (r *Runner) preRunE(_ *cobra.Command, args []string) error {
const op errors.Op = "cmdpull.preRunE"
if len(args) == 1 {
args = append(args, pkg.CurDir)
} else {
_, err := os.Lstat(args[1])
if err == nil || os.IsExist(err) {
resolvedPath, err := argutil.ResolveSymlink(r.ctx, args[1])
if err != nil {
return errors.E(op, err)
}
args[1] = resolvedPath
}
}
destination, err := parse.ParseArgs(r.ctx, args, parse.Options{
SetGit: func(git *kptfilev1.Git) error {
r.Pull.Origin = remote.NewGitOrigin(git)
return nil
},
SetOci: func(oci *kptfilev1.Oci) error {
r.Pull.Origin = remote.NewOciOrigin(oci)
return nil
},
})
if err != nil {
return err
}

p, err := pkg.New(destination)
if err != nil {
return errors.E(op, types.UniquePath(destination), err)
}
r.Pull.Destination = string(p.UniquePath)

return nil
}

func (r *Runner) runE(c *cobra.Command, _ []string) error {
const op errors.Op = "cmdpull.runE"
if err := r.Pull.Run(r.ctx); err != nil {
return errors.E(op, types.UniquePath(r.Pull.Destination), err)
}

return nil
}
161 changes: 161 additions & 0 deletions internal/cmdpush/cmdpush.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
// Copyright 2021 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 cmdpush

import (
"context"
"fmt"
"os"
"path/filepath"
"strings"

docs "github.com/GoogleContainerTools/kpt/internal/docs/generated/pkgdocs"
"github.com/GoogleContainerTools/kpt/internal/errors"
"github.com/GoogleContainerTools/kpt/internal/pkg"
"github.com/GoogleContainerTools/kpt/internal/types"
"github.com/GoogleContainerTools/kpt/internal/util/argutil"
"github.com/GoogleContainerTools/kpt/internal/util/cmdutil"
"github.com/GoogleContainerTools/kpt/internal/util/parse"
"github.com/GoogleContainerTools/kpt/internal/util/push"
"github.com/GoogleContainerTools/kpt/internal/util/remote"
kptfilev1 "github.com/GoogleContainerTools/kpt/pkg/api/kptfile/v1"
"github.com/spf13/cobra"
)

// NewRunner returns a command runner
func NewRunner(ctx context.Context, parent string) *Runner {
r := &Runner{
ctx: ctx,
}
c := &cobra.Command{
Use: "push [DIR@VERSION] [flags]",
Args: cobra.MaximumNArgs(1),
Short: docs.PushShort,
Long: docs.PushShort + "\n" + docs.PushLong,
Example: docs.PushExamples,
RunE: r.runE,
PreRunE: r.preRunE,
}

c.Flags().StringVar(&r.Origin, "origin", "",
"assigns or changes the location where the package should be pushed. Default is to push it to "+
"the origin from which the package was pulled.")
c.Flags().BoolVar(&r.Increment, "increment", false,
"increment the version of the package when pushed. This will increment the DIR@VERSION if provided, "+
"otherwise it will increment the origin's version when pulled. The version must be semver or integer, and "+
"may have an optional leading 'v'")
cmdutil.FixDocs("kpt", parent, c)
r.Command = c
return r
}

func NewCommand(ctx context.Context, parent string) *cobra.Command {
return NewRunner(ctx, parent).Command
}

// Runner contains the run function
type Runner struct {
ctx context.Context
Push push.Command
Command *cobra.Command
Origin string
Increment bool
}

func (r *Runner) preRunE(_ *cobra.Command, args []string) error {
const op errors.Op = "cmdpush.preRunE"

var path string
var ref string

if len(args) >= 1 {
parts := strings.Split(args[0], "@")
if len(parts) > 2 {
return errors.E(op, errors.InvalidParam, fmt.Errorf("at most 1 version permitted"))
}

path = parts[0]
if len(parts) == 2 {
ref = parts[1]
}
}

if path == "" {
// default to current directory
path = "."
}

resolvedPath, err := argutil.ResolveSymlink(r.ctx, path)
if err != nil {
return err
}

if ref != "" {
r.Push.Ref = ref
}

r.Push.Increment = r.Increment

r.Push.Pkg, err = pkg.New(resolvedPath)
if err != nil {
return errors.E(op, err)
}
relPath, err := resolveRelPath(r.Push.Pkg.UniquePath)
if err != nil {
return errors.E(op, r.Push.Pkg.UniquePath, err)
}
if strings.HasPrefix(relPath, pkg.ParentDir) {
return errors.E(op, r.Push.Pkg.UniquePath, fmt.Errorf("package path must be under current working directory"))
}

if r.Origin != "" {
_, err := parse.ParseArgs(r.ctx, []string{r.Origin, path}, parse.Options{
SetOci: func(oci *kptfilev1.Oci) error {
r.Push.Origin = remote.NewOciOrigin(oci)
return nil
},
})
if err != nil {
return err
}
}

return nil
}

func (r *Runner) runE(c *cobra.Command, _ []string) error {
const op errors.Op = "cmdpush.runE"
if err := r.Push.Run(r.ctx); err != nil {
return errors.E(op, r.Push.Pkg.UniquePath, err)
}

return nil
}

func resolveRelPath(path types.UniquePath) (string, error) {
const op errors.Op = "cmdpush.resolveRelPath"
cwd, err := os.Getwd()
if err != nil {
return "", errors.E(op, errors.IO,
fmt.Errorf("error looking up current working directory: %w", err))
}

relPath, err := filepath.Rel(cwd, path.String())
if err != nil {
return "", errors.E(op, errors.IO,
fmt.Errorf("error resolving the relative path: %w", err))
}
return relPath, nil
}
Loading

0 comments on commit 2bc4bd9

Please sign in to comment.