|
| 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 | +} |
0 commit comments