refact client apis
parent
1e0f48b9b3
commit
0b87a4e26c
|
|
@ -26,12 +26,18 @@ const NA = "n/a"
|
||||||
var supportedMetricsAPIVersions = []string{"v1beta1"}
|
var supportedMetricsAPIVersions = []string{"v1beta1"}
|
||||||
|
|
||||||
type (
|
type (
|
||||||
|
// GKV tracks api resource version info.
|
||||||
|
GKV struct {
|
||||||
|
Group, Kind, Version string
|
||||||
|
}
|
||||||
|
|
||||||
// APIGroup represents a K8s resource descriptor.
|
// APIGroup represents a K8s resource descriptor.
|
||||||
APIGroup struct {
|
APIGroup struct {
|
||||||
Resource string
|
GKV
|
||||||
Group, Kind, Version string
|
|
||||||
Plural, Singular string
|
Resource string
|
||||||
Aliases []string
|
Plural, Singular string
|
||||||
|
Aliases []string
|
||||||
}
|
}
|
||||||
|
|
||||||
// Collection of empty interfaces.
|
// Collection of empty interfaces.
|
||||||
|
|
@ -114,10 +120,9 @@ func (a *APIClient) CheckNSAccess(n string) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// CanIAccess checks if user has access to a certain resource.
|
func makeSAR(ns, name, resURL string) *authorizationv1.SelfSubjectAccessReview {
|
||||||
func (a *APIClient) canIAccess(ns, name, resURL string, verbs []string) (bool, error) {
|
|
||||||
_, gr := schema.ParseResourceArg(strings.ToLower(resURL))
|
_, gr := schema.ParseResourceArg(strings.ToLower(resURL))
|
||||||
sar := &authorizationv1.SelfSubjectAccessReview{
|
return &authorizationv1.SelfSubjectAccessReview{
|
||||||
Spec: authorizationv1.SelfSubjectAccessReviewSpec{
|
Spec: authorizationv1.SelfSubjectAccessReviewSpec{
|
||||||
ResourceAttributes: &authorizationv1.ResourceAttributes{
|
ResourceAttributes: &authorizationv1.ResourceAttributes{
|
||||||
Namespace: ns,
|
Namespace: ns,
|
||||||
|
|
@ -128,32 +133,24 @@ func (a *APIClient) canIAccess(ns, name, resURL string, verbs []string) (bool, e
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
user, _ := a.Config().CurrentUserName()
|
// CanIAccess checks if user has access to a certain resource.
|
||||||
groups, _ := a.Config().CurrentGroupNames()
|
func (a *APIClient) canIAccess(ns, name, resURL string, verbs []string) (bool, error) {
|
||||||
log.Debug().Msgf("AuthInfo user/groups: %s:%v", user, groups)
|
sar := makeSAR(ns, name, resURL)
|
||||||
|
dial := a.DialOrDie().AuthorizationV1().SelfSubjectAccessReviews()
|
||||||
var (
|
|
||||||
resp *authorizationv1.SelfSubjectAccessReview
|
|
||||||
err error
|
|
||||||
allow bool
|
|
||||||
)
|
|
||||||
for _, v := range verbs {
|
for _, v := range verbs {
|
||||||
sar.Spec.ResourceAttributes.Verb = v
|
sar.Spec.ResourceAttributes.Verb = v
|
||||||
resp, err = a.DialOrDie().AuthorizationV1().SelfSubjectAccessReviews().Create(sar)
|
resp, err := dial.Create(sar)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msgf("CanIAccess")
|
log.Error().Err(err).Msgf("CanIAccess")
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
log.Debug().Msgf("CHECKING ACCESS group:%q|resource:%q|namespace:%q|name:%q, verb:%s access:%t -- %s", gr.Group, gr.Resource, ns, name, v, resp.Status.Allowed, resp.Status.Reason)
|
|
||||||
if !resp.Status.Allowed {
|
if !resp.Status.Allowed {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
allow = true
|
|
||||||
}
|
}
|
||||||
log.Debug().Msgf("GRANT ACCESS? >>> %t", allow)
|
return true, nil
|
||||||
|
|
||||||
return allow, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CurrentNamespaceName return namespace name set via either cli arg or cluster config.
|
// CurrentNamespaceName return namespace name set via either cli arg or cluster config.
|
||||||
|
|
@ -330,16 +327,23 @@ func (a *APIClient) supportsMxServer() bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, discoveredAPIGroup := range apiGroups.Groups {
|
for _, grp := range apiGroups.Groups {
|
||||||
if discoveredAPIGroup.Name != metricsapi.GroupName {
|
if grp.Name != metricsapi.GroupName {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if checkMetricsVersion(grp) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for _, version := range discoveredAPIGroup.Versions {
|
return false
|
||||||
for _, supportedVersion := range supportedMetricsAPIVersions {
|
}
|
||||||
if version.Version == supportedVersion {
|
|
||||||
return true
|
func checkMetricsVersion(grp metav1.APIGroup) bool {
|
||||||
}
|
for _, version := range grp.Versions {
|
||||||
|
for _, supportedVersion := range supportedMetricsAPIVersions {
|
||||||
|
if version.Version == supportedVersion {
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -14,13 +14,19 @@ const (
|
||||||
helpTitleFmt = " [aqua::b]%s "
|
helpTitleFmt = " [aqua::b]%s "
|
||||||
)
|
)
|
||||||
|
|
||||||
type helpView struct {
|
type (
|
||||||
*tview.TextView
|
helpItem struct {
|
||||||
|
key, description string
|
||||||
|
}
|
||||||
|
|
||||||
app *appView
|
helpView struct {
|
||||||
current igniter
|
*tview.TextView
|
||||||
actions keyActions
|
|
||||||
}
|
app *appView
|
||||||
|
current igniter
|
||||||
|
actions keyActions
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
func newHelpView(app *appView, current igniter) *helpView {
|
func newHelpView(app *appView, current igniter) *helpView {
|
||||||
v := helpView{TextView: tview.NewTextView(), app: app, actions: make(keyActions)}
|
v := helpView{TextView: tview.NewTextView(), app: app, actions: make(keyActions)}
|
||||||
|
|
@ -58,10 +64,41 @@ func (v *helpView) backCmd(evt *tcell.EventKey) *tcell.EventKey {
|
||||||
func (v *helpView) init(_ context.Context, _ string) {
|
func (v *helpView) init(_ context.Context, _ string) {
|
||||||
v.resetTitle()
|
v.resetTitle()
|
||||||
|
|
||||||
type helpItem struct {
|
v.showGeneral()
|
||||||
key, description string
|
v.showNav()
|
||||||
}
|
v.showHelp()
|
||||||
|
v.app.setHints(v.hints())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *helpView) showHelp() {
|
||||||
|
views := []helpItem{
|
||||||
|
{"?", "Help"},
|
||||||
|
{"Ctrl-a", "Aliases view"},
|
||||||
|
}
|
||||||
|
fmt.Fprintf(v, "️️\n😱 [aqua::b]%s\n", "Help")
|
||||||
|
for _, h := range views {
|
||||||
|
v.printHelp(h.key, h.description)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *helpView) showNav() {
|
||||||
|
navigation := []helpItem{
|
||||||
|
{"g", "Goto Top"},
|
||||||
|
{"G", "Goto Bottom"},
|
||||||
|
{"Ctrl-b", "Page Down"},
|
||||||
|
{"Ctrl-f", "Page Up"},
|
||||||
|
{"h", "Left"},
|
||||||
|
{"l", "Right"},
|
||||||
|
{"k", "Up"},
|
||||||
|
{"j", "Down"},
|
||||||
|
}
|
||||||
|
fmt.Fprintf(v, "\n🤖 [aqua::b]%s\n", "View Navigation")
|
||||||
|
for _, h := range navigation {
|
||||||
|
v.printHelp(h.key, h.description)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (v *helpView) showGeneral() {
|
||||||
general := []helpItem{
|
general := []helpItem{
|
||||||
{":<cmd>", "Command mode"},
|
{":<cmd>", "Command mode"},
|
||||||
{"/<term>", "Filter mode"},
|
{"/<term>", "Filter mode"},
|
||||||
|
|
@ -77,31 +114,6 @@ func (v *helpView) init(_ context.Context, _ string) {
|
||||||
for _, h := range general {
|
for _, h := range general {
|
||||||
v.printHelp(h.key, h.description)
|
v.printHelp(h.key, h.description)
|
||||||
}
|
}
|
||||||
|
|
||||||
navigation := []helpItem{
|
|
||||||
{"g", "Goto Top"},
|
|
||||||
{"G", "Goto Bottom"},
|
|
||||||
{"Ctrl-b", "Page Down"},
|
|
||||||
{"Ctrl-f", "Page Up"},
|
|
||||||
{"h", "Left"},
|
|
||||||
{"l", "Right"},
|
|
||||||
{"k", "Up"},
|
|
||||||
{"j", "Down"},
|
|
||||||
}
|
|
||||||
fmt.Fprintf(v, "\n🤖 [aqua::b]%s\n", "View Navigation")
|
|
||||||
for _, h := range navigation {
|
|
||||||
v.printHelp(h.key, h.description)
|
|
||||||
}
|
|
||||||
|
|
||||||
views := []helpItem{
|
|
||||||
{"?", "Help"},
|
|
||||||
{"Ctrl-a", "Aliases view"},
|
|
||||||
}
|
|
||||||
fmt.Fprintf(v, "️️\n😱 [aqua::b]%s\n", "Help")
|
|
||||||
for _, h := range views {
|
|
||||||
v.printHelp(h.key, h.description)
|
|
||||||
}
|
|
||||||
v.app.setHints(v.hints())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v *helpView) printHelp(key, desc string) {
|
func (v *helpView) printHelp(key, desc string) {
|
||||||
|
|
|
||||||
|
|
@ -100,7 +100,6 @@ func (v *logsView) doLoad(path, co string, prevLogs bool) error {
|
||||||
|
|
||||||
l := v.CurrentPage().Item.(*logView)
|
l := v.CurrentPage().Item.(*logView)
|
||||||
l.logs.Clear()
|
l.logs.Clear()
|
||||||
l.path = path
|
|
||||||
l.setTitle(path, co)
|
l.setTitle(path, co)
|
||||||
|
|
||||||
c := make(chan string, 10)
|
c := make(chan string, 10)
|
||||||
|
|
|
||||||
|
|
@ -50,9 +50,11 @@ func allCRDs(c k8s.Connection, m map[string]resCmd) {
|
||||||
ff := crd.ExtFields()
|
ff := crd.ExtFields()
|
||||||
|
|
||||||
grp := k8s.APIGroup{
|
grp := k8s.APIGroup{
|
||||||
Group: ff["group"].(string),
|
GKV: k8s.GKV{
|
||||||
Kind: ff["kind"].(string),
|
Group: ff["group"].(string),
|
||||||
Version: ff["version"].(string),
|
Kind: ff["kind"].(string),
|
||||||
|
Version: ff["version"].(string),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
res := resCmd{
|
res := resCmd{
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue