Skip to content

Commit

Permalink
kpt pkg fix command impl
Browse files Browse the repository at this point in the history
  • Loading branch information
phanimarupaka committed Aug 3, 2020
1 parent 3d4456f commit 10ea762
Show file tree
Hide file tree
Showing 9 changed files with 469 additions and 5 deletions.
2 changes: 2 additions & 0 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/GoogleContainerTools/kpt/internal/cmddesc"
"github.com/GoogleContainerTools/kpt/internal/cmddiff"
"github.com/GoogleContainerTools/kpt/internal/cmdfix"
"github.com/GoogleContainerTools/kpt/internal/cmdget"
"github.com/GoogleContainerTools/kpt/internal/cmdinit"
"github.com/GoogleContainerTools/kpt/internal/cmdsync"
Expand All @@ -33,6 +34,7 @@ func GetAnthosCommands(name string) []*cobra.Command {
cmdget.NewCommand(name), cmdinit.NewCommand(name),
cmdsync.NewCommand(name),
cmdupdate.NewCommand(name), cmddiff.NewCommand(name),
cmdfix.NewCommand(name),
}

// apply cross-cutting issues to commands
Expand Down
23 changes: 23 additions & 0 deletions commands/configcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,16 @@
package commands

import (
"fmt"

"github.com/GoogleContainerTools/kpt/internal/docs/generated/cfgdocs"
"github.com/GoogleContainerTools/kpt/internal/util/functions"
"github.com/GoogleContainerTools/kpt/internal/util/setters"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/cmd/config/configcobra"
"sigs.k8s.io/kustomize/kyaml/fieldmeta"
"sigs.k8s.io/kustomize/kyaml/kio"
kyamlsetters "sigs.k8s.io/kustomize/kyaml/setters"
)

