Skip to content

Commit 7402a37

Browse files
Merge pull request #37 from PDOK/jd/memory-limit
Jd/memory limit
2 parents 44f1dac + 3ef4440 commit 7402a37

File tree

3 files changed

+52
-27
lines changed

3 files changed

+52
-27
lines changed

internal/controller/mapserver/deployment.go

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,24 @@ func GetEnvVarsForDeployment[O pdoknlv3.WMSWFS](obj O, blobsSecretName string) [
218218
// Resources for mapserver container
219219
func GetResourcesForDeployment[O pdoknlv3.WMSWFS](obj O) v1.ResourceRequirements {
220220
resources := v1.ResourceRequirements{
221-
Limits: v1.ResourceList{
222-
v1.ResourceMemory: resource.MustParse("800M"),
223-
},
224-
Requests: v1.ResourceList{
225-
v1.ResourceCPU: resource.MustParse("0.15"),
226-
},
221+
Limits: v1.ResourceList{},
222+
Requests: v1.ResourceList{},
223+
}
224+
225+
maxResourceVal := func(v1 *resource.Quantity, v2 *resource.Quantity) *resource.Quantity {
226+
if v1 != nil && v2 != nil {
227+
if v1.Value() > v2.Value() {
228+
return v1
229+
} else {
230+
return v2
231+
}
232+
} else if v1 != nil && v2 == nil {
233+
return v1
234+
} else if v1 == nil || v2 != nil {
235+
return v2
236+
}
237+
238+
return &resource.Quantity{}
227239
}
228240

229241
objResources := &v1.ResourceRequirements{}
@@ -243,38 +255,51 @@ func GetResourcesForDeployment[O pdoknlv3.WMSWFS](obj O) v1.ResourceRequirements
243255

244256
}
245257

246-
if obj.Type() == pdoknlv3.ServiceTypeWMS && obj.Options().UseWebserviceProxy() {
247-
resources.Requests[v1.ResourceCPU] = resource.MustParse("0.1")
258+
/**
259+
Set CPU request and limit
260+
*/
261+
cpuRequest := objResources.Requests.Cpu()
262+
if cpuRequest == nil || cpuRequest.IsZero() {
263+
cpuRequest = smoothoperatorutils.Pointer(resource.MustParse("0.15"))
264+
if obj.Type() == pdoknlv3.ServiceTypeWMS && obj.Options().UseWebserviceProxy() {
265+
cpuRequest = smoothoperatorutils.Pointer(resource.MustParse("0.1"))
266+
}
267+
}
268+
resources.Requests[v1.ResourceCPU] = *cpuRequest
269+
270+
cpuLimit := objResources.Limits.Cpu()
271+
if cpuLimit != nil && !cpuLimit.IsZero() {
272+
resources.Limits[v1.ResourceCPU] = *maxResourceVal(cpuLimit, cpuRequest)
248273
}
249274

250-
if objResources.Limits.Cpu() != nil && objResources.Requests.Cpu().Value() > resources.Requests.Cpu().Value() {
251-
resources.Limits[v1.ResourceCPU] = *objResources.Limits.Cpu()
275+
/**
276+
Set memory limit/request if the request is higher than the limit the request is used as limit
277+
*/
278+
memoryRequest := objResources.Requests.Memory()
279+
if memoryRequest != nil && !memoryRequest.IsZero() {
280+
resources.Requests[v1.ResourceMemory] = *memoryRequest
252281
}
253282

254-
if objResources.Requests.Memory() != nil && !objResources.Requests.Memory().IsZero() {
255-
resources.Requests[v1.ResourceMemory] = *objResources.Requests.Memory()
283+
memoryLimit := objResources.Limits.Memory()
284+
if memoryLimit == nil || memoryLimit.IsZero() {
285+
memoryLimit = smoothoperatorutils.Pointer(resource.MustParse("800M"))
256286
}
287+
resources.Limits[v1.ResourceMemory] = *maxResourceVal(memoryLimit, memoryRequest)
257288

289+
/**
290+
Set ephemeral-storage if there is no ephemeral volume
291+
*/
258292
if use, _ := mapperutils.UseEphemeralVolume(obj); !use {
259-
minimumEphemeralStorageLimit := resource.MustParse("200M")
260293
ephemeralStorageRequest := mapperutils.EphemeralStorageRequest(obj)
261-
ephemeralStorageLimit := mapperutils.EphemeralStorageLimit(obj)
262-
263294
if ephemeralStorageRequest != nil {
264295
resources.Requests[v1.ResourceEphemeralStorage] = *ephemeralStorageRequest
265296
}
266297

267-
if ephemeralStorageLimit != nil && ephemeralStorageLimit.Value() > minimumEphemeralStorageLimit.Value() {
268-
// Request higher than limit, use request as limit
269-
if ephemeralStorageRequest != nil && ephemeralStorageRequest.Value() > ephemeralStorageLimit.Value() {
270-
resources.Limits[v1.ResourceEphemeralStorage] = *ephemeralStorageRequest
271-
} else {
272-
resources.Limits[v1.ResourceEphemeralStorage] = *ephemeralStorageLimit
273-
}
274-
} else {
275-
// No limit given or the given limit is lower than the default, use default
276-
resources.Limits[v1.ResourceEphemeralStorage] = minimumEphemeralStorageLimit
298+
ephemeralStorageLimit := mapperutils.EphemeralStorageLimit(obj)
299+
if ephemeralStorageLimit == nil || ephemeralStorageLimit.IsZero() {
300+
ephemeralStorageLimit = smoothoperatorutils.Pointer(resource.MustParse("200M"))
277301
}
302+
resources.Limits[v1.ResourceEphemeralStorage] = *maxResourceVal(ephemeralStorageLimit, ephemeralStorageRequest)
278303
}
279304

280305
return resources

internal/controller/wfs_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ var _ = Describe("WFS Controller", func() {
241241
Expect(container.Ports[0].ContainerPort).Should(Equal(int32(80)))
242242
Expect(container.Image).Should(Equal(reconcilerImages.MapserverImage))
243243
Expect(container.ImagePullPolicy).Should(Equal(v1.PullIfNotPresent))
244-
Expect(container.Resources.Limits.Memory().String()).Should(Equal("800M"))
244+
Expect(container.Resources.Limits.Memory().String()).Should(Equal("12M"))
245245
Expect(container.Resources.Requests.Cpu().String()).Should(Equal("150m"))
246246
Expect(len(container.LivenessProbe.Exec.Command)).Should(Equal(3))
247247
Expect(container.LivenessProbe.Exec.Command[2]).Should(Equal("wget -SO- -T 10 -t 2 'http://127.0.0.1:80/mapserver?SERVICE=wfs&request=GetCapabilities' 2>&1 | egrep -aiA10 'HTTP/1.1 200' | egrep -i 'Content-Type: text/xml'"))

internal/controller/wms_controller_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ var _ = Describe("WMS Controller", func() {
238238
Expect(containerMapserver.Ports[0].ContainerPort).Should(Equal(int32(80)))
239239
Expect(containerMapserver.Image).Should(Equal(reconcilerImages.MapserverImage))
240240
Expect(containerMapserver.ImagePullPolicy).Should(Equal(corev1.PullIfNotPresent))
241-
Expect(containerMapserver.Resources.Limits.Memory().String()).Should(Equal("800M"))
241+
Expect(containerMapserver.Resources.Limits.Memory().String()).Should(Equal("12M"))
242242
Expect(containerMapserver.Resources.Requests.Cpu().String()).Should(Equal("100m"))
243243
Expect(len(containerMapserver.LivenessProbe.Exec.Command)).Should(Equal(3))
244244
Expect(containerMapserver.LivenessProbe.Exec.Command[2]).Should(Equal("wget -SO- -T 10 -t 2 'http://127.0.0.1:80/mapserver?SERVICE=wms&request=GetCapabilities' 2>&1 | egrep -aiA10 'HTTP/1.1 200' | egrep -i 'Content-Type: text/xml'"))

0 commit comments

Comments
 (0)