Skip to content

Commit

Permalink
Align label sorting in Connect with what Web UI does (#48086)
Browse files Browse the repository at this point in the history
* Move TransformCommandLabels from lib/web/ui to lib/ui

* Prepare labels in lib/teleterm same way as in lib/web
  • Loading branch information
ravicious authored Oct 30, 2024
1 parent 57daece commit 3a33aab
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 159 deletions.
23 changes: 10 additions & 13 deletions lib/teleterm/apiserver/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

api "github.com/gravitational/teleport/gen/proto/go/teleport/lib/teleterm/v1"
"github.com/gravitational/teleport/lib/teleterm/daemon"
"github.com/gravitational/teleport/lib/ui"
)

// New creates an instance of Handler
Expand Down Expand Up @@ -59,17 +60,13 @@ type Handler struct {
Config
}

// sortedLabels is a sort wrapper that sorts labels by name
type APILabels []*api.Label

func (s APILabels) Len() int {
return len(s)
}

func (s APILabels) Less(i, j int) bool {
return s[i].Name < s[j].Name
}

func (s APILabels) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
func makeAPILabels(uiLabels []ui.Label) []*api.Label {
apiLabels := make([]*api.Label, 0, len(uiLabels))
for _, uiLabel := range uiLabels {
apiLabels = append(apiLabels, &api.Label{
Name: uiLabel.Name,
Value: uiLabel.Value,
})
}
return apiLabels
}
20 changes: 3 additions & 17 deletions lib/teleterm/apiserver/handler/handler_apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ package handler

import (
"context"
"sort"

"github.com/gravitational/trace"

"github.com/gravitational/teleport/api/types"
api "github.com/gravitational/teleport/gen/proto/go/teleport/lib/teleterm/v1"
"github.com/gravitational/teleport/lib/teleterm/clusters"
"github.com/gravitational/teleport/lib/ui"
)

// GetApps gets apps with filters and returns paginated results
Expand Down Expand Up @@ -77,14 +77,7 @@ func newAPIApp(clusterApp clusters.App) *api.App {
})
}

apiLabels := APILabels{}
for name, value := range app.GetAllLabels() {
apiLabels = append(apiLabels, &api.Label{
Name: name,
Value: value,
})
}
sort.Sort(apiLabels)
apiLabels := makeAPILabels(ui.MakeLabelsWithoutInternalPrefixes(app.GetAllLabels()))

