@@ -551,6 +551,106 @@ func TestGetPodVolumesFromDisk(t *testing.T) {
551
551
}
552
552
}
553
553
554
+ // Test for https://github.com/kubernetes/kubernetes/pull/19600
555
+ func TestCleanupOrphanedVolumes (t * testing.T ) {
556
+ testKubelet := newTestKubelet (t )
557
+ kubelet := testKubelet .kubelet
558
+ kubeClient := testKubelet .fakeKubeClient
559
+ plug := & volume.FakeVolumePlugin {PluginName : "fake" , Host : nil }
560
+ kubelet .volumePluginMgr .InitPlugins ([]volume.VolumePlugin {plug }, & volumeHost {kubelet })
561
+
562
+ // create a volume "on disk"
563
+ volsOnDisk := []struct {
564
+ podUID types.UID
565
+ volName string
566
+ }{
567
+ {"podUID" , "myrealvol" },
568
+ }
569
+
570
+ pathsOnDisk := []string {}
571
+ for i := range volsOnDisk {
572
+ fv := volume.FakeVolume {PodUID : volsOnDisk [i ].podUID , VolName : volsOnDisk [i ].volName , Plugin : plug }
573
+ fv .SetUp (nil )
574
+ pathsOnDisk = append (pathsOnDisk , fv .GetPath ())
575
+ }
576
+
577
+ // store the claim in fake kubelet database
578
+ claim := api.PersistentVolumeClaim {
579
+ ObjectMeta : api.ObjectMeta {
580
+ Name : "myclaim" ,
581
+ Namespace : "test" ,
582
+ },
583
+ Spec : api.PersistentVolumeClaimSpec {
584
+ VolumeName : "myrealvol" ,
585
+ },
586
+ Status : api.PersistentVolumeClaimStatus {
587
+ Phase : api .ClaimBound ,
588
+ },
589
+ }
590
+ kubeClient .ReactionChain = fake .NewSimpleClientset (& api.PersistentVolumeClaimList {Items : []api.PersistentVolumeClaim {
591
+ claim ,
592
+ }}).ReactionChain
593
+
594
+ // Create a pod referencing the volume via a PersistentVolumeClaim
595
+ pod := api.Pod {
596
+ ObjectMeta : api.ObjectMeta {
597
+ UID : "podUID" ,
598
+ Name : "pod" ,
599
+ Namespace : "test" ,
600
+ },
601
+ Spec : api.PodSpec {
602
+ Volumes : []api.Volume {
603
+ {
604
+ Name : "myvolumeclaim" ,
605
+ VolumeSource : api.VolumeSource {
606
+ PersistentVolumeClaim : & api.PersistentVolumeClaimVolumeSource {
607
+ ClaimName : "myclaim" ,
608
+ },
609
+ },
610
+ },
611
+ },
612
+ },
613
+ }
614
+
615
+ // The pod is pending and not running yet. Test that cleanupOrphanedVolumes
616
+ // won't remove the volume from disk if the volume is referenced only
617
+ // indirectly by a claim.
618
+ err := kubelet .cleanupOrphanedVolumes ([]* api.Pod {& pod }, []* kubecontainer.Pod {})
619
+ if err != nil {
620
+ t .Errorf ("cleanupOrphanedVolumes failed: %v" , err )
621
+ }
622
+
623
+ volumesFound := kubelet .getPodVolumesFromDisk ()
624
+ if len (volumesFound ) != len (pathsOnDisk ) {
625
+ t .Errorf ("Expected to find %d cleaners, got %d" , len (pathsOnDisk ), len (volumesFound ))
626
+ }
627
+ for _ , ep := range pathsOnDisk {
628
+ found := false
629
+ for _ , cl := range volumesFound {
630
+ if ep == cl .GetPath () {
631
+ found = true
632
+ break
633
+ }
634
+ }
635
+ if ! found {
636
+ t .Errorf ("Could not find a volume with path %s" , ep )
637
+ }
638
+ }
639
+
640
+ // The pod is deleted -> kubelet should delete the volume
641
+ err = kubelet .cleanupOrphanedVolumes ([]* api.Pod {}, []* kubecontainer.Pod {})
642
+ if err != nil {
643
+ t .Errorf ("cleanupOrphanedVolumes failed: %v" , err )
644
+ }
645
+ volumesFound = kubelet .getPodVolumesFromDisk ()
646
+ if len (volumesFound ) != 0 {
647
+ t .Errorf ("Expected to find 0 cleaners, got %d" , len (volumesFound ))
648
+ }
649
+ for _ , cl := range volumesFound {
650
+ t .Errorf ("Found unexpected volume %s" , cl .GetPath ())
651
+ }
652
+ }
653
+
554
654
type stubVolume struct {
555
655
path string
556
656
volume.MetricsNil
0 commit comments