Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding --show-tree flag to both "./kubestr browse pvc" & "./kubestr browse snapshot" commands #278

Merged
merged 43 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
36882b5
Adding the kubestr browse pvc command. Handling kubestr browse suppor…
shlokc9 Jul 24, 2024
0927b25
Adding browse snapshot command. Updating browse command to browse pvc…
shlokc9 Jul 24, 2024
c2d3570
chore(deps): bump github/codeql-action in the github-actions group (#…
dependabot[bot] Jul 22, 2024
b6bf82c
chore(deps): bump docker/build-push-action in the docker group (#273)
dependabot[bot] Jul 22, 2024
32cecd0
Merge branch 'kastenhq:master' into browse-pvc-snapshot
shlokc9 Jul 24, 2024
a6ac177
Adding --show-tree flag for browse snapshot & browse pvc commands
shlokc9 Jul 24, 2024
e2aa023
Removing unused snapshot function parameter in cleanup
shlokc9 Jul 25, 2024
1f10988
Adding KubeExecutor Exec helper function to execute tree command
shlokc9 Jul 26, 2024
34744f6
Adding --show-tree logic in pvc_inspector.go
shlokc9 Jul 26, 2024
cbed58b
Adding --show-tree logic in snapshot_inspector.go
shlokc9 Jul 26, 2024
2f8e255
Printing out the tree structure for --show-tree
shlokc9 Jul 27, 2024
9098f94
Updating mock tests for new code changes
shlokc9 Jul 27, 2024
a0c97b9
Updating mount path in container args for creating a browse pod
shlokc9 Jul 29, 2024
16e29c4
Updating the CSITestSuite.TestCreateInspectorApplication for changes …
shlokc9 Jul 29, 2024
8b44243
Adding Deprecated msg to the 'browse' command
shlokc9 Jul 29, 2024
0bb5fd5
Adding mock tests for SnapshotBrowserStepper
shlokc9 Jul 27, 2024
778b71c
Adding fake tests for snapshot_inspector.go
shlokc9 Jul 29, 2024
b17344a
Renamed testcase CSITestSuite.TestCreateInspectorApplication to TestC…
shlokc9 Jul 29, 2024
f848045
Adding snapshot_inspector_steps_test.go
shlokc9 Jul 29, 2024
78d4981
Updating mock tests for new code changes
shlokc9 Jul 27, 2024
74bd34d
Updating the mount paths in CSITestSuite.TestCreateInspectorApplicati…
shlokc9 Jul 29, 2024
4e160b1
Updating Deprecated msg for 'browse' command
shlokc9 Jul 30, 2024
62af178
Making namespace, runAsUser & localport flags persistent
shlokc9 Jul 31, 2024
dc4a65e
Removing namespace, runAsUser & localport flags for browse snapshot b…
shlokc9 Jul 31, 2024
2d419ec
Adding --show-tree flag for browse snapshot & browse pvc commands
shlokc9 Jul 24, 2024
0314b8e
Updating namespace flag usage for better understanding
shlokc9 Jul 31, 2024
f63aebf
Removing storage class flag
shlokc9 Jul 31, 2024
e0f5dcf
Adding --show-tree logic in snapshot_inspector.go
shlokc9 Jul 26, 2024
d7b492c
Updating mock objects for SnapshotBrowserStepper
shlokc9 Aug 1, 2024
938e625
Adding --show-tree flag for browse snapshot & browse pvc commands
shlokc9 Jul 24, 2024
127e473
Removing storage class flag
shlokc9 Jul 31, 2024
e781e2e
Adding --show-tree flag for browse snapshot & browse pvc commands
shlokc9 Jul 24, 2024
ffd10ad
Adding --show-tree logic in snapshot_inspector.go
shlokc9 Jul 26, 2024
09e7564
Merge branch 'master' into browse-show-tree
shlokc9 Aug 1, 2024
3ae24b3
Passing showTree var as function argument
shlokc9 Aug 1, 2024
e79535a
Making --show-tree a persistent flag
shlokc9 Aug 1, 2024
df910e2
Removing ShowTree dummy condition
shlokc9 Aug 1, 2024
36b01b0
Merge branch 'master' into browse-show-tree
shlokc9 Aug 2, 2024
1f20ed3
Removing duplicate browseSnapshotCmd
shlokc9 Aug 2, 2024
0d60092
Adding --show-tree flag for browse snapshot & browse pvc commands
shlokc9 Jul 24, 2024
dddd9bf
Making --show-tree a persistent flag
shlokc9 Aug 1, 2024
5a44008
Adding --show-tree flag for browse snapshot & browse pvc commands
shlokc9 Jul 24, 2024
02ec051
Making --show-tree a persistent flag
shlokc9 Aug 1, 2024
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
9 changes: 9 additions & 0 deletions cmd/rootCmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ var (
},
}

showTree bool

browsePvcCmd = &cobra.Command{
Use: "pvc [PVC name]",
Short: "Browse the contents of a CSI PVC via file browser",
Expand All @@ -106,6 +108,7 @@ var (
csiCheckVolumeSnapshotClass,
csiCheckRunAsUser,
browseLocalPort,
shlokc9 marked this conversation as resolved.
Show resolved Hide resolved
showTree,
)
},
}
Expand All @@ -120,6 +123,7 @@ var (
namespace,
csiCheckRunAsUser,
browseLocalPort,
showTree,
)
},
}
Expand Down Expand Up @@ -195,6 +199,7 @@ func init() {
browseCmd.PersistentFlags().StringVarP(&namespace, "namespace", "n", fio.DefaultNS, "The namespace of the resource to browse.")
browseCmd.PersistentFlags().Int64VarP(&csiCheckRunAsUser, "runAsUser", "u", 0, "Runs the inspector pod as a user (int)")
browseCmd.PersistentFlags().IntVarP(&browseLocalPort, "localport", "l", 8080, "The local port to expose the inspector")
browseCmd.PersistentFlags().BoolVarP(&showTree, "show-tree", "t", false, "Prints the contents of given PVC or VolumeSnapshot")

browseCmd.AddCommand(browsePvcCmd)
browsePvcCmd.Flags().StringVarP(&csiCheckVolumeSnapshotClass, "volumesnapshotclass", "v", "", "The name of a VolumeSnapshotClass. (Required)")
Expand Down Expand Up @@ -361,6 +366,7 @@ func CsiPvcBrowse(ctx context.Context,
volumeSnapshotClass string,
runAsUser int64,
localPort int,
showTree bool,
) error {
kubecli, err := kubestr.LoadKubeCli()
if err != nil {
Expand All @@ -382,6 +388,7 @@ func CsiPvcBrowse(ctx context.Context,
VolumeSnapshotClass: volumeSnapshotClass,
RunAsUser: runAsUser,
LocalPort: localPort,
ShowTree: showTree,
})
if err != nil {
fmt.Printf("Failed to run PVC browser (%s)\n", err.Error())
Expand All @@ -394,6 +401,7 @@ func CsiSnapshotBrowse(ctx context.Context,
namespace string,
runAsUser int64,
localPort int,
showTree bool,
) error {
kubecli, err := kubestr.LoadKubeCli()
if err != nil {
Expand All @@ -414,6 +422,7 @@ func CsiSnapshotBrowse(ctx context.Context,
Namespace: namespace,
RunAsUser: runAsUser,
LocalPort: localPort,
ShowTree: showTree,
})
if err != nil {
fmt.Printf("Failed to run Snapshot browser (%s)\n", err.Error())
Expand Down
17 changes: 17 additions & 0 deletions pkg/csi/csi_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -568,3 +568,20 @@ func (p *portforward) PortForwardAPod(req *types.PortForwardAPodRequest) error {
func (p *portforward) FetchRestConfig() (*rest.Config, error) {
return kube.LoadConfig()
}

//go:generate go run github.com/golang/mock/mockgen -destination=mocks/mock_kube_executor.go -package=mocks . KubeExecutor
type KubeExecutor interface {
Exec(ctx context.Context, namespace string, podName string, ContainerName string, command []string) (string, error)
}

type kubeExec struct {
kubeCli kubernetes.Interface
}

func (k *kubeExec) Exec(ctx context.Context, namespace string, podName string, ContainerName string, command []string) (string, error) {
if k.kubeCli == nil {
return "", fmt.Errorf("kubeCli not initialized")
}
stdout, _, err := kankube.Exec(ctx, k.kubeCli, namespace, podName, ContainerName, command, nil)
return stdout, err
}
50 changes: 50 additions & 0 deletions pkg/csi/mocks/mock_kube_executor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions pkg/csi/mocks/mock_pvc_browser_stepper.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions pkg/csi/mocks/mock_snapshot_browser_stepper.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

50 changes: 45 additions & 5 deletions pkg/csi/pvc_inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,18 @@ func (r *PVCBrowseRunner) RunPVCBrowse(ctx context.Context, args *types.PVCBrows
dynCli: r.DynCli,
},
portForwardOps: &portforward{},
kubeExecutor: &kubeExec{
kubeCli: r.KubeCli,
},
cleanerOps: &cleanse{
kubeCli: r.KubeCli,
dynCli: r.DynCli,
},
}
if args.ShowTree {
fmt.Println("Show Tree works for PVC!")
return nil
}
return r.RunPVCBrowseHelper(ctx, args)
}

Expand All @@ -70,19 +77,29 @@ func (r *PVCBrowseRunner) RunPVCBrowseHelper(ctx context.Context, args *types.PV
return errors.Wrap(err, "Failed to validate arguments.")
}

fmt.Println("Taking a snapshot")
fmt.Println("Taking a snapshot.")
snapName := snapshotPrefix + time.Now().Format("20060102150405")
r.snapshot, err = r.browserSteps.SnapshotPVC(ctx, args, snapName)
if err != nil {
return errors.Wrap(err, "Failed to snapshot PVC.")
}

fmt.Println("Creating the file browser application.")
fmt.Println("Creating the browser pod.")
r.pod, r.pvc, err = r.browserSteps.CreateInspectorApplication(ctx, args, r.snapshot, sc)
if err != nil {
return errors.Wrap(err, "Failed to create inspector application.")
}

if args.ShowTree {
fmt.Println("Printing the tree structure from root directory.")
stdout, err := r.browserSteps.ExecuteTreeCommand(ctx, args, r.pod)
if err != nil {
return errors.Wrap(err, "Failed to execute tree command in pod.")
}
fmt.Printf("\n%s\n\n", stdout)
return nil
}

fmt.Println("Forwarding the port.")
err = r.browserSteps.PortForwardAPod(ctx, r.pod, args.LocalPort)
if err != nil {
Expand All @@ -97,6 +114,7 @@ type PVCBrowserStepper interface {
ValidateArgs(ctx context.Context, args *types.PVCBrowseArgs) (*sv1.StorageClass, error)
SnapshotPVC(ctx context.Context, args *types.PVCBrowseArgs, snapshotName string) (*snapv1.VolumeSnapshot, error)
CreateInspectorApplication(ctx context.Context, args *types.PVCBrowseArgs, snapshot *snapv1.VolumeSnapshot, storageClass *sv1.StorageClass) (*v1.Pod, *v1.PersistentVolumeClaim, error)
ExecuteTreeCommand(ctx context.Context, args *types.PVCBrowseArgs, pod *v1.Pod) (string, error)
PortForwardAPod(ctx context.Context, pod *v1.Pod, localPort int) error
Cleanup(ctx context.Context, pvc *v1.PersistentVolumeClaim, pod *v1.Pod, snapshot *snapv1.VolumeSnapshot)
}
Expand All @@ -108,6 +126,7 @@ type pvcBrowserSteps struct {
snapshotCreateOps SnapshotCreator
portForwardOps PortForwarder
cleanerOps Cleaner
kubeExecutor KubeExecutor
SnapshotGroupVersion *metav1.GroupVersionForDiscovery
}

Expand Down Expand Up @@ -194,19 +213,40 @@ func (p *pvcBrowserSteps) CreateInspectorApplication(ctx context.Context, args *
Namespace: args.Namespace,
RunAsUser: args.RunAsUser,
ContainerImage: "filebrowser/filebrowser:v2",
ContainerArgs: []string{"--noauth", "-r", "/data"},
MountPath: "/data",
ContainerArgs: []string{"--noauth", "-r", "/pvc-data"},
MountPath: "/pvc-data",
}
if args.ShowTree {
podArgs = &types.CreatePodArgs{
GenerateName: clonedPodGenerateName,
PVCName: pvc.Name,
Namespace: args.Namespace,
RunAsUser: args.RunAsUser,
ContainerImage: "alpine:3.19",
Command: []string{"/bin/sh"},
ContainerArgs: []string{"-c", "while true; do sleep 3600; done"},
MountPath: "/pvc-data",
}
}
pod, err := p.createAppOps.CreatePod(ctx, podArgs)
if err != nil {
return nil, pvc, errors.Wrap(err, "Failed to create restored Pod")
return nil, pvc, errors.Wrap(err, "Failed to create browse Pod")
}
if err = p.createAppOps.WaitForPodReady(ctx, args.Namespace, pod.Name); err != nil {
return pod, pvc, errors.Wrap(err, "Pod failed to become ready")
}
return pod, pvc, nil
}

func (p *pvcBrowserSteps) ExecuteTreeCommand(ctx context.Context, args *types.PVCBrowseArgs, pod *v1.Pod) (string, error) {
command := []string{"tree", "/pvc-data"}
stdout, err := p.kubeExecutor.Exec(ctx, args.Namespace, pod.Name, pod.Spec.Containers[0].Name, command)
if err != nil {
return "", errors.Wrapf(err, "Error running command:(%v)", command)
}
return stdout, nil
}

func (p *pvcBrowserSteps) PortForwardAPod(ctx context.Context, pod *v1.Pod, localPort int) error {
var wg sync.WaitGroup
wg.Add(1)
Expand Down
8 changes: 4 additions & 4 deletions pkg/csi/pvc_inspector_steps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,8 @@ func (s *CSITestSuite) TestCreateInspectorApplicationForPVC(c *C) {
GenerateName: clonedPodGenerateName,
PVCName: "pvc1",
Namespace: "ns",
ContainerArgs: []string{"--noauth", "-r", "/data"},
MountPath: "/data",
ContainerArgs: []string{"--noauth", "-r", "/pvc-data"},
MountPath: "/pvc-data",
RunAsUser: 100,
ContainerImage: "filebrowser/filebrowser:v2",
}).Return(&v1.Pod{
Expand Down Expand Up @@ -596,8 +596,8 @@ func (s *CSITestSuite) TestCreateInspectorApplicationForPVC(c *C) {
GenerateName: clonedPodGenerateName,
PVCName: "pvc1",
Namespace: "ns",
ContainerArgs: []string{"--noauth", "-r", "/data"},
MountPath: "/data",
ContainerArgs: []string{"--noauth", "-r", "/pvc-data"},
MountPath: "/pvc-data",
RunAsUser: 100,
ContainerImage: "filebrowser/filebrowser:v2",
}).Return(&v1.Pod{
Expand Down
Loading
Loading