Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add memory/CPU requests and limits to deploy command #987

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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