From 8e5b6d76dfa973a1e84debe7f924ccf470575564 Mon Sep 17 00:00:00 2001 From: sam lockart <35046326+alam0rt@users.noreply.github.com> Date: Sat, 23 Nov 2024 05:26:05 +0000 Subject: [PATCH] Skip forbidden resources as they are rarely required (#55) Co-authored-by: Ahmet Alp Balkan --- .github/workflows/ci.yml | 2 +- .github/workflows/release.yml | 2 +- cmd/kubectl-tree/query.go | 12 ++++++++++-- go.mod | 2 +- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 93cac72..8a67bee 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,7 @@ jobs: - name: Setup Go uses: actions/setup-go@v1 with: - go-version: '1.20' + go-version-file: 'go.mod' - name: GoReleaser uses: goreleaser/goreleaser-action@v1 with: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 80a730f..739f2ad 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,7 +12,7 @@ jobs: - name: Setup Go uses: actions/setup-go@v1 with: - go-version: '1.20' + go-version-file: 'go.mod' - name: GoReleaser uses: goreleaser/goreleaser-action@v1 with: diff --git a/cmd/kubectl-tree/query.go b/cmd/kubectl-tree/query.go index 0591d2c..3e9517b 100644 --- a/cmd/kubectl-tree/query.go +++ b/cmd/kubectl-tree/query.go @@ -2,10 +2,12 @@ package main import ( "context" + stderrors "errors" "fmt" "sync" "time" + "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/client-go/dynamic" @@ -33,8 +35,14 @@ func getAllResources(client dynamic.Interface, apis []apiResource, allNs bool) ( klog.V(4).Infof("[query api] start: %s", a.GroupVersionResource()) v, err := queryAPI(client, a, allNs) if err != nil { - klog.V(4).Infof("[query api] error querying: %s, error=%v", a.GroupVersionResource(), err) - errResult = err + if errors.IsForbidden(err) { + // should not fail the overall process, but print an info message indicating the permission issue + klog.V(4).Infof("[query api] skipping forbidden resource: %s", a.GroupVersionResource()) + klog.Infof("cannot query %s (forbidden), omitting from the tree", a.GroupVersionResource().GroupResource()) + } else { + klog.V(4).Infof("[query api] error querying: %s, error=%v", a.GroupVersionResource(), err) + errResult = stderrors.Join(errResult, fmt.Errorf("failed to query the %s resources: %w", a.GroupVersionResource(), err)) + } return } mu.Lock() diff --git a/go.mod b/go.mod index fd52224..8e9884c 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/ahmetb/kubectl-tree -go 1.20 +go 1.22 require ( github.com/fatih/color v1.15.0