From 47dedb1c4891f41f358d0dd1e5488052c66483a3 Mon Sep 17 00:00:00 2001 From: "Han Verstraete (OpenFaaS Ltd)" Date: Tue, 6 Sep 2022 12:05:07 +0200 Subject: [PATCH] Output requests and limits Signed-off-by: Han Verstraete (OpenFaaS Ltd) --- main.go | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index f718156..3011327 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,7 @@ import ( "context" "flag" "fmt" + "io" "log" "os" "strconv" @@ -52,6 +53,25 @@ func (t *Timeout) GetReadTimeout() time.Duration { return r } +type FunctionResources struct { + Memory string + CPU string +} + +func (r *FunctionResources) GetMemory() string { + if r.Memory == "0" { + return "" + } + return r.Memory +} + +func (r *FunctionResources) GetCpu() string { + if r.CPU == "0" { + return "" + } + return r.CPU +} + type Function struct { Name string MaxInflight *int @@ -60,8 +80,10 @@ type Function struct { // https://docs.openfaas.com/tutorials/expanded-timeouts/ // of-watchdog: exec_timeout // classic-watchdog: - Timeout *Timeout - Scaling *Scaling + Timeout *Timeout + Scaling *Scaling + Requests *FunctionResources + Limits *FunctionResources } func (f *Function) GetMaxInflight() string { @@ -506,6 +528,10 @@ Features detected: } else if execTimeout > gwUpstreamTimeout { fmt.Printf("⚠️ function %s exec_timeout (%s) is greater than gateway.upstream_timeout (%s)\n", fn.Name, execTimeout, gwUpstreamTimeout) } + + if fn.Requests.Memory == "0" { + fmt.Printf("⚠️ %s no memory requests set\n", fn.Name) + } } if len(functions) > 0 && scalingDown == 0 { @@ -556,11 +582,30 @@ func printFunction(fn Function, autoscaling bool) { } } + fmt.Fprintf(w, "\nresources and limits\n\n") + + printResources(w, "- requests", fn.Requests) + printResources(w, "- limits", fn.Requests) + fmt.Fprintln(w) w.Flush() fmt.Print(b.String()) } +func printResources(w io.Writer, name string, resources *FunctionResources) { + fmt.Fprintf(w, name+":") + + if resources.CPU == "0" && resources.Memory == "0" { + fmt.Fprintln(w, "\t ") + return + } + + fmt.Fprintln(w, "\t CPU: "+resources.GetCpu()) + fmt.Fprintln(w, "\t Memory: "+resources.GetMemory()) + + return +} + func readFunctions(deps []v1.Deployment) []Function { var functions []Function @@ -650,6 +695,18 @@ func readFunctions(deps []v1.Deployment) []Function { function.Scaling.ZeroDuration = scaleZeroDuration } + req := &FunctionResources{ + Memory: functionContainer.Resources.Requests.Memory().String(), + CPU: functionContainer.Resources.Requests.Cpu().String(), + } + function.Requests = req + + lim := &FunctionResources{ + Memory: functionContainer.Resources.Limits.Memory().String(), + CPU: functionContainer.Resources.Limits.Cpu().String(), + } + function.Limits = lim + functions = append(functions, function) }