Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix get all command #135

Merged
merged 1 commit into from
Jul 25, 2024
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
19 changes: 18 additions & 1 deletion get/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
32 changes: 32 additions & 0 deletions get/all_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
},
},
},
}
}