Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Garbage collector #152

Merged
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
54 changes: 39 additions & 15 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ import (
"flag"
"fmt"
"os"
"time"

// TODO when to update oadp-operator version in go.mod?
"github.com/openshift/oadp-operator/api/v1alpha1"
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
Expand Down Expand Up @@ -113,7 +115,7 @@ func main() {

restConfig := ctrl.GetConfigOrDie()

enforcedBackupSpec, enforcedRestoreSpec, err := getEnforcedSpec(restConfig, oadpNamespace)
dpaConfiguration, err := getDPAConfiguration(restConfig, oadpNamespace)
if err != nil {
setupLog.Error(err, "unable to get enforced spec")
os.Exit(1)
Expand Down Expand Up @@ -151,7 +153,7 @@ func main() {
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
OADPNamespace: oadpNamespace,
EnforcedBackupSpec: enforcedBackupSpec,
EnforcedBackupSpec: dpaConfiguration.EnforceBackupSpec,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to setup NonAdminBackup controller with manager")
os.Exit(1)
Expand All @@ -160,7 +162,7 @@ func main() {
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
OADPNamespace: oadpNamespace,
EnforcedRestoreSpec: enforcedRestoreSpec,
EnforcedRestoreSpec: dpaConfiguration.EnforceRestoreSpec,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to setup NonAdminRestore controller with manager")
os.Exit(1)
Expand All @@ -174,6 +176,17 @@ func main() {
os.Exit(1)
}
// +kubebuilder:scaffold:builder
if dpaConfiguration.GarbageCollectionPeriod.Duration > 0 {
if err = (&controller.GarbageCollectorReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
OADPNamespace: oadpNamespace,
Frequency: dpaConfiguration.GarbageCollectionPeriod.Duration,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to setup GarbageCollector controller with manager")
os.Exit(1)
}
}

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
setupLog.Error(err, "unable to set up health check")
Expand All @@ -191,32 +204,43 @@ func main() {
}
}

func getEnforcedSpec(restConfig *rest.Config, oadpNamespace string) (*velerov1.BackupSpec, *velerov1.RestoreSpec, error) {
func getDPAConfiguration(restConfig *rest.Config, oadpNamespace string) (v1alpha1.NonAdmin, error) {
dpaConfiguration := v1alpha1.NonAdmin{
GarbageCollectionPeriod: &metav1.Duration{
Duration: 24 * time.Hour, //nolint:revive // 1 day
mateusoliveira43 marked this conversation as resolved.
Show resolved Hide resolved
},
EnforceBackupSpec: &velerov1.BackupSpec{},
EnforceRestoreSpec: &velerov1.RestoreSpec{},
}

dpaClientScheme := runtime.NewScheme()
utilruntime.Must(v1alpha1.AddToScheme(dpaClientScheme))
dpaClient, err := client.New(restConfig, client.Options{
Scheme: dpaClientScheme,
})
if err != nil {
return nil, nil, err
return dpaConfiguration, err
}
// TODO we could pass DPA name as env var and do a get call directly. Better?
dpaList := &v1alpha1.DataProtectionApplicationList{}
err = dpaClient.List(context.Background(), dpaList)
err = dpaClient.List(context.Background(), dpaList, &client.ListOptions{Namespace: oadpNamespace})
if err != nil {
return nil, nil, err
return dpaConfiguration, err
}
enforcedBackupSpec := &velerov1.BackupSpec{}
enforcedRestoreSpec := &velerov1.RestoreSpec{}
for _, dpa := range dpaList.Items {
if dpa.Namespace == oadpNamespace {
if dpa.Spec.NonAdmin != nil && dpa.Spec.NonAdmin.EnforceBackupSpec != nil {
enforcedBackupSpec = dpa.Spec.NonAdmin.EnforceBackupSpec
if nonAdmin := dpa.Spec.NonAdmin; nonAdmin != nil {
if nonAdmin.EnforceBackupSpec != nil {
dpaConfiguration.EnforceBackupSpec = nonAdmin.EnforceBackupSpec
}
if nonAdmin.EnforceRestoreSpec != nil {
dpaConfiguration.EnforceRestoreSpec = nonAdmin.EnforceRestoreSpec
}
if dpa.Spec.NonAdmin.EnforceRestoreSpec != nil {
enforcedRestoreSpec = dpa.Spec.NonAdmin.EnforceRestoreSpec
if nonAdmin.GarbageCollectionPeriod != nil {
dpaConfiguration.GarbageCollectionPeriod.Duration = nonAdmin.GarbageCollectionPeriod.Duration
}
break
}
}
return enforcedBackupSpec, enforcedRestoreSpec, nil

return dpaConfiguration, nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/google/uuid v1.6.0
github.com/onsi/ginkgo/v2 v2.19.0
github.com/onsi/gomega v1.33.1
github.com/openshift/oadp-operator v1.0.2-0.20241203211837-06081c0bb045
github.com/openshift/oadp-operator v1.0.2-0.20250205172928-bb0c25a9c6af
github.com/stretchr/testify v1.9.0
github.com/vmware-tanzu/velero v1.14.0
k8s.io/api v0.30.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ github.com/onsi/ginkgo/v2 v2.19.0 h1:9Cnnf7UHo57Hy3k6/m5k3dRfGTMXGvxhHFvkDTCTpvA
github.com/onsi/ginkgo/v2 v2.19.0/go.mod h1:rlwLi9PilAFJ8jCg9UE1QP6VBpd6/xj3SRC0d6TU0To=
github.com/onsi/gomega v1.33.1 h1:dsYjIxxSR755MDmKVsaFQTE22ChNBcuuTWgkUDSubOk=
github.com/onsi/gomega v1.33.1/go.mod h1:U4R44UsT+9eLIaYRB2a5qajjtQYn0hauxvRm16AVYg0=
github.com/openshift/oadp-operator v1.0.2-0.20241203211837-06081c0bb045 h1:Pwh5fEmVgR4DexV47DCCYT+R1tDI/4myXiEh77qKYdg=
github.com/openshift/oadp-operator v1.0.2-0.20241203211837-06081c0bb045/go.mod h1:ndXHIyjyavYVLFIi2EwfvpwUUSwPnjJo//CoyICO4aA=
github.com/openshift/oadp-operator v1.0.2-0.20250205172928-bb0c25a9c6af h1:qYsD1Hu2yt0S5lUcol603NeeSnEtDIEt7mLRzuFTdOo=
github.com/openshift/oadp-operator v1.0.2-0.20250205172928-bb0c25a9c6af/go.mod h1:gNUsBZgNcoS90Z68mNSUgx7pwtOc3THddWJx6uGV14A=
github.com/openshift/velero v0.10.2-0.20241211163542-fa8f2486175b h1:ykGzFFul6lhtphKH4V6lWzdIW1oGEtiFfOdS1WyOuNw=
github.com/openshift/velero v0.10.2-0.20241211163542-fa8f2486175b/go.mod h1:bbcPBz7mGYVY7ORWbQhD2Yx09e2ZKH2gqS+MQj+zJCU=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down
35 changes: 26 additions & 9 deletions internal/common/function/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,15 +515,20 @@ func CheckVeleroBackupMetadata(obj client.Object) bool {
return false
}

if !checkLabelAnnotationValueIsValid(objLabels, constant.NabOriginNACUUIDLabel) {
if !CheckLabelAnnotationValueIsValid(objLabels, constant.NabOriginNACUUIDLabel) {
return false
}

return CheckVeleroBackupAnnotations(obj)
}

// CheckVeleroBackupAnnotations return true if Velero Backup object has required Non Admin annotations, false otherwise
func CheckVeleroBackupAnnotations(obj client.Object) bool {
annotations := obj.GetAnnotations()
if !checkLabelAnnotationValueIsValid(annotations, constant.NabOriginNamespaceAnnotation) {
if !CheckLabelAnnotationValueIsValid(annotations, constant.NabOriginNamespaceAnnotation) {
return false
}
if !checkLabelAnnotationValueIsValid(annotations, constant.NabOriginNameAnnotation) {
if !CheckLabelAnnotationValueIsValid(annotations, constant.NabOriginNameAnnotation) {
return false
}

Expand All @@ -545,11 +550,16 @@ func CheckVeleroRestoreMetadata(obj client.Object) bool {
return false
}

return CheckVeleroRestoreAnnotations(obj)
}

// CheckVeleroRestoreAnnotations return true if Velero Restore object has required Non Admin annotations, false otherwise
func CheckVeleroRestoreAnnotations(obj client.Object) bool {
annotations := obj.GetAnnotations()
if !checkLabelAnnotationValueIsValid(annotations, constant.NarOriginNamespaceAnnotation) {
if !CheckLabelAnnotationValueIsValid(annotations, constant.NarOriginNamespaceAnnotation) {
return false
}
if !checkLabelAnnotationValueIsValid(annotations, constant.NarOriginNameAnnotation) {
if !CheckLabelAnnotationValueIsValid(annotations, constant.NarOriginNameAnnotation) {
return false
}

Expand All @@ -566,20 +576,26 @@ func CheckVeleroBackupStorageLocationMetadata(obj client.Object) bool {
return false
}

if !checkLabelAnnotationValueIsValid(objLabels, constant.NabslOriginNACUUIDLabel) {
if !CheckLabelAnnotationValueIsValid(objLabels, constant.NabslOriginNACUUIDLabel) {
return false
}

return CheckVeleroBackupStorageLocationAnnotations(obj)
}

// CheckVeleroBackupStorageLocationAnnotations return true if Velero BackupStorageLocation object has required Non Admin annotations, false otherwise
func CheckVeleroBackupStorageLocationAnnotations(obj client.Object) bool {
annotations := obj.GetAnnotations()
if !checkLabelAnnotationValueIsValid(annotations, constant.NabslOriginNamespaceAnnotation) {
if !CheckLabelAnnotationValueIsValid(annotations, constant.NabslOriginNamespaceAnnotation) {
return false
}
if !checkLabelAnnotationValueIsValid(annotations, constant.NabslOriginNameAnnotation) {
if !CheckLabelAnnotationValueIsValid(annotations, constant.NabslOriginNameAnnotation) {
return false
}

return true
}

func checkLabelValue(objLabels map[string]string, key string, value string) bool {
got, exists := objLabels[key]
if !exists {
Expand All @@ -588,7 +604,8 @@ func checkLabelValue(objLabels map[string]string, key string, value string) bool
return got == value
}

func checkLabelAnnotationValueIsValid(labelsOrAnnotations map[string]string, key string) bool {
// CheckLabelAnnotationValueIsValid return true if key exists among labels/annotations and has a valid length, false otherwise
func CheckLabelAnnotationValueIsValid(labelsOrAnnotations map[string]string, key string) bool {
value, exists := labelsOrAnnotations[key]
if !exists {
return false
Expand Down
Loading