Skip to content

Commit e8b598e

Browse files
committed
Allow recovery to new cluster
Signed-off-by: Kasakaze <[email protected]>
1 parent bf73079 commit e8b598e

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

pkg/cstor/cstor.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"context"
2121
"fmt"
2222
"net"
23+
"os"
2324
"strings"
2425
"time"
2526

@@ -111,6 +112,9 @@ type Plugin struct {
111112
// namespace in which openebs is installed, default is openebs
112113
namespace string
113114

115+
// nodeID is used to identify the node on which the program is running
116+
nodeID string
117+
114118
// cl stores cloud connection information
115119
cl *cloud.Conn
116120

@@ -218,6 +222,13 @@ func (p *Plugin) Init(config map[string]string) error {
218222
p.namespace = ns
219223
}
220224

225+
nodeID := os.Getenv("VELERO_NODE_ID")
226+
if nodeID == "" {
227+
return errors.New("env VELERO_NODE_ID not set")
228+
}
229+
p.Log.Infof("env VELERO_NODE_ID: ", nodeID)
230+
p.nodeID = nodeID
231+
221232
conf, err := rest.InClusterConfig()
222233
if err != nil {
223234
p.Log.Errorf("Failed to get cluster config : %s", err.Error())

pkg/cstor/cvc_operation.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ limitations under the License.
1717
package cstor
1818

1919
import (
20+
"context"
2021
"encoding/json"
2122
"fmt"
2223

2324
cstorv1 "github.com/openebs/api/v2/pkg/apis/cstor/v1"
2425
maya "github.com/openebs/cstor-csi/pkg/utils"
2526
"github.com/pkg/errors"
2627
k8serrors "k8s.io/apimachinery/pkg/api/errors"
28+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2729
)
2830

2931
// (Kasakaze)todo: Determine whether it is csiVolume, if so, cvc must be backed up
@@ -83,10 +85,13 @@ func (p *Plugin) restoreCVC(volumeID, pvcName, pvcNamespace, snapName string) er
8385
rCount = fmt.Sprint(rcvc.Spec.Provision.ReplicaCount)
8486
cspcName = rcvc.ObjectMeta.Labels["openebs.io/cstor-pool-cluster"]
8587
snapshotID = ""
86-
// (Kasakaze)todo: If the data is migrated to another cluster, the nodeID may not be the same
8788
nodeID = rcvc.Publish.NodeID
8889
policyName = rcvc.ObjectMeta.Labels["openebs.io/volume-policy"]
8990
)
91+
nodeID, err = p.getValidNodeID(nodeID)
92+
if err != nil {
93+
return errors.Cause(err)
94+
}
9095

9196
err = maya.ProvisionVolume(size, volumeID, rCount,
9297
cspcName, snapshotID,
@@ -114,3 +119,22 @@ func (p *Plugin) downloadCVC(volumeID, snapName string) (*cstorv1.CStorVolumeCon
114119

115120
return cvc, nil
116121
}
122+
123+
// If the backup cvc nodeID does not belong to the current cluster, use the environment variable OPENEBS_NODE_ID
124+
func (p *Plugin) getValidNodeID(nodeID string) (string, error) {
125+
if nodeID == "" {
126+
return p.nodeID, nil
127+
}
128+
129+
_, err := p.K8sClient.CoreV1().Nodes().Get(context.TODO(), nodeID, metav1.GetOptions{})
130+
if err != nil {
131+
if !k8serrors.IsNotFound(err) {
132+
return "", errors.Cause(err)
133+
}
134+
135+
p.Log.Warnf("invalid nodeID(%s), use env VELERO_NODE_ID(%s)", nodeID, p.nodeID)
136+
nodeID = p.nodeID
137+
}
138+
139+
return nodeID, nil
140+
}

0 commit comments

Comments
 (0)