Skip to content

Commit

Permalink
Address CRs
Browse files Browse the repository at this point in the history
  • Loading branch information
kimlisa committed Sep 23, 2024
1 parent cde17d4 commit 5ee774f
Show file tree
Hide file tree
Showing 19 changed files with 978 additions and 964 deletions.
6 changes: 2 additions & 4 deletions gen/proto/go/teleport/lib/teleterm/v1/kube.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1,552 changes: 776 additions & 776 deletions gen/proto/go/teleport/lib/teleterm/v1/service.pb.go

Large diffs are not rendered by default.

5 changes: 2 additions & 3 deletions gen/proto/ts/teleport/lib/teleterm/v1/kube_pb.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 28 additions & 28 deletions gen/proto/ts/teleport/lib/teleterm/v1/service_pb.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions lib/teleterm/api/uri/uri.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ var pathDbs = urlpath.New("/clusters/:cluster/dbs/:dbName")
var pathLeafDbs = urlpath.New("/clusters/:cluster/leaves/:leaf/dbs/:dbName")
var pathKubes = urlpath.New("/clusters/:cluster/kubes/:kubeName")
var pathLeafKubes = urlpath.New("/clusters/:cluster/leaves/:leaf/kubes/:kubeName")
var pathKubeResourceNamespace = urlpath.New("/clusters/:cluster/kube-resources/namespace/:kubeName/:namespaceName")
var pathLeafKubeResourceNamespace = urlpath.New("/clusters/:cluster/leaves/:leaf/kube-resources/namespace/:kubeName/:namespaceName")
var pathKubeResourceNamespace = urlpath.New("/clusters/:cluster/kubes/:kubeName/namespaces/:kubeNamespaceName")
var pathLeafKubeResourceNamespace = urlpath.New("/clusters/:cluster/leaves/:leaf/kubes/:kubeName/namespaces/:kubeNamespaceName")
var pathApps = urlpath.New("/clusters/:cluster/apps/:appName")
var pathLeafApps = urlpath.New("/clusters/:cluster/leaves/:leaf/apps/:appName")

