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

[cherry-pick release-1.5 ]Capture endpoint hash for global plugins telemetry #847

Merged
merged 2 commits into from
Jan 28, 2025
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
2 changes: 1 addition & 1 deletion .github/workflows/cli-coexistence_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
build-args: |
TANZU_CLI_COEXISTENCE_LEGACY_TANZU_CLI_DIR=/app/legacy-tanzu-cli
TANZU_CLI_COEXISTENCE_LEGACY_TANZU_CLI_VERSION=v0.28.1
TANZU_CLI_COEXISTENCE_LEGACY_TANZU_CLI_URL=https://ent.box.com/shared/static/984uoy8mayq6rpdrfwh1omxurc0ixo57.gz
TANZU_CLI_COEXISTENCE_LEGACY_TANZU_CLI_URL=https://storage.googleapis.com/tanzu-cli-installer-packages/bin/v0.28.1/tanzu-cli-linux-amd64.tar.gz
TANZU_CLI_COEXISTENCE_NEW_TANZU_CLI_DIR=/app/tanzu-cli

- name: Run Docker image
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ TANZU_CLI_COEXISTENCE_LEGACY_TANZU_CLI_VERSION = v0.28.1
endif

ifndef TANZU_CLI_COEXISTENCE_LEGACY_TANZU_CLI_URL
TANZU_CLI_COEXISTENCE_LEGACY_TANZU_CLI_URL = https://ent.box.com/shared/static/984uoy8mayq6rpdrfwh1omxurc0ixo57.gz # tanzu-cli-linux-amd64 v0.28.1 bundle url
TANZU_CLI_COEXISTENCE_LEGACY_TANZU_CLI_URL = https://storage.googleapis.com/tanzu-cli-installer-packages/bin/v0.28.1/tanzu-cli-linux-amd64.tar.gz # tanzu-cli-linux-amd64 v0.28.1 bundle url
endif

