Skip to content

Commit

Permalink
Add min CPU and Memory to workload container (#66)
Browse files Browse the repository at this point in the history
* Add min CPU and Memory to workload container

* Fix example

* Update pipeline timeout

* Update docs for 1.1.18

---------

Co-authored-by: Eric K <[email protected]>
  • Loading branch information
MajidAbuRmila and enk21 authored Feb 18, 2024
1 parent 25007e7 commit a64e55e
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 9 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Run-Acc-And-Unit-Tests
on:
push:
branches-ignore:
- 'v*'
- "v*"

workflow_dispatch:

Expand All @@ -28,7 +28,7 @@ jobs:
run: go build -v ./...

- name: Run Tests
run: go test -v ./...
run: go test -v ./... -timeout 30m
env:
TF_ACC: true
CPLN_ORG: terraform-test-org
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ HOSTNAME=controlplane.com
NAMESPACE=com
NAME=cpln
BINARY=terraform-provider-${NAME}
VERSION=1.1.17
VERSION=1.1.18
OS_ARCH=linux_amd64

default: install
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,4 +268,5 @@ tar -cvzf terraform-provider-cpln_1.0.0_windows_amd64.zip terraform-provider-cpl
- v1.1.15 - Fix issue with tag values that were stored as number types.
- v1.1.16 - Add org creation. Add org properties.
- v1.1.17 - Add schedule to volume set. Add status to domain output. Add Control Plane tracing and custom tags to tracing. Add external ID to ECR secret. Add generic elastic to org logging. Add geo properties to locations. Add missing properties to workload.
- v1.1.18 - Add min CPU and Memory to container.

2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ terraform {
required_providers {
cpln = {
source = "controlplane-com/cpln"
version = "1.1.17"
version = "1.1.18"
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions docs/resources/workload.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ Containers which attempt to use these ports will not be able to bind:
- **inherit_env** (Boolean) Enables inheritance of GVC environment variables. A variable in spec.env will override a GVC variable with the same name.
- **cpu** (String) Reserved CPU of the workload when capacityAI is disabled. Maximum CPU when CapacityAI is enabled. Default: "50m".
- **memory** (String) Reserved memory of the workload when capacityAI is disabled. Maximum memory when CapacityAI is enabled. Default: "128Mi".
- **min_cpu** (String) Minimum CPU when capacity AI is enabled.
- **min_memory** (String) Minimum memory when capacity AI is enabled.
- **gpu_nvidia** (Block List, Max: 1) ([see below](#nestedblock--container--gpu_nvidia))
- **liveness_probe** (Block List, Max: 1) Liveness Probe ([see below](#nestedblock--container--liveness_probe)).
- **readiness_probe** (Block List, Max: 1) Readiness Probe ([see below](#nestedblock--container--readiness_probe)).
Expand Down Expand Up @@ -476,6 +478,9 @@ resource "cpln_workload" "new" {
memory = "128Mi"
cpu = "50m"
min_memory = "56Mi"
min_cpu = "25m"
ports {
protocol = "http"
number = "8080"
Expand Down Expand Up @@ -547,7 +552,7 @@ resource "cpln_workload" "new" {
}
options {
capacity_ai = false
capacity_ai = true
timeout_seconds = 30
suspend = false
Expand All @@ -563,7 +568,7 @@ resource "cpln_workload" "new" {
local_options {
location = "aws-us-west-2"
capacity_ai = false
capacity_ai = true
timeout_seconds = 30
suspend = false
Expand Down
2 changes: 2 additions & 0 deletions internal/provider/client/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ type ContainerSpec struct {
LivenessProbe *HealthCheckSpec `json:"livenessProbe,omitempty"`
CPU *string `json:"cpu,omitempty"`
GPU *GpuResource `json:"gpu,omitempty"`
MinCPU *string `json:"minCpu,omitempty"`
MinMemory *string `json:"minMemory,omitempty"`
Env *[]NameValue `json:"env,omitempty"`
Args *[]string `json:"args,omitempty"`
Volumes *[]VolumeSpec `json:"volumes,omitempty"`
Expand Down
61 changes: 58 additions & 3 deletions internal/provider/resource_workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,16 @@ func resourceWorkload() *schema.Resource {
Default: "128Mi",
ValidateFunc: CpuMemoryValidator,
},
"min_cpu": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: CpuMemoryValidator,
},
"min_memory": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: CpuMemoryValidator,
},
"working_directory": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -1020,6 +1030,14 @@ func buildContainers(containers []interface{}, workloadSpec *client.WorkloadSpec
newContainer.GPU = buildGpuNvidia(c["gpu_nvidia"].([]interface{}))
}

if c["min_cpu"] != nil {
newContainer.MinCPU = GetString(c["min_cpu"].(string))
}

if c["min_memory"] != nil {
newContainer.MinMemory = GetString(c["min_memory"].(string))
}

if c["port"] != nil && c["port"].(int) > 0 {
// newContainer.Port = GetPortInt(c["port"])

Expand Down Expand Up @@ -1861,6 +1879,14 @@ func flattenContainer(containers *[]client.ContainerSpec, legacyPort bool) []int
c["gpu_nvidia"] = flattenGpuNvidia(container.GPU)
}

if container.MinCPU != nil {
c["min_cpu"] = *container.MinCPU
}

if container.MinMemory != nil {
c["min_memory"] = *container.MinMemory
}

if container.Command != nil {
c["command"] = *container.Command
}
Expand Down Expand Up @@ -2787,6 +2813,9 @@ func workloadSpecValidate(workloadSpec *client.WorkloadSpec) diag.Diagnostics {
return diag.FromErr(fmt.Errorf("rollout options are only available when workload type is 'standard'"))
}

hasMinCpu := false
hasMinMemory := false

for _, c := range *workloadSpec.Containers {

if *workloadSpec.Type == "cron" {
Expand All @@ -2811,17 +2840,25 @@ func workloadSpecValidate(workloadSpec *client.WorkloadSpec) diag.Diagnostics {
return diag.FromErr(fmt.Errorf("capacity AI must be disabled when using GPUs. Please remove the GPU selection from the containers or disable Capacity AI"))
}
}

if c.MinCPU != nil {
hasMinCpu = true
}

if c.MinMemory != nil {
hasMinMemory = true
}
}

if workloadSpec.DefaultOptions != nil {
if e := validateOptions(*workloadSpec.Type, "", workloadSpec.DefaultOptions); e != nil {
if e := validateOptions(*workloadSpec.Type, "", workloadSpec.DefaultOptions, hasMinCpu, hasMinMemory); e != nil {
return e
}
}

if workloadSpec.LocalOptions != nil && len(*workloadSpec.LocalOptions) > 0 {
for _, o := range *workloadSpec.LocalOptions {
if e := validateOptions(*workloadSpec.Type, "local_options - ", &o); e != nil {
if e := validateOptions(*workloadSpec.Type, "local_options - ", &o, hasMinCpu, hasMinMemory); e != nil {
return e
}
}
Expand All @@ -2831,14 +2868,22 @@ func workloadSpecValidate(workloadSpec *client.WorkloadSpec) diag.Diagnostics {
return nil
}

func validateOptions(workloadType, errorMsg string, options *client.Options) diag.Diagnostics {
func validateOptions(workloadType, errorMsg string, options *client.Options, hasMinCpu bool, hasMinMemory bool) diag.Diagnostics {

if options != nil && options.AutoScaling != nil {
if workloadType == "cron" {
if options.CapacityAI != nil && *options.CapacityAI {
return diag.FromErr(fmt.Errorf(errorMsg + "capacity AI must be false when workload type is 'cron'"))
}

if hasMinCpu {
return diag.FromErr(fmt.Errorf("min_cpu is not allowed for workload of type cron"))
}

if hasMinMemory {
return diag.FromErr(fmt.Errorf("min_memory is not allowed for workload of type cron"))
}

if options.AutoScaling.MinScale != nil && *options.AutoScaling.MinScale != 1 {
return diag.FromErr(fmt.Errorf(errorMsg + "min scale must be set to 1 when workload type is 'cron'"))
}
Expand All @@ -2850,6 +2895,16 @@ func validateOptions(workloadType, errorMsg string, options *client.Options) dia
if options.AutoScaling.Metric == nil || strings.TrimSpace(*options.AutoScaling.Metric) == "" {
return diag.FromErr(fmt.Errorf(errorMsg + "scaling strategy metric is required"))
}

if options.CapacityAI == nil || !*options.CapacityAI {
if hasMinCpu {
return diag.FromErr(fmt.Errorf("capacity AI must be enabled to include minimum CPU value"))
}

if hasMinMemory {
return diag.FromErr(fmt.Errorf("capacity AI must be enabled to include minimum memory value"))
}
}
}
}

Expand Down
Loading

0 comments on commit a64e55e

Please sign in to comment.