Skip to content

Commit cff29d0

Browse files
committed
move resource calculations to utils
Signed-off-by: Oleg <97077423+RobotSail@users.noreply.github.com>
1 parent 4b067fe commit cff29d0

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

controllers/statefulset.go

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
1414

1515
clusterv1alpha1 "github.com/redhat-et/ipfs-operator/api/v1alpha1"
16+
"github.com/redhat-et/ipfs-operator/controllers/utils"
1617
)
1718

1819
// These declare constants for timeouts.
@@ -65,38 +66,7 @@ func (r *IpfsClusterReconciler) statefulSet(m *clusterv1alpha1.IpfsCluster,
6566
) controllerutil.MutateFn {
6667
ssName := "ipfs-cluster-" + m.Name
6768

68-
// Determine resource constraints from how much we are storing.
69-
// for every TB of storage, Request 1GB of memory and limit if we exceed 2x this amount.
70-
// memory floor is 2G.
71-
// The CPU requirement starts at 4 cores and increases by 500m for every TB of storage
72-
// many block storage providers have a maximum block storage of 16TB, so in this case, the
73-
// biggest node we would allocate would request a minimum allocation of 16G of RAM and 12 cores
74-
// and would permit usage up to twice this size
75-
76-
ipfsStoragei64, _ := m.Spec.IpfsStorage.AsInt64()
77-
ipfsStorageTB := ipfsStoragei64 / 1024 / 1024 / 1024 / 1024
78-
ipfsMilliCoresMin := 250 + (500 * ipfsStorageTB)
79-
ipfsRAMGBMin := ipfsStorageTB
80-
if ipfsRAMGBMin < 2 {
81-
ipfsRAMGBMin = 2
82-
}
83-
84-
// ipfsRAMMinQuantity := resource.NewScaledQuantity(ipfsRAMGBMin, resource.Giga)
85-
ipfsRAMMaxQuantity := resource.NewScaledQuantity(2*ipfsRAMGBMin, resource.Giga)
86-
// ipfsCoresMinQuantity := resource.NewScaledQuantity(ipfsMilliCoresMin, resource.Milli)
87-
ipfsCoresMaxQuantity := resource.NewScaledQuantity(2*ipfsMilliCoresMin, resource.Milli)
88-
89-
ipfsResources := corev1.ResourceRequirements{
90-
// Requests: corev1.ResourceList{
91-
// corev1.ResourceMemory: *ipfsRAMMinQuantity,
92-
// corev1.ResourceCPU: *ipfsCoresMinQuantity,
93-
// },
94-
Limits: corev1.ResourceList{
95-
corev1.ResourceMemory: *ipfsRAMMaxQuantity,
96-
corev1.ResourceCPU: *ipfsCoresMaxQuantity,
97-
},
98-
}
99-
69+
ipfsResources := utils.IPFSContainerResources(m.Spec.IpfsStorage.Value())
10070
expected := &appsv1.StatefulSet{
10171
ObjectMeta: metav1.ObjectMeta{
10272
Name: ssName,

controllers/utils/utils.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55

66
"github.com/go-logr/logr"
7+
corev1 "k8s.io/api/core/v1"
8+
"k8s.io/apimachinery/pkg/api/resource"
79
"sigs.k8s.io/controller-runtime/pkg/client"
810
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
911
)
@@ -40,3 +42,40 @@ func ErrFunc(err error) controllerutil.MutateFn {
4042
return err
4143
}
4244
}
45+
46+
// IPFSContainerResources Returns the resource requests/requirements for running a single IPFS Container
47+
// depending on the storage requested by the user.
48+
func IPFSContainerResources(ipfsStorageBytes int64) (ipfsResources corev1.ResourceRequirements) {
49+
50+
// Determine resource constraints from how much we are storing.
51+
// for every TB of storage, Request 1GB of memory and limit if we exceed 2x this amount.
52+
// memory floor is 2G.
53+
// The CPU requirement starts at 4 cores and increases by 500m for every TB of storage
54+
// many block storage providers have a maximum block storage of 16TB, so in this case, the
55+
// biggest node we would allocate would request a minimum allocation of 16G of RAM and 12 cores
56+
// and would permit usage up to twice this size
57+
58+
ipfsStorageTB := ipfsStorageBytes / 1024 / 1024 / 1024 / 1024
59+
ipfsMilliCoresMin := 250 + (500 * ipfsStorageTB)
60+
ipfsRAMGBMin := ipfsStorageTB
61+
if ipfsRAMGBMin < 2 {
62+
ipfsRAMGBMin = 2
63+
}
64+
65+
// ipfsRAMMinQuantity := resource.NewScaledQuantity(ipfsRAMGBMin, resource.Giga)
66+
ipfsRAMMaxQuantity := resource.NewScaledQuantity(2*ipfsRAMGBMin, resource.Giga)
67+
// ipfsCoresMinQuantity := resource.NewScaledQuantity(ipfsMilliCoresMin, resource.Milli)
68+
ipfsCoresMaxQuantity := resource.NewScaledQuantity(2*ipfsMilliCoresMin, resource.Milli)
69+
70+
ipfsResources = corev1.ResourceRequirements{
71+
// Requests: corev1.ResourceList{
72+
// corev1.ResourceMemory: *ipfsRAMMinQuantity,
73+
// corev1.ResourceCPU: *ipfsCoresMinQuantity,
74+
// },
75+
Limits: corev1.ResourceList{
76+
corev1.ResourceMemory: *ipfsRAMMaxQuantity,
77+
corev1.ResourceCPU: *ipfsCoresMaxQuantity,
78+
},
79+
}
80+
return
81+
}

0 commit comments

Comments
 (0)