Skip to content

Commit fb0317f

Browse files
authored
Merge pull request #38 from mtougeron/bugfix-nil-pointer
Check to make sure the annotation is set before trying to use it
2 parents 262f017 + f999ff7 commit fb0317f

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

kubernetes.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -249,15 +249,23 @@ func processPersistentVolumeClaim(pvc *corev1.PersistentVolumeClaim) (string, ma
249249
}
250250

251251
var volumeID string
252-
if pvc.GetAnnotations()["volume.beta.kubernetes.io/storage-provisioner"] == "ebs.csi.aws.com" {
252+
annotations := pvc.GetAnnotations()
253+
if annotations == nil {
254+
log.Errorf("cannot get PVC annotations")
255+
return "", nil, errors.New("cannot get PVC annotations")
256+
}
257+
if provisionedBy, ok := annotations["volume.beta.kubernetes.io/storage-provisioner"]; !ok {
258+
log.Errorf("cannot get volume.beta.kubernetes.io/storage-provisioner annotation")
259+
return "", nil, errors.New("cannot get volume.beta.kubernetes.io/storage-provisioner annotation")
260+
} else if provisionedBy == "ebs.csi.aws.com" {
253261
volumeID = pv.Spec.CSI.VolumeHandle
254-
} else if pvc.GetAnnotations()["volume.beta.kubernetes.io/storage-provisioner"] == "kubernetes.io/aws-ebs" {
262+
} else if provisionedBy == "kubernetes.io/aws-ebs" {
255263
volumeID = parseAWSVolumeID(pv.Spec.PersistentVolumeSource.AWSElasticBlockStore.VolumeID)
256264
}
257265
log.WithFields(log.Fields{"namespace": pvc.GetNamespace(), "pvc": pvc.GetName(), "volumeID": volumeID}).Debugln("parsed volumeID:", volumeID)
258266
if len(volumeID) == 0 {
259267
log.Errorf("Cannot parse VolumeID")
260-
return "", nil, errors.New("Cannot parse VolumeID")
268+
return "", nil, errors.New("cannot parse VolumeID")
261269
}
262270

263271
return volumeID, tags, nil

0 commit comments

Comments
 (0)