Skip to content

Commit

Permalink
Avoid uselessly trying to migrate plugins
Browse files Browse the repository at this point in the history
The only time we need to migrate context-scope plugins is after moving
from a CLI < 1.3 to a newer one.  This commit makes use of the existing
global initializer to do this migration once, for all contexts.

This avoids an unnecessary slow down for every CLI commands, especially
shell completion which should be as fast as possible.

Signed-off-by: Marc Khouzam <[email protected]>
  • Loading branch information
marckhouzam committed Nov 28, 2024
1 parent 92c98e8 commit 05e593e
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
20 changes: 8 additions & 12 deletions pkg/catalog/cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package catalog

import (
configlib "github.com/vmware-tanzu/tanzu-plugin-runtime/config"
configtypes "github.com/vmware-tanzu/tanzu-plugin-runtime/config/types"
)

Expand Down Expand Up @@ -47,27 +46,24 @@ func DeleteIncorrectPluginEntriesFromCatalog() {
}

// MigrateContextPluginsAsStandaloneIfNeeded updates the catalog cache to move all the
// context-scoped plugins associated with the active context as standalone plugins
// context-scoped plugins as standalone plugins
// and removes the context-scoped plugin mapping from the catalog cache.
// This is to ensure backwards compatibility when user migrates from pre v1.3 version of
// the CLI, the context-scoped plugins are still gets shown as installed
// This is to ensure backwards compatibility when the user migrates from pre v1.3 version of
// the CLI, the context-scoped plugins are still shown as installed
func MigrateContextPluginsAsStandaloneIfNeeded() {
activeContexts, err := configlib.GetAllActiveContextsList()
if err != nil || len(activeContexts) == 0 {
return
}

c, lockedFile, err := getCatalogCache(true)
if err != nil {
return
}
defer lockedFile.Close()

for _, ac := range activeContexts {
for pluginKey, installPath := range c.ServerPlugins[ac] {
for _, association := range c.ServerPlugins {
for pluginKey, installPath := range association {
c.StandAlonePlugins.Add(pluginKey, installPath)
}
delete(c.ServerPlugins, ac)
}
// Delete all entries by reassigning to a new empty map
c.ServerPlugins = make(map[string]PluginAssociation)

_ = saveCatalogCache(c, lockedFile)
}
22 changes: 18 additions & 4 deletions pkg/globalinit/catalog_cache_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,35 @@ import (
)

// This global initializer checks if the last executed CLI version is < 1.3.0.
// If so it refreshes the plugin catalog to make sure any remapping data is in the catalog.
// If so it refreshes the plugin catalog to:
// 1- migrate context-scoped plugins as standalone plugins
// 2- make sure any remapping data is in the catalog

func init() {
RegisterInitializer("Plugin Info Catalog Initializer", triggerForPreCommandRemapping, refreshPluginCatalog)
RegisterInitializer("Plugin Info Catalog Initializer", triggerForPreCommandRemapping, updatePluginCatalog)
}

func triggerForPreCommandRemapping() bool {
// If the last executed CLI version is < 1.3.0, we need to refresh the plugin catalog.
return lastversion.GetLastExecutedCLIVersion() == lastversion.OlderThan1_3_0
}

// refreshPluginCatalog reads the info from each installed plugin
// updatePluginCatalog does the following catalog udpates:
// 1- Migrate context-scoped plugins as standalone plugins
// 2- Reads the info from each installed plugin and updates the plugin catalog
//
// with the latest info. We need to do this to make sure the command re-mapping
// data is in the cache.
func updatePluginCatalog(outStream io.Writer) error {
catalog.MigrateContextPluginsAsStandaloneIfNeeded()

return refreshPluginsForRemapping(outStream)
}

// refreshPluginsForRemapping reads the info from each installed plugin
// and updates the plugin catalog with the latest info.
// We need to do this to make sure the command re-mapping data is in the cache.
func refreshPluginCatalog(outStream io.Writer) error {
func refreshPluginsForRemapping(outStream io.Writer) error {
plugins, err := pluginsupplier.GetInstalledPlugins()
if err != nil {
return err
Expand Down
5 changes: 0 additions & 5 deletions pkg/pluginsupplier/plugin_supplier.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,6 @@ import (

// GetInstalledPlugins return the installed plugins
func GetInstalledPlugins() ([]cli.PluginInfo, error) {
// Migrate context-scoped plugins as standalone plugin if required
// TODO(anujc): Think on how to invoke this function just once after the newer version
// of the CLI gets installed as we just need to do this migration once
catalog.MigrateContextPluginsAsStandaloneIfNeeded()

// Get all the standalone plugins found in the catalog
standAloneCatalog, err := catalog.NewContextCatalog("")
if err != nil {
Expand Down

0 comments on commit 05e593e

Please sign in to comment.