1
1
package restore
2
2
3
3
import (
4
+ "fmt"
5
+ "slices"
6
+ "strings"
7
+
4
8
"github.com/red-hat-storage/odf-cli/cmd/odf/root"
5
9
"github.com/rook/kubectl-rook-ceph/pkg/k8sutil"
10
+ "github.com/rook/kubectl-rook-ceph/pkg/logging"
6
11
pkgrestore "github.com/rook/kubectl-rook-ceph/pkg/restore"
7
12
"github.com/spf13/cobra"
8
13
)
9
14
15
+ // groupVersions defines the supported CRD groups and their corresponding API versions.
16
+ var groupVersions = map [string ]string {
17
+ "ocs.openshift.io" : "v1" ,
18
+ "ceph.rook.io" : "v1" ,
19
+ "csi.ceph.io" : "v1alpha1" ,
20
+ "odf.openshift.io" : "v1alpha1" ,
21
+ "noobaa.io" : "v1alpha1" ,
22
+ "csiaddons.openshift.io" : "v1alpha1" ,
23
+ }
24
+
25
+ // groupNameKeys returns the keys of a string map. It is used to print out supported group names.
26
+ func groupNameKeys (m map [string ]string ) []string {
27
+ out := make ([]string , 0 , len (m ))
28
+ for k := range m {
29
+ out = append (out , k )
30
+ }
31
+ return out
32
+ }
33
+
34
+ // parseFullyQualifiedCRD takes a fully qualified CRD type of the form "resource.group"
35
+ // (for example, "cephclusters.ceph.rook.io") and returns the resource name, group name, and
36
+ // the API version associated with that group. It returns an error if the format is invalid
37
+ // or the group is not recognized.
38
+ func parseFullyQualifiedCRD (fqcrd string ) (resourceName , groupName , version string , err error ) {
39
+ parts := strings .SplitN (fqcrd , "." , 2 )
40
+ if len (parts ) != 2 {
41
+ return "" , "" , "" , fmt .Errorf ("invalid CRD format %q; expected format <resource>.<group>" , fqcrd )
42
+ }
43
+ resourceName = parts [0 ]
44
+ groupName = parts [1 ]
45
+
46
+ version , ok := groupVersions [groupName ]
47
+ if ! ok {
48
+ return "" , "" , "" , fmt .Errorf ("unsupported group %q; supported groups are: %v" , groupName , groupNameKeys (groupVersions ))
49
+ }
50
+ return resourceName , groupName , version , nil
51
+ }
52
+
10
53
// deletedCmd represents the deleted command
11
54
var deletedCmd = & cobra.Command {
12
55
Use : "deleted" ,
@@ -20,8 +63,54 @@ var deletedCmd = &cobra.Command{
20
63
},
21
64
Run : func (cmd * cobra.Command , args []string ) {
22
65
k8sutil .SetDeploymentScale (cmd .Context (), root .ClientSets .Kube , root .OperatorNamespace , "ocs-operator" , 0 )
23
- pkgrestore .RestoreCrd (cmd .Context (), root .ClientSets , root .OperatorNamespace , root .StorageClusterNamespace , args )
24
- k8sutil .SetDeploymentScale (cmd .Context (), root .ClientSets .Kube , root .OperatorNamespace , "ocs-operator" , 1 )
66
+ // Parse the fully qualified CRD (e.g. "cephclusters.ceph.rook.io").
67
+ resourceName , groupName , version , err := parseFullyQualifiedCRD (args [0 ])
68
+ if err != nil {
69
+ logging .Fatal (fmt .Errorf ("Error parsing CRD type: %v\n " , err ))
70
+ }
71
+ // Construct a new args slice with the resource name as the first argument.
72
+ newArgs := make ([]string , len (args ))
73
+ newArgs [0 ] = resourceName
74
+ if len (args ) > 1 {
75
+ newArgs [1 ] = args [1 ]
76
+ }
77
+ var customResources []pkgrestore.CustomResource
78
+ if slices .Contains (newArgs , "storageclusters" ) {
79
+ customResources = []pkgrestore.CustomResource {
80
+ // ceph.rook.io/v1
81
+ {Group : "ceph.rook.io" , Version : "v1" , Resource : "cephblockpoolradosnamespaces" },
82
+ {Group : "ceph.rook.io" , Version : "v1" , Resource : "cephblockpools" },
83
+ {Group : "ceph.rook.io" , Version : "v1" , Resource : "cephbucketnotifications" },
84
+ {Group : "ceph.rook.io" , Version : "v1" , Resource : "cephbuckettopics" },
85
+ {Group : "ceph.rook.io" , Version : "v1" , Resource : "cephclients" },
86
+ {Group : "ceph.rook.io" , Version : "v1" , Resource : "cephclusters" },
87
+ {Group : "ceph.rook.io" , Version : "v1" , Resource : "cephcosidrivers" },
88
+ {Group : "ceph.rook.io" , Version : "v1" , Resource : "cephfilesystemmirrors" },
89
+ {Group : "ceph.rook.io" , Version : "v1" , Resource : "cephfilesystems" },
90
+ {Group : "ceph.rook.io" , Version : "v1" , Resource : "cephfilesystemsubvolumegroups" },
91
+ {Group : "ceph.rook.io" , Version : "v1" , Resource : "cephnfses" },
92
+ {Group : "ceph.rook.io" , Version : "v1" , Resource : "cephobjectrealms" },
93
+ {Group : "ceph.rook.io" , Version : "v1" , Resource : "cephobjectstores" },
94
+ {Group : "ceph.rook.io" , Version : "v1" , Resource : "cephobjectstoreusers" },
95
+ {Group : "ceph.rook.io" , Version : "v1" , Resource : "cephobjectzonegroups" },
96
+ {Group : "ceph.rook.io" , Version : "v1" , Resource : "cephobjectzones" },
97
+ {Group : "ceph.rook.io" , Version : "v1" , Resource : "cephrbdmirrors" },
25
98
99
+ // noobaa.io/v1alpha1
100
+ {Group : "noobaa.io" , Version : "v1alpha1" , Resource : "backingstores" },
101
+ {Group : "noobaa.io" , Version : "v1alpha1" , Resource : "bucketclasses" },
102
+ {Group : "noobaa.io" , Version : "v1alpha1" , Resource : "namespacestores" },
103
+ {Group : "noobaa.io" , Version : "v1alpha1" , Resource : "noobaaaccounts" },
104
+ {Group : "noobaa.io" , Version : "v1alpha1" , Resource : "noobaas" },
105
+
106
+ // ocs.openshift.io/v1alpha1
107
+ {Group : "ocs.openshift.io" , Version : "v1alpha1" , Resource : "storageconsumers" },
108
+ }
109
+ } else {
110
+ customResources = []pkgrestore.CustomResource {}
111
+ }
112
+
113
+ pkgrestore .RestoreCrd (cmd .Context (), root .ClientSets , root .OperatorNamespace , root .StorageClusterNamespace , groupName , version , "ocs-operator" , customResources , newArgs )
114
+ k8sutil .SetDeploymentScale (cmd .Context (), root .ClientSets .Kube , root .OperatorNamespace , "ocs-operator" , 1 )
26
115
},
27
116
}
0 commit comments