Skip to content

Commit

Permalink
Customize resource quota cap
Browse files Browse the repository at this point in the history
  • Loading branch information
cheina97 committed Nov 13, 2023
1 parent 67b9c36 commit 3eb4ac1
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
4 changes: 4 additions & 0 deletions operators/cmd/tenant-operator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import (

clv1alpha1 "github.com/netgroup-polito/CrownLabs/operators/api/v1alpha1"
clv1alpha2 "github.com/netgroup-polito/CrownLabs/operators/api/v1alpha2"
"github.com/netgroup-polito/CrownLabs/operators/pkg/forge"
controllers "github.com/netgroup-polito/CrownLabs/operators/pkg/tenant-controller"
"github.com/netgroup-polito/CrownLabs/operators/pkg/tenantwh"
"github.com/netgroup-polito/CrownLabs/operators/pkg/utils/args"
Expand Down Expand Up @@ -99,6 +100,9 @@ func main() {
flag.Var(&mydrivePVCsSize, "mydrive-pvcs-size", "The dimension of the user's personal space")
flag.StringVar(&mydrivePVCsStorageClassName, "mydrive-pvcs-storage-class-name", "rook-nfs", "The name for the user's storage class")
flag.StringVar(&myDrivePVCsNamespace, "mydrive-pvcs-namespace", "mydrive-pvcs", "The namespace where the PVCs are created")
flag.IntVar(&forge.CapInstance, "cap-instance", 10, "The cap number of instances that can be requested by a Tenant.")
flag.IntVar(&forge.CapCPU, "cap-cpu", 4, "The cap amount of CPU cores that can be requested by a Tenant.")
flag.IntVar(&forge.CapMemoryGiga, "cap-memory-giga", 8, "The cap amount of RAM memory in gigabytes that can be requested by a Tenant.")

klog.InitFlags(nil)
flag.Parse()
Expand Down
24 changes: 15 additions & 9 deletions operators/pkg/forge/resourcequota.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@ import (
const (
// InstancesCountKey -> The key for accessing at the total number of instances in the corev1.ResourceList map.
InstancesCountKey = "count/instances.crownlabs.polito.it"

// CapInstance -> The cap number of instances that can be requested by a Tenant.
CapInstance = 10
)

var (
// CapInstance -> The cap number of instances that can be requested by a Tenant.
CapInstance int

// CapCPU -> The cap amount of CPU cores that can be requested by a Tenant.
CapCPU = *resource.NewQuantity(25, resource.DecimalSI)
CapCPU int

// CapMemory -> The cap amount of RAM memory that can be requested by a Tenant.
CapMemory = *resource.NewScaledQuantity(50, resource.Giga)
// CapMemoryGiga -> The cap amount of RAM memory in gigabytes that can be requested by a Tenant.
CapMemoryGiga int

// SandboxCPUQuota -> The maximum amount of CPU cores that can be used by a sandbox namespace.
SandboxCPUQuota = *resource.NewQuantity(4, resource.DecimalSI)
Expand All @@ -62,9 +62,15 @@ func TenantResourceList(workspaces []clv1alpha1.Workspace, override *clv1alpha2.
quota.Instances += workspaces[i].Spec.Quota.Instances
}

quota.CPU = CapResourceQuantity(quota.CPU, CapCPU)
quota.Memory = CapResourceQuantity(quota.Memory, CapMemory)
quota.Instances = CapIntegerQuantity(quota.Instances, CapInstance)
if CapCPU > 0 {
quota.CPU = CapResourceQuantity(quota.CPU, *resource.NewQuantity(int64(CapCPU), resource.DecimalSI))
}
if CapMemoryGiga > 0 {
quota.Memory = CapResourceQuantity(quota.Memory, *resource.NewScaledQuantity(int64(CapMemoryGiga), resource.Giga))
}
if CapInstance > 0 {
quota.Instances = CapIntegerQuantity(quota.Instances, uint32(CapInstance))
}

return quota
}
Expand Down
3 changes: 3 additions & 0 deletions operators/pkg/forge/resourcequota_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ var _ = Describe("Resource quota spec forging", func() {
sampleWorkspace1.Spec.Quota = quota1
sampleWorkspace2.Spec.Quota = quota2
workspaces = append(workspaces, sampleWorkspace1, sampleWorkspace2)
forge.CapCPU = 25
forge.CapMemoryGiga = 40
forge.CapInstance = 5
})

JustBeforeEach(func() {
Expand Down

0 comments on commit 3eb4ac1

Please sign in to comment.