Skip to content

Commit a9f14a4

Browse files
authored
Merge pull request #76 from thepetk/ft/add_memorylimit_resource_field
Implement DevfileRegistry CRD MemoryLimit field
2 parents 7c6b2a6 + 0642ab7 commit a9f14a4

File tree

4 files changed

+195
-0
lines changed

4 files changed

+195
-0
lines changed

api/v1alpha1/devfileregistry_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ type DevfileRegistrySpecContainer struct {
8282
// +operator-sdk:csv:customresourcedefinitions:type=spec
8383
// +optional
8484
ImagePullPolicy corev1.PullPolicy `json:"imagePullPolicy,omitempty"`
85+
// Sets the memory limit for the container
86+
// +operator-sdk:csv:customresourcedefinitions:type=spec
87+
// +optional
88+
MemoryLimit string `json:"memoryLimit,omitempty"`
8589
}
8690

8791
// DevfileRegistrySpecStorage defines the desired state of the storage for the DevfileRegistry

config/crd/bases/registry.devfile.io_devfileregistries.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ spec:
5656
imagePullPolicy:
5757
description: Sets the image pull policy for the container
5858
type: string
59+
memoryLimit:
60+
description: Sets the memory limit for the container
61+
type: string
5962
type: object
6063
devfileIndexImage:
6164
description: Sets the container image containing devfile stacks to
@@ -84,6 +87,9 @@ spec:
8487
imagePullPolicy:
8588
description: Sets the image pull policy for the container
8689
type: string
90+
memoryLimit:
91+
description: Sets the memory limit for the container
92+
type: string
8793
type: object
8894
ociRegistryImage:
8995
description: Overrides the container image used for the OCI registry.
@@ -100,6 +106,9 @@ spec:
100106
imagePullPolicy:
101107
description: Sets the image pull policy for the container
102108
type: string
109+
memoryLimit:
110+
description: Sets the memory limit for the container
111+
type: string
103112
type: object
104113
registryViewerImage:
105114
description: Overrides the container image used for the registry viewer.

pkg/registry/defaults.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package registry
1919
import (
2020
registryv1alpha1 "github.com/devfile/registry-operator/api/v1alpha1"
2121
corev1 "k8s.io/api/core/v1"
22+
"k8s.io/apimachinery/pkg/api/resource"
2223
)
2324

