From b65e279f9f503571e8613c71be48678ce3634d1b Mon Sep 17 00:00:00 2001 From: Lucas Roesler Date: Sat, 26 Feb 2022 11:38:25 +0100 Subject: [PATCH] feat: Add missing fields to the describe output Add the function * constraints * environment variables * secrets * requests and limits This can be tested as follows: Create a test cluster using KinD ```sh kind create cluster --config=cluster.yaml arkade install openfaas -a=false --function-pull-policy=IfNotPresent --wait faas-cli store deploy nodeinfo --annotation sample=true --label status=418 ``` Now deploy a sample function ```sh $ faas-cli describe nodeinfo Name: nodeinfo Status: Ready Replicas: 1 Available replicas: 1 Invocations: 0 Image: ghcr.io/openfaas/nodeinfo:latest Function process: URL: http://127.0.0.1:8080/function/nodeinfo Async URL: http://127.0.0.1:8080/async-function/nodeinfo Labels faas_function : nodeinfo status : 418 Annotations sample : true prometheus.io.scrape : false Constraints Environment Secrets Requests Limits ``` Now we add some more interesting values, like a secret and env variables. ```sh $ faas-cli secret create db-password --from-literal=password Creating secret: db-password. Created: 202 Accepted $ faas-cli store deploy nodeinfo --annotation sample=true --label status=418 --secret db-password --env db-user=postgres --env db-host=rds.aws.example.com Deployed. 202 Accepted. URL: http://127.0.0.1:8080/function/nodeinfo $ faas-cli describe nodeinfo Name: nodeinfo Status: Ready Replicas: 1 Available replicas: 1 Invocations: 0 Image: ghcr.io/openfaas/nodeinfo:latest Function process: URL: http://127.0.0.1:8080/function/nodeinfo Async URL: http://127.0.0.1:8080/async-function/nodeinfo Labels: faas_function: nodeinfo status: 418 uid: 736815901 Annotations: prometheus.io.scrape: false sample: true Constraints: Environment: Secrets: - db-password Requests: Limits: ``` To see how multiple Secrets and the Requests and Limits are rendered, use ```yaml version: 1.0 provider: name: openfaas gateway: http://127.0.0.1:8080 functions: nodeinfo: lang: Dockerfile image: ghcr.io/openfaas/nodeinfo:latest secrets: - cache-password - db-password environment: db-host: rds.aws.example.com cache-host: redis.aws.example.com labels: status: 481 annotations: sample: "true" requests: cpu: 100m memory: 128Mi limits: cpu: 200m memory: 256Mi ``` then ```sh $ faas-cli secret create cache-password --from-literal=password Creating secret: cache-password. Created: 202 Accepted $ faas-cli deploy Deploying: nodeinfo. Deployed. 202 Accepted. URL: http://127.0.0.1:8080/function/nodeinfo $ faas-cli describe nodeinfo Name: nodeinfo Status: Ready Replicas: 1 Available replicas: 1 Invocations: 0 Image: ghcr.io/openfaas/nodeinfo:latest Function process: URL: http://127.0.0.1:8080/function/nodeinfo Async URL: http://127.0.0.1:8080/async-function/nodeinfo Labels: faas_function: nodeinfo status: 481 uid: 679245186 Annotations: prometheus.io.scrape: false sample: true Constraints: Environment: Secrets: - cache-password - db-password Requests: CPU: 100m Memory: 128Mi Limits: CPU: 200m Memory: 256Mi ``` Signed-off-by: Lucas Roesler --- commands/describe.go | 71 +++++++++++++++++++++++++++++++------------- schema/describe.go | 17 ++++------- 2 files changed, 55 insertions(+), 33 deletions(-) diff --git a/commands/describe.go b/commands/describe.go index e98cf880..60f8de6a 100644 --- a/commands/describe.go +++ b/commands/describe.go @@ -14,6 +14,7 @@ import ( "github.com/openfaas/faas-cli/proxy" "github.com/openfaas/faas-cli/schema" "github.com/openfaas/faas-cli/stack" + "github.com/openfaas/faas-provider/types" "github.com/spf13/cobra" ) @@ -33,7 +34,7 @@ var describeCmd = &cobra.Command{ Use: "describe FUNCTION_NAME [--gateway GATEWAY_URL]", Short: "Describe an OpenFaaS function", Long: `Display details of an OpenFaaS function`, - Example: `faas-cli describe figlet + Example: `faas-cli describe figlet faas-cli describe env --gateway http://127.0.0.1:8080 faas-cli describe echo -g http://127.0.0.1.8080`, PreRunE: preRunDescribe, @@ -103,20 +104,11 @@ func runDescribe(cmd *cobra.Command, args []string) error { url, asyncURL := getFunctionURLs(gatewayAddress, functionName, functionNamespace) funcDesc := schema.FunctionDescription{ - Name: function.Name, - Status: status, - Replicas: int(function.Replicas), - AvailableReplicas: int(function.AvailableReplicas), - InvocationCount: int(invocationCount), - Image: function.Image, - EnvProcess: function.EnvProcess, - URL: url, - AsyncURL: asyncURL, - Labels: function.Labels, - Annotations: function.Annotations, - } - if function.Usage != nil { - funcDesc.Usage = function.Usage + FunctionStatus: function, + Status: status, + InvocationCount: int(invocationCount), + URL: url, + AsyncURL: asyncURL, } printFunctionDescription(funcDesc) @@ -140,17 +132,26 @@ func getFunctionURLs(gateway string, functionName string, functionNamespace stri func printFunctionDescription(funcDesc schema.FunctionDescription) { w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent) + process := "" + if funcDesc.EnvProcess != "" { + process = funcDesc.EnvProcess + } fmt.Fprintln(w, "Name:\t "+funcDesc.Name) fmt.Fprintln(w, "Status:\t "+funcDesc.Status) - fmt.Fprintln(w, "Replicas:\t "+strconv.Itoa(funcDesc.Replicas)) - fmt.Fprintln(w, "Available replicas:\t "+strconv.Itoa(funcDesc.AvailableReplicas)) + fmt.Fprintln(w, "Replicas:\t "+strconv.Itoa(int(funcDesc.Replicas))) + fmt.Fprintln(w, "Available replicas:\t "+strconv.Itoa(int(funcDesc.AvailableReplicas))) fmt.Fprintln(w, "Invocations:\t "+strconv.Itoa(funcDesc.InvocationCount)) fmt.Fprintln(w, "Image:\t "+funcDesc.Image) - fmt.Fprintln(w, "Function process:\t "+funcDesc.EnvProcess) + fmt.Fprintln(w, "Function process:\t "+process) fmt.Fprintln(w, "URL:\t "+funcDesc.URL) fmt.Fprintln(w, "Async URL:\t "+funcDesc.AsyncURL) printMap(w, "Labels", *funcDesc.Labels) printMap(w, "Annotations", *funcDesc.Annotations) + printList(w, "Constraints", funcDesc.Constraints) + printMap(w, "Environment", funcDesc.EnvVars) + printList(w, "Secrets", funcDesc.Secrets) + printResources(w, "Requests", funcDesc.Requests) + printResources(w, "Limits", funcDesc.Limits) if funcDesc.Usage != nil { fmt.Println() @@ -160,14 +161,13 @@ func printFunctionDescription(funcDesc schema.FunctionDescription) { cpu = 1 } fmt.Fprintf(w, "CPU:\t %.0f Mi\n", (cpu)) - } w.Flush() } func printMap(w *tabwriter.Writer, name string, m map[string]string) { - fmt.Fprintf(w, name) + fmt.Fprintf(w, name+":") if len(m) == 0 { fmt.Fprintln(w, " \t ") @@ -175,8 +175,37 @@ func printMap(w *tabwriter.Writer, name string, m map[string]string) { } for key, value := range m { - fmt.Fprintln(w, " \t "+key+" : "+value) + fmt.Fprintln(w, " \t "+key+": "+value) } return } + +func printList(w *tabwriter.Writer, name string, data []string) { + fmt.Fprintf(w, name+":") + + if len(data) == 0 { + fmt.Fprintln(w, " \t ") + return + } + + for _, value := range data { + fmt.Fprintln(w, " \t - "+value) + } + + return +} + +func printResources(w *tabwriter.Writer, name string, data *types.FunctionResources) { + fmt.Fprintf(w, name+":") + + if data == nil { + fmt.Fprintln(w, " \t ") + return + } + + fmt.Fprintln(w, " \t CPU: "+data.CPU) + fmt.Fprintln(w, " \t Memory: "+data.Memory) + + return +} diff --git a/schema/describe.go b/schema/describe.go index 1450cee3..c3b5e298 100644 --- a/schema/describe.go +++ b/schema/describe.go @@ -7,16 +7,9 @@ import "github.com/openfaas/faas-provider/types" //FunctionDescription information related to a function type FunctionDescription struct { - Name string - Status string - Replicas int - AvailableReplicas int - InvocationCount int - Image string - EnvProcess string - URL string - AsyncURL string - Labels *map[string]string - Annotations *map[string]string - Usage *types.FunctionUsage + types.FunctionStatus + Status string + InvocationCount int + URL string + AsyncURL string }