I am trying to set a fixed resource value to my kubernetes container. When I try the following:
//cont = v1.Container
log.Println("Before", cont.Resources.Requests.Memory())
cont.Resources.Requests.Memory().SetMilli(512) //new wanted value
log.Println("After", cont.Resources.Requests.Memory())
log.Println("Before", cont.Resources.Requests.Cpu())
cont.Resources.Requests.Cpu().SetScaled(2, resource.Giga) //new wanted value
log.Println("After", cont.Resources.Requests.Cpu())
I get this:
Before 0
After 0
Before 0
After 0
Why doesn't Set
update my values to the newer ones? How can I set the CPU & RAM resources properly?
Thanks to Dim's answer, I managed to find a solution. It seems that when resources are not set, one cannot set the new value in the data structure returned by Memory(). The proper way to do it is this:
cont.Resources.Requests = make(map[v1core.ResourceName]resource.Quantity)
cont.Resources.Requests[v1core.ResourceMemory] = *resource.NewQuantity(int64(512), resource.BinarySI)
cont.Resources.Requests[v1core.ResourceCPU] = *resource.NewQuantity(int64(4), resource.BinarySI)
Cheers!