|
7 | 7 |
|
8 | 8 | . "github.com/onsi/ginkgo/v2" |
9 | 9 | . "github.com/onsi/gomega" |
| 10 | + appsv1 "k8s.io/api/apps/v1" |
10 | 11 | v1 "k8s.io/api/core/v1" |
| 12 | + "k8s.io/apimachinery/pkg/api/resource" |
11 | 13 | metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
12 | 14 |
|
13 | 15 | "github.com/redhat-et/ipfs-operator/api/v1alpha1" |
@@ -100,6 +102,121 @@ var _ = Describe("IPFS Reconciler", func() { |
100 | 102 | }) |
101 | 103 | }) |
102 | 104 |
|
| 105 | +var _ = Describe("StatefulSet creation", func() { |
| 106 | + var ipfsReconciler *controllers.IpfsClusterReconciler |
| 107 | + var ipfs *v1alpha1.IpfsCluster |
| 108 | + var sts *appsv1.StatefulSet |
| 109 | + // var configMapScripts *v1.ConfigMap |
| 110 | + // var secret *v1.Secret |
| 111 | + // var svc *v1.Service |
| 112 | + // var ctx context.Context |
| 113 | + |
| 114 | + const ( |
| 115 | + myName = "my-fav-ipfs-node" |
| 116 | + scriptsName = "my-scripts" |
| 117 | + secretName = "my-secret" |
| 118 | + svcName = "my-svc" |
| 119 | + namespace = "test" |
| 120 | + ) |
| 121 | + BeforeEach(func() { |
| 122 | + // ctx = context.TODO() |
| 123 | + ipfsReconciler = &controllers.IpfsClusterReconciler{ |
| 124 | + Scheme: k8sClient.Scheme(), |
| 125 | + Client: k8sClient, |
| 126 | + } |
| 127 | + // configMapScripts = &v1.ConfigMap{ObjectMeta: metav1.ObjectMeta{ |
| 128 | + // Name: scriptsName, |
| 129 | + // }} |
| 130 | + ipfs = &v1alpha1.IpfsCluster{ |
| 131 | + ObjectMeta: metav1.ObjectMeta{ |
| 132 | + Name: myName, |
| 133 | + Namespace: namespace, |
| 134 | + }, |
| 135 | + } |
| 136 | + sts = &appsv1.StatefulSet{} |
| 137 | + // secret = &v1.Secret{ObjectMeta: metav1.ObjectMeta{Name: secretName}} |
| 138 | + // svc = &v1.Service{ObjectMeta: metav1.ObjectMeta{Name: svcName}} |
| 139 | + }) |
| 140 | + |
| 141 | + When("ipfsResources is specified", func() { |
| 142 | + var cpuLimit = resource.NewQuantity(58, "m") |
| 143 | + var memoryLimit = resource.NewQuantity(10000, "m") |
| 144 | + var cpu = resource.NewQuantity(20, "m") |
| 145 | + var memory = resource.NewQuantity(40, "M") |
| 146 | + BeforeEach(func() { |
| 147 | + ipfs.Spec.IPFSResources = &v1.ResourceRequirements{ |
| 148 | + Limits: v1.ResourceList{ |
| 149 | + v1.ResourceLimitsCPU: *cpuLimit, |
| 150 | + v1.ResourceLimitsMemory: *memoryLimit, |
| 151 | + }, |
| 152 | + Requests: v1.ResourceList{ |
| 153 | + v1.ResourceCPU: *cpu, |
| 154 | + v1.ResourceMemory: *memory, |
| 155 | + }, |
| 156 | + } |
| 157 | + ipfs.Spec.ClusterStorage = *resource.NewQuantity(1, "Gi") |
| 158 | + ipfs.Spec.IpfsStorage = *resource.NewQuantity(1, "Gi") |
| 159 | + }) |
| 160 | + It("uses the IPFSCluster's IPFSResources setting", func() { |
| 161 | + mutFn := ipfsReconciler.StatefulSet(ipfs, sts, svcName, secretName, scriptsName) |
| 162 | + err := mutFn() |
| 163 | + Expect(err).NotTo(HaveOccurred()) |
| 164 | + |
| 165 | + // find IPFS Container |
| 166 | + var ipfsContainer v1.Container |
| 167 | + for _, container := range sts.Spec.Template.Spec.Containers { |
| 168 | + if container.Name == controllers.ContainerIPFS { |
| 169 | + ipfsContainer = container |
| 170 | + break |
| 171 | + } |
| 172 | + } |
| 173 | + Expect(ipfsContainer).NotTo(BeNil()) |
| 174 | + ipfsResources := ipfsContainer.Resources |
| 175 | + Expect(ipfsResources.Limits).NotTo(BeEmpty()) |
| 176 | + Expect(ipfsResources.Limits[v1.ResourceLimitsCPU]).To(Equal(*cpuLimit)) |
| 177 | + Expect(ipfsResources.Limits[v1.ResourceLimitsMemory]).To(Equal(*memoryLimit)) |
| 178 | + Expect(ipfsResources.Requests).NotTo(BeEmpty()) |
| 179 | + Expect(ipfsResources.Requests[v1.ResourceCPU]).To(Equal(*cpu)) |
| 180 | + Expect(ipfsResources.Requests[v1.ResourceMemory]).To(Equal(*memory)) |
| 181 | + }) |
| 182 | + }) |
| 183 | + |
| 184 | + When("ipfsResources is omitted", func() { |
| 185 | + BeforeEach(func() { |
| 186 | + ipfs.Spec.ClusterStorage = *resource.NewQuantity(5, "T") |
| 187 | + ipfs.Spec.IpfsStorage = *resource.NewQuantity(16, "T") |
| 188 | + }) |
| 189 | + It("automatically computes resources requirements", func() { |
| 190 | + mutFn := ipfsReconciler.StatefulSet(ipfs, sts, svcName, secretName, scriptsName) |
| 191 | + err := mutFn() |
| 192 | + Expect(err).NotTo(HaveOccurred()) |
| 193 | + |
| 194 | + // find IPFS Container |
| 195 | + var ipfsContainer v1.Container |
| 196 | + for _, container := range sts.Spec.Template.Spec.Containers { |
| 197 | + if container.Name == controllers.ContainerIPFS { |
| 198 | + ipfsContainer = container |
| 199 | + break |
| 200 | + } |
| 201 | + } |
| 202 | + Expect(ipfsContainer).NotTo(BeNil()) |
| 203 | + ipfsResources := ipfsContainer.Resources |
| 204 | + Expect(ipfsResources.Limits).NotTo(BeEmpty()) |
| 205 | + Expect(ipfsResources.Requests).NotTo(BeEmpty()) |
| 206 | + |
| 207 | + cpuLimit := ipfsResources.Limits[v1.ResourceLimitsCPU] |
| 208 | + memoryLimit := ipfsResources.Limits[v1.ResourceLimitsMemory] |
| 209 | + Expect(cpuLimit.Value()).NotTo(Equal(0)) |
| 210 | + Expect(memoryLimit.Value()).NotTo(Equal(0)) |
| 211 | + |
| 212 | + cpu := ipfsResources.Requests[v1.ResourceCPU] |
| 213 | + memory := ipfsResources.Requests[v1.ResourceMemory] |
| 214 | + Expect(cpu.Value()).NotTo(Equal(0)) |
| 215 | + Expect(memory.Value()).NotTo(Equal(0)) |
| 216 | + }) |
| 217 | + }) |
| 218 | +}) |
| 219 | + |
103 | 220 | // The k8s client will encode and copy data from the StringData to Data |
104 | 221 | // This function mimics the behavior for tests. |
105 | 222 | func secretStringToData(secret *v1.Secret) { |
|
0 commit comments