Skip to content

Commit af1bbcd

Browse files
authored
NonAdminDownloadRequests implementation (#233)
Signed-off-by: Tiger Kaovilai <[email protected]> Signed-off-by: Tiger Kaovilai <[email protected]>
1 parent 0fb24a2 commit af1bbcd

23 files changed

+1476
-1
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,7 @@ go.work
2626
*.swp
2727
*.swo
2828
*~
29+
30+
# debug
31+
__debug_bin*
32+
debug.test*

PROJECT

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,13 @@ resources:
3535
kind: NonAdminBackupStorageLocation
3636
path: github.com/migtools/oadp-non-admin/api/v1alpha1
3737
version: v1alpha1
38+
- api:
39+
crdVersion: v1
40+
namespaced: true
41+
controller: true
42+
domain: openshift.io
43+
group: oadp
44+
kind: NonAdminDownloadRequest
45+
path: github.com/migtools/oadp-non-admin/api/v1alpha1
46+
version: v1alpha1
3847
version: "3"

api/v1alpha1/nonadmin_types.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ limitations under the License.
1616

1717
package v1alpha1
1818

19-
// NonAdminPhase is a simple one high-level summary of the lifecycle of a NonAdminBackup, NonAdminRestore or NonAdminBackupStorageLocation.
19+
// NonAdminPhase is a simple one high-level summary of the lifecycle of a NonAdminBackup, NonAdminRestore, NonAdminBackupStorageLocation, or NonAdminDownloadRequest
2020
// +kubebuilder:validation:Enum=New;BackingOff;Created;Deleting
2121
type NonAdminPhase string
2222

api/v1alpha1/nonadminbackup_types.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package v1alpha1
1919
import (
2020
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
2121
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
23+
"github.com/migtools/oadp-non-admin/internal/common/constant"
2224
)
2325

2426
// NonAdminBackupSpec defines the desired state of NonAdminBackup
@@ -188,3 +190,18 @@ type NonAdminBackupList struct {
188190
func init() {
189191
SchemeBuilder.Register(&NonAdminBackup{}, &NonAdminBackupList{})
190192
}
193+
194+
// Helper Functions to avoid digging into NAB controller to understand how to get desired values
195+
196+
// VeleroBackupName returns the name of the VeleroBackup object.
197+
func (nab *NonAdminBackup) VeleroBackupName() string {
198+
if nab.Status.VeleroBackup == nil {
199+
return constant.EmptyString
200+
}
201+
return nab.Status.VeleroBackup.Name
202+
}
203+
204+
// UsesNaBSL returns true if backup is using NonAdminBackupStorageLocation
205+
func (nab *NonAdminBackup) UsesNaBSL() bool {
206+
return nab.Spec.BackupSpec != nil && nab.Spec.BackupSpec.StorageLocation != constant.EmptyString
207+
}
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
/*
2+
Copyright 2024.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha1
18+
19+
import (
20+
"fmt"
21+
22+
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
23+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
24+
)
25+
26+
// NonAdminDownloadRequestSpec defines the desired state of NonAdminDownloadRequest.
27+
// Mirrors velero DownloadRequestSpec to allow non admins to download information for a non admin backup/restore
28+
type NonAdminDownloadRequestSpec struct {
29+
// Target is what to download (e.g. logs for a backup).
30+
Target velerov1.DownloadTarget `json:"target"`
31+
}
32+
33+
// VeleroDownloadRequest represents VeleroDownloadRequest
34+
type VeleroDownloadRequest struct {
35+
// VeleroDownloadRequestStatus represents VeleroDownloadRequestStatus
36+
// +optional
37+
Status *velerov1.DownloadRequestStatus `json:"status,omitempty"`
38+
}
39+
40+
// NonAdminDownloadRequestStatus defines the observed state of NonAdminDownloadRequest.
41+
type NonAdminDownloadRequestStatus struct {
42+
// +optional
43+
VeleroDownloadRequest VeleroDownloadRequest `json:"velero,omitempty"`
44+
// phase is a simple one high-level summary of the lifecycle of an NonAdminDownloadRequest
45+
Phase NonAdminPhase `json:"phase,omitempty"`
46+
47+
Conditions []metav1.Condition `json:"conditions,omitempty"`
48+
}
49+
50+
// +kubebuilder:object:root=true
51+
// +kubebuilder:subresource:status
52+
// +kubebuilder:resource:path=nonadmindownloadrequests,shortName=nadr
53+
// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.phase"
54+
55+
// NonAdminDownloadRequest is the Schema for the nonadmindownloadrequests API.
56+
type NonAdminDownloadRequest struct {
57+
metav1.TypeMeta `json:",inline"`
58+
metav1.ObjectMeta `json:"metadata,omitempty"`
59+
60+
Spec NonAdminDownloadRequestSpec `json:"spec,omitempty"`
61+
Status NonAdminDownloadRequestStatus `json:"status,omitempty"`
62+
}
63+
64+
// +kubebuilder:object:root=true
65+
66+
// NonAdminDownloadRequestList contains a list of NonAdminDownloadRequest.
67+
type NonAdminDownloadRequestList struct {
68+
metav1.TypeMeta `json:",inline"`
69+
metav1.ListMeta `json:"metadata,omitempty"`
70+
Items []NonAdminDownloadRequest `json:"items"`
71+
}
72+
73+
func init() {
74+
SchemeBuilder.Register(&NonAdminDownloadRequest{}, &NonAdminDownloadRequestList{})
75+
}
76+
77+
// NonAdminDownloadRequestConditionType prevents untyped strings for NADR conditions functions
78+
type NonAdminDownloadRequestConditionType string
79+
80+
const (
81+
// ConditionNonAdminBackupStorageLocationNotUsed block download requests processing if NaBSL is not used
82+
ConditionNonAdminBackupStorageLocationNotUsed NonAdminDownloadRequestConditionType = "NonAdminBackupStorageLocationNotUsed"
83+
// ConditionNonAdminBackupNotAvailable indicates backup is not available, and will backoff download request
84+
ConditionNonAdminBackupNotAvailable NonAdminDownloadRequestConditionType = "NonAdminBackupNotAvailable"
85+
// ConditionNonAdminRestoreNotAvailable indicates restore is not available, and will backoff download request
86+
ConditionNonAdminRestoreNotAvailable NonAdminDownloadRequestConditionType = "NonAdminRestoreNotAvailable"
87+
// ConditionNonAdminProcessed indicates that the NADR is in a terminal state
88+
ConditionNonAdminProcessed NonAdminDownloadRequestConditionType = "Processed"
89+
)
90+
91+
// ReadyForProcessing returns if this NonAdminDownloadRequests is in a state ready for processing
92+
//
93+
// Terminal conditions include
94+
// - NonAdminBackupStorageLocationNotUsed: we currently require NaBSL usage on the NAB/NAR to process this download request
95+
// returns true if ready for processing, false otherwise
96+
func (nadr *NonAdminDownloadRequest) ReadyForProcessing() bool {
97+
// if nadr has ConditionNonAdminBackupStorageLocationUsed return false
98+
if nadr.Status.Conditions != nil {
99+
for _, condition := range nadr.Status.Conditions {
100+
if condition.Type == string(ConditionNonAdminBackupStorageLocationNotUsed) &&
101+
condition.Status == metav1.ConditionTrue {
102+
return false
103+
}
104+
}
105+
}
106+
return true // required fields are set via velero validation markers
107+
}
108+
109+
// VeleroDownloadRequestName defines velero download request name for this NonAdminDownloadRequest
110+
func (nadr *NonAdminDownloadRequest) VeleroDownloadRequestName() string {
111+
return fmt.Sprintf("nadr-%s", string(nadr.GetUID()))
112+
}

api/v1alpha1/nonadminrestore_types.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ package v1alpha1
1919
import (
2020
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
2121
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
23+
"github.com/migtools/oadp-non-admin/internal/common/constant"
2224
)
2325

2426
// NonAdminRestoreSpec defines the desired state of NonAdminRestore
@@ -157,3 +159,18 @@ type NonAdminRestoreList struct {
157159
func init() {
158160
SchemeBuilder.Register(&NonAdminRestore{}, &NonAdminRestoreList{})
159161
}
162+
163+
// Helper Functions to avoid digging into NAR controller to understand how to get desired values
164+
165+
// VeleroRestoreName returns the name of the VeleroRestore object.
166+
func (nar *NonAdminRestore) VeleroRestoreName() string {
167+
if nar.Status.VeleroRestore == nil {
168+
return constant.EmptyString
169+
}
170+
return nar.Status.VeleroRestore.Name
171+
}
172+
173+
// NonAdminBackupName returns NonAdminBackup name of this NAR
174+
func (nar *NonAdminRestore) NonAdminBackupName() string {
175+
return nar.Spec.RestoreSpec.BackupName
176+
}

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 118 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,14 @@ func main() {
208208
setupLog.Error(err, "unable to setup NonAdminBackupStorageLocation controller with manager")
209209
os.Exit(1)
210210
}
211+
if err = (&controller.NonAdminDownloadRequestReconciler{
212+
Client: mgr.GetClient(),
213+
Scheme: mgr.GetScheme(),
214+
OADPNamespace: oadpNamespace,
215+
}).SetupWithManager(mgr); err != nil {
216+
setupLog.Error(err, "unable to create controller", "controller", "NonAdminDownloadRequest")
217+
os.Exit(1)
218+
}
211219
// +kubebuilder:scaffold:builder
212220
if dpaConfiguration.BackupSyncPeriod.Duration > 0 {
213221
if err = (&controller.NonAdminBackupSynchronizerReconciler{

0 commit comments

Comments
 (0)