Skip to content

Commit

Permalink
Fixes ListFromCluster() by avoiding the volume mounts from the init c…
Browse files Browse the repository at this point in the history
…ontainers and the source volume
  • Loading branch information
mik-dass committed Mar 15, 2021
1 parent 24b8a6b commit 06649e7
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 0 deletions.
14 changes: 14 additions & 0 deletions pkg/storage/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,25 @@ func (k kubernetesClient) ListFromCluster() (StorageList, error) {
return StorageList{}, err
}

initContainerVolumeMounts := make(map[string]bool)
for _, container := range pod.Spec.InitContainers {
for _, volumeMount := range container.VolumeMounts {
initContainerVolumeMounts[volumeMount.Name] = true
}
}

var storage []Storage
var volumeMounts []Storage
for _, container := range pod.Spec.Containers {
for _, volumeMount := range container.VolumeMounts {

// avoid the volume mounts from the init containers
// and the source volume volume mount
_, ok := initContainerVolumeMounts[volumeMount.Name]
if ok || volumeMount.Name == OdoSourceVolume {
continue
}

volumeMounts = append(volumeMounts, Storage{
ObjectMeta: metav1.ObjectMeta{Name: volumeMount.Name},
Spec: StorageSpec{
Expand Down
80 changes: 80 additions & 0 deletions pkg/storage/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/openshift/odo/pkg/testingutil"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
ktesting "k8s.io/client-go/testing"
)
Expand Down Expand Up @@ -228,6 +229,85 @@ func Test_kubernetesClient_ListFromCluster(t *testing.T) {
want: StorageList{},
wantErr: true,
},
{
name: "case 9: avoid the source volume's mount",
fields: fields{
generic: generic{
appName: "app",
componentName: "nodejs",
},
},
returnedPods: &corev1.PodList{
Items: []corev1.Pod{
*testingutil.CreateFakePodWithContainers("nodejs", "pod-0", []corev1.Container{
testingutil.CreateFakeContainerWithVolumeMounts("container-0", []corev1.VolumeMount{
{Name: "volume-0-vol", MountPath: "/data"},
{Name: "volume-1-vol", MountPath: "/path"},
}),
testingutil.CreateFakeContainerWithVolumeMounts("container-1", []corev1.VolumeMount{
{Name: "volume-1-vol", MountPath: "/path"},
{Name: OdoSourceVolume, MountPath: "/path1"},
}),
}),
},
},
returnedPVCs: &corev1.PersistentVolumeClaimList{
Items: []corev1.PersistentVolumeClaim{
*testingutil.FakePVC("volume-0", "5Gi", map[string]string{"component": "nodejs", storageLabels.DevfileStorageLabel: "volume-0"}),
*testingutil.FakePVC("volume-1", "10Gi", map[string]string{"component": "nodejs", storageLabels.DevfileStorageLabel: "volume-1"}),
},
},
want: StorageList{
Items: []Storage{
generateStorage(GetMachineReadableFormat("volume-0", "5Gi", "/data"), "", "container-0"),
generateStorage(GetMachineReadableFormat("volume-1", "10Gi", "/path"), "", "container-0"),
generateStorage(GetMachineReadableFormat("volume-1", "10Gi", "/path"), "", "container-1"),
},
},
wantErr: false,
},
{
name: "case 10: avoid the volume mounts used by the init containers",
fields: fields{
generic: generic{
appName: "app",
componentName: "nodejs",
},
},
returnedPods: &corev1.PodList{
Items: []corev1.Pod{
{
ObjectMeta: metav1.ObjectMeta{
Name: "nodejs",
Labels: map[string]string{
"component": "nodejs",
},
},
Spec: corev1.PodSpec{
InitContainers: []corev1.Container{
{
Name: "supervisord",
VolumeMounts: []corev1.VolumeMount{
{
Name: "odo-shared-project",
MountPath: "/opt/",
},
},
},
},
},
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
},
},
},
},
returnedPVCs: &corev1.PersistentVolumeClaimList{
Items: []corev1.PersistentVolumeClaim{},
},
want: StorageList{},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 06649e7

Please sign in to comment.