Skip to content

Commit

Permalink
Add memory/CPU requests and limits to deploy command
Browse files Browse the repository at this point in the history
* 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) <[email protected]>
  • Loading branch information
alexellis committed Nov 20, 2023
1 parent f72db8d commit 9981e9e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 4 deletions.
40 changes: 38 additions & 2 deletions commands/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
22 changes: 20 additions & 2 deletions commands/store_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{})

Expand Down Expand Up @@ -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}
Expand Down

0 comments on commit 9981e9e

Please sign in to comment.