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