forked from migtools/oadp-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget.go
More file actions
187 lines (158 loc) · 5.34 KB
/
get.go
File metadata and controls
187 lines (158 loc) · 5.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
Copyright The Velero Contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package backup
import (
"context"
"fmt"
"time"
"github.com/migtools/oadp-cli/cmd/non-admin/output"
"github.com/migtools/oadp-cli/cmd/shared"
nacv1alpha1 "github.com/migtools/oadp-non-admin/api/v1alpha1"
"github.com/spf13/cobra"
"github.com/vmware-tanzu/velero/pkg/client"
kbclient "sigs.k8s.io/controller-runtime/pkg/client"
)
func NewGetCommand(f client.Factory, use string) *cobra.Command {
c := &cobra.Command{
Use: use + " [NAME]",
Short: "Get non-admin backup(s)",
Long: "Get one or more non-admin backups",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// Get the current namespace from kubectl context
userNamespace, err := shared.GetCurrentNamespace()
if err != nil {
return fmt.Errorf("failed to determine current namespace: %w", err)
}
// Create client with full scheme
kbClient, err := shared.NewClientWithFullScheme(f)
if err != nil {
return err
}
if len(args) == 1 {
// Get specific backup
backupName := args[0]
var nab nacv1alpha1.NonAdminBackup
err := kbClient.Get(context.Background(), kbclient.ObjectKey{
Namespace: userNamespace,
Name: backupName,
}, &nab)
if err != nil {
return fmt.Errorf("failed to get NonAdminBackup %q: %w", backupName, err)
}
if printed, err := output.PrintWithFormat(cmd, &nab); printed || err != nil {
return err
}
// If no output format specified, print table format for single item
list := &nacv1alpha1.NonAdminBackupList{
Items: []nacv1alpha1.NonAdminBackup{nab},
}
return printNonAdminBackupTable(list)
} else {
// List all backups in namespace
var nabList nacv1alpha1.NonAdminBackupList
err := kbClient.List(context.Background(), &nabList, &kbclient.ListOptions{
Namespace: userNamespace,
})
if err != nil {
return fmt.Errorf("failed to list NonAdminBackups: %w", err)
}
if printed, err := output.PrintWithFormat(cmd, &nabList); printed || err != nil {
return err
}
// Print table format
return printNonAdminBackupTable(&nabList)
}
},
Example: ` # Get all non-admin backups in the current namespace
kubectl oadp nonadmin backup get
# Get a specific non-admin backup
kubectl oadp nonadmin backup get my-backup
# Get backups in YAML format
kubectl oadp nonadmin backup get -o yaml
# Get a specific backup in JSON format
kubectl oadp nonadmin backup get my-backup -o json`,
}
output.BindFlags(c.Flags())
output.ClearOutputFlagDefault(c)
return c
}
func printNonAdminBackupTable(nabList *nacv1alpha1.NonAdminBackupList) error {
if len(nabList.Items) == 0 {
fmt.Println("No non-admin backups found.")
return nil
}
// Print header
fmt.Printf("%-30s %-15s %-15s %-20s %-10s %-10s\n", "NAME", "REQUEST PHASE", "VELERO PHASE", "CREATED", "AGE", "DURATION")
// Print each backup
for _, nab := range nabList.Items {
status := getBackupStatus(&nab)
veleroPhase := getVeleroPhase(&nab)
created := nab.CreationTimestamp.Format("2006-01-02 15:04:05")
age := formatAge(nab.CreationTimestamp.Time)
duration := getBackupDuration(&nab)
fmt.Printf("%-30s %-15s %-15s %-20s %-10s %-10s\n", nab.Name, status, veleroPhase, created, age, duration)
}
return nil
}
func getBackupStatus(nab *nacv1alpha1.NonAdminBackup) string {
if nab.Status.Phase != "" {
return string(nab.Status.Phase)
}
return "Unknown"
}
func getVeleroPhase(nab *nacv1alpha1.NonAdminBackup) string {
if nab.Status.VeleroBackup != nil && nab.Status.VeleroBackup.Status != nil {
if nab.Status.VeleroBackup.Status.Phase != "" {
return string(nab.Status.VeleroBackup.Status.Phase)
}
}
return "N/A"
}
func getBackupDuration(nab *nacv1alpha1.NonAdminBackup) string {
// Check if we have completion timestamp
if nab.Status.VeleroBackup != nil && nab.Status.VeleroBackup.Status != nil {
if !nab.Status.VeleroBackup.Status.CompletionTimestamp.IsZero() {
// Calculate duration from request creation to completion
duration := nab.Status.VeleroBackup.Status.CompletionTimestamp.Time.Sub(nab.CreationTimestamp.Time)
return formatDuration(duration)
}
}
return "N/A"
}
func formatDuration(d time.Duration) string {
if d < time.Minute {
return fmt.Sprintf("%ds", int(d.Seconds()))
} else if d < time.Hour {
return fmt.Sprintf("%dm%ds", int(d.Minutes()), int(d.Seconds())%60)
} else {
hours := int(d.Hours())
minutes := int(d.Minutes()) % 60
return fmt.Sprintf("%dh%dm", hours, minutes)
}
}
func formatAge(t time.Time) string {
duration := time.Since(t)
days := int(duration.Hours() / 24)
hours := int(duration.Hours()) % 24
minutes := int(duration.Minutes()) % 60
if days > 0 {
return fmt.Sprintf("%dd", days)
} else if hours > 0 {
return fmt.Sprintf("%dh", hours)
} else if minutes > 0 {
return fmt.Sprintf("%dm", minutes)
} else {
return "1m"
}
}