From b60bfd0fe0cb5b23efc054fab53d0caa195ece85 Mon Sep 17 00:00:00 2001 From: Morten Torkildsen Date: Sun, 18 Apr 2021 18:45:56 -0700 Subject: [PATCH] Allow users to specify the inventory id for RG with flag (#1766) --- commands/initcmd.go | 13 +++++--- commands/initcmd_test.go | 66 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 5 deletions(-) diff --git a/commands/initcmd.go b/commands/initcmd.go index 53982d1fe8..740c4ca8dd 100644 --- a/commands/initcmd.go +++ b/commands/initcmd.go @@ -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) } @@ -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 } diff --git a/commands/initcmd_test.go b/commands/initcmd_test.go index 1ab93fb20e..13ec7b6f4a 100644 --- a/commands/initcmd_test.go +++ b/commands/initcmd_test.go @@ -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 ( @@ -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) + }) + } +}