Skip to content

Commit

Permalink
Update telemetry to support command-remapping (vmware-tanzu#848)
Browse files Browse the repository at this point in the history
- When building the command-tree of each plugin, we include all root
commands since the plugin can provide many such commands; before a
plugin only provided its own name as a root command.

- When building the command-tree of each plugin, we also include any
applicable target. This is important so telemetry can map the command
provided by the user with the command tree.

- When saving a command invocation to telemetry, we need to pass the
entire command path, since the plugin name is no longer necessarily a
prefix in the invocation.

- Fix finding aliases by determining the source command-path within
the plugin command-tree and invoking it with "-h".

- To allow older CLIs (< 1.5.3) to continue using the old command-tree
format for telemetry, this commit uses a new command_tree_v2.yaml file
for the new format. Switching back and forth from new to old CLIs will
therefore not mess with the same plugin command-tree cache and will
keep telemetry in a better shape for older CLIs.

Signed-off-by: Marc Khouzam <[email protected]>
  • Loading branch information
marckhouzam committed Jan 29, 2025
1 parent d756c78 commit 496b678
Show file tree
Hide file tree
Showing 10 changed files with 719 additions and 234 deletions.
4 changes: 4 additions & 0 deletions pkg/cli/plugin_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ func getCmdForPluginEx(p *PluginInfo, cmdName string, mapEntry *plugin.CommandMa
"scope": p.Scope,
"type": common.CommandTypePlugin,
"pluginInstallationPath": p.InstallationPath,
// Telemetry uses the below annotation to identify the source
// of the command within the plugin so that it can determine how
// to invoke this command directly from the plugin binary.
common.AnnotationForCmdSrcPath: strings.Join(srcHierarchy, " "),
},
Hidden: hidden,
Aliases: aliases,
Expand Down
69 changes: 66 additions & 3 deletions pkg/cli/plugin_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/stretchr/testify/assert"

"github.com/vmware-tanzu/tanzu-cli/pkg/common"
"github.com/vmware-tanzu/tanzu-plugin-runtime/plugin"
)

Expand Down Expand Up @@ -43,10 +44,72 @@ func TestGetCmdForPlugin(t *testing.T) {
cmd := GetCmdForPlugin(pi)

err = cmd.Execute()
assert.Equal(cmd.Name(), pi.Name)
assert.Equal(cmd.Short, pi.Description)
assert.Equal(cmd.Aliases, pi.Aliases)
assert.Nil(err)

assert.Equal(pi.Name, cmd.Name())
assert.Equal(pi.Description, cmd.Short)
assert.Equal(pi.Aliases, cmd.Aliases)

annotations := cmd.Annotations
assert.Equal(5, len(annotations))
assert.Equal(string(pi.Group), annotations["group"])
assert.Equal(pi.Scope, annotations["scope"])
assert.Equal(common.CommandTypePlugin, annotations["type"])
assert.Equal(pi.InstallationPath, annotations["pluginInstallationPath"])
// No remapping in this test
assert.Equal("", annotations[common.AnnotationForCmdSrcPath])
}

func TestGetCmdForRemappedPlugin(t *testing.T) {
assert := assert.New(t)

dir, err := os.MkdirTemp("", "tanzu-cli-getcmd")
assert.Nil(err)
defer os.RemoveAll(dir)

path, err := setupFakePlugin(dir, "fakefoo", "")
assert.Nil(err)

const (
originalCmdName = "fakefoo"
renamedCmdName = "fakefoo2"
)

pi := &PluginInfo{
Name: originalCmdName,
Description: "Fake foo",
Group: plugin.SystemCmdGroup,
Aliases: []string{"ff"},
InstallationPath: path,
Hidden: true,
}

remapping := &plugin.CommandMapEntry{
SourceCommandPath: originalCmdName,
DestinationCommandPath: renamedCmdName,
Description: "Other desc",
Aliases: []string{"ff2"},
}

cmd := getCmdForPluginEx(pi, renamedCmdName, remapping)

err = cmd.Execute()
assert.Nil(err)

assert.Equal(renamedCmdName, cmd.Name())
assert.Equal(remapping.Description, cmd.Short)
assert.Equal(remapping.Aliases, cmd.Aliases)
// A remapped command should not be hidden even if the original command is hidden
assert.False(cmd.Hidden)

annotations := cmd.Annotations
assert.Equal(5, len(annotations))
assert.Equal(string(pi.Group), annotations["group"])
assert.Equal(pi.Scope, annotations["scope"])
assert.Equal(common.CommandTypePlugin, annotations["type"])
assert.Equal(pi.InstallationPath, annotations["pluginInstallationPath"])
// We should see the remapped command name in the annotations
assert.Equal(remapping.SourceCommandPath, annotations[common.AnnotationForCmdSrcPath])
}

func TestEnvForPlugin(t *testing.T) {
Expand Down
5 changes: 5 additions & 0 deletions pkg/common/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,8 @@ const CoreName = "core"

// CommandTypePlugin represents the command type is plugin
const CommandTypePlugin = "plugin"

// Command Annotations
const (
AnnotationForCmdSrcPath = "cmdSrcPath"
)
Loading

0 comments on commit 496b678

Please sign in to comment.