-
Notifications
You must be signed in to change notification settings - Fork 93
OADP-5900: DPA config for backup repo cache and full maintenance interval #1705
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
Merged
openshift-merge-bot
merged 1 commit into
openshift:master
from
sseago:backup-repo-config
Apr 14, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| corev1 "k8s.io/api/core/v1" | ||
| "k8s.io/apimachinery/pkg/api/errors" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil" | ||
|
|
||
| oadpv1alpha1 "github.com/openshift/oadp-operator/api/v1alpha1" | ||
| "github.com/openshift/oadp-operator/pkg/common" | ||
| ) | ||
|
|
||
| func isBackupRepositoryCmRequired(config *oadpv1alpha1.NodeAgentConfig) bool { | ||
| return config != nil && (config.KopiaRepoOptions.CacheLimitMB != nil || len(config.KopiaRepoOptions.FullMaintenanceInterval) > 0) | ||
| } | ||
|
|
||
| // updateBackupRepositoryCM handles the creation or update of the BackupRepository ConfigMap with all required data. | ||
| func (r *DataProtectionApplicationReconciler) updateBackupRepositoryCM(cm *corev1.ConfigMap) error { | ||
| // Set the owner reference to ensure the ConfigMap is managed by the DPA | ||
| if err := controllerutil.SetControllerReference(r.dpa, cm, r.Scheme); err != nil { | ||
| return fmt.Errorf("failed to set controller reference: %w", err) | ||
| } | ||
|
|
||
| // Convert KopiaRepoOptions to a generic map | ||
| configBackupRepositoryJSON, err := json.Marshal(r.dpa.Spec.Configuration.NodeAgent.KopiaRepoOptions) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to serialize backup repository config: %w", err) | ||
| } | ||
|
|
||
| cm.Name = common.BackupRepoConfigMapPrefix + r.dpa.Name | ||
| cm.Namespace = r.NamespacedName.Namespace | ||
| cm.Labels = map[string]string{ | ||
| "app.kubernetes.io/instance": r.dpa.Name, | ||
| "app.kubernetes.io/managed-by": common.OADPOperator, | ||
| "app.kubernetes.io/component": "backup-repository-config", | ||
| oadpv1alpha1.OadpOperatorLabel: "True", | ||
| } | ||
|
|
||
| if cm.Data == nil { | ||
| cm.Data = make(map[string]string) | ||
| } | ||
| cm.Data["kopia"] = string(configBackupRepositoryJSON) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // GetBackupRepositoryConfigMapName returns the NamespacedName of the BackupRepository ConfigMap | ||
| func (r *DataProtectionApplicationReconciler) GetBackupRepositoryConfigMapName() types.NamespacedName { | ||
| return types.NamespacedName{ | ||
| Name: common.BackupRepoConfigMapPrefix + r.dpa.Name, | ||
| Namespace: r.NamespacedName.Namespace, | ||
| } | ||
| } | ||
|
|
||
| // ReconcileBackupRepositoryConfigMap handles creation, update, and deletion of the BackupRepository ConfigMap. | ||
| func (r *DataProtectionApplicationReconciler) ReconcileBackupRepositoryConfigMap(log logr.Logger) (bool, error) { | ||
| dpa := r.dpa | ||
| cmName := r.GetBackupRepositoryConfigMapName() | ||
| configMap := corev1.ConfigMap{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: cmName.Name, | ||
| Namespace: cmName.Namespace, | ||
| }, | ||
| } | ||
|
|
||
| // Delete CM if it is not required | ||
| if !isBackupRepositoryCmRequired(dpa.Spec.Configuration.NodeAgent) { | ||
| err := r.Get(r.Context, cmName, &configMap) | ||
| if err != nil && !errors.IsNotFound(err) { | ||
| return false, err | ||
| } | ||
| if errors.IsNotFound(err) { | ||
| return true, nil | ||
| } | ||
| deleteContext := context.Background() | ||
| if err := r.Delete(deleteContext, &configMap); err != nil { | ||
| if errors.IsNotFound(err) { | ||
| return true, nil | ||
| } | ||
| return false, err | ||
| } | ||
| r.EventRecorder.Event(&configMap, corev1.EventTypeNormal, "DeletedBackupRepositoryConfigMap", "BackupRepository config map deleted") | ||
| return true, nil | ||
| } | ||
|
|
||
| op, err := controllerutil.CreateOrPatch(r.Context, r.Client, &configMap, func() error { | ||
| return r.updateBackupRepositoryCM(&configMap) | ||
| }) | ||
| if err != nil { | ||
| return false, fmt.Errorf("failed to create or patch config map: %w", err) | ||
| } | ||
|
|
||
| if op == controllerutil.OperationResultCreated { | ||
| r.EventRecorder.Event(&configMap, corev1.EventTypeNormal, "CreatedBackupRepositoryConfigMap", "BackupRepository config map created") | ||
| } else if op == controllerutil.OperationResultUpdated { | ||
| r.EventRecorder.Event(&configMap, corev1.EventTypeNormal, "UpdatedBackupRepositoryConfigMap", "BackupRepository config map updated") | ||
| } | ||
|
|
||
| return true, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package controller | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "testing" | ||
|
|
||
| "github.com/go-logr/logr" | ||
| "github.com/stretchr/testify/require" | ||
| corev1 "k8s.io/api/core/v1" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| "k8s.io/client-go/tools/record" | ||
| "k8s.io/utils/ptr" | ||
|
|
||
| oadpv1alpha1 "github.com/openshift/oadp-operator/api/v1alpha1" | ||
| ) | ||
|
|
||
| func TestDataProtectionApplicationReconciler_updateBackupRepositoryCM(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| cm *corev1.ConfigMap | ||
| dpa *oadpv1alpha1.DataProtectionApplication | ||
| wantCM *corev1.ConfigMap | ||
| }{ | ||
| { | ||
| name: "backup repository cm is updated successfully with full config", | ||
| cm: &corev1.ConfigMap{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "backup-repository-test-dpa", | ||
| Namespace: "test-ns", | ||
| }, | ||
| }, | ||
| dpa: &oadpv1alpha1.DataProtectionApplication{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "test-dpa", | ||
| Namespace: "test-ns", | ||
| }, | ||
| Spec: oadpv1alpha1.DataProtectionApplicationSpec{ | ||
| BackupImages: ptr.To(false), | ||
| Configuration: &oadpv1alpha1.ApplicationConfig{ | ||
| Velero: &oadpv1alpha1.VeleroConfig{ | ||
| NoDefaultBackupLocation: true, | ||
| }, | ||
| NodeAgent: &oadpv1alpha1.NodeAgentConfig{ | ||
| KopiaRepoOptions: oadpv1alpha1.KopiaRepoOptions{ | ||
| CacheLimitMB: ptr.To(int64(4096)), | ||
| FullMaintenanceInterval: "fastGC", | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| wantCM: &corev1.ConfigMap{ | ||
| ObjectMeta: metav1.ObjectMeta{ | ||
| Name: "backup-repository-test-dpa", | ||
| Namespace: "test-ns", | ||
| Labels: map[string]string{ | ||
| "app.kubernetes.io/instance": "test-dpa", | ||
| "app.kubernetes.io/managed-by": "oadp-operator", | ||
| "app.kubernetes.io/component": "backup-repository-config", | ||
| "openshift.io/oadp": "True", | ||
| }, | ||
| }, | ||
| Data: map[string]string{ | ||
| "kopia": `{"cacheLimitMB":4096,"fullMaintenanceInterval":"fastGC"}`, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| fakeClient, err := getFakeClientFromObjects(tt.cm, tt.dpa) | ||
| if err != nil { | ||
| t.Errorf("error in creating fake client, likely programmer error") | ||
| } | ||
| r := &DataProtectionApplicationReconciler{ | ||
| Client: fakeClient, | ||
| Scheme: fakeClient.Scheme(), | ||
| Log: logr.Discard(), | ||
| Context: context.Background(), | ||
| NamespacedName: types.NamespacedName{ | ||
| Namespace: tt.cm.Namespace, | ||
| Name: tt.cm.Name, | ||
| }, | ||
| EventRecorder: record.NewFakeRecorder(10), | ||
| dpa: tt.dpa, | ||
| } | ||
|
|
||
| err = r.updateBackupRepositoryCM(tt.cm) | ||
| require.NoError(t, err) | ||
| require.Equal(t, tt.wantCM.ObjectMeta.Name, tt.cm.ObjectMeta.Name, "ConfigMap Name does not match") | ||
| require.Equal(t, tt.wantCM.ObjectMeta.Namespace, tt.cm.ObjectMeta.Namespace, "ConfigMap Namespace does not match") | ||
| require.Equal(t, tt.wantCM.ObjectMeta.Labels, tt.cm.ObjectMeta.Labels, "ConfigMap Labels do not match") | ||
|
|
||
| // Compare Data fields, we need to unmarshal the JSON to ignore key order | ||
| expectedData := tt.wantCM.Data["kopia"] | ||
| actualData := tt.cm.Data["kopia"] | ||
|
|
||
| var expectedMap map[string]interface{} | ||
| var actualMap map[string]interface{} | ||
|
|
||
| require.NoError(t, json.Unmarshal([]byte(expectedData), &expectedMap), "Failed to unmarshal expected Data") | ||
| require.NoError(t, json.Unmarshal([]byte(actualData), &actualMap), "Failed to unmarshal actual Data") | ||
| require.Equal(t, expectedMap, actualMap, "ConfigMap Data does not match") | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the idea to have a separate struct is because this would be datamover options and not FSB options? (I think I heard some discussion around this)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think separate to use marshal:
https://github.com/openshift/oadp-operator/pull/1705/files/2e2b78de35561c27a6dd42af95d88e012eb879a2#diff-3d7a2c405414ff19b6660300351bf73aa3c836948cfe76a80f4e9e4d7b689d07R31
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mateusoliveira43 This is for BackupRepository configuration, so used for both Datamover and FSB. I grouped them together because they go into the same ConfigMap entry -- as @mpryc mentioned, so a single Marshal statement will generate the json chunk we need.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
alright