return &api.App{
Uri: clusterApp.URI.String(),
Expand All @@ -103,14 +96,7 @@ func newAPIApp(clusterApp clusters.App) *api.App {

func newSAMLIdPServiceProviderAPIApp(clusterApp clusters.SAMLIdPServiceProvider) *api.App {
provider := clusterApp.Provider
apiLabels := APILabels{}
for name, value := range provider.GetAllLabels() {
apiLabels = append(apiLabels, &api.Label{
Name: name,
Value: value,
})
}
sort.Sort(apiLabels)
apiLabels := makeAPILabels(ui.MakeLabelsWithoutInternalPrefixes(provider.GetAllLabels()))

// Keep in sync with lib/web/ui/app.go.
return &api.App{
Expand Down
12 changes: 2 additions & 10 deletions lib/teleterm/apiserver/handler/handler_databases.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ package handler

import (
"context"
"sort"

"github.com/gravitational/trace"

api "github.com/gravitational/teleport/gen/proto/go/teleport/lib/teleterm/v1"
"github.com/gravitational/teleport/lib/teleterm/clusters"
"github.com/gravitational/teleport/lib/ui"
)

// GetDatabases gets databases with filters and returns paginated results
Expand Down Expand Up @@ -84,15 +84,7 @@ func (s *Handler) ListDatabaseUsers(ctx context.Context, req *api.ListDatabaseUs
}

func newAPIDatabase(db clusters.Database) *api.Database {
apiLabels := APILabels{}
for name, value := range db.GetAllLabels() {
apiLabels = append(apiLabels, &api.Label{
Name: name,
Value: value,
})
}

sort.Sort(apiLabels)
apiLabels := makeAPILabels(ui.MakeLabelsWithoutInternalPrefixes(db.GetAllLabels()))

return &api.Database{
Uri: db.URI.String(),
Expand Down
31 changes: 6 additions & 25 deletions lib/teleterm/apiserver/handler/handler_kubes.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package handler

import (
"context"
"sort"

"github.com/gravitational/trace"

Expand Down Expand Up @@ -74,22 +73,11 @@ func (s *Handler) ListKubernetesResources(ctx context.Context, req *api.ListKube
}

func newAPIKube(kube clusters.Kube) *api.Kube {
apiLabels := APILabels{}
for name, value := range kube.KubernetesCluster.GetStaticLabels() {
apiLabels = append(apiLabels, &api.Label{
Name: name,
Value: value,
})
}

for name, cmd := range kube.KubernetesCluster.GetDynamicLabels() {
apiLabels = append(apiLabels, &api.Label{
Name: name,
Value: cmd.GetResult(),
})
}

sort.Sort(apiLabels)
staticLabels := kube.KubernetesCluster.GetStaticLabels()
dynamicLabels := kube.KubernetesCluster.GetDynamicLabels()
apiLabels := makeAPILabels(
ui.MakeLabelsWithoutInternalPrefixes(staticLabels, ui.TransformCommandLabels(dynamicLabels)),
)

return &api.Kube{
Name: kube.KubernetesCluster.GetName(),
Expand All @@ -99,14 +87,7 @@ func newAPIKube(kube clusters.Kube) *api.Kube {
}

func newApiKubeResource(resource *types.KubernetesResourceV1, kubeCluster string, resourceURI uri.ResourceURI) *api.KubeResource {
uiLabels := ui.MakeLabelsWithoutInternalPrefixes(resource.GetStaticLabels())
apiLabels := APILabels{}
for _, uiLabel := range uiLabels {
apiLabels = append(apiLabels, &api.Label{
Name: uiLabel.Name,
Value: uiLabel.Value,
})
}
apiLabels := makeAPILabels(ui.MakeLabelsWithoutInternalPrefixes(resource.GetStaticLabels()))

return &api.KubeResource{
Uri: resourceURI.AppendKube(kubeCluster).AppendKubeResourceNamespace(resource.GetName()).String(),
Expand Down
21 changes: 4 additions & 17 deletions lib/teleterm/apiserver/handler/handler_servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ package handler

import (
"context"
"sort"

"github.com/gravitational/trace"

api "github.com/gravitational/teleport/gen/proto/go/teleport/lib/teleterm/v1"
"github.com/gravitational/teleport/lib/teleterm/clusters"
"github.com/gravitational/teleport/lib/ui"
)

// GetServers accepts parameterized input to enable searching, sorting, and pagination
Expand All @@ -47,24 +47,11 @@ func (s *Handler) GetServers(ctx context.Context, req *api.GetServersRequest) (*
}

func newAPIServer(server clusters.Server) *api.Server {
apiLabels := APILabels{}
serverLabels := server.GetStaticLabels()
for name, value := range serverLabels {
apiLabels = append(apiLabels, &api.Label{
Name: name,
Value: value,
})
}

serverCmdLabels := server.GetCmdLabels()
for name, cmd := range serverCmdLabels {
apiLabels = append(apiLabels, &api.Label{
Name: name,
Value: cmd.GetResult(),
})
}

sort.Sort(apiLabels)
apiLabels := makeAPILabels(
ui.MakeLabelsWithoutInternalPrefixes(serverLabels, ui.TransformCommandLabels(serverCmdLabels)),
)

return &api.Server{
Uri: server.URI.String(),
Expand Down
10 changes: 10 additions & 0 deletions lib/ui/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,3 +87,13 @@ func (s sortedLabels) Less(i, j int) bool {
func (s sortedLabels) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}

func TransformCommandLabels(commandLabels map[string]types.CommandLabel) map[string]string {
labels := make(map[string]string, len(commandLabels))

for name, cmd := range commandLabels {
labels[name] = cmd.GetResult()
}

return labels
}
16 changes: 16 additions & 0 deletions lib/ui/labels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"testing"

"github.com/stretchr/testify/require"

"github.com/gravitational/teleport/api/types"
)

func TestMakeLabels(t *testing.T) {
Expand Down Expand Up @@ -142,3 +144,17 @@ func TestMakeLabels(t *testing.T) {
})
}
}

func TestTransformCommandLabels(t *testing.T) {
commandLabels := map[string]types.CommandLabel{
"label1": &types.CommandLabelV2{
Result: "value1",
},
}
labels := TransformCommandLabels(commandLabels)
expected := map[string]string{
"label1": "value1",
}

require.Equal(t, expected, labels)
}
33 changes: 0 additions & 33 deletions lib/web/ui/labels.go

This file was deleted.

41 changes: 0 additions & 41 deletions lib/web/ui/labels_test.go

This file was deleted.

6 changes: 3 additions & 3 deletions lib/web/ui/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type AWSMetadata struct {
func MakeServer(clusterName string, server types.Server, logins []string, requiresRequest bool) Server {
serverLabels := server.GetStaticLabels()
serverCmdLabels := server.GetCmdLabels()
uiLabels := ui.MakeLabelsWithoutInternalPrefixes(serverLabels, transformCommandLabels(serverCmdLabels))
uiLabels := ui.MakeLabelsWithoutInternalPrefixes(serverLabels, ui.TransformCommandLabels(serverCmdLabels))

uiServer := Server{
Kind: server.GetKind(),
Expand Down Expand Up @@ -131,7 +131,7 @@ type KubeCluster struct {
func MakeKubeCluster(cluster types.KubeCluster, accessChecker services.AccessChecker, requiresRequest bool) KubeCluster {
staticLabels := cluster.GetStaticLabels()
dynamicLabels := cluster.GetDynamicLabels()
uiLabels := ui.MakeLabelsWithoutInternalPrefixes(staticLabels, transformCommandLabels(dynamicLabels))
uiLabels := ui.MakeLabelsWithoutInternalPrefixes(staticLabels, ui.TransformCommandLabels(dynamicLabels))
kubeUsers, kubeGroups := getAllowedKubeUsersAndGroupsForCluster(accessChecker, cluster)
return KubeCluster{
Kind: cluster.GetKind(),
Expand Down Expand Up @@ -166,7 +166,7 @@ func MakeKubeClusters(clusters []types.KubeCluster, accessChecker services.Acces
for _, cluster := range clusters {
staticLabels := cluster.GetStaticLabels()
dynamicLabels := cluster.GetDynamicLabels()
uiLabels := ui.MakeLabelsWithoutInternalPrefixes(staticLabels, transformCommandLabels(dynamicLabels))
uiLabels := ui.MakeLabelsWithoutInternalPrefixes(staticLabels, ui.TransformCommandLabels(dynamicLabels))

kubeUsers, kubeGroups := getAllowedKubeUsersAndGroupsForCluster(accessChecker, cluster)

Expand Down

0 comments on commit 3a33aab

Please sign in to comment.