Skip to content

Commit

Permalink
Updating CreatePodArgs to consume PVC args in []string{} format inste…
Browse files Browse the repository at this point in the history
…ad of string (#285)

* Updating the PVCName, MountPath, DevicePath signature in CreatePodArgs

* Refactoring PVCName, MountPath, DevicePath variables into a single PVCMap with path definitions

* Removing unused PVCName variable from CreatePodArgs

* Updating DevicePath and MountPath error messages
  • Loading branch information
shlokc9 authored Aug 7, 2024
1 parent 6a5cc24 commit e2e1a6e
Show file tree
Hide file tree
Showing 11 changed files with 194 additions and 103 deletions.
7 changes: 5 additions & 2 deletions pkg/block/block_mount.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,16 @@ func (b *blockMountChecker) Mount(ctx context.Context) (*BlockMountCheckerResult
tB = time.Now()
_, err = b.appCreator.CreatePod(ctx, &types.CreatePodArgs{
Name: b.podName,
PVCName: b.pvcName,
Namespace: b.args.Namespace,
RunAsUser: b.args.RunAsUser,
ContainerImage: b.args.ContainerImage,
Command: []string{"/bin/sh"},
ContainerArgs: []string{"-c", "tail -f /dev/null"},
DevicePath: "/mnt/block",
PVCMap: map[string]types.VolumePath{
b.pvcName: {
DevicePath: "/mnt/block",
},
},
})
if err != nil {
fmt.Printf(" -> Failed to create Pod (%v)\n", err)
Expand Down
7 changes: 5 additions & 2 deletions pkg/block/block_mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,13 +299,16 @@ func TestBlockMountCheckerMount(t *testing.T) {
createPodArgs := func(b *blockMountChecker) *types.CreatePodArgs {
return &types.CreatePodArgs{
Name: b.podName,
PVCName: b.pvcName,
Namespace: b.args.Namespace,
RunAsUser: b.args.RunAsUser,
ContainerImage: b.args.ContainerImage,
Command: []string{"/bin/sh"},
ContainerArgs: []string{"-c", "tail -f /dev/null"},
DevicePath: "/mnt/block",
PVCMap: map[string]types.VolumePath{
b.pvcName: {
DevicePath: "/mnt/block",
},
},
}
}
createPod := func(b *blockMountChecker) *v1.Pod {
Expand Down
39 changes: 20 additions & 19 deletions pkg/csi/csi_ops.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,27 +208,28 @@ func (c *applicationCreate) CreatePod(ctx context.Context, args *types.CreatePod
Command: args.Command,
Args: args.ContainerArgs,
}},
Volumes: []v1.Volume{{
Name: volumeNameInPod,
VolumeSource: v1.VolumeSource{
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
ClaimName: args.PVCName,
},
}},
},
},
}

if args.MountPath != "" {
pod.Spec.Containers[0].VolumeMounts = []v1.VolumeMount{{
Name: volumeNameInPod,
MountPath: args.MountPath,
}}
} else { // args.DevicePath
pod.Spec.Containers[0].VolumeDevices = []v1.VolumeDevice{{
Name: volumeNameInPod,
DevicePath: args.DevicePath,
}}
for pvcName, path := range args.PVCMap {
pod.Spec.Volumes = append(pod.Spec.Volumes, v1.Volume{
Name: fmt.Sprintf("%s-%s", volumeNameInPod, pvcName),
VolumeSource: v1.VolumeSource{
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
ClaimName: pvcName,
},
},
})
if len(path.MountPath) != 0 {
pod.Spec.Containers[0].VolumeMounts = append(pod.Spec.Containers[0].VolumeMounts, v1.VolumeMount{
Name: fmt.Sprintf("%s-%s", volumeNameInPod, pvcName),
MountPath: path.MountPath,
})
} else {
pod.Spec.Containers[0].VolumeDevices = append(pod.Spec.Containers[0].VolumeDevices, v1.VolumeDevice{
Name: fmt.Sprintf("%s-%s", volumeNameInPod, pvcName),
DevicePath: path.DevicePath,
})
}
}

if args.RunAsUser > 0 {
Expand Down
113 changes: 70 additions & 43 deletions pkg/csi/csi_ops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,12 +517,15 @@ func (s *CSITestSuite) TestCreatePod(c *C) {
cli: fake.NewSimpleClientset(),
args: &types.CreatePodArgs{
GenerateName: "name",
PVCName: "pvcname",
Namespace: "ns",
Command: []string{"somecommand"},
RunAsUser: 1000,
ContainerImage: "containerimage",
MountPath: "/mnt/fs",
PVCMap: map[string]types.VolumePath{
"pvcname": {
MountPath: "/mnt/fs",
},
},
},
errChecker: IsNil,
podChecker: NotNil,
Expand All @@ -532,10 +535,13 @@ func (s *CSITestSuite) TestCreatePod(c *C) {
cli: fake.NewSimpleClientset(),
args: &types.CreatePodArgs{
GenerateName: "name",
PVCName: "pvcname",
Namespace: "ns",
Command: []string{"somecommand"},
MountPath: "/mnt/fs",
PVCMap: map[string]types.VolumePath{
"pvcname": {
MountPath: "/mnt/fs",
},
},
},
failCreates: true,
errChecker: NotNil,
Expand All @@ -546,10 +552,13 @@ func (s *CSITestSuite) TestCreatePod(c *C) {
cli: fake.NewSimpleClientset(),
args: &types.CreatePodArgs{
GenerateName: "",
PVCName: "pvcname",
Namespace: "ns",
Command: []string{"somecommand"},
MountPath: "/mnt/fs",
PVCMap: map[string]types.VolumePath{
"pvcname": {
MountPath: "/mnt/fs",
},
},
},
errChecker: NotNil,
podChecker: IsNil,
Expand All @@ -560,10 +569,13 @@ func (s *CSITestSuite) TestCreatePod(c *C) {
args: &types.CreatePodArgs{
GenerateName: "name",
Name: "name",
PVCName: "pvcname",
Namespace: "ns",
Command: []string{"somecommand"},
MountPath: "/mnt/fs",
PVCMap: map[string]types.VolumePath{
"pvcname": {
MountPath: "/mnt/fs",
},
},
},
errChecker: NotNil,
podChecker: IsNil,
Expand All @@ -573,9 +585,9 @@ func (s *CSITestSuite) TestCreatePod(c *C) {
cli: fake.NewSimpleClientset(),
args: &types.CreatePodArgs{
GenerateName: "name",
PVCName: "",
Namespace: "ns",
Command: []string{"somecommand"},
PVCMap: map[string]types.VolumePath{"pvcname": {}},
},
errChecker: NotNil,
podChecker: IsNil,
Expand All @@ -585,11 +597,14 @@ func (s *CSITestSuite) TestCreatePod(c *C) {
cli: fake.NewSimpleClientset(),
args: &types.CreatePodArgs{
GenerateName: "name",
PVCName: "",
Namespace: "ns",
Command: []string{"somecommand"},
MountPath: "/mnt/fs",
DevicePath: "/mnt/dev",
PVCMap: map[string]types.VolumePath{
"pvcname": {
MountPath: "/mnt/fs",
DevicePath: "/mnt/dev",
},
},
},
errChecker: NotNil,
podChecker: IsNil,
Expand All @@ -599,10 +614,9 @@ func (s *CSITestSuite) TestCreatePod(c *C) {
cli: fake.NewSimpleClientset(),
args: &types.CreatePodArgs{
GenerateName: "name",
PVCName: "",
Namespace: "ns",
Command: []string{"somecommand"},
MountPath: "/mnt/fs",
PVCMap: map[string]types.VolumePath{"": {MountPath: "/mnt/fs"}},
},
errChecker: NotNil,
podChecker: IsNil,
Expand All @@ -612,10 +626,13 @@ func (s *CSITestSuite) TestCreatePod(c *C) {
cli: fake.NewSimpleClientset(),
args: &types.CreatePodArgs{
GenerateName: "name",
PVCName: "pvcname",
Namespace: "",
Command: []string{"somecommand"},
MountPath: "/mnt/fs",
PVCMap: map[string]types.VolumePath{
"pvcname": {
MountPath: "/mnt/fs",
},
},
},
errChecker: NotNil,
podChecker: IsNil,
Expand All @@ -625,10 +642,13 @@ func (s *CSITestSuite) TestCreatePod(c *C) {
cli: fake.NewSimpleClientset(),
args: &types.CreatePodArgs{
GenerateName: "name",
PVCName: "pvcname",
Namespace: "ns",
Command: []string{"somecommand"},
MountPath: "/mnt/fs",
PVCMap: map[string]types.VolumePath{
"pvcname": {
MountPath: "/mnt/fs",
},
},
},
errChecker: IsNil,
podChecker: NotNil,
Expand All @@ -637,11 +657,14 @@ func (s *CSITestSuite) TestCreatePod(c *C) {
description: "ns namespace pod is created (Name/DevicePath)",
cli: fake.NewSimpleClientset(),
args: &types.CreatePodArgs{
Name: "name",
PVCName: "pvcname",
Namespace: "ns",
Command: []string{"somecommand"},
DevicePath: "/mnt/dev",
Name: "name",
Namespace: "ns",
Command: []string{"somecommand"},
PVCMap: map[string]types.VolumePath{
"pvcname": {
DevicePath: "/mnt/dev",
},
},
},
errChecker: IsNil,
podChecker: NotNil,
Expand Down Expand Up @@ -678,27 +701,31 @@ func (s *CSITestSuite) TestCreatePod(c *C) {
c.Assert(len(pod.Spec.Containers), Equals, 1)
c.Assert(pod.Spec.Containers[0].Command, DeepEquals, tc.args.Command)
c.Assert(pod.Spec.Containers[0].Args, DeepEquals, tc.args.ContainerArgs)
if tc.args.MountPath != "" {
c.Assert(pod.Spec.Containers[0].VolumeMounts, DeepEquals, []v1.VolumeMount{{
Name: "persistent-storage",
MountPath: tc.args.MountPath,
}})
c.Assert(pod.Spec.Containers[0].VolumeDevices, IsNil)
} else {
c.Assert(pod.Spec.Containers[0].VolumeDevices, DeepEquals, []v1.VolumeDevice{{
Name: "persistent-storage",
DevicePath: tc.args.DevicePath,
}})
c.Assert(pod.Spec.Containers[0].VolumeMounts, IsNil)
}
c.Assert(pod.Spec.Volumes, DeepEquals, []v1.Volume{{
Name: "persistent-storage",
VolumeSource: v1.VolumeSource{
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
ClaimName: tc.args.PVCName,
index := 0
for pvcName, path := range tc.args.PVCMap {
if len(path.MountPath) != 0 {
c.Assert(pod.Spec.Containers[0].VolumeMounts[index], DeepEquals, v1.VolumeMount{
Name: fmt.Sprintf("persistent-storage-%s", pvcName),
MountPath: path.MountPath,
})
c.Assert(pod.Spec.Containers[0].VolumeDevices, IsNil)
} else {
c.Assert(pod.Spec.Containers[0].VolumeDevices[index], DeepEquals, v1.VolumeDevice{
Name: fmt.Sprintf("persistent-storage-%s", pvcName),
DevicePath: path.DevicePath,
})
c.Assert(pod.Spec.Containers[0].VolumeMounts, IsNil)
}
c.Assert(pod.Spec.Volumes[index], DeepEquals, v1.Volume{
Name: fmt.Sprintf("persistent-storage-%s", pvcName),
VolumeSource: v1.VolumeSource{
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{
ClaimName: pvcName,
},
},
}},
})
})
index++
}
if tc.args.ContainerImage == "" {
c.Assert(pod.Spec.Containers[0].Image, Equals, common.DefaultPodImage)
} else {
Expand Down
14 changes: 10 additions & 4 deletions pkg/csi/pvc_inspector.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,23 +209,29 @@ func (p *pvcBrowserSteps) CreateInspectorApplication(ctx context.Context, args *
}
podArgs := &types.CreatePodArgs{
GenerateName: clonedPodGenerateName,
PVCName: pvc.Name,
Namespace: args.Namespace,
RunAsUser: args.RunAsUser,
ContainerImage: "filebrowser/filebrowser:v2",
ContainerArgs: []string{"--noauth", "-r", "/pvc-data"},
MountPath: "/pvc-data",
PVCMap: map[string]types.VolumePath{
pvc.Name: {
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",
PVCMap: map[string]types.VolumePath{
pvc.Name: {
MountPath: "/pvc-data",
},
},
}
}
pod, err := p.createAppOps.CreatePod(ctx, podArgs)
Expand Down
14 changes: 10 additions & 4 deletions pkg/csi/pvc_inspector_steps_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,12 +539,15 @@ func (s *CSITestSuite) TestCreateInspectorApplicationForPVC(c *C) {
}, nil),
f.createAppOps.EXPECT().CreatePod(gomock.Any(), &types.CreatePodArgs{
GenerateName: clonedPodGenerateName,
PVCName: "pvc1",
Namespace: "ns",
ContainerArgs: []string{"--noauth", "-r", "/pvc-data"},
MountPath: "/pvc-data",
RunAsUser: 100,
ContainerImage: "filebrowser/filebrowser:v2",
PVCMap: map[string]types.VolumePath{
"pvc1": {
MountPath: "/pvc-data",
},
},
}).Return(&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod1",
Expand Down Expand Up @@ -594,12 +597,15 @@ func (s *CSITestSuite) TestCreateInspectorApplicationForPVC(c *C) {
}, nil),
f.createAppOps.EXPECT().CreatePod(gomock.Any(), &types.CreatePodArgs{
GenerateName: clonedPodGenerateName,
PVCName: "pvc1",
Namespace: "ns",
ContainerArgs: []string{"--noauth", "-r", "/pvc-data"},
MountPath: "/pvc-data",
RunAsUser: 100,
ContainerImage: "filebrowser/filebrowser:v2",
PVCMap: map[string]types.VolumePath{
"pvc1": {
MountPath: "/pvc-data",
},
},
}).Return(&v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "pod1",
Expand Down
Loading

0 comments on commit e2e1a6e

Please sign in to comment.