From e6843a87b19e9ea41a9da280f5e3d7d1280a5bbf Mon Sep 17 00:00:00 2001 From: Sebastian Nickel Date: Thu, 25 Jul 2024 12:31:38 +0200 Subject: [PATCH] fix get all command --- get/all.go | 19 ++++++++++++++++++- get/all_test.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/get/all.go b/get/all.go index d5fb565..89f130a 100644 --- a/get/all.go +++ b/get/all.go @@ -9,10 +9,12 @@ import ( "strings" "text/tabwriter" + infrastructure "github.com/ninech/apis/infrastructure/v1alpha1" management "github.com/ninech/apis/management/v1alpha1" meta "github.com/ninech/apis/meta/v1alpha1" "github.com/ninech/nctl/api" "github.com/ninech/nctl/internal/format" + kerrors "k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/runtime/schema" @@ -86,7 +88,9 @@ func (cmd *allCmd) getProjectContent(ctx context.Context, client *api.Client, pr // types we handle them as warnings to be able to // return as many resources as we can if err := client.List(ctx, u, runtimeclient.InNamespace(project)); err != nil { - warnings = append(warnings, err.Error()) + if !kerrors.IsForbidden(err) { + warnings = append(warnings, err.Error()) + } continue } // we convert to a list of pointers so that we can @@ -159,12 +163,25 @@ OUTER: return result, nil } +func excludeListType(gvk schema.GroupVersionKind) bool { + // ClusterData is a non-namespaced resource and used to allow + // connecting to deplo.io application replicas. + if strings.EqualFold(gvk.Kind, infrastructure.ClusterDataKind+"list") && + strings.EqualFold(gvk.Group, infrastructure.Group) { + return true + } + return false +} + func nineListTypes(s *runtime.Scheme) []schema.GroupVersionKind { var lists []schema.GroupVersionKind for gvk := range s.AllKnownTypes() { if !strings.HasSuffix(strings.ToLower(gvk.Kind), "list") { continue } + if excludeListType(gvk) { + continue + } if strings.HasSuffix(strings.ToLower(gvk.Group), "nine.ch") { lists = append(lists, gvk) } diff --git a/get/all_test.go b/get/all_test.go index 4745613..ecfc204 100644 --- a/get/all_test.go +++ b/get/all_test.go @@ -199,6 +199,19 @@ staging melon Release apps.nine.ch kinds: []string{"jackofalltrades"}, errorExpected: true, }, + "excluded list kinds are not shown": { + projects: test.Projects(organization, "dev"), + objects: []client.Object{ + testApplication("banana", "dev"), testRelease("pear", "dev"), + testClusterData(), + }, + outputFormat: full, + allProjects: true, + output: `PROJECT NAME KIND GROUP +dev banana Application apps.nine.ch +dev pear Release apps.nine.ch +`, + }, } { t.Run(name, func(t *testing.T) { testCase := testCase @@ -284,3 +297,22 @@ func testCluster(name, project string) *infra.KubernetesCluster { Spec: infra.KubernetesClusterSpec{}, } } + +func testClusterData() *infra.ClusterData { + return &infra.ClusterData{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test", + }, + TypeMeta: metav1.TypeMeta{ + Kind: infra.ClusterDataKind, + APIVersion: infra.SchemeGroupVersion.String(), + }, + Spec: infra.ClusterDataSpec{ + ForProvider: infra.ClusterDataParameters{ + ClusterReference: meta.Reference{ + Name: "test", + }, + }, + }, + } +}