feat(support-bundle): check if the cluster IsNamespacedScopeRBAC and use current namespace (#1055)

feat(support-bundle): add IsNamespacedScope check
This commit is contained in:
Dexter Yan
2023-03-22 11:33:54 -04:00
committed by GitHub
parent 26d176a994
commit 79f8e6efab
2 changed files with 50 additions and 5 deletions
+16 -5
View File
@@ -114,11 +114,6 @@ func runTroubleshoot(v *viper.Viper, arg []string) error {
return errors.Wrap(err, "unable to parse selector")
}
namespace := ""
if v.GetString("namespace") != "" {
namespace = v.GetString("namespace")
}
config, err := k8sutil.GetRESTConfig()
if err != nil {
return errors.Wrap(err, "failed to convert kube flags to rest config")
@@ -129,6 +124,22 @@ func runTroubleshoot(v *viper.Viper, arg []string) error {
return errors.Wrap(err, "failed to convert create k8s client")
}
namespace := ""
if v.GetString("namespace") != "" {
namespace = v.GetString("namespace")
} else {
IsNamespacedScopeRBAC, err := k8sutil.IsNamespacedScopeRBAC(client)
if err != nil {
return errors.Wrap(err, "failed to check if cluster is namespaced")
}
if !IsNamespacedScopeRBAC {
kubeconfig := k8sutil.GetKubeconfig()
namespace, _, _ = kubeconfig.Namespace()
}
}
var bundlesFromCluster []string
// Search cluster for Troubleshoot objects in cluster
+34
View File
@@ -0,0 +1,34 @@
package k8sutil
import (
"context"
authorizationv1 "k8s.io/api/authorization/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
func IsNamespacedScopeRBAC(client kubernetes.Interface) (bool, error) {
ctx := context.Background()
sar := &authorizationv1.SelfSubjectAccessReview{
Spec: authorizationv1.SelfSubjectAccessReviewSpec{
ResourceAttributes: &authorizationv1.ResourceAttributes{
Namespace: "",
Verb: "list",
Resource: "secrets,configmaps",
},
},
}
resp, err := client.AuthorizationV1().SelfSubjectAccessReviews().Create(ctx, sar, metav1.CreateOptions{})
if err != nil {
return false, err
}
if resp.Status.Allowed {
return true, nil
} else {
return false, nil
}
}