Skip to content
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
16 changes: 16 additions & 0 deletions tool/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,19 @@ func FormatLabels(labels map[string]string, verbose bool) string {
namespaced = append(namespaced, teleportNamespaced...)
return strings.Join(append(result, namespaced...), ",")
}

// FormatResourceName returns the resource's name or its name as originally
// discovered in the cloud by the Teleport Discovery Service.
// In verbose mode, it always returns the resource name.
// In non-verbose mode, if the resource came from discovery and has the
// discovered name label, it returns the discovered name.
func FormatResourceName(r types.ResourceWithLabels, verbose bool) string {
if !verbose {
// return the (shorter) discovered name in non-verbose mode.
discoveredName, ok := r.GetAllLabels()[types.DiscoveredNameLabel]
if ok && discoveredName != "" {
return discoveredName
}
}
return r.GetName()
}
23 changes: 4 additions & 19 deletions tool/tctl/common/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ func (c *databaseServerCollection) writeText(w io.Writer, verbose bool) error {
labels := common.FormatLabels(server.GetDatabase().GetAllLabels(), verbose)
rows = append(rows, []string{
server.GetHostname(),
nameOrDiscoveredName(server.GetDatabase(), verbose),
common.FormatResourceName(server.GetDatabase(), verbose),
server.GetDatabase().GetProtocol(),
server.GetDatabase().GetURI(),
labels,
Expand Down Expand Up @@ -732,7 +732,7 @@ func (c *databaseCollection) writeText(w io.Writer, verbose bool) error {
for _, database := range c.databases {
labels := common.FormatLabels(database.GetAllLabels(), verbose)
rows = append(rows, []string{
nameOrDiscoveredName(database, verbose),
common.FormatResourceName(database, verbose),
database.GetProtocol(),
database.GetURI(),
labels,
Expand Down Expand Up @@ -877,7 +877,7 @@ func (c *kubeServerCollection) writeText(w io.Writer, verbose bool) error {
}
labels := common.FormatLabels(kube.GetAllLabels(), verbose)
rows = append(rows, []string{
nameOrDiscoveredName(kube, verbose),
common.FormatResourceName(kube, verbose),
labels,
server.GetTeleportVersion(),
})
Expand Down Expand Up @@ -929,7 +929,7 @@ func (c *kubeClusterCollection) writeText(w io.Writer, verbose bool) error {
for _, cluster := range c.clusters {
labels := common.FormatLabels(cluster.GetAllLabels(), verbose)
rows = append(rows, []string{
nameOrDiscoveredName(cluster, verbose),
common.FormatResourceName(cluster, verbose),
labels,
})
}
Expand Down Expand Up @@ -1192,18 +1192,3 @@ func (c *userGroupCollection) writeText(w io.Writer, verbose bool) error {
_, err := t.AsBuffer().WriteTo(w)
return trace.Wrap(err)
}

// nameOrDiscoveredName returns the resource's name or its name as originally
// discovered in the cloud by the Teleport Discovery Service.
// In verbose mode, it always returns the resource name.
// In non-verbose mode, if the resource came from discovery and has the
// discovered name label, it returns the discovered name.
func nameOrDiscoveredName(r types.ResourceWithLabels, verbose bool) string {
if !verbose {
originalName, ok := r.GetAllLabels()[types.DiscoveredNameLabel]
if ok && originalName != "" {
return originalName
}
}
return r.GetName()
}
13 changes: 7 additions & 6 deletions tool/tctl/common/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/google/uuid"
"github.com/stretchr/testify/require"

"github.com/gravitational/teleport/api"
Expand Down Expand Up @@ -132,10 +133,10 @@ func testKubeServerCollection_writeText(t *testing.T) {
types.DiscoveredNameLabel: "cluster3",
}
kubeServers := []types.KubeServer{
mustCreateNewKubeServer(t, "cluster1", nil),
mustCreateNewKubeServer(t, "cluster2", longLabelFixture),
mustCreateNewKubeServer(t, "afirstCluster", longLabelFixture),
mustCreateNewKubeServer(t, "cluster3-eks-us-west-1-123456789012", eksDiscoveredNameLabel),
mustCreateNewKubeServer(t, "cluster1", "_", nil),
mustCreateNewKubeServer(t, "cluster2", "_", longLabelFixture),
mustCreateNewKubeServer(t, "afirstCluster", "_", longLabelFixture),
mustCreateNewKubeServer(t, "cluster3-eks-us-west-1-123456789012", "_", eksDiscoveredNameLabel),
}
test := writeTextTest{
collection: &kubeServerCollection{servers: kubeServers},
Expand Down Expand Up @@ -307,10 +308,10 @@ func mustCreateNewKubeCluster(t *testing.T, name string, extraStaticLabels map[s
return cluster
}

func mustCreateNewKubeServer(t *testing.T, name string, extraStaticLabels map[string]string) types.KubeServer {
func mustCreateNewKubeServer(t *testing.T, name, hostname string, extraStaticLabels map[string]string) *types.KubernetesServerV3 {
t.Helper()
cluster := mustCreateNewKubeCluster(t, name, extraStaticLabels)
kubeServer, err := types.NewKubernetesServerV3FromCluster(cluster, "some-host", "some-hostid")
kubeServer, err := types.NewKubernetesServerV3FromCluster(cluster, hostname, uuid.New().String())
require.NoError(t, err)
return kubeServer
}
Expand Down
3 changes: 3 additions & 0 deletions tool/tctl/common/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,9 @@ func makeAndRunTestAuthServer(t *testing.T, opts ...testServerOptionFunc) (auth
}

func waitForDatabases(t *testing.T, auth *service.TeleportProcess, dbs []servicecfg.Database) {
if len(dbs) == 0 {
return
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
for {
Expand Down
Loading