Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to specify the inventory id for RG with flag #1766

Merged
merged 1 commit into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})
}
}