2425
const (
@@ -32,6 +33,11 @@ const (
3233
DefaultRegistryViewerImagePullPolicy = corev1.PullAlways
3334
DefaultOCIRegistryImagePullPolicy = corev1.PullAlways
3435

36+
// Default memory limits
37+
DefaultDevfileIndexMemoryLimit = "256Mi"
38+
DefaultRegistryViewerMemoryLimit = "256Mi"
39+
DefaultOCIRegistryMemoryLimit = "256Mi"
40+
3541
// Defaults/constants for devfile registry storages
3642
DefaultDevfileRegistryVolumeSize = "1Gi"
3743
DevfileRegistryVolumeEnabled = false
@@ -73,6 +79,13 @@ func GetRegistryViewerImagePullPolicy(cr *registryv1alpha1.DevfileRegistry) core
7379
return DefaultRegistryViewerImagePullPolicy
7480
}
7581

82+
// GetRegistryViewerMemoryLimit returns the memory limit for the registry viewer container.
83+
// In case of invalid quantity given, it returns the default value.
84+
// Default: resource.Quantity{s: "256Mi"}
85+
func GetRegistryViewerMemoryLimit(cr *registryv1alpha1.DevfileRegistry) resource.Quantity {
86+
return getDevfileRegistrySpecContainer(cr.Spec.RegistryViewer.MemoryLimit, DefaultRegistryViewerMemoryLimit)
87+
}
88+
7689
// GetOCIRegistryImage returns the container image for the OCI registry to be deployed on the Devfile Registry.
7790
// Default: "quay.io/devfile/oci-registry:next"
7891
func GetOCIRegistryImage(cr *registryv1alpha1.DevfileRegistry) string {
@@ -93,6 +106,13 @@ func GetOCIRegistryImagePullPolicy(cr *registryv1alpha1.DevfileRegistry) corev1.
93106
return DefaultOCIRegistryImagePullPolicy
94107
}
95108

109+
// GetOCIRegistryMemoryLimit returns the memory limit for the OCI registry container.
110+
// In case of invalid quantity given, it returns the default value.
111+
// Default: resource.Quantity{s: "256Mi"}
112+
func GetOCIRegistryMemoryLimit(cr *registryv1alpha1.DevfileRegistry) resource.Quantity {
113+
return getDevfileRegistrySpecContainer(cr.Spec.OciRegistry.MemoryLimit, DefaultOCIRegistryMemoryLimit)
114+
}
115+
96116
// GetDevfileIndexImage returns the container image for the devfile index server to be deployed on the Devfile Registry.
97117
// Default: "quay.io/devfile/devfile-index:next"
98118
func GetDevfileIndexImage(cr *registryv1alpha1.DevfileRegistry) string {
@@ -113,6 +133,13 @@ func GetDevfileIndexImagePullPolicy(cr *registryv1alpha1.DevfileRegistry) corev1
113133
return DefaultDevfileIndexImagePullPolicy
114134
}
115135

136+
// GetDevfileIndexMemoryLimit returns the memory limit for the devfile index container.
137+
// In case of invalid quantity given, it returns the default value.
138+
// Default: resource.Quantity{s: "256Mi"}
139+
func GetDevfileIndexMemoryLimit(cr *registryv1alpha1.DevfileRegistry) resource.Quantity {
140+
return getDevfileRegistrySpecContainer(cr.Spec.DevfileIndex.MemoryLimit, DefaultDevfileIndexMemoryLimit)
141+
}
142+
116143
func getDevfileRegistryVolumeSize(cr *registryv1alpha1.DevfileRegistry) string {
117144
if cr.Spec.Storage.RegistryVolumeSize != "" {
118145
return cr.Spec.Storage.RegistryVolumeSize
@@ -167,3 +194,13 @@ func IsHeadlessEnabled(cr *registryv1alpha1.DevfileRegistry) bool {
167194
}
168195
return DefaultDevfileRegistryHeadlessEnabled
169196
}
197+
198+
func getDevfileRegistrySpecContainer(quantity string, defaultValue string) resource.Quantity {
199+
if quantity != "" {
200+
resourceQuantity, err := resource.ParseQuantity(quantity)
201+
if err == nil {
202+
return resourceQuantity
203+
}
204+
}
205+
return resource.MustParse(defaultValue)
206+
}

pkg/registry/defaults_test.go

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
registryv1alpha1 "github.com/devfile/registry-operator/api/v1alpha1"
2424
corev1 "k8s.io/api/core/v1"
25+
"k8s.io/apimachinery/pkg/api/resource"
2526
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2627
)
2728

@@ -189,6 +190,120 @@ func TestGetDevfileRegistryVolumeSource(t *testing.T) {
189190

190191
}
191192

193+
func TestGetDevfileIndexMemoryLimit(t *testing.T) {
194+
tests := []struct {
195+
name string
196+
cr registryv1alpha1.DevfileRegistry
197+
want resource.Quantity
198+
}{
199+
{
200+
name: "Case 1: Memory Limit size set in DevfileRegistry CR",
201+
cr: registryv1alpha1.DevfileRegistry{
202+
Spec: registryv1alpha1.DevfileRegistrySpec{
203+
DevfileIndex: registryv1alpha1.DevfileRegistrySpecContainer{
204+
MemoryLimit: "5Gi",
205+
},
206+
},
207+
},
208+
want: resource.MustParse("5Gi"),
209+
},
210+
{
211+
name: "Case 2: Memory Limit size not set in DevfileRegistry CR",
212+
cr: registryv1alpha1.DevfileRegistry{
213+
Spec: registryv1alpha1.DevfileRegistrySpec{
214+
DevfileIndex: registryv1alpha1.DevfileRegistrySpecContainer{},
215+
},
216+
},
217+
want: resource.MustParse(DefaultDevfileIndexMemoryLimit),
218+
},
219+
}
220+
for _, tt := range tests {
221+
t.Run(tt.name, func(t *testing.T) {
222+
volSize := GetDevfileIndexMemoryLimit(&tt.cr)
223+
if volSize != tt.want {
224+
t.Errorf("TestGetDevfileIndexMemoryLimit error: storage size mismatch, expected: %v got: %v", tt.want, volSize)
225+
}
226+
})
227+
}
228+
229+
}
230+
231+
func TestGetOCIRegistryMemoryLimit(t *testing.T) {
232+
tests := []struct {
233+
name string
234+
cr registryv1alpha1.DevfileRegistry
235+
want resource.Quantity
236+
}{
237+
{
238+
name: "Case 1: Memory Limit size set in DevfileRegistry CR",
239+
cr: registryv1alpha1.DevfileRegistry{
240+
Spec: registryv1alpha1.DevfileRegistrySpec{
241+
OciRegistry: registryv1alpha1.DevfileRegistrySpecContainer{
242+
MemoryLimit: "5Gi",
243+
},
244+
},
245+
},
246+
want: resource.MustParse("5Gi"),
247+
},
248+
{
249+
name: "Case 2: Memory Limit size not set in DevfileRegistry CR",
250+
cr: registryv1alpha1.DevfileRegistry{
251+
Spec: registryv1alpha1.DevfileRegistrySpec{
252+
OciRegistry: registryv1alpha1.DevfileRegistrySpecContainer{},
253+
},
254+
},
255+
want: resource.MustParse(DefaultOCIRegistryMemoryLimit),
256+
},
257+
}
258+
for _, tt := range tests {
259+
t.Run(tt.name, func(t *testing.T) {
260+
volSize := GetOCIRegistryMemoryLimit(&tt.cr)
261+
if volSize != tt.want {
262+
t.Errorf("TestGetOCIRegistryMemoryLimit error: storage size mismatch, expected: %v got: %v", tt.want, volSize)
263+
}
264+
})
265+
}
266+
267+
}
268+
269+
func TestGetRegistryViewerMemoryLimit(t *testing.T) {
270+
tests := []struct {
271+
name string
272+
cr registryv1alpha1.DevfileRegistry
273+
want resource.Quantity
274+
}{
275+
{
276+
name: "Case 1: Memory Limit size set in DevfileRegistry CR",
277+
cr: registryv1alpha1.DevfileRegistry{
278+
Spec: registryv1alpha1.DevfileRegistrySpec{
279+
RegistryViewer: registryv1alpha1.DevfileRegistrySpecContainer{
280+
MemoryLimit: "5Gi",
281+
},
282+
},
283+
},
284+
want: resource.MustParse("5Gi"),
285+
},
286+
{
287+
name: "Case 2: Memory Limit size not set in DevfileRegistry CR",
288+
cr: registryv1alpha1.DevfileRegistry{
289+
Spec: registryv1alpha1.DevfileRegistrySpec{
290+
RegistryViewer: registryv1alpha1.DevfileRegistrySpecContainer{},
291+
},
292+
},
293+
want: resource.MustParse(DefaultRegistryViewerMemoryLimit),
294+
},
295+
}
296+
for _, tt := range tests {
297+
t.Run(tt.name, func(t *testing.T) {
298+
volSize := GetRegistryViewerMemoryLimit(&tt.cr)
299+
if volSize != tt.want {
300+
t.Errorf("TestGetRegistryViewerMemoryLimit error: storage size mismatch, expected: %v got: %v", tt.want, volSize)
301+
}
302+
})
303+
}
304+
305+
}
306+
192307
func TestGetDevfileRegistryVolumeSize(t *testing.T) {
193308
tests := []struct {
194309
name string
@@ -284,3 +399,33 @@ func TestIsTelemetryEnabled(t *testing.T) {
284399
}
285400

286401
}
402+
403+
func Test_getDevfileRegistrySpecContainer(t *testing.T) {
404+
tests := []struct {
405+
name string
406+
quantity string
407+
defaultValue string
408+
want resource.Quantity
409+
}{
410+
{
411+
name: "Case 1: DevfileRegistrySpecContainer given correct quantity",
412+
quantity: "256Mi",
413+
defaultValue: "512Mi",
414+
want: resource.MustParse("256Mi"),
415+
},
416+
{
417+
name: "Case 2: DevfileRegistrySpecContainer given correct quantity",
418+
quantity: "test",
419+
defaultValue: "512Mi",
420+
want: resource.MustParse("512Mi"),
421+
},
422+
}
423+
for _, tt := range tests {
424+
t.Run(tt.name, func(t *testing.T) {
425+
result := getDevfileRegistrySpecContainer(tt.quantity, tt.defaultValue)
426+
if result != tt.want {
427+
t.Errorf("func TestgetDevfileRegistrySpecContainer(t *testing.T) {\n error: enablement value mismatch, expected: %v got: %v", tt.want, result)
428+
}
429+
})
430+
}
431+
}

0 commit comments

Comments
 (0)