diff --git a/pkg/controller/provider/BUILD.bazel b/pkg/controller/provider/BUILD.bazel index b5065cb1d..38accba76 100644 --- a/pkg/controller/provider/BUILD.bazel +++ b/pkg/controller/provider/BUILD.bazel @@ -33,6 +33,7 @@ go_library( "//vendor/k8s.io/apimachinery/pkg/api/errors", "//vendor/k8s.io/apimachinery/pkg/api/resource", "//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:meta", + "//vendor/k8s.io/apimachinery/pkg/labels", "//vendor/k8s.io/apimachinery/pkg/util/intstr", "//vendor/k8s.io/apiserver/pkg/storage/names", "//vendor/sigs.k8s.io/controller-runtime/pkg/client", diff --git a/pkg/controller/provider/controller.go b/pkg/controller/provider/controller.go index 3178e11f3..51ed6edfa 100644 --- a/pkg/controller/provider/controller.go +++ b/pkg/controller/provider/controller.go @@ -21,7 +21,9 @@ import ( "fmt" "os" "path/filepath" + "strings" "sync" + "time" api "github.com/konveyor/forklift-controller/pkg/apis/forklift/v1beta1" "github.com/konveyor/forklift-controller/pkg/controller/base" @@ -41,6 +43,7 @@ import ( appsv1 "k8s.io/api/apps/v1" v1 "k8s.io/api/core/v1" k8serr "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/labels" "k8s.io/apiserver/pkg/storage/names" "sigs.k8s.io/controller-runtime/pkg/client" "sigs.k8s.io/controller-runtime/pkg/controller" @@ -157,6 +160,28 @@ func (r Reconciler) Reconcile(ctx context.Context, request reconcile.Request) (r _ = r.DB().Close(true) } } + + // Wait for the PV to release + time.Sleep(5 * time.Second) + + // List all PVs with a specific prefix and label + labelSelector := labels.SelectorFromSet(labels.Set{"subapp": "ova-server"}) + pvList := &v1.PersistentVolumeList{} + err = r.Client.List(context.TODO(), pvList, &client.ListOptions{ + LabelSelector: labelSelector, + }) + if err != nil { + r.Log.Error(err, "Failed to list PVs") + } else { + for _, pv := range pvList.Items { + if strings.HasPrefix(pv.Name, "ova-server-pv-") && pv.Status.Phase == v1.VolumeReleased { + err = r.Client.Delete(context.TODO(), &pv) + if err != nil { + r.Log.Error(err, "Failed to delete PV", "PV Name", pv.Name) + } + } + } + } } return } else {