ifndef TANZU_CLI_CEIP_OPT_IN_PROMPT_ANSWER
Expand Down
12 changes: 12 additions & 0 deletions pkg/telemetry/metrics_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,18 @@ func computeEndpointSHAWithCtxTypePrefix(curCtx map[configtypes.ContextType]*con
return string(configtypes.ContextTypeTMC) + ":" + computeEndpointSHAForTMCContext(ctx)
}
return ""
case configtypes.TargetGlobal:
// If Target is "global" and tanzu context type is active, use it as most plugins in Tanzu Platform are global target plugins
ctx, exists := curCtx[configtypes.ContextTypeTanzu]
if exists {
return string(configtypes.ContextTypeTanzu) + ":" + computeEndpointSHAForTanzuContext(ctx)
}
// There could be "global" plugins which could use `k8s` contexts. So get endpoint from k8s context as a fallback
ctx, exists = curCtx[configtypes.ContextTypeK8s]
if exists {
return string(configtypes.ContextTypeK8s) + ":" + computeEndpointSHAForK8sContext(ctx)
}
return ""
}
return ""
}
Expand Down
83 changes: 83 additions & 0 deletions pkg/telemetry/metrics_helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,89 @@ var _ = Describe("metrics helper tests", func() {
Expect(epHashStr).To(BeEmpty())
})
})
Context("when the plugin target is global and the current active context is of type 'tanzu'", func() {
It("should return the hash of tanzu active context prefixed with 'tanzu'", func() {
pluginInfo := &cli.PluginInfo{
Name: "global-plugin",
Version: "1.0.0",
Target: configtypes.TargetGlobal,
}
// when tanzu context has active org
ctx, err := configlib.GetContext("test-tanzu-context")
Expect(err).ToNot(HaveOccurred())
ctx.AdditionalMetadata[configlib.ProjectNameKey] = ""
ctx.AdditionalMetadata[configlib.SpaceNameKey] = ""
// Also when the ClusterGroupNameKey is not configured under AdditionalMetadata
err = configlib.SetContext(ctx, true)
Expect(err).ToNot(HaveOccurred())

epHashStr := getEndpointSHAWithCtxTypePrefix(pluginInfo)
Expect(epHashStr).ToNot(BeEmpty())
Expect(epHashStr).To(Equal(string(configtypes.ContextTypeTanzu) + ":" + computeEndpointSHAForTanzuContext(ctx)))

// when tanzu context has active project
ctx.AdditionalMetadata[configlib.ProjectNameKey] = testProjectName
ctx.AdditionalMetadata[configlib.ProjectIDKey] = testProjectID
ctx.AdditionalMetadata[configlib.SpaceNameKey] = ""
ctx.AdditionalMetadata[configlib.ClusterGroupNameKey] = ""
err = configlib.SetContext(ctx, true)
Expect(err).ToNot(HaveOccurred())

epHashStr = getEndpointSHAWithCtxTypePrefix(pluginInfo)
Expect(epHashStr).ToNot(BeEmpty())
Expect(epHashStr).To(Equal(string(configtypes.ContextTypeTanzu) + ":" + computeEndpointSHAForTanzuContext(ctx)))

// when tanzu context has active space
ctx.AdditionalMetadata[configlib.ProjectNameKey] = testProjectName
ctx.AdditionalMetadata[configlib.ProjectIDKey] = testProjectID
ctx.AdditionalMetadata[configlib.SpaceNameKey] = testSpaceName
ctx.AdditionalMetadata[configlib.ClusterGroupNameKey] = ""
err = configlib.SetContext(ctx, true)
Expect(err).ToNot(HaveOccurred())

// when tanzu context has active clustergroup
ctx.AdditionalMetadata[configlib.ProjectNameKey] = testProjectName
ctx.AdditionalMetadata[configlib.ProjectIDKey] = testProjectID
ctx.AdditionalMetadata[configlib.SpaceNameKey] = ""
ctx.AdditionalMetadata[configlib.ClusterGroupNameKey] = testClusterGroupName
err = configlib.SetContext(ctx, true)
Expect(err).ToNot(HaveOccurred())

epHashStr = getEndpointSHAWithCtxTypePrefix(pluginInfo)
Expect(epHashStr).ToNot(BeEmpty())
Expect(epHashStr).To(Equal(string(configtypes.ContextTypeTanzu) + ":" + computeEndpointSHAForTanzuContext(ctx)))
})
})
Context("when the plugin target is global and the current active context is of type 'kubernetes'", func() {
It("should return the hash of kubernetes active context prefixed with 'kubernetes'", func() {
pluginInfo := &cli.PluginInfo{
Name: "global-plugin",
Version: "1.0.0",
Target: configtypes.TargetGlobal,
}
epHashStr := getEndpointSHAWithCtxTypePrefix(pluginInfo)
Expect(epHashStr).ToNot(BeEmpty())

ctx, err := configlib.GetActiveContext(configtypes.ContextTypeK8s)
Expect(err).ToNot(HaveOccurred())
Expect(epHashStr).To(Equal(string(configtypes.ContextTypeK8s) + ":" + computeEndpointSHAForK8sContext(ctx)))
})
})
Context("when the plugin target is global and there is no active context of type tanzu or k8s", func() {
It("should return the empty string", func() {
pluginInfo := &cli.PluginInfo{
Name: "global-plugin",
Version: "1.0.0",
Target: configtypes.TargetK8s,
}
// remove the active contexts of type k8s
err := configlib.RemoveActiveContext(configtypes.ContextTypeK8s)
Expect(err).ToNot(HaveOccurred())

epHashStr := getEndpointSHAWithCtxTypePrefix(pluginInfo)
Expect(epHashStr).To(BeEmpty())
})
})
Context("when the plugin target is mission-control and the current active context is of context type 'mission-control'", func() {
It("should return the hash of mission-control active context prefixed with 'mission-control'", func() {
pluginInfo := &cli.PluginInfo{
Expand Down
Loading