From 6783fe35f4de37c27e5851b3809286f7d2c912ee Mon Sep 17 00:00:00 2001 From: Nishant Gupta Date: Mon, 22 Nov 2021 04:26:48 -0800 Subject: [PATCH] Make segmentStoreResources.requests not a mandatory field Signed-off-by: Nishant Gupta --- pkg/apis/pravega/v1beta1/pravega.go | 8 ++++++ .../pravega/v1beta1/pravegacluster_types.go | 5 +++- .../v1beta1/pravegacluster_types_test.go | 28 +++++++++++++++++-- test/e2e/webhook_test.go | 17 ++++++++--- 4 files changed, 51 insertions(+), 7 deletions(-) diff --git a/pkg/apis/pravega/v1beta1/pravega.go b/pkg/apis/pravega/v1beta1/pravega.go index 8d1629ee1..2bdbd4860 100644 --- a/pkg/apis/pravega/v1beta1/pravega.go +++ b/pkg/apis/pravega/v1beta1/pravega.go @@ -429,6 +429,14 @@ func (s *PravegaSpec) withDefaults() (changed bool) { } } + if s.SegmentStoreResources.Requests == nil { + changed = true + s.SegmentStoreResources.Requests = map[corev1.ResourceName]resource.Quantity{ + corev1.ResourceCPU: s.SegmentStoreResources.Limits[v1.ResourceCPU], + corev1.ResourceMemory: s.SegmentStoreResources.Limits[v1.ResourceMemory], + } + } + if s.SegmentStoreResources.Requests[v1.ResourceMemory] == (resource.Quantity{}) { changed = true s.SegmentStoreResources.Requests[v1.ResourceMemory] = s.SegmentStoreResources.Limits[v1.ResourceMemory] diff --git a/pkg/apis/pravega/v1beta1/pravegacluster_types.go b/pkg/apis/pravega/v1beta1/pravegacluster_types.go index c49fb6db5..8f9332e79 100644 --- a/pkg/apis/pravega/v1beta1/pravegacluster_types.go +++ b/pkg/apis/pravega/v1beta1/pravegacluster_types.go @@ -1221,7 +1221,10 @@ func (p *PravegaCluster) ValidateSegmentStoreMemorySettings() error { } if p.Spec.Pravega.SegmentStoreResources.Requests == nil { - return fmt.Errorf("spec.pravega.segmentStoreResources.requests cannot be empty") + p.Spec.Pravega.SegmentStoreResources.Requests = map[corev1.ResourceName]resource.Quantity{ + corev1.ResourceCPU: {}, + corev1.ResourceMemory: {}, + } } totalMemoryLimitsQuantity := p.Spec.Pravega.SegmentStoreResources.Limits[corev1.ResourceMemory] diff --git a/pkg/apis/pravega/v1beta1/pravegacluster_types_test.go b/pkg/apis/pravega/v1beta1/pravegacluster_types_test.go index 207d623e7..8563727aa 100644 --- a/pkg/apis/pravega/v1beta1/pravegacluster_types_test.go +++ b/pkg/apis/pravega/v1beta1/pravegacluster_types_test.go @@ -542,11 +542,35 @@ var _ = Describe("PravegaCluster Types Spec", func() { }) Context("empty segmentStoreResources.requests object", func() { + var ( + changed bool + ) + + BeforeEach(func() { + p.Spec.Pravega.Options["pravegaservice.cache.size.max"] = "1610612736" + p.Spec.Pravega.SegmentStoreJVMOptions = []string{"-Xmx1g", "-XX:MaxDirectMemorySize=2560m"} + p.Spec.Pravega.SegmentStoreResources = &corev1.ResourceRequirements{ + Limits: corev1.ResourceList{ + corev1.ResourceCPU: resource.MustParse("2000m"), + corev1.ResourceMemory: resource.MustParse("4Gi"), + }, + } + changed = p.WithDefaults() + }) + + It("Should set memory and cpu requests to memory and cpu limits respectively", func() { + Ω(changed).Should(Equal(true)) + }) + }) + + Context("empty segmentStoreResources.requests object validation check", func() { var ( err error ) BeforeEach(func() { + p.Spec.Pravega.Options["pravegaservice.cache.size.max"] = "1610612736" + p.Spec.Pravega.SegmentStoreJVMOptions = []string{"-Xmx1g", "-XX:MaxDirectMemorySize=2560m"} p.Spec.Pravega.SegmentStoreResources = &corev1.ResourceRequirements{ Limits: corev1.ResourceList{ corev1.ResourceCPU: resource.MustParse("2000m"), @@ -556,8 +580,8 @@ var _ = Describe("PravegaCluster Types Spec", func() { err = p.ValidateSegmentStoreMemorySettings() }) - It("Should return error", func() { - Ω(strings.ContainsAny(err.Error(), "spec.pravega.segmentStoreResources.requests cannot be empty")).Should(Equal(true)) + It("should return nil", func() { + Ω(err).Should(BeNil()) }) }) diff --git a/test/e2e/webhook_test.go b/test/e2e/webhook_test.go index 75d0dd167..b88c76d42 100644 --- a/test/e2e/webhook_test.go +++ b/test/e2e/webhook_test.go @@ -65,9 +65,18 @@ func testWebhook(t *testing.T) { noSegmentStoreResourceRequests := pravega_e2eutil.NewDefaultCluster(namespace) noSegmentStoreResourceRequests.WithDefaults() noSegmentStoreResourceRequests.Spec.Pravega.SegmentStoreResources.Requests = nil - _, err = pravega_e2eutil.CreatePravegaCluster(t, f, ctx, noSegmentStoreResourceRequests) - g.Expect(err).To(HaveOccurred(), "Spec.Pravega.SegmentStoreResources.Requests cannot be empty") - g.Expect(err.Error()).To(ContainSubstring("spec.pravega.segmentStoreResources.requests cannot be empty")) + pravega, err := pravega_e2eutil.CreatePravegaCluster(t, f, ctx, noSegmentStoreResourceRequests) + g.Expect(err).NotTo(HaveOccurred()) + + err = pravega_e2eutil.DeletePravegaCluster(t, f, ctx, pravega) + g.Expect(err).NotTo(HaveOccurred()) + + err = pravega_e2eutil.WaitForPravegaClusterToTerminate(t, f, ctx, pravega) + g.Expect(err).NotTo(HaveOccurred()) + + //creating the setup for running the test + err = pravega_e2eutil.InitialSetup(t, f, ctx, namespace) + g.Expect(err).NotTo(HaveOccurred()) // Test webhook with no value for segment store memory limits noMemoryLimits := pravega_e2eutil.NewClusterWithNoSegmentStoreMemoryLimits(namespace) @@ -142,7 +151,7 @@ func testWebhook(t *testing.T) { // Test webhook with a valid Pravega cluster version format validVersion := pravega_e2eutil.NewClusterWithVersion(namespace, "0.6.0") validVersion.WithDefaults() - pravega, err := pravega_e2eutil.CreatePravegaCluster(t, f, ctx, validVersion) + pravega, err = pravega_e2eutil.CreatePravegaCluster(t, f, ctx, validVersion) g.Expect(err).NotTo(HaveOccurred()) podSize := 2