Skip to content

Commit

Permalink
Add plugin group versioning and description (vmware-tanzu#270)
Browse files Browse the repository at this point in the history
Signed-off-by: Marc Khouzam <kmarc@vmware.com>
  • Loading branch information
marckhouzam authored and vuil committed May 16, 2023
1 parent 35c914a commit c7df086
Show file tree
Hide file tree
Showing 39 changed files with 1,140 additions and 502 deletions.
11 changes: 7 additions & 4 deletions cmd/plugin/builder/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ Below are the flags available with the `tanzu builder inventory plugin-group add

```txt
--deactivate mark plugin-group as deactivated
--description string a description for the plugin-group
-h, --help help for add
--manifest string manifest file specifying plugin-group details that needs to be processed
--name string name of the plugin-group
Expand All @@ -309,13 +310,14 @@ Below are the flags available with the `tanzu builder inventory plugin-group add
--publisher string name of the publisher
--repository string repository to publish plugin inventory image
--vendor string name of the vendor
--version string version of the plugin-group
```

Below are some examples:

```shell
# Add plugin-group entries to the inventory database based on the specified plugin-group manifest file
tanzu builder inventory plugin-group add --name v1.0.0 --repository project-stg.registry.vmware.com/test/v1/tanzu-cli/plugins --vendor vmware --publisher tkg --manifest ./artifacts/plugins/plugin_group_manifest.yaml
tanzu builder inventory plugin-group add --name default --version v1.0.0 --repository project-stg.registry.vmware.com/test/v1/tanzu-cli/plugins --vendor vmware --publisher tkg --manifest ./artifacts/plugins/plugin_group_manifest.yaml --description "Plugins required by Tanzu Kubernetes Grid"
```

Here the `--manifest` flag is used to provide metadata about the plugin-group including which plugins to associate with the plugin-group.
Expand Down Expand Up @@ -343,19 +345,20 @@ Below are the flags available with `tanzu builder inventory plugin-group activat

```txt
-h, --help help for activate
--name string name of the plugin group
--name string name of the plugin-group
--plugin-inventory-image-tag string tag to which plugin inventory image needs to be published (default "latest")
--publisher string name of the publisher
--repository string repository to publish plugin inventory image
--vendor string name of the vendor
--version string version of the plugin-group
```

Below are some examples:

```shell
# Activate plugin-group in the inventory database
tanzu builder inventory plugin-group activate --name v1.0.0 --repository localhost:5002/test/v1/tanzu-cli/plugins --vendor vmware --publisher tkg1
tanzu builder inventory plugin-group activate --name default --version v1.0.0 --repository localhost:5002/test/v1/tanzu-cli/plugins --vendor vmware --publisher tkg1

# Dectivate plugin-group in the inventory database
tanzu builder inventory plugin-group deactivate --name v1.0.0 --repository localhost:5002/test/v1/tanzu-cli/plugins --vendor vmware --publisher tkg1
tanzu builder inventory plugin-group deactivate --name default --version v1.0.0 --repository localhost:5002/test/v1/tanzu-cli/plugins --vendor vmware --publisher tkg1
```
18 changes: 12 additions & 6 deletions cmd/plugin/builder/inventory/inventory_plugin_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ type InventoryPluginGroupUpdateOptions struct {
Publisher string
Vendor string
GroupName string
GroupVersion string
Description string
DeactivatePluginGroup bool
Override bool

Expand Down Expand Up @@ -75,18 +77,20 @@ func (ipuo *InventoryPluginGroupUpdateOptions) PluginGroupAdd() error {

func (ipuo *InventoryPluginGroupUpdateOptions) getPluginGroupFromManifest() (*plugininventory.PluginGroup, error) {
pg := plugininventory.PluginGroup{
Vendor: ipuo.Vendor,
Publisher: ipuo.Publisher,
Name: ipuo.GroupName,
Hidden: ipuo.DeactivatePluginGroup,
Plugins: make([]*plugininventory.PluginGroupPluginEntry, 0),
Vendor: ipuo.Vendor,
Publisher: ipuo.Publisher,
Name: ipuo.GroupName,
Description: ipuo.Description,
Hidden: ipuo.DeactivatePluginGroup,
Versions: make(map[string][]*plugininventory.PluginGroupPluginEntry, 0),
}

pluginGroupManifest, err := helpers.ReadPluginGroupManifest(ipuo.PluginGroupManifestFile)
if err != nil {
return nil, err
}

var plugins []*plugininventory.PluginGroupPluginEntry
for _, plugin := range pluginGroupManifest.Plugins {
pge := plugininventory.PluginGroupPluginEntry{
PluginIdentifier: plugininventory.PluginIdentifier{
Expand All @@ -96,8 +100,9 @@ func (ipuo *InventoryPluginGroupUpdateOptions) getPluginGroupFromManifest() (*pl
},
Mandatory: !plugin.IsContextScoped,
}
pg.Plugins = append(pg.Plugins, &pge)
plugins = append(plugins, &pge)
}
pg.Versions[ipuo.GroupVersion] = plugins

return &pg, nil
}
Expand Down Expand Up @@ -126,6 +131,7 @@ func (ipuo *InventoryPluginGroupUpdateOptions) UpdatePluginGroupActivationState(
Publisher: ipuo.Publisher,
Name: ipuo.GroupName,
Hidden: ipuo.DeactivatePluginGroup,
Versions: map[string][]*plugininventory.PluginGroupPluginEntry{ipuo.GroupVersion: {}},
}

// Insert PluginGroup to the database
Expand Down
79 changes: 58 additions & 21 deletions cmd/plugin/builder/inventory/inventory_plugin_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,27 @@ var _ = Describe("Unit tests for inventory plugin-group add", func() {
}
db := plugininventory.NewSQLiteInventory(referencedDBFile, "")
pgEntry := plugininventory.PluginGroup{
Vendor: "fakevendor",
Publisher: "fakepublisher",
Name: "v1.0.0",
Hidden: false,
Plugins: []*plugininventory.PluginGroupPluginEntry{
{
PluginIdentifier: plugininventory.PluginIdentifier{Name: "foo", Target: "global", Version: "v0.0.2"},
Mandatory: false,
Vendor: "fakevendor",
Publisher: "fakepublisher",
Name: "default",
Description: "Desc for plugin",
Hidden: false,
Versions: map[string][]*plugininventory.PluginGroupPluginEntry{
"v1.0.0": {
{
PluginIdentifier: plugininventory.PluginIdentifier{Name: "foo", Target: "global", Version: "v0.0.2"},
Mandatory: false,
},
},
"v2.0.0": {
{
PluginIdentifier: plugininventory.PluginIdentifier{Name: "foo", Target: "global", Version: "v0.0.2"},
Mandatory: false,
},
{
PluginIdentifier: plugininventory.PluginIdentifier{Name: "bar", Target: "mission-control", Version: "v0.0.3"},
Mandatory: false,
},
},
},
}
Expand All @@ -131,7 +144,9 @@ var _ = Describe("Unit tests for inventory plugin-group add", func() {
Vendor: "fakevendor",
Publisher: "fakepublisher",
PluginGroupManifestFile: pluginGroupManifestFile,
GroupName: "v1.0.0",
GroupName: "default",
GroupVersion: "v1.0.0",
Description: "Desc for plugin",
DeactivatePluginGroup: false,
Override: false,
}
Expand Down Expand Up @@ -186,11 +201,15 @@ var _ = Describe("Unit tests for inventory plugin-group add", func() {
Expect(err).NotTo(HaveOccurred())
Expect(pgEntries).NotTo(BeNil())
Expect(len(pgEntries)).To(Equal(1))
Expect(pgEntries[0].Name).To(Equal("v1.0.0"))
Expect(pgEntries[0].Name).To(Equal("default"))
Expect(pgEntries[0].Publisher).To(Equal("fakepublisher"))
Expect(pgEntries[0].Vendor).To(Equal("fakevendor"))
Expect(pgEntries[0].Description).To(Equal("Desc for plugin"))
Expect(pgEntries[0].Hidden).To(Equal(ipgu.DeactivatePluginGroup))
Expect(len(pgEntries[0].Plugins)).To(Equal(2))
Expect(len(pgEntries[0].Versions)).To(Equal(1))

plugins := pgEntries[0].Versions["v1.0.0"]
Expect(len(plugins)).To(Equal(2))
})

var _ = It("when specified plugin-group already exist in the inventory database and override is not provided, adding plugin group should throw error", func() {
Expand Down Expand Up @@ -221,11 +240,17 @@ var _ = Describe("Unit tests for inventory plugin-group add", func() {
Expect(err).NotTo(HaveOccurred())
Expect(pgEntries).NotTo(BeNil())
Expect(len(pgEntries)).To(Equal(1))
Expect(pgEntries[0].Name).To(Equal("v1.0.0"))
Expect(pgEntries[0].Name).To(Equal("default"))
Expect(pgEntries[0].Publisher).To(Equal("fakepublisher"))
Expect(pgEntries[0].Vendor).To(Equal("fakevendor"))
Expect(pgEntries[0].Description).To(Equal("Desc for plugin"))
Expect(pgEntries[0].Hidden).To(Equal(ipgu.DeactivatePluginGroup))
Expect(len(pgEntries[0].Plugins)).To(Equal(2))
Expect(len(pgEntries[0].Versions)).To(Equal(2))

plugins := pgEntries[0].Versions["v1.0.0"]
Expect(len(plugins)).To(Equal(2))
plugins = pgEntries[0].Versions["v2.0.0"]
Expect(len(plugins)).To(Equal(2))
})

var _ = It("when inventory database cannot be published from the repository", func() {
Expand All @@ -249,7 +274,9 @@ var _ = Describe("Unit tests for inventory plugin-group add", func() {
ImgpkgOptions: fakeImgpkgWrapper,
Vendor: "fakevendor",
Publisher: "fakepublisher",
GroupName: "v1.0.0",
GroupName: "default",
GroupVersion: "v1.0.0",
Description: "Desc for plugin",
DeactivatePluginGroup: false,
}
})
Expand All @@ -273,7 +300,7 @@ var _ = Describe("Unit tests for inventory plugin-group add", func() {
err := ipgu.UpdatePluginGroupActivationState()
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("error while updating activation state of plugin group"))
Expect(err.Error()).To(ContainSubstring("unable to update plugin-group 'fakevendor-fakepublisher/v1.0.0'. This might be possible because the provided plugin-group doesn't exists"))
Expect(err.Error()).To(ContainSubstring("unable to update plugin-group 'fakevendor-fakepublisher/default:v1.0.0'. This might be possible because the provided plugin-group version doesn't exists"))
})

var _ = It("when specified plugin-group exists in the inventory database, updating the activation state with 'DeactivatePluginGroup=true' should be successful", func() {
Expand All @@ -287,15 +314,19 @@ var _ = Describe("Unit tests for inventory plugin-group add", func() {

// verify that the local db file was updated correctly before publishing the database to remote repository
db := plugininventory.NewSQLiteInventory(referencedDBFile, "")
pgEntries, err := db.GetPluginGroups(plugininventory.PluginGroupFilter{IncludeHidden: true})
pgEntries, err := db.GetPluginGroups(plugininventory.PluginGroupFilter{IncludeHidden: false})
Expect(err).NotTo(HaveOccurred())
Expect(pgEntries).NotTo(BeNil())
Expect(len(pgEntries)).To(Equal(1))
Expect(pgEntries[0].Name).To(Equal("v1.0.0"))
Expect(pgEntries[0].Name).To(Equal("default"))
Expect(pgEntries[0].Publisher).To(Equal("fakepublisher"))
Expect(pgEntries[0].Vendor).To(Equal("fakevendor"))
Expect(pgEntries[0].Hidden).To(Equal(ipgu.DeactivatePluginGroup))
Expect(len(pgEntries[0].Plugins)).To(Equal(1))
Expect(pgEntries[0].Description).To(Equal("Desc for plugin"))
Expect(pgEntries[0].Hidden).To(Equal(false))
Expect(len(pgEntries[0].Versions)).To(Equal(1))

plugins := pgEntries[0].Versions["v2.0.0"]
Expect(len(plugins)).To(Equal(2))
})

var _ = It("when specified plugin-group exists in the inventory database, updating the activation state with 'DeactivatePluginGroup=false' should be successful", func() {
Expand All @@ -313,11 +344,17 @@ var _ = Describe("Unit tests for inventory plugin-group add", func() {
Expect(err).NotTo(HaveOccurred())
Expect(pgEntries).NotTo(BeNil())
Expect(len(pgEntries)).To(Equal(1))
Expect(pgEntries[0].Name).To(Equal("v1.0.0"))
Expect(pgEntries[0].Name).To(Equal("default"))
Expect(pgEntries[0].Publisher).To(Equal("fakepublisher"))
Expect(pgEntries[0].Vendor).To(Equal("fakevendor"))
Expect(pgEntries[0].Description).To(Equal("Desc for plugin"))
Expect(pgEntries[0].Hidden).To(Equal(ipgu.DeactivatePluginGroup))
Expect(len(pgEntries[0].Plugins)).To(Equal(1))
Expect(len(pgEntries[0].Versions)).To(Equal(2))

plugins := pgEntries[0].Versions["v1.0.0"]
Expect(len(plugins)).To(Equal(1))
plugins = pgEntries[0].Versions["v2.0.0"]
Expect(len(plugins)).To(Equal(2))
})

var _ = It("when inventory database cannot be published from the repository", func() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/plugin/builder/inventory_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func newInventoryPluginDeactivateCmd() *cobra.Command {
return pluginDeactivateCmd
}

func getActivateDeactivateBaseCmd() (*cobra.Command, *inventoryPluginActivateDeactivateFlags) { // nolint:dupl
func getActivateDeactivateBaseCmd() (*cobra.Command, *inventoryPluginActivateDeactivateFlags) {
var flags = &inventoryPluginActivateDeactivateFlags{}

var activateDeactivateCmd = &cobra.Command{}
Expand Down
24 changes: 18 additions & 6 deletions cmd/plugin/builder/inventory_plugin_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func newInventoryPluginGroupCmd() *cobra.Command {

type inventoryPluginGroupAddFlags struct {
GroupName string
GroupVersion string
Description string
Repository string
InventoryImageTag string
ManifestFile string
Expand All @@ -51,6 +53,8 @@ func newInventoryPluginGroupAddCmd() *cobra.Command {
RunE: func(cmd *cobra.Command, args []string) error {
pgaOptions := inventory.InventoryPluginGroupUpdateOptions{
GroupName: ipgaFlags.GroupName,
GroupVersion: ipgaFlags.GroupVersion,
Description: ipgaFlags.Description,
Repository: ipgaFlags.Repository,
InventoryImageTag: ipgaFlags.InventoryImageTag,
PluginGroupManifestFile: ipgaFlags.ManifestFile,
Expand All @@ -64,16 +68,19 @@ func newInventoryPluginGroupAddCmd() *cobra.Command {
},
}

pluginGroupAddCmd.Flags().StringVarP(&ipgaFlags.GroupName, "name", "", "", "name of the plugin group")
pluginGroupAddCmd.Flags().StringVarP(&ipgaFlags.GroupName, "name", "", "", "name of the plugin-group")
pluginGroupAddCmd.Flags().StringVarP(&ipgaFlags.GroupVersion, "version", "", "", "version of the plugin-group")
pluginGroupAddCmd.Flags().StringVarP(&ipgaFlags.Description, "description", "", "", "a description for the plugin-group")
pluginGroupAddCmd.Flags().StringVarP(&ipgaFlags.Repository, "repository", "", "", "repository to publish plugin inventory image")
pluginGroupAddCmd.Flags().StringVarP(&ipgaFlags.InventoryImageTag, "plugin-inventory-image-tag", "", "latest", "tag to which plugin inventory image needs to be published")
pluginGroupAddCmd.Flags().StringVarP(&ipgaFlags.ManifestFile, "manifest", "", "", "manifest file specifying plugin-group details that needs to be processed")
pluginGroupAddCmd.Flags().StringVarP(&ipgaFlags.Vendor, "vendor", "", "", "name of the vendor")
pluginGroupAddCmd.Flags().StringVarP(&ipgaFlags.Publisher, "publisher", "", "", "name of the publisher")
pluginGroupAddCmd.Flags().BoolVarP(&ipgaFlags.DeactivatePluginGroup, "deactivate", "", false, "mark plugin-group as deactivated")
pluginGroupAddCmd.Flags().BoolVarP(&ipgaFlags.Override, "override", "", false, "override the plugin-group if already exists")
pluginGroupAddCmd.Flags().BoolVarP(&ipgaFlags.Override, "override", "", false, "overwrite the plugin-group version if it already exists")

_ = pluginGroupAddCmd.MarkFlagRequired("name")
_ = pluginGroupAddCmd.MarkFlagRequired("version")
_ = pluginGroupAddCmd.MarkFlagRequired("repository")
_ = pluginGroupAddCmd.MarkFlagRequired("vendor")
_ = pluginGroupAddCmd.MarkFlagRequired("publisher")
Expand All @@ -84,21 +91,23 @@ func newInventoryPluginGroupAddCmd() *cobra.Command {

type inventoryPluginGroupActivateDeactivateFlags struct {
GroupName string
GroupVersion string
Repository string
InventoryImageTag string
ManifestFile string
Publisher string
Vendor string
}

func newInventoryPluginGroupActivateCmd() *cobra.Command {
func newInventoryPluginGroupActivateCmd() *cobra.Command { //nolint:dupl
pluginGroupActivateCmd, flags := getPluginGroupActivateDeactivateBaseCmd()
pluginGroupActivateCmd.Use = "activate"
pluginGroupActivateCmd.Short = "Activate the existing plugin-group in the inventory database available on the remote repository"
pluginGroupActivateCmd.Example = ""
pluginGroupActivateCmd.RunE = func(cmd *cobra.Command, args []string) error {
pguOptions := inventory.InventoryPluginGroupUpdateOptions{
GroupName: flags.GroupName,
GroupVersion: flags.GroupVersion,
Repository: flags.Repository,
InventoryImageTag: flags.InventoryImageTag,
Vendor: flags.Vendor,
Expand All @@ -111,14 +120,15 @@ func newInventoryPluginGroupActivateCmd() *cobra.Command {
return pluginGroupActivateCmd
}

func newInventoryPluginGroupDeactivateCmd() *cobra.Command {
func newInventoryPluginGroupDeactivateCmd() *cobra.Command { //nolint:dupl
pluginGroupDeactivateCmd, flags := getPluginGroupActivateDeactivateBaseCmd()
pluginGroupDeactivateCmd.Use = "deactivate"
pluginGroupDeactivateCmd.Short = "Deactivate the existing plugin-group in the inventory database available on the remote repository"
pluginGroupDeactivateCmd.Example = ""
pluginGroupDeactivateCmd.RunE = func(cmd *cobra.Command, args []string) error {
pguOptions := inventory.InventoryPluginGroupUpdateOptions{
GroupName: flags.GroupName,
GroupVersion: flags.GroupVersion,
Repository: flags.Repository,
InventoryImageTag: flags.InventoryImageTag,
Vendor: flags.Vendor,
Expand All @@ -131,19 +141,21 @@ func newInventoryPluginGroupDeactivateCmd() *cobra.Command {
return pluginGroupDeactivateCmd
}

func getPluginGroupActivateDeactivateBaseCmd() (*cobra.Command, *inventoryPluginGroupActivateDeactivateFlags) { // nolint:dupl
func getPluginGroupActivateDeactivateBaseCmd() (*cobra.Command, *inventoryPluginGroupActivateDeactivateFlags) {
var flags = &inventoryPluginGroupActivateDeactivateFlags{}

var activateDeactivateCmd = &cobra.Command{}
activateDeactivateCmd.SilenceUsage = true

activateDeactivateCmd.Flags().StringVarP(&flags.GroupName, "name", "", "", "name of the plugin group")
activateDeactivateCmd.Flags().StringVarP(&flags.GroupName, "name", "", "", "name of the plugin-group")
activateDeactivateCmd.Flags().StringVarP(&flags.GroupVersion, "version", "", "", "version of the plugin-group")
activateDeactivateCmd.Flags().StringVarP(&flags.Repository, "repository", "", "", "repository to publish plugin inventory image")
activateDeactivateCmd.Flags().StringVarP(&flags.InventoryImageTag, "plugin-inventory-image-tag", "", "latest", "tag to which plugin inventory image needs to be published")
activateDeactivateCmd.Flags().StringVarP(&flags.Vendor, "vendor", "", "", "name of the vendor")
activateDeactivateCmd.Flags().StringVarP(&flags.Publisher, "publisher", "", "", "name of the publisher")

_ = activateDeactivateCmd.MarkFlagRequired("name")
_ = activateDeactivateCmd.MarkFlagRequired("version")
_ = activateDeactivateCmd.MarkFlagRequired("repository")
_ = activateDeactivateCmd.MarkFlagRequired("vendor")
_ = activateDeactivateCmd.MarkFlagRequired("publisher")
Expand Down
Loading

0 comments on commit c7df086

Please sign in to comment.