diff --git a/pkg/splunk/enterprise/finalizers.go b/pkg/splunk/enterprise/finalizers.go index 0a755e7db..50919c9ac 100644 --- a/pkg/splunk/enterprise/finalizers.go +++ b/pkg/splunk/enterprise/finalizers.go @@ -37,48 +37,50 @@ func DeleteSplunkPvc(cr splcommon.MetaObject, c splcommon.ControllerClient) erro scopedLog := log.WithName("DeleteSplunkPvc").WithValues("kind", objectKind, "name", cr.GetName(), "namespace", cr.GetNamespace()) - var component string + var components []string switch objectKind { case "Standalone": - component = "standalone" + components = append(components, "standalone") case "LicenseMaster": - component = "license-master" + components = append(components, "license-master") case "SearchHeadCluster": - component = "search-head" + components = append(components, "search-head", "deployer") case "IndexerCluster": - component = "indexer" + components = append(components, "indexer") case "ClusterMaster": - component = "cluster-master" + components = append(components, "cluster-master") default: scopedLog.Info("Skipping PVC removal") return nil } - // get list of PVCs associated with this CR - labels := map[string]string{ - "app.kubernetes.io/instance": fmt.Sprintf("splunk-%s-%s", cr.GetName(), component), - } - listOpts := []client.ListOption{ - client.InNamespace(cr.GetNamespace()), - client.MatchingLabels(labels), - } - pvclist := corev1.PersistentVolumeClaimList{} - if err := c.List(context.Background(), &pvclist, listOpts...); err != nil { - return err - } + for _, component := range components { + // get list of PVCs associated with this CR + labels := map[string]string{ + "app.kubernetes.io/instance": fmt.Sprintf("splunk-%s-%s", cr.GetName(), component), + } + listOpts := []client.ListOption{ + client.InNamespace(cr.GetNamespace()), + client.MatchingLabels(labels), + } + pvclist := corev1.PersistentVolumeClaimList{} + if err := c.List(context.Background(), &pvclist, listOpts...); err != nil { + return err + } - if len(pvclist.Items) == 0 { - scopedLog.Info("No PVC found") - return nil - } + if len(pvclist.Items) == 0 { + scopedLog.Info("No PVC found") + return nil + } - // delete each PVC - for _, pvc := range pvclist.Items { - scopedLog.Info("Deleting PVC", "name", pvc.ObjectMeta.Name) - if err := c.Delete(context.Background(), &pvc); err != nil { - return err + // delete each PVC + for _, pvc := range pvclist.Items { + scopedLog.Info("Deleting PVC", "name", pvc.ObjectMeta.Name) + if err := c.Delete(context.Background(), &pvc); err != nil { + return err + } } - } + } return nil } diff --git a/pkg/splunk/enterprise/finalizers_test.go b/pkg/splunk/enterprise/finalizers_test.go index 4f70789a4..aa79d1a19 100644 --- a/pkg/splunk/enterprise/finalizers_test.go +++ b/pkg/splunk/enterprise/finalizers_test.go @@ -95,6 +95,18 @@ func splunkDeletionTester(t *testing.T, cr splcommon.MetaObject, delete func(spl {ListOpts: listOptsA}, {ListOpts: listOptsB}, } + // account for extra calls in the shc case due to the deployer + if component == "search-head" { + labelsC := map[string]string{ + "app.kubernetes.io/instance": fmt.Sprintf("splunk-%s-%s", cr.GetName(), "deployer"), + } + listOptsC := []client.ListOption{ + client.InNamespace(cr.GetNamespace()), + client.MatchingLabels(labelsC), + } + mockCalls["Delete"] = append(mockCalls["Delete"], spltest.MockFuncCall{MetaName: "*v1.PersistentVolumeClaim-test-splunk-pvc-stack1-var"}) + mockCalls["List"] = append(mockCalls["List"], spltest.MockFuncCall{ListOpts: listOptsC}) + } mockCalls["Get"] = []spltest.MockFuncCall{ {MetaName: "*v1.StatefulSet-test-splunk-test-monitoring-console"}, {MetaName: "*v1.Secret-test-splunk-test-secret"},