From 36e3ec9c1f4dca88d734b319f64f7c8f545fde33 Mon Sep 17 00:00:00 2001 From: Chris Moos Date: Fri, 24 Apr 2015 23:44:37 -0700 Subject: [PATCH] Fix crash in kubelet when persistent volume claim is not bound. While testing #6105 ran into this issue, kubelet crashed because controller had not yet bound the claim to a volume. --- .../persistent_claim/persistent_claim.go | 4 ++ .../persistent_claim/persistent_claim_test.go | 50 +++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/pkg/volume/persistent_claim/persistent_claim.go b/pkg/volume/persistent_claim/persistent_claim.go index 3ffc4c01f2..52c55d4f10 100644 --- a/pkg/volume/persistent_claim/persistent_claim.go +++ b/pkg/volume/persistent_claim/persistent_claim.go @@ -57,6 +57,10 @@ func (plugin *persistentClaimPlugin) NewBuilder(spec *volume.Spec, podRef *api.O return nil, err } + if claim.Status.VolumeRef == nil { + return nil, fmt.Errorf("The claim %+v is not yet bound to a volume", claim.Name) + } + pv, err := plugin.host.GetKubeClient().PersistentVolumes().Get(claim.Status.VolumeRef.Name) if err != nil { glog.Errorf("Error finding persistent volume for claim: %+v\n", claim.Name) diff --git a/pkg/volume/persistent_claim/persistent_claim_test.go b/pkg/volume/persistent_claim/persistent_claim_test.go index 186eb65582..bef5bd4f7e 100644 --- a/pkg/volume/persistent_claim/persistent_claim_test.go +++ b/pkg/volume/persistent_claim/persistent_claim_test.go @@ -185,6 +185,56 @@ func TestNewBuilder(t *testing.T) { } } +func TestNewBuilderClaimNotBound(t *testing.T) { + pv := &api.PersistentVolume{ + ObjectMeta: api.ObjectMeta{ + Name: "pvC", + }, + Spec: api.PersistentVolumeSpec{ + PersistentVolumeSource: api.PersistentVolumeSource{ + GCEPersistentDisk: &api.GCEPersistentDiskVolumeSource{}, + }, + ClaimRef: nil, + }, + } + claim := &api.PersistentVolumeClaim{ + ObjectMeta: api.ObjectMeta{ + Name: "claimC", + Namespace: "nsA", + }, + Status: api.PersistentVolumeClaimStatus{ + Phase: api.ClaimBound, + VolumeRef: nil, + }, + } + podVolume := api.VolumeSource{ + PersistentVolumeClaimVolumeSource: &api.PersistentVolumeClaimVolumeSource{ + ReadOnly: false, + ClaimName: "claimC", + }, + } + o := testclient.NewObjects(api.Scheme) + o.Add(pv) + o.Add(claim) + client := &testclient.Fake{ReactFn: testclient.ObjectReaction(o, latest.RESTMapper)} + + plugMgr := volume.VolumePluginMgr{} + plugMgr.InitPlugins(testProbeVolumePlugins(), newTestHost(t, client)) + + plug, err := plugMgr.FindPluginByName("kubernetes.io/persistent-claim") + if err != nil { + t.Errorf("Can't find the plugin by name") + } + spec := &volume.Spec{ + Name: "vol1", + VolumeSource: podVolume, + } + builder, err := plug.NewBuilder(spec, &api.ObjectReference{UID: types.UID("poduid")}, volume.VolumeOptions{}) + if builder != nil { + t.Errorf("Expected a nil builder if the claim wasn't bound") + } +} + func testProbeVolumePlugins() []volume.VolumePlugin { allPlugins := []volume.VolumePlugin{} allPlugins = append(allPlugins, gce_pd.ProbeVolumePlugins()...)