Skip to content

Commit 15a4914

Browse files
committed
Add Support for Restoring OCS Operator CRs in ODF CLI
Signed-off-by: Oded Viner <[email protected]>
1 parent 1629ef0 commit 15a4914

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

cmd/odf/restore/crds.go

+54-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,53 @@
11
package restore
22

33
import (
4+
"fmt"
5+
"strings"
6+
47
"github.com/red-hat-storage/odf-cli/cmd/odf/root"
58
"github.com/rook/kubectl-rook-ceph/pkg/k8sutil"
69
pkgrestore "github.com/rook/kubectl-rook-ceph/pkg/restore"
710
"github.com/spf13/cobra"
811
)
912

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+
1051
// deletedCmd represents the deleted command
1152
var deletedCmd = &cobra.Command{
1253
Use: "deleted",
@@ -20,8 +61,19 @@ var deletedCmd = &cobra.Command{
2061
},
2162
Run: func(cmd *cobra.Command, args []string) {
2263
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)
2477
k8sutil.SetDeploymentScale(cmd.Context(), root.ClientSets.Kube, root.OperatorNamespace, "ocs-operator", 1)
25-
2678
},
2779
}

0 commit comments

Comments
 (0)