1
1
package restore
2
2
3
3
import (
4
+ "fmt"
5
+ "strings"
6
+
4
7
"github.com/red-hat-storage/odf-cli/cmd/odf/root"
5
8
"github.com/rook/kubectl-rook-ceph/pkg/k8sutil"
6
9
pkgrestore "github.com/rook/kubectl-rook-ceph/pkg/restore"
7
10
"github.com/spf13/cobra"
8
11
)
9
12
13
+ // groupVersions defines the supported CRD groups and their corresponding API versions.
14
+ var groupVersions = map [string ]string {
15
+ "ocs.openshift.io" : "v1" ,
16
+ "ceph.rook.io" : "v1" ,
17
+ "storage.k8s.io" : "v1" ,
18
+ "odf.openshift.io" : "v1alpha1" ,
19
+ "noobaa.io" : "v1alpha1" ,
20
+ "csiaddons.openshift.io" : "v1alpha1" ,
21
+ }
22
+
23
+ // keys returns the keys of a string map. It is used to print out supported group names.
24
+ func keys (m map [string ]string ) []string {
25
+ out := make ([]string , 0 , len (m ))
26
+ for k := range m {
27
+ out = append (out , k )
28
+ }
29
+ return out
30
+ }
31
+
32
+ // parseFullyQualifiedCRD takes a fully qualified CRD type of the form "resource.group"
33
+ // (for example, "cephclusters.ceph.rook.io") and returns the resource name, group name, and
34
+ // the API version associated with that group. It returns an error if the format is invalid
35
+ // or the group is not recognized.
36
+ func parseFullyQualifiedCRD (fqcrd string ) (resourceName , groupName , version string , err error ) {
37
+ parts := strings .SplitN (fqcrd , "." , 2 )
38
+ if len (parts ) != 2 {
39
+ return "" , "" , "" , fmt .Errorf ("invalid CRD format %q; expected format <resource>.<group>" , fqcrd )
40
+ }
41
+ resourceName = parts [0 ]
42
+ groupName = parts [1 ]
43
+
44
+ ver , ok := groupVersions [groupName ]
45
+ if ! ok {
46
+ return "" , "" , "" , fmt .Errorf ("unsupported group %q; supported groups are: %v" , groupName , keys (groupVersions ))
47
+ }
48
+ return resourceName , groupName , ver , nil
49
+ }
50
+
10
51
// deletedCmd represents the deleted command
11
52
var deletedCmd = & cobra.Command {
12
53
Use : "deleted" ,
@@ -20,8 +61,19 @@ var deletedCmd = &cobra.Command{
20
61
},
21
62
Run : func (cmd * cobra.Command , args []string ) {
22
63
k8sutil .SetDeploymentScale (cmd .Context (), root .ClientSets .Kube , root .OperatorNamespace , "ocs-operator" , 0 )
23
- pkgrestore .RestoreCrd (cmd .Context (), root .ClientSets , root .OperatorNamespace , root .StorageClusterNamespace , args )
64
+ // Parse the fully qualified CRD (e.g. "cephclusters.ceph.rook.io").
65
+ resourceName , groupName , version , err := parseFullyQualifiedCRD (args [0 ])
66
+ if err != nil {
67
+ fmt .Printf ("Error parsing CRD type: %v\n " , err )
68
+ return
69
+ }
70
+ // Construct a new args slice with the resource name as the first argument.
71
+ newArgs := make ([]string , len (args ))
72
+ newArgs [0 ] = resourceName
73
+ if len (args ) > 1 {
74
+ newArgs [1 ] = args [1 ]
75
+ }
76
+ pkgrestore .RestoreCrd (cmd .Context (), root .ClientSets , root .OperatorNamespace , root .StorageClusterNamespace , groupName , version , newArgs )
24
77
k8sutil .SetDeploymentScale (cmd .Context (), root .ClientSets .Kube , root .OperatorNamespace , "ocs-operator" , 1 )
25
-
26
78
},
27
79
}
0 commit comments