From 9981e9ea7065d5dc2d4a17013aca04a1c97fe4df Mon Sep 17 00:00:00 2001 From: "Alex Ellis (OpenFaaS Ltd)" Date: Mon, 20 Nov 2023 10:28:12 +0000 Subject: [PATCH] Add memory/CPU requests and limits to deploy command * Allows for memory/CPU requests and limits to be passed via deploy and store deploy when no additional YAML file is being used. Tested by supplying requests values and looking at the output of faas-cli describe. Signed-off-by: Alex Ellis (OpenFaaS Ltd) --- commands/deploy.go | 40 ++++++++++++++++++++++++++++++++++++++-- commands/store_deploy.go | 22 ++++++++++++++++++++-- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/commands/deploy.go b/commands/deploy.go index 34b784f0..10baf2e4 100644 --- a/commands/deploy.go +++ b/commands/deploy.go @@ -77,6 +77,11 @@ func init() { deployCmd.Flags().DurationVar(&timeoutOverride, "timeout", commandTimeout, "Timeout for any HTTP calls made to the OpenFaaS API.") + deployCmd.Flags().StringVar(&cpuRequest, "cpu-request", "", "Supply the CPU request for the function in Mi (when not using a YAML file)") + deployCmd.Flags().StringVar(&cpuLimit, "cpu-limit", "", "Supply the CPU limit for the function in Mi (when not using a YAML file)") + deployCmd.Flags().StringVar(&memoryRequest, "memory-request", "", "Supply the memory request for the function in Mi (when not using a YAML file)") + deployCmd.Flags().StringVar(&memoryLimit, "memory-limit", "", "Supply the memory limit for the function in Mi (when not using a YAML file)") + faasCmd.AddCommand(deployCmd) } @@ -290,6 +295,7 @@ Error: %s`, fprocessErr.Error()) if len(image) == 0 || len(functionName) == 0 { return fmt.Errorf("to deploy a function give --yaml/-f or a --image and --name flag") } + gateway = getGatewayURL(gateway, defaultGateway, "", os.Getenv(openFaaSURLEnvironment)) cliAuth, err := proxy.NewCLIAuth(token, gateway) if err != nil { @@ -303,8 +309,21 @@ Error: %s`, fprocessErr.Error()) // default to a readable filesystem until we get more input about the expected behavior // and if we want to add another flag for this case defaultReadOnlyRFS := false - statusCode, err := deployImage(ctx, proxyClient, image, fprocess, functionName, "", deployFlags, - tlsInsecure, defaultReadOnlyRFS, token, functionNamespace) + statusCode, err := deployImage(ctx, + proxyClient, + image, + fprocess, + functionName, + "", + deployFlags, + tlsInsecure, + defaultReadOnlyRFS, + token, + functionNamespace, + cpuRequest, + cpuLimit, + memoryRequest, + memoryLimit) if err != nil { return err } @@ -334,6 +353,10 @@ func deployImage( readOnlyRootFilesystem bool, token string, namespace string, + cpuRequest string, + cpuLimit string, + memoryRequest string, + memoryLimit string, ) (int, error) { var statusCode int @@ -375,6 +398,19 @@ func deployImage( Namespace: namespace, } + if len(cpuRequest) > 0 || len(memoryRequest) > 0 { + deploySpec.FunctionResourceRequest.Requests = &stack.FunctionResources{ + CPU: cpuRequest, + Memory: memoryRequest, + } + } + if len(cpuLimit) > 0 || len(memoryLimit) > 0 { + deploySpec.FunctionResourceRequest.Limits = &stack.FunctionResources{ + CPU: cpuLimit, + Memory: memoryLimit, + } + } + if msg := checkTLSInsecure(gateway, deploySpec.TLSInsecure); len(msg) > 0 { fmt.Println(msg) } diff --git a/commands/store_deploy.go b/commands/store_deploy.go index 737a12c4..9e2581e6 100644 --- a/commands/store_deploy.go +++ b/commands/store_deploy.go @@ -31,6 +31,11 @@ func init() { storeDeployCmd.Flags().StringVarP(&token, "token", "k", "", "Pass a JWT token to use instead of basic auth") storeDeployCmd.Flags().DurationVar(&timeoutOverride, "timeout", commandTimeout, "Timeout for any HTTP calls made to the OpenFaaS API.") + storeDeployCmd.Flags().StringVar(&cpuRequest, "cpu-request", "", "Supply the CPU request for the function in Mi") + storeDeployCmd.Flags().StringVar(&cpuLimit, "cpu-limit", "", "Supply the CPU limit for the function in Mi") + storeDeployCmd.Flags().StringVar(&memoryRequest, "memory-request", "", "Supply the memory request for the function in Mi") + storeDeployCmd.Flags().StringVar(&memoryLimit, "memory-limit", "", "Supply the memory limit for the function in Mi") + // Set bash-completion. _ = storeDeployCmd.Flags().SetAnnotation("handler", cobra.BashCompSubdirsInDir, []string{}) @@ -134,8 +139,21 @@ func runStoreDeploy(cmd *cobra.Command, args []string) error { return err } - statusCode, err := deployImage(context.Background(), proxyClient, imageName, item.Fprocess, itemName, "", storeDeployFlags, - tlsInsecure, item.ReadOnlyRootFilesystem, token, functionNamespace) + statusCode, err := deployImage(context.Background(), + proxyClient, + imageName, + item.Fprocess, + itemName, + "", + storeDeployFlags, + tlsInsecure, + item.ReadOnlyRootFilesystem, + token, + functionNamespace, + cpuRequest, + cpuLimit, + memoryRequest, + memoryLimit) if badStatusCode(statusCode) { failedStatusCode := map[string]int{itemName: statusCode}