Skip to content

Commit

Permalink
Allow users to specify the inventory id for RG with flag (#1766)
Browse files Browse the repository at this point in the history
  • Loading branch information
mortent committed Apr 19, 2021
1 parent cf588df commit b60bfd0
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
13 changes: 8 additions & 5 deletions commands/initcmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,14 @@ func (io *KptInitOptions) Run(args []string) error {
randomSuffix := common.RandomStr(time.Now().UTC().UnixNano())
io.name = fmt.Sprintf("%s-%s", defaultInventoryName, randomSuffix)
}
// Set the init options inventory id label.
id, err := generateID(io.name, io.namespace, time.Now())
if err != nil {
return err
if io.inventoryID == "" {
// Set the init options inventory id label.
id, err := generateID(io.name, io.namespace, time.Now())
if err != nil {
return err
}
io.inventoryID = id
}
io.inventoryID = id
if !io.Quiet {
fmt.Fprintf(io.ioStreams.Out, "namespace: %s is used for inventory object\n", io.namespace)
}
Expand Down Expand Up @@ -189,5 +191,6 @@ func NewCmdInit(f cmdutil.Factory, ioStreams genericclioptions.IOStreams) *cobra
cmd.Flags().StringVar(&io.name, "name", "", "Inventory object name")
cmd.Flags().BoolVar(&io.force, "force", false, "Set inventory values even if already set in Kptfile")
cmd.Flags().BoolVar(&io.Quiet, "quiet", false, "If true, do not print output during initialization of Kptfile")
cmd.Flags().StringVar(&io.inventoryID, "inventory-id", "", "Inventory id for the package")
return cmd
}
66 changes: 66 additions & 0 deletions commands/initcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (
"testing"
"time"

"github.com/GoogleContainerTools/kpt/pkg/kptfile"
"github.com/GoogleContainerTools/kpt/pkg/kptfile/kptfileutil"
"github.com/stretchr/testify/assert"
"k8s.io/cli-runtime/pkg/genericclioptions"
cmdtesting "k8s.io/kubectl/pkg/cmd/testing"
"sigs.k8s.io/kustomize/kyaml/yaml"
)

var (
Expand Down Expand Up @@ -202,3 +204,67 @@ func TestKptInitOptions_updateKptfile(t *testing.T) {
})
}
}

func TestInitCmd(t *testing.T) {
testCases := map[string]struct {
name string
namespace string
id string
expectedID string
}{
"generates an inventory id if one is not provided": {
name: "foo",
namespace: "bar",
id: "",
expectedID: "0717f9c46d01349e9d575ab1a5131886fb086a43",
},
"uses the inventory id if one is provided": {
name: "foo",
namespace: "bar",
id: "abc123",
expectedID: "abc123",
},
}

for tn, tc := range testCases {
t.Run(tn, func(t *testing.T) {
tf := cmdtesting.NewTestFactory().WithNamespace("test-ns")
defer tf.Cleanup()
ioStreams, _, _, _ := genericclioptions.NewTestIOStreams() //nolint:dogsled

dir, err := ioutil.TempDir("", "kpt-init-options-test")
assert.NoError(t, err)
kf := kptfile.KptFile{
ResourceMeta: yaml.ResourceMeta{
ObjectMeta: yaml.ObjectMeta{
NameMeta: yaml.NameMeta{
Name: filepath.Base(dir),
},
},
TypeMeta: yaml.TypeMeta{
APIVersion: kptfile.TypeMeta.APIVersion,
Kind: kptfile.TypeMeta.Kind},
},
}
err = kptfileutil.WriteFile(dir, kf)
if !assert.NoError(t, err) {
t.FailNow()
}

io := NewKptInitOptions(tf, ioStreams)
io.name = tc.name
io.namespace = tc.namespace
io.inventoryID = tc.id
err = io.Run([]string{dir})
if !assert.NoError(t, err) {
t.FailNow()
}

newKf, err := kptfileutil.ReadFile(dir)
if !assert.NoError(t, err) {
t.FailNow()
}
assert.Contains(t, newKf.Inventory.InventoryID, tc.expectedID)
})
}
}

0 comments on commit b60bfd0

Please sign in to comment.