From 428d7e9e9569709d6966dbee2563a239d16c34be Mon Sep 17 00:00:00 2001 From: Francesco Cheinasso Date: Tue, 25 Jul 2023 19:36:10 +0200 Subject: [PATCH] Customize resource quota cap --- operators/cmd/tenant-operator/main.go | 4 +++ .../tenant-operator/templates/deployment.yaml | 3 +++ operators/deploy/tenant-operator/values.yaml | 8 ++++++ operators/pkg/forge/resourcequota.go | 26 ++++++++++++------- operators/pkg/forge/resourcequota_test.go | 3 +++ 5 files changed, 34 insertions(+), 10 deletions(-) diff --git a/operators/cmd/tenant-operator/main.go b/operators/cmd/tenant-operator/main.go index 837d55c21..2692795f2 100644 --- a/operators/cmd/tenant-operator/main.go +++ b/operators/cmd/tenant-operator/main.go @@ -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" @@ -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", 25, "The cap amount of CPU cores that can be requested by a Tenant.") + flag.IntVar(&forge.CapMemoryGiga, "cap-memory-giga", 50, "The cap amount of RAM memory in gigabytes that can be requested by a Tenant.") klog.InitFlags(nil) flag.Parse() diff --git a/operators/deploy/tenant-operator/templates/deployment.yaml b/operators/deploy/tenant-operator/templates/deployment.yaml index 7154a9503..b7c7670d8 100644 --- a/operators/deploy/tenant-operator/templates/deployment.yaml +++ b/operators/deploy/tenant-operator/templates/deployment.yaml @@ -49,6 +49,9 @@ spec: - "--mydrive-pvcs-size={{ .Values.configurations.mydrivePVCsSize }}" - "--mydrive-pvcs-storage-class-name={{ .Values.configurations.mydrivePVCsStorageClassName }}" - "--mydrive-pvcs-namespace={{ .Values.configurations.mydrivePVCsNamespace }}" + - "--cap-instance={{ .Values.configurations.tenant.resourcecaps.instances }}" + - "--cap-cpu={{ .Values.configurations.tenant.resourcecaps.cpu }}" + - "--cap-memory-giga={{ .Values.configurations.tenant.resourcecaps.memory }}" ports: - name: metrics containerPort: 8080 diff --git a/operators/deploy/tenant-operator/values.yaml b/operators/deploy/tenant-operator/values.yaml index 10ce93c15..c5af1bbb1 100644 --- a/operators/deploy/tenant-operator/values.yaml +++ b/operators/deploy/tenant-operator/values.yaml @@ -19,6 +19,14 @@ configurations: maxConcurrentReconciles: 1 sandboxClusterRole: crownlabs-sandbox tenantNamespaceKeepAlive: 168h + tenant: + resourcecaps: + # Maximum number of virtual cpus that can be requested by a tenant. 0 means unlimited. + cpu: 25 + # Maximum amount of memory that can be requested by a tenant (expressed in GB). 0 means unlimited. + memory: 50 + # Maximum number of instances that can be created by a tenant. 0 means unlimited. + instances: 10 image: repository: crownlabs/tenant-operator diff --git a/operators/pkg/forge/resourcequota.go b/operators/pkg/forge/resourcequota.go index aa21880dd..5208f0e8c 100644 --- a/operators/pkg/forge/resourcequota.go +++ b/operators/pkg/forge/resourcequota.go @@ -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 ( - // CapCPU -> The cap amount of CPU cores that can be requested by a Tenant. - CapCPU = *resource.NewQuantity(25, resource.DecimalSI) + // CapInstance -> The maximum number of instances that can be started by a Tenant. + CapInstance int + + // CapCPU -> The total amount of CPU cores that can be requested by a Tenant. + CapCPU int - // CapMemory -> The cap amount of RAM memory that can be requested by a Tenant. - CapMemory = *resource.NewScaledQuantity(50, resource.Giga) + // CapMemoryGiga -> The total amount of RAM (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) @@ -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 } diff --git a/operators/pkg/forge/resourcequota_test.go b/operators/pkg/forge/resourcequota_test.go index 15092fcf8..1e8be9284 100644 --- a/operators/pkg/forge/resourcequota_test.go +++ b/operators/pkg/forge/resourcequota_test.go @@ -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() {