Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 96 additions & 96 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,111 +11,111 @@ jobs:
build-darwin-arm:
runs-on: macos-latest
steps:
- name: Set up Go
uses: actions/setup-go@v4.1.0
with:
go-version: "1.22.0"

- name: Check out Code
uses: actions/checkout@v4.0.0

- name: Build
run: |
cd src
go build -v -ldflags "-X main.Version=${{ inputs.version }}" -o kubectl-browse-pvc .
mv kubectl-browse-pvc ..

- name: Fix permissions
run: chmod +x ./kubectl-browse-pvc

- name: Upload Artifact
uses: actions/upload-artifact@v4
with:
name: kubectl-browse-pvc-darwin-arm
path: |
./kubectl-browse-pvc
LICENSE
- name: Set up Go
uses: actions/setup-go@v4.1.0
with:
go-version: "1.22.0"

- name: Check out Code
uses: actions/checkout@v4.0.0

- name: Build
run: |
cd src
go build -v -ldflags "-X main.Version=${{ inputs.version }}" -o kubectl-browse-pvc cmd/browse-pvc/main.go
mv kubectl-browse-pvc ..

- name: Fix permissions
run: chmod +x ./kubectl-browse-pvc

- name: Upload Artifact
uses: actions/upload-artifact@v4
with:
name: kubectl-browse-pvc-darwin-arm
path: |
./kubectl-browse-pvc
LICENSE

build-darwin-x86_64:
runs-on: macos-13
steps:
- name: Set up Go
uses: actions/setup-go@v4.1.0
with:
go-version: "1.22.0"

- name: Check out Code
uses: actions/checkout@v4.0.0

- name: Build
run: |
cd src
go build -v -ldflags "-X main.Version=${{ inputs.version }}" -o kubectl-browse-pvc .
mv kubectl-browse-pvc ..

- name: Fix permissions
run: chmod +x ./kubectl-browse-pvc

- name: Upload Artifact
uses: actions/upload-artifact@v4
with:
name: kubectl-browse-pvc-darwin-x86_64
path: |
./kubectl-browse-pvc
LICENSE
- name: Set up Go
uses: actions/setup-go@v4.1.0
with:
go-version: "1.22.0"

- name: Check out Code
uses: actions/checkout@v4.0.0

- name: Build
run: |
cd src
go build -v -ldflags "-X main.Version=${{ inputs.version }}" -o kubectl-browse-pvc cmd/browse-pvc/main.go
mv kubectl-browse-pvc ..

- name: Fix permissions
run: chmod +x ./kubectl-browse-pvc

- name: Upload Artifact
uses: actions/upload-artifact@v4
with:
name: kubectl-browse-pvc-darwin-x86_64
path: |
./kubectl-browse-pvc
LICENSE

build-linux-x86_64:
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v4.1.0
with:
go-version: "1.22.0"

- name: Check out Code
uses: actions/checkout@v4.0.0

- name: Build
run: |
cd src
go build -v -ldflags "-X main.Version=${{ inputs.version }}" -o kubectl-browse-pvc .
mv kubectl-browse-pvc ..

- name: Fix permissions
run: chmod +x ./kubectl-browse-pvc

- name: Upload Artifact
uses: actions/upload-artifact@v4
with:
name: kubectl-browse-pvc-linux-x86_64
path: |
./kubectl-browse-pvc
LICENSE
- name: Set up Go
uses: actions/setup-go@v4.1.0
with:
go-version: "1.22.0"

- name: Check out Code
uses: actions/checkout@v4.0.0

- name: Build
run: |
cd src
go build -v -ldflags "-X main.Version=${{ inputs.version }}" -o kubectl-browse-pvc cmd/browse-pvc/main.go
mv kubectl-browse-pvc ..

- name: Fix permissions
run: chmod +x ./kubectl-browse-pvc

