@@ -79,10 +79,11 @@ type CreateOptions struct {
7979 * velerobackup.CreateOptions // Embed Velero's CreateOptions
8080
8181 // NAB-specific fields
82- Name string // The NonAdminBackup resource name (maps to Velero's BackupName)
83- client kbclient.WithWatch
84- currentNamespace string
85- storageLocationFromConfig bool // Track if storage location came from config
82+ Name string // The NonAdminBackup resource name (maps to Velero's BackupName)
83+ client kbclient.WithWatch
84+ currentNamespace string
85+ storageLocationFromConfig bool // Track if storage location came from config
86+ storageLocationAutoSelected bool // Track if storage location was auto-selected
8687}
8788
8889func NewCreateOptions () * CreateOptions {
@@ -147,15 +148,6 @@ func (o *CreateOptions) Validate(c *cobra.Command, args []string, f client.Facto
147148func (o * CreateOptions ) Complete (args []string , f client.Factory ) error {
148149 o .Name = args [0 ]
149150
150- // Load default NABSL from config if not provided via flag
151- if o .StorageLocation == "" {
152- defaultNABSL := getNABSLFromConfig ()
153- if defaultNABSL != "" {
154- o .StorageLocation = defaultNABSL
155- o .storageLocationFromConfig = true
156- }
157- }
158-
159151 // Create client with NonAdmin scheme
160152 client , err := shared .NewClientWithScheme (f , shared.ClientOptions {
161153 IncludeNonAdminTypes : true ,
@@ -172,6 +164,37 @@ func (o *CreateOptions) Complete(args []string, f client.Factory) error {
172164
173165 o .client = client
174166 o .currentNamespace = currentNS
167+
168+ // Load default NABSL from config if not provided via flag, or auto-select if exactly one exists
169+ if o .StorageLocation == "" {
170+ defaultNABSL := getNABSLFromConfig ()
171+ if defaultNABSL != "" {
172+ o .StorageLocation = defaultNABSL
173+ o .storageLocationFromConfig = true
174+ } else {
175+ // Auto-select NABSL if exactly one approved/created exists in the namespace
176+ nabslList := & nacv1alpha1.NonAdminBackupStorageLocationList {}
177+ if err := o .client .List (context .TODO (), nabslList , & kbclient.ListOptions {
178+ Namespace : currentNS ,
179+ }); err != nil {
180+ return fmt .Errorf ("failed to list NonAdminBackupStorageLocations: %w" , err )
181+ }
182+
183+ // Filter to only approved/created NABSLs (exclude pending/rejected)
184+ var usableNABSLs []nacv1alpha1.NonAdminBackupStorageLocation
185+ for _ , nabsl := range nabslList .Items {
186+ if nabsl .Status .Phase == nacv1alpha1 .NonAdminPhaseCreated {
187+ usableNABSLs = append (usableNABSLs , nabsl )
188+ }
189+ }
190+
191+ if len (usableNABSLs ) == 1 {
192+ o .StorageLocation = usableNABSLs [0 ].Name
193+ o .storageLocationAutoSelected = true
194+ }
195+ }
196+ }
197+
175198 return nil
176199}
177200
@@ -193,6 +216,10 @@ func (o *CreateOptions) Run(c *cobra.Command, f client.Factory) error {
193216 if o .storageLocationFromConfig {
194217 fmt .Printf ("Using default nonadmin backup storage location from config: %s\n " , o .StorageLocation )
195218 }
219+ if o .storageLocationAutoSelected {
220+ fmt .Printf ("Auto-selected storage location: %s (only NABSL in namespace)\n " , o .StorageLocation )
221+ fmt .Printf ("Warning: If you create another NABSL in this namespace, future backups may not use the same location.\n " )
222+ }
196223
197224 fmt .Printf ("NonAdminBackup request %q submitted successfully.\n " , nonAdminBackup .Name )
198225 fmt .Printf ("Run `oc oadp nonadmin backup describe %s` or `oc oadp nonadmin backup logs %s` for more details.\n " , nonAdminBackup .Name , nonAdminBackup .Name )
0 commit comments