From f82a7bf316d07cd0e881a4663f40e5f121d42800 Mon Sep 17 00:00:00 2001 From: Suraj Deshmukh Date: Thu, 11 Feb 2021 17:40:11 +0530 Subject: [PATCH] add reclaim policy --- pkg/components/rook-ceph/component.go | 7 ++-- pkg/components/rook-ceph/component_test.go | 43 ++++++++++++++++++++++ pkg/components/rook-ceph/manifests.go | 2 +- 3 files changed, 48 insertions(+), 4 deletions(-) diff --git a/pkg/components/rook-ceph/component.go b/pkg/components/rook-ceph/component.go index 24f9fb3aa..82da955e4 100644 --- a/pkg/components/rook-ceph/component.go +++ b/pkg/components/rook-ceph/component.go @@ -45,8 +45,9 @@ type component struct { // StorageClass provides struct to enable it or make it default. type StorageClass struct { - Enable bool `hcl:"enable,optional"` - Default bool `hcl:"default,optional"` + Enable bool `hcl:"enable,optional"` + Default bool `hcl:"default,optional"` + ReclaimPolicy string `hcl:reclaim_policy,optional` } // NewConfig returns new Rook Ceph component configuration with default values set. @@ -56,7 +57,7 @@ func NewConfig() *component { return &component{ Namespace: "rook", MonitorCount: 1, - StorageClass: &StorageClass{}, + StorageClass: &StorageClass{ReclaimPolicy: "Retain"}, } } diff --git a/pkg/components/rook-ceph/component_test.go b/pkg/components/rook-ceph/component_test.go index 4d7104cc7..a6ad3d977 100644 --- a/pkg/components/rook-ceph/component_test.go +++ b/pkg/components/rook-ceph/component_test.go @@ -19,6 +19,7 @@ import ( "github.com/hashicorp/hcl/v2" + "github.com/kinvolk/lokomotive/pkg/components/internal/testutil" "github.com/kinvolk/lokomotive/pkg/components/util" ) @@ -82,3 +83,45 @@ component "rook-ceph" { t.Fatalf("Rendered manifests shouldn't be empty") } } + +func TestConversion(t *testing.T) { + testCases := []struct { + name string + inputConfig string + expectedManifestName string + expected string + jsonPath string + }{ + { + name: "default reclaim policy", + inputConfig: `component "rook-ceph" {}`, + expectedManifestName: "", + jsonPath: "{.reclaimPolicy}", + expected: "Retain", + }, + { + name: "over-ridden reclaim policy", + inputConfig: `component "rook-ceph" { + storage_class { + reclaim_policy = "Delete" + } + }`, + expectedManifestName: "", + jsonPath: "{.reclaimPolicy}", + expected: "Delete", + }, + } + + for _, tc := range testCases { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + + component := NewConfig() + m := testutil.RenderManifests(t, component, Name, tc.inputConfig) + gotConfig := testutil.ConfigFromMap(t, m, tc.expectedManifestName) + + testutil.MatchJSONPathStringValue(t, gotConfig, tc.jsonPath, tc.expected) + }) + } +} diff --git a/pkg/components/rook-ceph/manifests.go b/pkg/components/rook-ceph/manifests.go index fa346010b..58170abf3 100644 --- a/pkg/components/rook-ceph/manifests.go +++ b/pkg/components/rook-ceph/manifests.go @@ -161,7 +161,7 @@ parameters: csi.storage.k8s.io/fstype: xfs # Delete the rbd volume when a PVC is deleted -reclaimPolicy: Delete +reclaimPolicy: {{ .StorageClass.ReclaimPolicy }} {{- end }} `,