Skip to content
This repository has been archived by the owner on Oct 10, 2023. It is now read-only.

Commit

Permalink
Make k8s a special target
Browse files Browse the repository at this point in the history
  • Loading branch information
anujc25 committed Nov 22, 2022
1 parent e115874 commit e63f022
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
34 changes: 33 additions & 1 deletion cli/core/pkg/command/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"time"

"github.com/vmware-tanzu/tanzu-framework/cli/core/pkg/common"

cliv1alpha1 "github.com/vmware-tanzu/tanzu-framework/apis/cli/v1alpha1"

"github.com/aunum/log"
Expand Down Expand Up @@ -102,7 +104,21 @@ func NewRootCmd() (*cobra.Command, error) {
}

for _, plugin := range plugins {
RootCmd.AddCommand(cli.GetCmd(plugin))
// Only add plugins that should be available as root level command
if isPluginRootCmdTargeted(plugin) {
cmd := cli.GetCmd(plugin)
// check and find if same command already exists as part of root command
matchedCmd := findSubCommand(RootCmd, cmd)
if matchedCmd == nil { // If the subcommand for the plugin doesn't exist add the command
RootCmd.AddCommand(cmd)
} else if plugin.Scope == common.PluginScopeStandalone {
// If the subcommand already exists but plugin is `Context-Scoped` plugin
// then context-scoped plugin gets higher precedence, so, replace the existing command
// to point to new command by removing and adding new command.
RootCmd.RemoveCommand(matchedCmd)
RootCmd.AddCommand(cmd)
}
}
}

duplicateAliasWarning()
Expand Down Expand Up @@ -241,3 +257,19 @@ func Execute() error {
}
return root.Execute()
}

func findSubCommand(rootCmd *cobra.Command, subCmd *cobra.Command) *cobra.Command {
arrSubCmd := rootCmd.Commands()
var foundCmd *cobra.Command
for i := range arrSubCmd {
if arrSubCmd[i].Name() == subCmd.Name() {
foundCmd = arrSubCmd[i]
}
}
return foundCmd
}

func isPluginRootCmdTargeted(plugin *cliapi.PluginDescriptor) bool {
// Only '<none>' targeted and `k8s` targeted plugins are considered root cmd targeted plugins
return plugin != nil && (plugin.Target == "" || plugin.Target == cliv1alpha1.TargetK8s)
}
3 changes: 3 additions & 0 deletions cli/core/pkg/discovery/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ func (l *LocalDiscovery) Manifest() ([]plugin.Discovered, error) {
if err != nil {
return nil, err
}
if dp.Name == "" {
continue
}
dp.Source = l.name
dp.DiscoveryType = l.Type()
plugins = append(plugins, dp)
Expand Down
35 changes: 35 additions & 0 deletions cli/core/pkg/pluginmanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,44 @@ func availablePlugins(discoveredServerPlugins, discoveredStandalonePlugins []plu
installedButNotDiscoveredPlugins := getInstalledButNotDiscoveredStandalonePlugins(availablePlugins, installedStandalonePluginDesc)
availablePlugins = append(availablePlugins, installedButNotDiscoveredPlugins...)

availablePlugins = removeDuplicates(availablePlugins)

return availablePlugins, nil
}

// removeDuplicates remove duplicate plugins.
// When there is a plugin name conflicts and target of both the plugins are same, remove one.
//
// Considering, we are adding `k8s` targeted plugins as root level commands as well for backward compatibility
// A plugin 'foo' getting discovered/installed with <none> targeted source and a plugin `foo` getting discovered
// with `k8s` discovery should be treated as same plugin.
// This function takes this case into consideration and ignores <none> targeted plugin for above the mentioned scenario.
func removeDuplicates(availablePlugins []plugin.Discovered) []plugin.Discovered {
mapOfSelectedPlugins := make(map[string]plugin.Discovered)
for i := range availablePlugins {
if availablePlugins[i].Target == "" {
key := fmt.Sprintf("%s_%s", availablePlugins[i].Name, cliv1alpha1.TargetK8s)
_, exists := mapOfSelectedPlugins[key]
if !exists {
mapOfSelectedPlugins[key] = availablePlugins[i]
}
} else {
key := fmt.Sprintf("%s_%s", availablePlugins[i].Name, availablePlugins[i].Target)
_, exists := mapOfSelectedPlugins[key]
if !exists || availablePlugins[i].Target == cliv1alpha1.TargetK8s || availablePlugins[i].Scope == common.PluginScopeContext {
mapOfSelectedPlugins[key] = availablePlugins[i]
}
}
}

var selectedPlugins []plugin.Discovered
for _, p := range mapOfSelectedPlugins {
selectedPlugins = append(selectedPlugins, p)
}

return selectedPlugins
}

func getInstalledButNotDiscoveredStandalonePlugins(availablePlugins []plugin.Discovered, installedPluginDesc []cliapi.PluginDescriptor) []plugin.Discovered {
var newPlugins []plugin.Discovered
for i := range installedPluginDesc {
Expand Down

0 comments on commit e63f022

Please sign in to comment.