Skip to content

Commit 168a98f

Browse files
committed
Add support for sizing sandboxes
1 parent 2ffe6fd commit 168a98f

File tree

7 files changed

+86
-8
lines changed

7 files changed

+86
-8
lines changed

README.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ kind: Sandbox
9494
metadata:
9595
name: foo
9696
spec:
97+
size: small
9798
owners:
9899
99100
```
@@ -137,9 +138,33 @@ One `RoleBinding` per name in the `owners` field
137138

138139
### ResourceQuota (sandbox-foo-resourcequota)
139140

141+
The `ResourceQuota` that is applied to the `Namespace` depends on the `size` of the `Sandbox` that was created.
142+
143+
#### Small
144+
140145
|Resource Name|Quantity|
141146
|---|---|
142-
|ResourceRequestsMemory|1Gi|
147+
|ResourceRequestsCPU|0.25|
148+
|ResourceLimitsCPU|0.5|
149+
|ResourceRequestsMemory|250Mi|
150+
|ResourceLimitsMemory|500Mi|
151+
|ResourceRequestsStorage|10Gi|
152+
|ResourcePersistentVolumeClaims|2|
153+
154+
#### Large
155+
156+
|Resource Name|Quantity|
157+
|---|---|
158+
|ResourceRequestsCPU|1|
159+
|ResourceLimitsCPU|2|
160+
|ResourceRequestsMemory|2Gi|
161+
|ResourceLimitsMemory|8Gi|
162+
|ResourceRequestsStorage|40Gi|
163+
|ResourcePersistentVolumeClaims|8|
164+
165+
```text
166+
NOTE: If no size is given, small is the default.
167+
```
143168

144169
## Managing Owners of a Sandbox
145170

@@ -153,6 +178,7 @@ kind: Sandbox
153178
metadata:
154179
name: foo
155180
spec:
181+
size: small
156182
owners:
157183
158184

apis/operators/v1alpha1/sandbox_types.go

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
// +k8s:openapi-gen=true
99
type SandboxSpec struct {
1010
Owners []string `json:"owners"`
11+
Size string `json:"size"`
1112
}
1213

1314
// SandboxStatus defines the observed state of Sandbox

controller/sandbox.go

+38-5
Original file line numberDiff line numberDiff line change
@@ -302,21 +302,54 @@ func getClusterRoleBinding(sandbox operatorsv1alpha1.Sandbox) rbacv1.ClusterRole
302302
}
303303

304304
func getResourceQuota(sandbox operatorsv1alpha1.Sandbox) corev1.ResourceQuota {
305+
var resourceQuotaSpec corev1.ResourceQuotaSpec
306+
if sandbox.Spec.Size == "large" {
307+
resourceQuotaSpec = getLargeResourceQuotaSpec()
308+
} else {
309+
resourceQuotaSpec = getSmallResourceQuotaSpec()
310+
}
311+
305312
resourceQuota := corev1.ResourceQuota{
306313
ObjectMeta: metav1.ObjectMeta{
307314
Name: "sandbox-" + sandbox.Name + "-resourcequota",
308315
Namespace: "sandbox-" + sandbox.Name,
309316
},
310-
Spec: corev1.ResourceQuotaSpec{
311-
Hard: corev1.ResourceList{
312-
corev1.ResourceRequestsMemory: resource.MustParse("1Gi"),
313-
},
314-
},
317+
Spec: resourceQuotaSpec,
315318
}
316319

317320
return resourceQuota
318321
}
319322

323+
func getLargeResourceQuotaSpec() corev1.ResourceQuotaSpec {
324+
resourceQuotaSpec := corev1.ResourceQuotaSpec{
325+
Hard: corev1.ResourceList{
326+
corev1.ResourceRequestsCPU: resource.MustParse("1"),
327+
corev1.ResourceLimitsCPU: resource.MustParse("2"),
328+
corev1.ResourceRequestsMemory: resource.MustParse("2Gi"),
329+
corev1.ResourceLimitsMemory: resource.MustParse("8Gi"),
330+
corev1.ResourceRequestsStorage: resource.MustParse("40Gi"),
331+
corev1.ResourcePersistentVolumeClaims: resource.MustParse("8"),
332+
},
333+
}
334+
335+
return resourceQuotaSpec
336+
}
337+
338+
func getSmallResourceQuotaSpec() corev1.ResourceQuotaSpec {
339+
resourceQuotaSpec := corev1.ResourceQuotaSpec{
340+
Hard: corev1.ResourceList{
341+
corev1.ResourceRequestsCPU: resource.MustParse("0.25"),
342+
corev1.ResourceLimitsCPU: resource.MustParse("0.5"),
343+
corev1.ResourceRequestsMemory: resource.MustParse("250Mi"),
344+
corev1.ResourceLimitsMemory: resource.MustParse("500Mi"),
345+
corev1.ResourceRequestsStorage: resource.MustParse("10Gi"),
346+
corev1.ResourcePersistentVolumeClaims: resource.MustParse("2"),
347+
},
348+
}
349+
350+
return resourceQuotaSpec
351+
}
352+
320353
// DefaultSubjects represents default subjects
321354
type DefaultSubjects struct{}
322355

controller/sandbox_integration_test.go

+17-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ func TestSandboxControllerIntegration(t *testing.T) {
3737
Name: "test",
3838
},
3939
Spec: operatorsv1alpha1.SandboxSpec{
40+
Size: "small",
4041
Owners: []string{"[email protected]"},
4142
},
4243
}
@@ -78,6 +79,21 @@ func TestSandboxControllerIntegration(t *testing.T) {
7879
t.Errorf("role not found: %v", err)
7980
}
8081

82+
resourceQuota := getResourceQuota(sandbox)
83+
err = wait.PollImmediate(intervalTime, waitTime, func() (bool, error) {
84+
geterr := client.Get(ctx, types.NamespacedName{Namespace: resourceQuota.Namespace, Name: resourceQuota.Name}, &corev1.ResourceQuota{})
85+
if geterr == nil {
86+
return true, nil
87+
} else if errors.IsNotFound(geterr) {
88+
return false, nil
89+
} else {
90+
return false, fmt.Errorf("get resourcequota: %w", geterr)
91+
}
92+
})
93+
if err != nil {
94+
t.Errorf("resourcequota not found: %v", err)
95+
}
96+
8197
if err := client.Delete(ctx, &sandbox); err != nil {
8298
t.Fatalf("delete sandbox: %v", err)
8399
}
@@ -89,7 +105,7 @@ func TestSandboxControllerIntegration(t *testing.T) {
89105
} else if err == nil {
90106
return false, nil
91107
} else {
92-
return false, fmt.Errorf("get sandbox: %v", err)
108+
return false, fmt.Errorf("get namespace: %v", err)
93109
}
94110
})
95111
if err != nil {

example/sandbox.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@ kind: Sandbox
33
metadata:
44
name: test
55
spec:
6+
size: small
67
owners:
78

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ go 1.13
44

55
require (
66
github.com/Azure/azure-sdk-for-go v32.5.0+incompatible
7-
github.com/Azure/go-autorest v13.3.3+incompatible
7+
github.com/Azure/go-autorest v13.3.3+incompatible // indirect
88
github.com/Azure/go-autorest/autorest v0.9.5 // indirect
99
github.com/Azure/go-autorest/autorest/azure/auth v0.4.2
1010
github.com/Azure/go-autorest/autorest/to v0.3.0 // indirect

go.sum

+1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ github.com/Azure/go-autorest/autorest/date v0.2.0 h1:yW+Zlqf26583pE43KhfnhFcdmSW
3737
github.com/Azure/go-autorest/autorest/date v0.2.0/go.mod h1:vcORJHLJEh643/Ioh9+vPmf1Ij9AEBM5FuBIXLmIy0g=
3838
github.com/Azure/go-autorest/autorest/mocks v0.1.0/go.mod h1:OTyCOPRA2IgIlWxVYxBee2F5Gr4kF2zd2J5cFRaIDN0=
3939
github.com/Azure/go-autorest/autorest/mocks v0.2.0/go.mod h1:OTyCOPRA2IgIlWxVYxBee2F5Gr4kF2zd2J5cFRaIDN0=
40+
github.com/Azure/go-autorest/autorest/mocks v0.3.0 h1:qJumjCaCudz+OcqE9/XtEPfvtOjOmKaui4EOpFI6zZc=
4041
github.com/Azure/go-autorest/autorest/mocks v0.3.0/go.mod h1:a8FDP3DYzQ4RYfVAxAN3SVSiiO77gL2j2ronKKP0syM=
4142
github.com/Azure/go-autorest/autorest/to v0.3.0 h1:zebkZaadz7+wIQYgC7GXaz3Wb28yKYfVkkBKwc38VF8=
4243
github.com/Azure/go-autorest/autorest/to v0.3.0/go.mod h1:MgwOyqaIuKdG4TL/2ywSsIWKAfJfgHDo8ObuUk3t5sA=

0 commit comments

Comments
 (0)