mirror of
https://github.com/replicatedhq/troubleshoot.git
synced 2026-08-27 00:37:20 +00:00
feat(support-bundle): check if the cluster IsNamespacedScopeRBAC and use current namespace (#1055)
feat(support-bundle): add IsNamespacedScope check
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user