From d5fcc7b2abd083e39505a93d782962cc9e665287 Mon Sep 17 00:00:00 2001 From: Shikachuu Date: Sat, 18 Sep 2021 14:33:56 +0200 Subject: [PATCH] Fixed nil pointer dereference while parsing memory limit Signed-off-by: Shikachuu --- pkg/provider/handlers/functions.go | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/pkg/provider/handlers/functions.go b/pkg/provider/handlers/functions.go index ef638a4..7f3a33b 100644 --- a/pkg/provider/handlers/functions.go +++ b/pkg/provider/handlers/functions.go @@ -93,7 +93,7 @@ func GetFunction(client *containerd.Client, name string, namespace string) (Func spec, err := c.Spec(ctx) if err != nil { - return Function{}, fmt.Errorf("unable to load function spec for reading secrets: %s, error %w", name, err) + return Function{}, fmt.Errorf("unable to load function spec for reading secrets and limits: %s, error %w", name, err) } info, err := c.Info(ctx) @@ -113,7 +113,7 @@ func GetFunction(client *containerd.Client, name string, namespace string) (Func fn.envVars = envVars fn.envProcess = envProcess fn.createdAt = info.CreatedAt - fn.memoryLimit = *spec.Linux.Resources.Memory.Limit + fn.memoryLimit = readMemoryLimitFromSpec(spec) replicas := 0 task, err := c.Task(ctx, nil) @@ -233,3 +233,19 @@ func findNamespace(target string, items []string) bool { } return false } + +func readMemoryLimitFromSpec(spec *specs.Spec) int64 { + if spec.Linux != nil { + return 0 + } + if spec.Linux.Resources == nil { + return 0 + } + if spec.Linux.Resources.Memory == nil { + return 0 + } + if spec.Linux.Resources.Memory.Limit == nil { + return 0 + } + return *spec.Linux.Resources.Memory.Limit +}