- name: Upload Artifact
uses: actions/upload-artifact@v4
with:
name: kubectl-browse-pvc-linux-x86_64
path: |
./kubectl-browse-pvc
LICENSE

build-linux-arm:
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v4.1.0
with:
go-version: "1.22.0"

- name: Check out Code
uses: actions/checkout@v4.0.0

- name: Build
run: |
cd src
GOARCH=arm64 go build -v -ldflags "-X main.Version=${{ inputs.version }}" -o kubectl-browse-pvc .
mv kubectl-browse-pvc ..

- name: Fix permissions
run: chmod +x ./kubectl-browse-pvc

- name: Upload Artifact
uses: actions/upload-artifact@v4
with:
name: kubectl-browse-pvc-linux-arm
path: |
./kubectl-browse-pvc
LICENSE
- name: Set up Go
uses: actions/setup-go@v4.1.0
with:
go-version: "1.22.0"

- name: Check out Code
uses: actions/checkout@v4.0.0

- name: Build
run: |
cd src
GOARCH=arm64 go build -v -ldflags "-X main.Version=${{ inputs.version }}" -o kubectl-browse-pvc cmd/browse-pvc/main.go
mv kubectl-browse-pvc ..

- name: Fix permissions
run: chmod +x ./kubectl-browse-pvc

- name: Upload Artifact
uses: actions/upload-artifact@v4
with:
name: kubectl-browse-pvc-linux-arm
path: |
./kubectl-browse-pvc
LICENSE
49 changes: 41 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,61 @@
# kubectl-browse-pvc

Kubectl plugin to browse a Kubernetes PVC from the command line

I constantly found myself spinning up dummy pods to exec into them so I could browse a PVC, this takes a few steps out of creating dummy pods to check out the contents of a PVC.
I constantly found myself spinning up dummy pods to exec into them so I could browse a PVC, this takes a few steps out of creating dummy pods to check out the contents of a PVC.

## Installation

Install via krew
```

```sh
kubectl krew install browse-pvc
```

Usage
## Usage

```
```sh
kubectl browse-pvc <pvc-name>
```
On a PVC. The tool spins up a pod that mounts the PVC and then execs into it allowing you to modify the contents of the PVC. The Job finishes and cleans up the pod when you disconnect.

On a PVC. The tool spins up a pod that mounts the PVC and then execs into it allowing you to modify the contents of the PVC. The Job finishes and cleans up the pod when you disconnect.

Commands can be described to run a command instead of popping a shell

```sh
kubectl browse-pvc <pvc-name> -- <command>
```
kubectl browse-pvc <pvc-name> -- <command>

A User ID can be described to set the user the container runs as

```sh
kubectl browse-pvc -u 1000 <pvc-name>
```

### Configuring auto-completion

A User ID can be described to set the user the container runs as
```sh
cat > kubectl_browse-pvc <<EOF
#!/usr/bin/env sh

# Call the __complete command passing it all arguments
kubectl browse-pvc __complete "\$@"
EOF

chmod +x kubectl_browse-pvc

# Important: the following command may require superuser permission
sudo mv kubectl_browse-pvc /usr/local/bin
```
kubectl browse-pvc -u 1000 <pvc-name>

## Dev

### Test

```sh
go test -v github.com/clbx/kubectl-browse-pvc/<MODULE>
```

Example: `go test -v github.com/clbx/kubectl-browse-pvc/internal/utils`

### Build
45 changes: 25 additions & 20 deletions src/main.go → cmd/browse-pvc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"time"

"github.com/briandowns/spinner"
"github.com/clbx/kubectl-browse-pvc/internal/monitor"
"github.com/clbx/kubectl-browse-pvc/internal/utils"
"github.com/spf13/cobra"
"golang.org/x/term"

Expand All @@ -29,6 +31,8 @@ var image string
var Version string
var containerUser int

var validCommands = []string{"image", "container-user"}

