Skip to content

fix(xray): disable edit/delete actions in XRay view when readonly mode is enabled#3858

Open
MichaelYLR wants to merge 2 commits intoderailed:masterfrom
MichaelYLR:MichaelYLR-patch-1
Open

fix(xray): disable edit/delete actions in XRay view when readonly mode is enabled#3858
MichaelYLR wants to merge 2 commits intoderailed:masterfrom
MichaelYLR:MichaelYLR-patch-1

Conversation

@MichaelYLR
Copy link

Fixes: #3822

Description
Fixed an issue where editing or deleting resources was still allowed in the XRay view while K9s was running in read-only mode (--readonly). The system now properly disables edit-related operations when in read-only mode.

Related Issue
#3822

Changes
Added a check for app.Config.ReadOnly() within the edit, delete, attach, and shell action handlers in the XRay view.
These functions are now disabled if the application is in read-only mode.

How to Test
Compile and run K9s: ./k9s --readonly
Enter the XRay view for a pod using the command :xray pod namespace_name.
Attempt to press e (edit) or d (delete).
Expected Result: The system will not enter the edit or delete workflows.

before:
image

after:
image

Fixed an issue where editing or deleting resources was still allowed in the XRay view while K9s was running in read-only mode (--readonly). The system now properly disables edit-related operations when in read-only mode.
Copy link
Owner

@derailed derailed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MichaelYLR Thank you for this update!

}

if client.Can(x.meta.Verbs, "edit") {
if !x.app.Config.IsReadOnly() && client.Can(x.meta.Verbs, "edit") {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MichaelYLR Let's follow the same pattern as we do for other views see bindDangerousKeys

@derailed derailed added in-progress Mark issue as being worked on needs-tlc Pr needs additional updates labels Mar 3, 2026
@gspeter-max
Copy link

Excellent fix for the readonly mode issue! 🎯

I noticed one important improvement that would make this PR even better by following the established pattern in the codebase.

Suggestion: Restore the client.Can() checks

Your current code:

if \!x.app.Config.IsReadOnly() {
    x.bindDangerousKeys(aa)  // Adds edit/delete for ALL resources
}

Issue: This adds edit/delete actions even for resources that don't support them (like Pods and Events).

Why Both Checks Matter

These two checks protect against different things:

  1. client.Can(meta.Verbs, "edit") → "Does the Kubernetes API support editing this resource type?"

    • Example: ConfigMap ✅ | Pod ❌ (immutable) | Event ❌ (immutable)
  2. IsReadOnly() → "Is K9s preventing modifications?"

    • Example: k9s --readonly flag

Established Pattern in the Codebase

See internal/view/browser.go:621-635:

if \!b.app.Config.IsReadOnly() {
    if client.Can(b.meta.Verbs, "edit") {
        aa.Add(ui.KeyE, ...)
    }
    if client.Can(b.meta.Verbs, "delete") {
        aa.Add(tcell.KeyCtrlD, ...)
    }
}

Every view (browser, container, pod, etc.) uses BOTH checks in this order.

Recommended Fix

In bindDangerousKeys(), restore the verb checks:

func (x *Xray) bindDangerousKeys(aa *ui.KeyActions) {
    if client.Can(x.meta.Verbs, "edit") {
        aa.Add(ui.KeyE, ui.NewKeyActionWithOpts("Edit", x.editCmd,
            ui.ActionOpts{
                Visible:   true,
                Dangerous: true,
            }))
    }
    if client.Can(x.meta.Verbs, "delete") {
        aa.Add(tcell.KeyCtrlD, ui.NewKeyActionWithOpts("Delete", x.deleteCmd,
            ui.ActionOpts{
                Visible:   true,
                Dangerous: true,
            }))
    }
}

This ensures:

  • ✅ Read-only mode blocks all edits (your fix!)
  • ✅ Immutable resources (Pods, Events) don't show edit options
  • ✅ Consistent with every other view in the codebase
  • ✅ Correct user experience (no confusing "Edit" on uneditable resources)

Summary: Great work fixing the readonly bug! Just add back the client.Can() checks to match the established pattern and prevent showing edit options on immutable resources.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

in-progress Mark issue as being worked on needs-tlc Pr needs additional updates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Even in read-only mode of the XRay view, editing or deleting Kubernetes resources is possible

3 participants