Skip to content

Commit

Permalink
Publish top kustomize cobra-commands.
Browse files Browse the repository at this point in the history
The PR seeks to expose some of the top level kustomize
commands (especially `build`) for reuse in other command
line tools (expecially `kubectl`, see kubernetes-sigs#1500).

Options

1. Expose the commands in the `api` module.

```
REPO/api/go.mod
REPO/api/builtins
REPO/api/kommands <- new
REPO/api/...
```

Disadvantage: This would make `api` module depend on cobra.  That's
bad for people who want to depend on the api, but want to write their
own commands at their own version of cobra.  The commands module and
the api module should remain distinct.

2. Expose the commands in their own `commands` module.

They'd appear alongside `api`, e.g. `

```
REPO/api/go.mod
REPO/api/builtins
REPO/api/...
REPO/commands/go.mod
REPO/commands/build
REPO/commands/edit
REPO/commands/...
```

Advantage: The commands would be consumed by the kustomize binary and
the kubectl binary in the same way.

Disadvantage: The kustomize binary module and the commands module
would evolve separately with their own version numbers, creating
confusion.

3. Expose the commands in the existing `kustomize` module

```
REPO/api/go.mod
REPO/api/builtins
REPO/api/...
REPO/kustomize/go.mod
REPO/kustomize/main.go
REPO/kustomize/commands/build
REPO/kustomize/commands/edit
REPO/kustomize/commands/...
```

Outside users, e.g. kubectl, could then

```
import sigs.k8s.io/kustomize/kustomize/v3/commands/build
```

and hopefully still get the `main` package
as they do now via:

```
go get sigs.k8s.io/kustomize/kustomize/v3
```

Advantage: 1) The commands and the kustomize binary sit at the same
version, so it's easy to see which version of kustomize is being used
by some other CLI. 2) The path to the binary doesn't change.

Disadvantage: It appears to be non-standard to place a main package at
the top level of a module that also ships subpackages.  Usually `main`
packages live as leaves under a directory called `cmd`.  This might
cause some problems.  If so, we can go with option 4.

4. Same as 3, but move `main.go` down one step.

```
REPO/api/go.mod
REPO/api/builtins
REPO/api/...
REPO/kustomize/go.mod
REPO/kustomize/cmd/main.go
REPO/kustomize/commands/build
REPO/kustomize/commands/edit
REPO/kustomize/commands/...
```
  • Loading branch information
monopole committed Feb 4, 2021
1 parent 1ee16d9 commit 8386cfe
Show file tree
Hide file tree
Showing 78 changed files with 1,385 additions and 91 deletions.
14 changes: 7 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
MYGOBIN := $(shell go env GOPATH)/bin
SHELL := /usr/bin/env bash
export PATH := $(MYGOBIN):$(PATH)
MODULES := '"cmd/config" "api/" "kustomize/" "kyaml/"'
MODULES := '"cmd/config" "api" "kommands" "cmd/kustomize" "kyaml"'

# Provide defaults for REPO_OWNER and REPO_NAME if not present.
# Typically these values would be provided by Prow.
Expand Down Expand Up @@ -91,7 +91,7 @@ $(MYGOBIN)/prchecker:

# Build from local source.
$(MYGOBIN)/kustomize:
cd kustomize; \
cd cmd/kustomize; \
go install .

.PHONY: install-tools
Expand Down Expand Up @@ -232,14 +232,14 @@ test-unit-kustomize-api: build-kustomize-api
test-unit-kustomize-plugins:
./hack/testUnitKustomizePlugins.sh

.PHONY: test-unit-kustomize-cli
test-unit-kustomize-cli:
cd kustomize; go test ./...
.PHONY: test-unit-kommands
test-unit-kommands:
cd kommands; go test ./...

.PHONY: test-unit-kustomize-all
test-unit-kustomize-all: \
test-unit-kustomize-api \
test-unit-kustomize-cli \
test-unit-kommands \
test-unit-kustomize-plugins

test-unit-cmd-all:
Expand Down Expand Up @@ -267,7 +267,7 @@ test-examples-e2e-kustomize: $(MYGOBIN)/mdrip $(MYGOBIN)/kind
set -e; \
/bin/rm -f $(MYGOBIN)/kustomize; \
echo "Installing kustomize from ."; \
cd kustomize; go install .; cd ..; \
cd cmd/kustomize; go install .; cd ..; \
./hack/testExamplesE2EAgainstKustomize.sh .; \
)