Expand Down Expand Up @@ -128,12 +128,12 @@ func (r ResourceURI) GetKubeName() string {
func (r ResourceURI) GetKubeResourceNamespace() string {
result, ok := pathKubeResourceNamespace.Match(r.path)
if ok {
return result.Params["namespaceName"]
return result.Params["kubeNamespaceName"]
}

result, ok = pathLeafKubeResourceNamespace.Match(r.path)
if ok {
return result.Params["namespaceName"]
return result.Params["kubeNamespaceName"]
}

return ""
Expand Down Expand Up @@ -207,8 +207,8 @@ func (r ResourceURI) AppendKube(name string) ResourceURI {
}

// AppendKubeResourceNamespace appends kube resource namespace segment to the URI.
func (r ResourceURI) AppendKubeResourceNamespace(kubeClusterName string, namespaceName string) ResourceURI {
r.path = fmt.Sprintf("%v/kube-resources/namespace/%v/%v", r.path, kubeClusterName, namespaceName)
func (r ResourceURI) AppendKubeResourceNamespace(kubeClusterName string, kubeNamespaceName string) ResourceURI {
r.path = fmt.Sprintf("%v/kubes/%v/namespaces/%v", r.path, kubeClusterName, kubeNamespaceName)
return r
}

Expand Down
2 changes: 1 addition & 1 deletion lib/teleterm/api/uri/uri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func TestString(t *testing.T) {
},
{
uri.NewClusterURI("teleport.sh").AppendKubeResourceNamespace("kube-cluster-name", "namespace-name"),
"/clusters/teleport.sh/kube-resources/namespace/kube-cluster-name/namespace-name",
"/clusters/teleport.sh/kubes/kube-cluster-name/namespaces/namespace-name",
},
}

Expand Down
10 changes: 5 additions & 5 deletions lib/teleterm/apiserver/handler/handler_kubes.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
api "github.com/gravitational/teleport/gen/proto/go/teleport/lib/teleterm/v1"
"github.com/gravitational/teleport/lib/teleterm/api/uri"
"github.com/gravitational/teleport/lib/teleterm/clusters"
"github.com/gravitational/teleport/lib/web/ui"
"github.com/gravitational/teleport/lib/ui"
)

// GetKubes accepts parameterized input to enable searching, sorting, and pagination
Expand Down Expand Up @@ -63,11 +63,11 @@ func (s *Handler) ListKubernetesResources(ctx context.Context, req *api.ListKube
}

response := &api.ListKubernetesResourcesResponse{
StartKey: resp.NextKey,
NextKey: resp.NextKey,
}

for _, kubeResource := range resp.Resources {
response.Items = append(response.Items, newApiKubeResource(kubeResource, req.GetKubernetesCluster(), clusterURI))
response.Resources = append(response.Resources, newApiKubeResource(kubeResource, req.GetKubernetesCluster(), clusterURI))
}

return response, nil
Expand Down Expand Up @@ -99,7 +99,7 @@ func newAPIKube(kube clusters.Kube) *api.Kube {
}

func newApiKubeResource(resource *types.KubernetesResourceV1, kubeCluster string, resourceURI uri.ResourceURI) *api.KubeResource {
uiLabels := ui.MakeUILabelsWithoutInternalPrefixes(resource.GetStaticLabels())
uiLabels := ui.MakeLabelsWithoutInternalPrefixes(resource.GetStaticLabels())
apiLabels := APILabels{}
for _, uiLabel := range uiLabels {
apiLabels = append(apiLabels, &api.Label{
Expand All @@ -109,7 +109,7 @@ func newApiKubeResource(resource *types.KubernetesResourceV1, kubeCluster string
}

return &api.KubeResource{
Uri: resourceURI.AppendKubeResource(resource.GetKind(), kubeCluster, resource.GetName()).String(),
Uri: resourceURI.AppendKubeResourceNamespace(kubeCluster, resource.GetName()).String(),
Kind: resource.GetKind(),
Name: resource.GetName(),
Labels: apiLabels,
Expand Down
14 changes: 5 additions & 9 deletions lib/teleterm/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -823,22 +823,18 @@ func (s *Service) ListKubernetesResources(ctx context.Context, clusterURI uri.Re
}

var proxyGRPCClient kubeproto.KubeServiceClient
var resources *kubeproto.ListKubernetesResourcesResponse

err = clusters.AddMetadataToRetryableError(ctx, func() error {
proxyGRPCClient, err = tc.NewKubernetesServiceClient(ctx, cluster.Name)
return trace.Wrap(err)
})
if err != nil {
return nil, trace.Wrap(err)
}

var resources *kubeproto.ListKubernetesResourcesResponse
if err != nil {
return trace.Wrap(err)
}

err = clusters.AddMetadataToRetryableError(ctx, func() error {
resources, err = proxyGRPCClient.ListKubernetesResources(ctx, &kubeproto.ListKubernetesResourcesRequest{
ResourceType: req.GetResourceType(),
Limit: req.GetLimit(),
StartKey: req.GetStartKey(),
StartKey: req.GetNextKey(),
PredicateExpression: req.GetPredicateExpression(),
SearchKeywords: client.ParseSearchKeywords(req.GetSearchKeywords(), ' '),
UseSearchAsRoles: req.GetUseSearchAsRoles(),
Expand Down
89 changes: 89 additions & 0 deletions lib/ui/labels.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Teleport
* Copyright (C) 2024 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package ui

import (
"sort"
"strings"

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

// Label describes label for webapp
type Label struct {
// Name is this label name
Name string `json:"name"`
// Value is this label value
Value string `json:"value"`
}

// MakeLabelsWithoutInternalPrefixes is a function that transforms map[string]string arguments passed to it to sorted slice of Labels.
// It also removes all Teleport internal labels from output.
func MakeLabelsWithoutInternalPrefixes(labelMaps ...map[string]string) []Label {
length := 0
for _, labelMap := range labelMaps {
length += len(labelMap)
}

labels := make([]Label, 0, length)

for _, labelMap := range labelMaps {
for name, value := range labelMap {
if strings.HasPrefix(name, types.TeleportInternalLabelPrefix) ||
strings.HasPrefix(name, types.TeleportHiddenLabelPrefix) {
continue
}

labels = append(labels, Label{Name: name, Value: value})
}
}

sort.Sort(sortedLabels(labels))

return labels
}

// sortedLabels is a sort wrapper that sorts labels by name
type sortedLabels []Label

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

func (s sortedLabels) Less(i, j int) bool {
labelA := strings.ToLower(s[i].Name)
labelB := strings.ToLower(s[j].Name)

for _, sortName := range types.BackSortedLabelPrefixes {
name := strings.ToLower(sortName)
if strings.Contains(labelA, name) && !strings.Contains(labelB, name) {
return false // labelA should be at the end
}
if !strings.Contains(labelA, name) && strings.Contains(labelB, name) {
return true // labelB should be at the end
}
}

// If neither label contains any of the sendToBackOfSortNames, sort them as usual
return labelA < labelB
}

func (s sortedLabels) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
Loading

0 comments on commit 5ee774f

Please sign in to comment.