Skip to content

Commit 187006f

Browse files
author
Moritz Clasmeier
committed
Add new tiny resource profile
1 parent 83fc530 commit 187006f

7 files changed

Lines changed: 211 additions & 5 deletions

File tree

internal/clusterdefaults/clusterdefaults.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func ApplyClusterDefaults(
3030
return nil, fmt.Errorf("deep-copying cluster defaults: %w", err)
3131
}
3232

33-
if err := mergo.Merge(config, defaultsCopy, mergo.WithoutDereference); err != nil {
33+
if err := mergo.Merge(config, defaultsCopy, mergo.WithoutDereference, mergo.WithAppendSlice); err != nil {
3434
return nil, fmt.Errorf("merging-in cluster defaults: %w", err)
3535
}
3636

internal/deployer/constants.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,20 @@ var (
66
centralCrName = "stackrox-central-services"
77
securedClusterCrName = "stackrox-secured-cluster-services"
88

9+
centralDbPVCSizeTiny = "10Gi"
910
centralDbPVCSizeSmall = "30Gi"
1011

12+
centralResourcesTiny = map[string]interface{}{
13+
"requests": map[string]string{
14+
"memory": "300Mi",
15+
"cpu": "90m",
16+
},
17+
"limits": map[string]string{
18+
"memory": "2Gi",
19+
"cpu": "1",
20+
},
21+
}
22+
1123
centralResourcesSmall = map[string]interface{}{
1224
"requests": map[string]string{
1325
"memory": "1Gi",
@@ -19,6 +31,17 @@ var (
1931
},
2032
}
2133

34+
centralDbResourcesTiny = map[string]interface{}{
35+
"requests": map[string]string{
36+
"memory": "400Mi",
37+
"cpu": "80m",
38+
},
39+
"limits": map[string]string{
40+
"memory": "2Gi",
41+
"cpu": "1",
42+
},
43+
}
44+
2245
centralDbResourcesSmall = map[string]interface{}{
2346
"requests": map[string]string{
2447
"memory": "1Gi",
@@ -52,6 +75,17 @@ var (
5275
},
5376
}
5477

78+
centralScannerV4DbResourcesTiny = map[string]interface{}{
79+
"requests": map[string]string{
80+
"memory": "400Mi",
81+
"cpu": "80m",
82+
},
83+
"limits": map[string]string{
84+
"memory": "2000Mi",
85+
"cpu": "1000m",
86+
},
87+
}
88+
5589
centralScannerV4DbResourcesSmall = map[string]interface{}{
5690
"requests": map[string]string{
5791
"memory": "512Mi",
@@ -63,6 +97,17 @@ var (
6397
},
6498
}
6599

100+
centralScannerV4IndexerResourcesTiny = map[string]interface{}{
101+
"requests": map[string]string{
102+
"memory": "300Mi",
103+
"cpu": "80m",
104+
},
105+
"limits": map[string]string{
106+
"memory": "2Gi",
107+
"cpu": "2000m",
108+
},
109+
}
110+
66111
centralScannerV4IndexerResourcesSmall = map[string]interface{}{
67112
"requests": map[string]string{
68113
"memory": "512Mi",
@@ -74,6 +119,17 @@ var (
74119
},
75120
}
76121

122+
centralScannerV4MatcherResourcesTiny = map[string]interface{}{
123+
"requests": map[string]string{
124+
"memory": "300Mi",
125+
"cpu": "80m",
126+
},
127+
"limits": map[string]string{
128+
"memory": "2Gi",
129+
"cpu": "1000m",
130+
},
131+
}
132+
77133
centralScannerV4MatcherResourcesSmall = map[string]interface{}{
78134
"requests": map[string]string{
79135
"memory": "512Mi",
@@ -87,6 +143,17 @@ var (
87143

88144
// Secured Cluster
89145

146+
securedClusterSensorResourcesTiny = map[string]interface{}{
147+
"requests": map[string]string{
148+
"memory": "300Mi",
149+
"cpu": "80m",
150+
},
151+
"limits": map[string]string{
152+
"memory": "2Gi",
153+
"cpu": "1000m",
154+
},
155+
}
156+
90157
securedClusterSensorResourcesSmall = map[string]interface{}{
91158
"requests": map[string]string{
92159
"memory": "500Mi",

internal/deployer/deploy_via_operator.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,38 @@ func (d *Deployer) createAdminPasswordSecret(ctx context.Context) error {
259259

260260
func getCentralResourcesOperator(resourcesProfile types.ResourceProfile) map[string]interface{} {
261261
switch resourcesProfile {
262+
case types.ResourceProfileTiny:
263+
return map[string]interface{}{
264+
"spec": map[string]interface{}{
265+
"central": map[string]interface{}{
266+
"resources": centralResourcesTiny,
267+
"db": map[string]interface{}{
268+
"resources": centralDbResourcesTiny,
269+
"persistence": map[string]interface{}{
270+
"persistentVolumeClaim": map[string]interface{}{
271+
"size": centralDbPVCSizeTiny,
272+
},
273+
},
274+
},
275+
},
276+
"scanner": map[string]interface{}{
277+
"scannerComponent": "Disabled",
278+
},
279+
"scannerV4": map[string]interface{}{
280+
"db": map[string]interface{}{
281+
"resources": centralScannerV4DbResourcesTiny,
282+
},
283+
"indexer": map[string]interface{}{
284+
"resources": centralScannerV4IndexerResourcesTiny,
285+
"scaling": noScaling,
286+
},
287+
"matcher": map[string]interface{}{
288+
"resources": centralScannerV4MatcherResourcesTiny,
289+
"scaling": noScaling,
290+
},
291+
},
292+
},
293+
}
262294
case types.ResourceProfileSmall:
263295
return map[string]interface{}{
264296
"spec": map[string]interface{}{
@@ -705,9 +737,44 @@ func (d *Deployer) deploySecuredClusterOperator(ctx context.Context) error {
705737

706738
func getSecuredClusterResourcesOperator(resourceProfile types.ResourceProfile) map[string]interface{} {
707739
switch resourceProfile {
740+
case types.ResourceProfileTiny:
741+
return map[string]interface{}{
742+
"spec": map[string]interface{}{
743+
"admissionControl": map[string]interface{}{
744+
"replicas": 1,
745+
},
746+
"sensor": map[string]interface{}{
747+
"resources": securedClusterSensorResourcesTiny,
748+
},
749+
"scanner": map[string]interface{}{
750+
"scannerComponent": "Disabled",
751+
},
752+
"scannerV4": map[string]interface{}{
753+
"scannerComponent": "Disabled",
754+
},
755+
// The "tiny" resource profile also patches the resources down for some containers, which do not
756+
// have first-class configurability exposed in the CR.
757+
"overlays": []map[string]interface{}{
758+
{
759+
"apiVersion": "apps/v1",
760+
"kind": "Deployment",
761+
"name": "sensor",
762+
"patches": []map[string]interface{}{
763+
{
764+
"path": "spec.template.spec.initContainers[name:crs].resources.requests",
765+
"value": `{"cpu":"80m", "memory": "150m"}`,
766+
},
767+
},
768+
},
769+
},
770+
},
771+
}
708772
case types.ResourceProfileSmall:
709773
return map[string]interface{}{
710774
"spec": map[string]interface{}{
775+
"admissionControl": map[string]interface{}{
776+
"replicas": 1,
777+
},
711778
"sensor": map[string]interface{}{
712779
"resources": securedClusterSensorResourcesSmall,
713780
},

internal/helpers/helpers.go

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,16 +113,16 @@ func deepCopySlice(s []interface{}) []interface{} {
113113
return result
114114
}
115115

116-
// deepMerge recursively merges overlay into base
116+
// DeepMerge recursively merges overlay into base.
117117
func DeepMerge(base, overlay map[string]interface{}) error {
118118
for k, v := range overlay {
119119
if IsNil(v) {
120120
continue
121121
}
122122
if baseVal, ok := base[k]; ok {
123-
// Both are maps - merge recursively
124123
if baseMap, baseIsMap := baseVal.(map[string]interface{}); baseIsMap {
125124
if overlayMap, overlayIsMap := v.(map[string]interface{}); overlayIsMap {
125+
// Both are maps - merge recursively.
126126
if err := DeepMerge(baseMap, overlayMap); err != nil {
127127
return err
128128
}
@@ -131,13 +131,41 @@ func DeepMerge(base, overlay map[string]interface{}) error {
131131
return fmt.Errorf("incompatible types in maps to merge (map vs. %T)", v)
132132
}
133133
}
134+
if _, ok := v.(map[string]interface{}); ok {
135+
return fmt.Errorf("incompatible types for key %q: %T vs. map", k, baseVal)
136+
}
137+
138+
if baseSlice, ok := toSlice(baseVal); ok {
139+
if overlaySlice, ok := toSlice(v); ok {
140+
base[k] = append(baseSlice, overlaySlice...)
141+
continue
142+
}
143+
return fmt.Errorf("incompatible types for key %q: slice vs. %T", k, v)
144+
}
145+
if _, ok := toSlice(v); ok {
146+
return fmt.Errorf("incompatible types for key %q: %T vs. slice", k, baseVal)
147+
}
134148
}
135-
// Override with overlay value
149+
136150
base[k] = v
137151
}
138152
return nil
139153
}
140154

155+
func toSlice(v interface{}) ([]interface{}, bool) {
156+
if s, ok := v.([]interface{}); ok {
157+
return s, true
158+
}
159+
if s, ok := v.([]map[string]interface{}); ok {
160+
out := make([]interface{}, len(s))
161+
for i, e := range s {
162+
out[i] = e
163+
}
164+
return out, true
165+
}
166+
return nil, false
167+
}
168+
141169
func MapToStruct(m map[string]interface{}, out interface{}) error {
142170
bytes, err := yaml.Marshal(m)
143171
if err != nil {

internal/helpers/helpers_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,48 @@ func TestLoadYAMLFileInvalidYAML(t *testing.T) {
147147
}
148148
}
149149

150+
func TestDeepMergeAppendsSlices(t *testing.T) {
151+
base := map[string]interface{}{
152+
"spec": map[string]interface{}{
153+
"imagePullSecrets": []interface{}{
154+
map[string]interface{}{"name": "secret-a"},
155+
},
156+
"keep": "this",
157+
},
158+
}
159+
160+
overlay := map[string]interface{}{
161+
"spec": map[string]interface{}{
162+
"imagePullSecrets": []interface{}{
163+
map[string]interface{}{"name": "secret-b"},
164+
},
165+
},
166+
}
167+
168+
err := DeepMerge(base, overlay)
169+
require.NoError(t, err)
170+
171+
spec := base["spec"].(map[string]interface{})
172+
require.Equal(t, "this", spec["keep"])
173+
174+
secrets := spec["imagePullSecrets"].([]interface{})
175+
require.Len(t, secrets, 2)
176+
require.Equal(t, map[string]interface{}{"name": "secret-a"}, secrets[0])
177+
require.Equal(t, map[string]interface{}{"name": "secret-b"}, secrets[1])
178+
}
179+
180+
func TestDeepMergeSliceTypeMismatch(t *testing.T) {
181+
base := map[string]interface{}{
182+
"items": []interface{}{"a"},
183+
}
184+
overlay := map[string]interface{}{
185+
"items": "not-a-slice",
186+
}
187+
188+
err := DeepMerge(base, overlay)
189+
require.ErrorContains(t, err, "incompatible types")
190+
}
191+
150192
func TestDeepCopy(t *testing.T) {
151193
original := map[string]interface{}{
152194
"a": "value",

internal/types/resources.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ type ResourceProfile int
1111
const (
1212
ResourceProfileAcsDefaults ResourceProfile = iota
1313
ResourceProfileAuto
14+
ResourceProfileTiny
1415
ResourceProfileSmall
1516
ResourceProfileMedium
1617
ResourceProfileCI
@@ -20,6 +21,7 @@ var (
2021
resourceProfileNames = map[ResourceProfile]string{
2122
ResourceProfileAcsDefaults: "acs-defaults",
2223
ResourceProfileAuto: "auto",
24+
ResourceProfileTiny: "tiny",
2325
ResourceProfileSmall: "small",
2426
ResourceProfileMedium: "medium",
2527
ResourceProfileCI: "ci",

tests/e2e/helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func deployArgsFromEnv() []string {
3939
if profile == "" {
4040
profile = "small"
4141
}
42-
return []string{"--resources=" + profile}
42+
return []string{"--verbose", "--resources=" + profile}
4343
}
4444

4545
func teardownAllDeployments() error {

0 commit comments

Comments
 (0)