Expand Down
21 changes: 21 additions & 0 deletions cmd/kustomize/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module sigs.k8s.io/kustomize/cmd/kustomize/v3

go 1.15

require (
k8s.io/client-go v0.18.10
sigs.k8s.io/kustomize/kommands v0.0.0
)

exclude (
sigs.k8s.io/kustomize/api v0.2.0
sigs.k8s.io/kustomize/cmd/config v0.2.0
)

replace sigs.k8s.io/kustomize/api => ./../../api

replace sigs.k8s.io/kustomize/kommands => ./../../kommands

replace sigs.k8s.io/kustomize/cmd/config => ./../config

replace sigs.k8s.io/kustomize/kyaml => ./../../kyaml
633 changes: 633 additions & 0 deletions cmd/kustomize/go.sum

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions cmd/kustomize/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2021 The Kubernetes Authors.
// SPDX-License-Identifier: Apache-2.0

// The kustomize CLI.
package main

import (
"os"

"sigs.k8s.io/kustomize/kommands"

// initialize auth
// This is here rather than in the libraries because of
// https://github.com/kubernetes-sigs/kustomize/issues/2060
_ "k8s.io/client-go/plugin/pkg/client/auth"
)

func main() {
if err := kommands.NewDefaultCommand().Execute(); err != nil {
os.Exit(1)
}
os.Exit(0)
}
3 changes: 2 additions & 1 deletion hack/imports.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
for f in $(find $1 -name '*.go'); do
echo $f
# go run go.coder.com/go-tools/cmd/goimports
~/gopath/bin/goimports -w $f
# go get golang.org/x/tools/cmd/goimports
goimports -w $f
done
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ https://github.com/hashicorp/go-getter#url-format
`

// NewCmdBuild creates a new build command.
func NewCmdBuild(out io.Writer) *cobra.Command {
func NewCmdBuild(out io.Writer, cmdName string) *cobra.Command {
var o Options
cmd := &cobra.Command{
Use: "build {path}",
Use: cmdName + " {path}",
Short: "Print configuration per contents of " +
konfig.DefaultKustomizationFileName(),
Example: examples,
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"

"github.com/spf13/pflag"

"sigs.k8s.io/kustomize/api/konfig"
)

Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"sigs.k8s.io/kustomize/api/ifc"
"sigs.k8s.io/kustomize/api/konfig"
"sigs.k8s.io/kustomize/api/loader"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/util"
"sigs.k8s.io/kustomize/kommands/internal/kustfile"
"sigs.k8s.io/kustomize/kommands/internal/util"
)

type createFlags struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/provider"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
"sigs.k8s.io/kustomize/kommands/internal/kustfile"
)

var factory = provider.NewDefaultDepProvider().GetKunstructuredFactory()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
"sigs.k8s.io/kustomize/kommands/internal/kustfile"
)

type addBaseOptions struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"testing"

"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/internal/commands/testutils"
"sigs.k8s.io/kustomize/kommands/internal/kustfile"
testutils_test "sigs.k8s.io/kustomize/kommands/internal/testutils"
)

const (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/loader"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/util"
"sigs.k8s.io/kustomize/kommands/internal/kustfile"
"sigs.k8s.io/kustomize/kommands/internal/util"
)

type addComponentOptions struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"

"sigs.k8s.io/kustomize/api/filesys"
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/internal/commands/testutils"
testutils_test "sigs.k8s.io/kustomize/kommands/internal/testutils"
)

const (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/konfig"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/util"
"sigs.k8s.io/kustomize/kommands/internal/kustfile"
"sigs.k8s.io/kustomize/kommands/internal/util"
)

// kindOfAdd is the kind of metadata being added: label or annotation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"sigs.k8s.io/kustomize/api/filesys"
valtest_test "sigs.k8s.io/kustomize/api/testutils/valtest"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/internal/commands/testutils"
"sigs.k8s.io/kustomize/kommands/internal/kustfile"
testutils_test "sigs.k8s.io/kustomize/kommands/internal/testutils"
)

func makeKustomization(t *testing.T) *types.Kustomization {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
"sigs.k8s.io/kustomize/kommands/internal/kustfile"
)

type addPatchOptions struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"

"sigs.k8s.io/kustomize/api/filesys"
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/internal/commands/testutils"
testutils_test "sigs.k8s.io/kustomize/kommands/internal/testutils"
)

const (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/loader"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/util"
"sigs.k8s.io/kustomize/kommands/internal/kustfile"
"sigs.k8s.io/kustomize/kommands/internal/util"
)

type addResourceOptions struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"testing"

"sigs.k8s.io/kustomize/api/filesys"
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/internal/commands/testutils"
testutils_test "sigs.k8s.io/kustomize/kommands/internal/testutils"
)

const (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/util"
"sigs.k8s.io/kustomize/kommands/internal/kustfile"
"sigs.k8s.io/kustomize/kommands/internal/util"
)

type addTransformerOptions struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"testing"

"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/internal/commands/testutils"
"sigs.k8s.io/kustomize/kommands/internal/kustfile"
testutils_test "sigs.k8s.io/kustomize/kommands/internal/testutils"
)

const (
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/ifc"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
"sigs.k8s.io/kustomize/kommands/internal/kustfile"
)

// newCmdAddConfigMap returns a new command.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"sigs.k8s.io/kustomize/api/types"

"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/util"
"sigs.k8s.io/kustomize/kommands/internal/util"
)

// flagsAndArgs encapsulates the options for add secret/configmap commands.
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/ifc"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
"sigs.k8s.io/kustomize/kommands/internal/kustfile"
)

// newCmdAddSecret returns a new command.
Expand Down
File renamed without changes.
10 changes: 5 additions & 5 deletions kustomize/internal/commands/edit/all.go → kommands/edit/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
"sigs.k8s.io/kustomize/api/ifc"
"sigs.k8s.io/kustomize/api/kv"
"sigs.k8s.io/kustomize/api/loader"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/edit/add"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/edit/fix"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/edit/listbuiltin"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/edit/remove"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/edit/set"
"sigs.k8s.io/kustomize/kommands/edit/add"
"sigs.k8s.io/kustomize/kommands/edit/fix"
"sigs.k8s.io/kustomize/kommands/edit/listbuiltin"
"sigs.k8s.io/kustomize/kommands/edit/remove"
"sigs.k8s.io/kustomize/kommands/edit/set"
)

// NewCmdEdit returns an instance of 'edit' subcommand.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package fix
import (
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
"sigs.k8s.io/kustomize/kommands/internal/kustfile"
)

// NewCmdFix returns an instance of 'fix' subcommand.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

"github.com/google/go-cmp/cmp"
"sigs.k8s.io/kustomize/api/filesys"
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/internal/commands/testutils"
testutils_test "sigs.k8s.io/kustomize/kommands/internal/testutils"
)

func TestFix(t *testing.T) {
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/konfig"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
"sigs.k8s.io/kustomize/kommands/internal/kustfile"
)

// kindOfAdd is the kind of metadata being added: label or annotation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"sigs.k8s.io/kustomize/api/filesys"
valtest_test "sigs.k8s.io/kustomize/api/testutils/valtest"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/internal/commands/testutils"
"sigs.k8s.io/kustomize/kommands/internal/kustfile"
testutils_test "sigs.k8s.io/kustomize/kommands/internal/testutils"
)

func makeKustomizationFS() filesys.FileSystem {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/konfig"
"sigs.k8s.io/kustomize/api/types"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
"sigs.k8s.io/kustomize/kommands/internal/kustfile"
)

type removePatchOptions struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"testing"

"sigs.k8s.io/kustomize/api/filesys"
testutils_test "sigs.k8s.io/kustomize/kustomize/v3/internal/commands/testutils"
testutils_test "sigs.k8s.io/kustomize/kommands/internal/testutils"
)

const (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/konfig"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
"sigs.k8s.io/kustomize/kommands/internal/kustfile"
)

type removeResourceOptions struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"errors"
"testing"

"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/edit/remove_test"
"sigs.k8s.io/kustomize/kommands/edit/remove_test"
)

func TestRemoveResources(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/spf13/cobra"
"sigs.k8s.io/kustomize/api/filesys"
"sigs.k8s.io/kustomize/api/konfig"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/kustfile"
"sigs.k8s.io/kustomize/kommands/internal/kustfile"
)

type removeTransformerOptions struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"testing"

"github.com/pkg/errors"
"sigs.k8s.io/kustomize/kustomize/v3/internal/commands/edit/remove_test"
"sigs.k8s.io/kustomize/kommands/edit/remove_test"
)

func TestRemoveTransformer(t *testing.T) {
Expand Down
Loading

0 comments on commit 8386cfe

Please sign in to comment.