knative/func

nil pointer dereference error on `func deploy` with emptyDir added

jrangelramos opened this issue · 5 comments

I notice nil poiter dereference error during func deploy after adding emptyDir thru func config volumes add

Steps to reproduce below:

$ func create -l node /tmp/emptydirfunc       
Created node function in /tmp/emptydirfunc
$ cd /tmp/emptydirfunc           
$ func config volumes add                     
? What do you want to mount as a Volume? EmptyDir
Please make sure to enable the EmptyDir extension flag: https://knative.dev/docs/serving/configuration/feature-flags/
? Please specify the path where the EmptyDir should be mounted: /myapp
Volume entry was added to the function configuration
[/tmp/emptydirfunc]$ cat func.yaml                  
specVersion: 0.35.0
name: emptydirfunc
runtime: node
created: 2023-10-27T16:16:09.864513227-03:00
run:
  volumes:
  - emptyDir: {}
    path: /myapp
$ func deploy            
Building function image
Still building
Still building
Yes, still building
Don't give up on me
🙌 Function built: image-registry.openshift-image-registry.svc:5000/jeff/emptydirfunc:latest
Pushing function image to the registry "image-registry.openshift-image-registry.svc:5000" using the "openshift" user credentials
⬆️  Deploying function to the cluster
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x22388a0]

goroutine 1 [running]:
knative.dev/func/pkg/knative.processVolumes({0xc00057f500, 0x1, 0x0?}, 0xc000b938a8, 0xc000b938b8, 0xc000b938b0)
	/home/jeramos/projects/github/knative-sandbox/kn-plugin-func/pkg/knative/deployer.go:837 +0x3c0

Error is happening here https://github.com/knative/func/blob/main/pkg/knative/deployer.go#L837
It expects emptyDir to contains the sizeLimit field (https://github.com/knative/func/blob/main/pkg/functions/function_volumes.go#L39) to be specified. By setting it, the deployment works properly

@zalsader I think you added emptyDir feature, any idea why this would be happening?

I guess we just need to add some nil check?

diff --git a/pkg/knative/deployer.go b/pkg/knative/deployer.go
index 67ea7a36..9e2dfd3f 100644
--- a/pkg/knative/deployer.go
+++ b/pkg/knative/deployer.go
@@ -834,10 +834,13 @@ func processVolumes(volumes []fn.Volume, referencedSecrets, referencedConfigMaps
 
 			if !createdVolumes.Has(volumeName) {
 
-				sizeLimit, err := resource.ParseQuantity(*vol.EmptyDir.SizeLimit)
-
-				if err != nil {
-					return nil, nil, fmt.Errorf("invalid quantity for sizeLimit: %s. Error: %s", *vol.EmptyDir.SizeLimit, err)
+				var sizeLimit *resource.Quantity
+				if vol.EmptyDir.SizeLimit != nil {
+					sl, err := resource.ParseQuantity(*vol.EmptyDir.SizeLimit)
+					if err != nil {
+						return nil, nil, fmt.Errorf("invalid quantity for sizeLimit: %s. Error: %s", *vol.EmptyDir.SizeLimit, err)
+					}
+					sizeLimit = &sl
 				}
 
 				newVolumes = append(newVolumes, corev1.Volume{
@@ -845,7 +848,7 @@ func processVolumes(volumes []fn.Volume, referencedSecrets, referencedConfigMaps
 					VolumeSource: corev1.VolumeSource{
 						EmptyDir: &corev1.EmptyDirVolumeSource{
 							Medium:    corev1.StorageMedium(vol.EmptyDir.Medium),
-							SizeLimit: &sizeLimit,
+							SizeLimit: sizeLimit,
 						},
 					},
 				})

fixed by #2062