func main() {

var kubeConfigFlags = genericclioptions.NewConfigFlags(true)
Expand All @@ -38,11 +42,12 @@ func main() {
}

var rootCmd = &cobra.Command{
Use: "kubectl-browse-pvc <PVC-NAME> [-- COMMAND [ARGS...]]",
Short: "Kubernetes PVC Browser",
Long: `Kubernetes PVC Browser`,
Version: Version,
Args: cobra.MinimumNArgs(1),
Use: "kubectl-browse-pvc <PVC-NAME> [-- COMMAND [ARGS...]]",
Short: "Kubernetes PVC Browser",
Long: `Kubernetes PVC Browser`,
Version: Version,
Args: cobra.MinimumNArgs(1),
ValidArgs: validCommands,
Run: func(cmd *cobra.Command, args []string) {
pvcName := args[0]
commandArgs := args[1:]
Expand Down Expand Up @@ -97,7 +102,7 @@ func browseCommand(kubeConfigFlags *genericclioptions.ConfigFlags, pvcName strin
log.Fatalf("Failed to get pods: %v", err)
}

attachedPod := findPodByPVC(*nsPods, *targetPvc)
attachedPod := utils.FindPodByPVC(*nsPods, *targetPvc)

manyAccessMode := false
for _, mode := range targetPvc.Spec.AccessModes {
Expand All @@ -114,18 +119,18 @@ func browseCommand(kubeConfigFlags *genericclioptions.ConfigFlags, pvcName strin
node = attachedPod.Spec.NodeName
}

options := &PodOptions{
image: image,
namespace: namespace,
pvc: *targetPvc,
cmd: []string{"/bin/sh", "-c", "--"},
args: commandArgs,
node: node,
user: int64(containerUser),
options := &utils.PodOptions{
Image: image,
Namespace: namespace,
Pvc: *targetPvc,
Cmd: []string{"/bin/sh", "-c", "--"},
Args: commandArgs,
Node: node,
User: int64(containerUser),
}

// Build the Job
pvcbGetJob := buildPvcbGetJob(*options)
pvcbGetJob := utils.BuildPvcbGetJob(*options)
// Create Job
pvcbGetJob, err = clientset.BatchV1().Jobs(namespace).Create(context.TODO(), pvcbGetJob, metav1.CreateOptions{})

Expand Down Expand Up @@ -203,7 +208,7 @@ func browseCommand(kubeConfigFlags *genericclioptions.ConfigFlags, pvcName strin
Post().
Resource("pods").
Name(pod.Name).
Namespace(options.namespace).
Namespace(options.Namespace).
SubResource("exec").
VersionedParams(&corev1.PodExecOptions{
Command: []string{"sh", "-c", "cd /mnt && (ash || bash || sh)"},
Expand All @@ -224,15 +229,15 @@ func browseCommand(kubeConfigFlags *genericclioptions.ConfigFlags, pvcName strin
}
defer term.Restore(0, oldState)

terminalSizeQueue := &sizeQueue{
resizeChan: make(chan remotecommand.TerminalSize, 1),
stopResizing: make(chan struct{}),
terminalSizeQueue := &monitor.SizeQueue{
ResizeChan: make(chan remotecommand.TerminalSize, 1),
StopResizing: make(chan struct{}),
}

// prime with initial term size
width, height, err := term.GetSize(int(os.Stdout.Fd()))
if err == nil {
terminalSizeQueue.resizeChan <- remotecommand.TerminalSize{Width: uint16(width), Height: uint16(height)}
terminalSizeQueue.ResizeChan <- remotecommand.TerminalSize{Width: uint16(width), Height: uint16(height)}
}

go terminalSizeQueue.MonitorSize()
Expand Down
1 change: 1 addition & 0 deletions src/go.mod → go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ require (
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/btree v1.0.1 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/google/uuid v1.3.0 // indirect
Expand Down
File renamed without changes.
Loading