Skip to content

Commit

Permalink
Capture endpoint hash for global plugins telemetry
Browse files Browse the repository at this point in the history
Signed-off-by: Prem Kumar Kalle <[email protected]>
  • Loading branch information
prkalle committed Jan 28, 2025
1 parent 29f9488 commit 3db418e
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
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

0 comments on commit 3db418e

Please sign in to comment.