Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 30 additions & 28 deletions pkg/splunk/enterprise/finalizers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
12 changes: 12 additions & 0 deletions pkg/splunk/enterprise/finalizers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down