const ShortHandRef = "$kpt-set"
Expand Down Expand Up @@ -129,6 +133,7 @@ func SetCommand(parent string) *cobra.Command {
setCmd.Flags().BoolVar(&autoRun, "auto-run", true,
`Automatically run functions after setting (if enabled for the package)`)
setCmd.RunE = func(c *cobra.Command, args []string) error {
warnIfSetterV1(args[0])
kustomizeCmd.SetArgs(args)
if err := kustomizeCmd.Execute(); err != nil {
return err
Expand Down Expand Up @@ -156,3 +161,21 @@ func SetCommand(parent string) *cobra.Command {
}
return &setCmd
}

// warnIfSetterV1 checks if the package is using V1 kyaml setters and prints
// warning message to upgrade them using kpt pkg fix command
func warnIfSetterV1(pkgPath string) {
l := kyamlsetters.LookupSetters{}
err := kio.Pipeline{
Inputs: []kio.Reader{&kio.LocalPackageReader{PackagePath: pkgPath}},
Filters: []kio.Filter{&l},
}.Execute()
if err != nil {
// do not throw error as it is just to warn users
return
}
if len(l.SetterCounts) > 0 {
fmt.Println("Warning: This package is using older version of setters which " +
"will be deprecated soon, please use 'kpt pkg fix -h' for instructions about upgrading it")
}
}
3 changes: 2 additions & 1 deletion commands/pkgcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package commands
import (
"github.com/GoogleContainerTools/kpt/internal/cmddesc"
"github.com/GoogleContainerTools/kpt/internal/cmddiff"
"github.com/GoogleContainerTools/kpt/internal/cmdfix"
"github.com/GoogleContainerTools/kpt/internal/cmdget"
"github.com/GoogleContainerTools/kpt/internal/cmdinit"
"github.com/GoogleContainerTools/kpt/internal/cmdsync"
Expand Down Expand Up @@ -45,7 +46,7 @@ func GetPkgCommand(name string) *cobra.Command {
}
pkg.AddCommand(
cmddesc.NewCommand(name), cmdget.NewCommand(name), cmdinit.NewCommand(name),
cmdsync.NewCommand(name), cmdupdate.NewCommand(name), cmddiff.NewCommand(name),
cmdfix.NewCommand(name), cmdsync.NewCommand(name), cmdupdate.NewCommand(name), cmddiff.NewCommand(name),
)
return pkg
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ require (
// Once there is a 0.18 release, we can import a semver release.
k8s.io/kubectl v0.0.0-20191219154910-1528d4eea6dd
sigs.k8s.io/cli-utils v0.17.0
sigs.k8s.io/kustomize/cmd/config v0.4.2
sigs.k8s.io/kustomize/kyaml v0.4.2
sigs.k8s.io/kustomize/cmd/config v0.4.3-0.20200730200907-17f935452fb9
sigs.k8s.io/kustomize/kyaml v0.4.3-0.20200730200907-17f935452fb9
)
7 changes: 5 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -644,24 +644,27 @@ modernc.org/golex v1.0.0/go.mod h1:b/QX9oBD/LhixY6NDh+IdGv17hgB+51fET1i2kPSmvk=
modernc.org/mathutil v1.0.0/go.mod h1:wU0vUrJsVWBZ4P6e7xtFJEhFSNsfRLJ8H458uRjg03k=
modernc.org/strutil v1.0.0/go.mod h1:lstksw84oURvj9y3tn8lGvRxyRC1S2+g5uuIzNfIOBs=
modernc.org/xc v1.0.0/go.mod h1:mRNCo0bvLjGhHO9WsyuKVU4q0ceiDDDoEeWDJHrNx8I=
sigs.k8s.io/cli-utils v0.16.0/go.mod h1:9Jqm9K2W6ShhCxsEuaz6HSRKKOXigPUx3ZfypGgxBLY=
sigs.k8s.io/cli-utils v0.17.0 h1:iQ0nhgU8DZiRphHTErI1IHcHYp2fZaULrrFN4NF3dCc=
sigs.k8s.io/cli-utils v0.17.0/go.mod h1:9Jqm9K2W6ShhCxsEuaz6HSRKKOXigPUx3ZfypGgxBLY=
sigs.k8s.io/controller-runtime v0.4.0 h1:wATM6/m+3w8lj8FXNaO6Fs/rq/vqoOjO1Q116Z9NPsg=
sigs.k8s.io/controller-runtime v0.4.0/go.mod h1:ApC79lpY3PHW9xj/w9pj+lYkLgwAAUZwfXkME1Lajns=
sigs.k8s.io/kustomize v1.0.11 h1:Yb+6DDt9+aR2AvQApvUaKS/ugteeG4MPyoFeUHiPOjk=
sigs.k8s.io/kustomize v2.0.3+incompatible h1:JUufWFNlI44MdtnjUqVnvh29rR37PQFzPbLXqhyOyX0=
sigs.k8s.io/kustomize v2.0.3+incompatible/go.mod h1:MkjgH3RdOWrievjo6c9T245dYlB5QeXV4WCbnt/PEpU=
sigs.k8s.io/kustomize/cmd/config v0.4.2 h1:t69piCQvGIbGTOVY+826vsCScQkh+jFOXtDMwx/AIWE=
sigs.k8s.io/kustomize/cmd/config v0.4.2/go.mod h1:w9l5mB/TKCMvBa8Syek1MP5dDrM2lBL05IyaEPN+PMc=
sigs.k8s.io/kustomize/kyaml v0.4.0 h1:jMQrJOJmiUz5Y018ki0mXWpEreEXjvad1NRfXTdi9vU=
sigs.k8s.io/kustomize/kyaml v0.4.0/go.mod h1:XJL84E6sOFeNrQ7CADiemc1B0EjIxHo3OhW4o1aJYNw=
sigs.k8s.io/kustomize/kyaml v0.4.1/go.mod h1:XJL84E6sOFeNrQ7CADiemc1B0EjIxHo3OhW4o1aJYNw=
sigs.k8s.io/kustomize/kyaml v0.4.2 h1:9/Tb90gnThv4vgUldZOLnrT+9Esdh7+Og2UIq024Ykg=
sigs.k8s.io/kustomize/kyaml v0.4.2/go.mod h1:XJL84E6sOFeNrQ7CADiemc1B0EjIxHo3OhW4o1aJYNw=
sigs.k8s.io/kustomize/kyaml v0.4.3-0.20200730200907-17f935452fb9 h1:2s0A6Cg5nNItPZgBNCtRz21DSkk4sA60EaFC7aDAgB8=
sigs.k8s.io/kustomize/kyaml v0.4.3-0.20200730200907-17f935452fb9/go.mod h1:bEzbO5pN9OvlEeCLvFHo8Pu7SA26Herc2m60UeWZBdI=
sigs.k8s.io/structured-merge-diff v0.0.0-20190525122527-15d366b2352e/go.mod h1:wWxsB5ozmmv/SG7nM11ayaAW51xMvak/t1r0CSlcokI=
sigs.k8s.io/structured-merge-diff v0.0.0-20190817042607-6149e4549fca/go.mod h1:IIgPezJWb76P0hotTxzDbWsMYB8APh18qZnxkomBpxA=
sigs.k8s.io/structured-merge-diff v1.0.1-0.20191108220359-b1b620dd3f06/go.mod h1:/ULNhyfzRopfcjskuui0cTITekDduZ7ycKN3oUT9R18=
sigs.k8s.io/testing_frameworks v0.1.2 h1:vK0+tvjF0BZ/RYFeZ1E6BYBwHJJXhjuZ3TdsEKH+UQM=
sigs.k8s.io/testing_frameworks v0.1.2/go.mod h1:ToQrwSC3s8Xf/lADdZp3Mktcql9CG0UAmdJG9th5i0w=
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
Expand Down
78 changes: 78 additions & 0 deletions internal/cmdfix/cmdfix.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright 2019 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 cmdfix contains the fix command
package cmdfix

import (
"strings"

docs "github.com/GoogleContainerTools/kpt/internal/docs/generated/pkgdocs"
"github.com/GoogleContainerTools/kpt/internal/gitutil"
"github.com/GoogleContainerTools/kpt/internal/util/cmdutil"
"github.com/GoogleContainerTools/kpt/internal/util/fix"
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/kyaml/errors"
)

// NewRunner returns a command runner
func NewRunner(parent string) *Runner {
r := &Runner{}
c := &cobra.Command{
Use: "fix LOCAL_PKG_DIR",
Args: cobra.ExactArgs(1),
Short: docs.FixShort,
Long: docs.FixShort + "\n" + docs.FixLong,
Example: docs.FixExamples,
PreRunE: r.preRunE,
RunE: r.runE,
SuggestFor: []string{"upgrade", "migrate"},
}
cmdutil.FixDocs("kpt", parent, c)
c.Flags().BoolVar(&r.Fix.DryRun, "dry-run", false,
`Dry run emits the actions`)
r.Command = c
return r
}

func NewCommand(parent string) *cobra.Command {
return NewRunner(parent).Command
}

// Runner contains the run function
type Runner struct {
Command *cobra.Command
Fix fix.Command
}

func (r *Runner) preRunE(c *cobra.Command, args []string) error {
r.Fix.PkgPath = args[0]
r.Fix.StdOut = c.OutOrStdout()

// require package is checked into git before trying to fix it
g := gitutil.NewLocalGitRunner(args[0])
if err := g.Run("status", "-s"); err != nil {
return errors.Errorf(
"kpt packages must be tracked by git before making fix to revert unwanted changes: %v", err)
}
if strings.TrimSpace(g.Stdout.String()) != "" {
return errors.Errorf("must commit package changes to git %s before attempting to fix it",
args[0])
}
return nil
}

func (r *Runner) runE(c *cobra.Command, args []string) error {
return r.Fix.Run()
}
Loading

0 comments on commit 10ea762

Please sign in to comment.