2017-11-13 23:31:36 +00:00
|
|
|
/*
|
2022-09-08 22:09:18 +00:00
|
|
|
Copyright the Velero contributors.
|
2017-11-13 23:31:36 +00:00
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2019-03-15 01:25:52 +00:00
|
|
|
package framework
|
2017-11-13 23:31:36 +00:00
|
|
|
|
|
|
|
import (
|
2017-11-29 17:23:21 +00:00
|
|
|
"encoding/json"
|
|
|
|
|
2018-05-13 13:28:09 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-11-13 23:31:36 +00:00
|
|
|
"golang.org/x/net/context"
|
2017-11-29 17:23:21 +00:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
2017-11-13 23:31:36 +00:00
|
|
|
|
2022-09-01 18:30:07 +00:00
|
|
|
"github.com/vmware-tanzu/velero/pkg/plugin/framework/common"
|
2019-09-30 21:26:56 +00:00
|
|
|
proto "github.com/vmware-tanzu/velero/pkg/plugin/generated"
|
2022-09-08 22:09:18 +00:00
|
|
|
vsv1 "github.com/vmware-tanzu/velero/pkg/plugin/velero/volumesnapshotter/v1"
|
2017-11-13 23:31:36 +00:00
|
|
|
)
|
|
|
|
|
2019-03-27 18:22:04 +00:00
|
|
|
// VolumeSnapshotterGRPCServer implements the proto-generated VolumeSnapshotterServer interface, and accepts
|
2017-11-13 23:31:36 +00:00
|
|
|
// gRPC calls and forwards them to an implementation of the pluggable interface.
|
2019-03-27 18:22:04 +00:00
|
|
|
type VolumeSnapshotterGRPCServer struct {
|
2022-09-01 18:30:07 +00:00
|
|
|
mux *common.ServerMux
|
2018-05-13 13:28:09 +00:00
|
|
|
}
|
|
|
|
|
2022-09-08 22:09:18 +00:00
|
|
|
func (s *VolumeSnapshotterGRPCServer) getImpl(name string) (vsv1.VolumeSnapshotter, error) {
|
2022-09-01 18:30:07 +00:00
|
|
|
impl, err := s.mux.GetHandler(name)
|
2018-05-13 13:28:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-09-08 22:09:18 +00:00
|
|
|
volumeSnapshotter, ok := impl.(vsv1.VolumeSnapshotter)
|
2018-05-13 13:28:09 +00:00
|
|
|
if !ok {
|
2019-03-27 18:22:04 +00:00
|
|
|
return nil, errors.Errorf("%T is not a volume snapshotter", impl)
|
2018-05-13 13:28:09 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 18:22:04 +00:00
|
|
|
return volumeSnapshotter, nil
|
2017-11-13 23:31:36 +00:00
|
|
|
}
|
|
|
|
|
2019-03-27 18:22:04 +00:00
|
|
|
// Init prepares the VolumeSnapshotter for usage using the provided map of
|
|
|
|
// configuration key-value pairs. It returns an error if the VolumeSnapshotter
|
2017-11-13 23:31:36 +00:00
|
|
|
// cannot be initialized from the provided config.
|
2019-04-09 17:50:05 +00:00
|
|
|
func (s *VolumeSnapshotterGRPCServer) Init(ctx context.Context, req *proto.VolumeSnapshotterInitRequest) (response *proto.Empty, err error) {
|
2019-03-13 18:07:52 +00:00
|
|
|
defer func() {
|
2022-09-01 18:30:07 +00:00
|
|
|
if recoveredErr := common.HandlePanic(recover()); recoveredErr != nil {
|
2019-03-13 18:07:52 +00:00
|
|
|
err = recoveredErr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-05-13 13:28:09 +00:00
|
|
|
impl, err := s.getImpl(req.Plugin)
|
|
|
|
if err != nil {
|
2022-09-01 18:30:07 +00:00
|
|
|
return nil, common.NewGRPCError(err)
|
2018-05-13 13:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := impl.Init(req.Config); err != nil {
|
2022-09-01 18:30:07 +00:00
|
|
|
return nil, common.NewGRPCError(err)
|
2017-11-13 23:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.Empty{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateVolumeFromSnapshot creates a new block volume, initialized from the provided snapshot,
|
|
|
|
// and with the specified type and IOPS (if using provisioned IOPS).
|
2019-03-27 18:22:04 +00:00
|
|
|
func (s *VolumeSnapshotterGRPCServer) CreateVolumeFromSnapshot(ctx context.Context, req *proto.CreateVolumeRequest) (response *proto.CreateVolumeResponse, err error) {
|
2019-03-13 18:07:52 +00:00
|
|
|
defer func() {
|
2022-09-01 18:30:07 +00:00
|
|
|
if recoveredErr := common.HandlePanic(recover()); recoveredErr != nil {
|
2019-03-13 18:07:52 +00:00
|
|
|
err = recoveredErr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-05-13 13:28:09 +00:00
|
|
|
impl, err := s.getImpl(req.Plugin)
|
|
|
|
if err != nil {
|
2022-09-01 18:30:07 +00:00
|
|
|
return nil, common.NewGRPCError(err)
|
2018-05-13 13:28:09 +00:00
|
|
|
}
|
|
|
|
|
2017-11-13 23:31:36 +00:00
|
|
|
snapshotID := req.SnapshotID
|
|
|
|
volumeType := req.VolumeType
|
|
|
|
volumeAZ := req.VolumeAZ
|
|
|
|
var iops *int64
|
|
|
|
|
|
|
|
if req.Iops != 0 {
|
|
|
|
iops = &req.Iops
|
|
|
|
}
|
|
|
|
|
2018-05-13 13:28:09 +00:00
|
|
|
volumeID, err := impl.CreateVolumeFromSnapshot(snapshotID, volumeType, volumeAZ, iops)
|
2017-11-13 23:31:36 +00:00
|
|
|
if err != nil {
|
2022-09-01 18:30:07 +00:00
|
|
|
return nil, common.NewGRPCError(err)
|
2017-11-13 23:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.CreateVolumeResponse{VolumeID: volumeID}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetVolumeInfo returns the type and IOPS (if using provisioned IOPS) for a specified block
|
|
|
|
// volume.
|
2019-03-27 18:22:04 +00:00
|
|
|
func (s *VolumeSnapshotterGRPCServer) GetVolumeInfo(ctx context.Context, req *proto.GetVolumeInfoRequest) (response *proto.GetVolumeInfoResponse, err error) {
|
2019-03-13 18:07:52 +00:00
|
|
|
defer func() {
|
2022-09-01 18:30:07 +00:00
|
|
|
if recoveredErr := common.HandlePanic(recover()); recoveredErr != nil {
|
2019-03-13 18:07:52 +00:00
|
|
|
err = recoveredErr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-05-13 13:28:09 +00:00
|
|
|
impl, err := s.getImpl(req.Plugin)
|
|
|
|
if err != nil {
|
2022-09-01 18:30:07 +00:00
|
|
|
return nil, common.NewGRPCError(err)
|
2018-05-13 13:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
volumeType, iops, err := impl.GetVolumeInfo(req.VolumeID, req.VolumeAZ)
|
2017-11-13 23:31:36 +00:00
|
|
|
if err != nil {
|
2022-09-01 18:30:07 +00:00
|
|
|
return nil, common.NewGRPCError(err)
|
2017-11-13 23:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
res := &proto.GetVolumeInfoResponse{
|
|
|
|
VolumeType: volumeType,
|
|
|
|
}
|
|
|
|
|
|
|
|
if iops != nil {
|
|
|
|
res.Iops = *iops
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreateSnapshot creates a snapshot of the specified block volume, and applies the provided
|
|
|
|
// set of tags to the snapshot.
|
2019-03-27 18:22:04 +00:00
|
|
|
func (s *VolumeSnapshotterGRPCServer) CreateSnapshot(ctx context.Context, req *proto.CreateSnapshotRequest) (response *proto.CreateSnapshotResponse, err error) {
|
2019-03-13 18:07:52 +00:00
|
|
|
defer func() {
|
2022-09-01 18:30:07 +00:00
|
|
|
if recoveredErr := common.HandlePanic(recover()); recoveredErr != nil {
|
2019-03-13 18:07:52 +00:00
|
|
|
err = recoveredErr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-05-13 13:28:09 +00:00
|
|
|
impl, err := s.getImpl(req.Plugin)
|
|
|
|
if err != nil {
|
2022-09-01 18:30:07 +00:00
|
|
|
return nil, common.NewGRPCError(err)
|
2018-05-13 13:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
snapshotID, err := impl.CreateSnapshot(req.VolumeID, req.VolumeAZ, req.Tags)
|
2017-11-13 23:31:36 +00:00
|
|
|
if err != nil {
|
2022-09-01 18:30:07 +00:00
|
|
|
return nil, common.NewGRPCError(err)
|
2017-11-13 23:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.CreateSnapshotResponse{SnapshotID: snapshotID}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteSnapshot deletes the specified volume snapshot.
|
2019-03-27 18:22:04 +00:00
|
|
|
func (s *VolumeSnapshotterGRPCServer) DeleteSnapshot(ctx context.Context, req *proto.DeleteSnapshotRequest) (response *proto.Empty, err error) {
|
2019-03-13 18:07:52 +00:00
|
|
|
defer func() {
|
2022-09-01 18:30:07 +00:00
|
|
|
if recoveredErr := common.HandlePanic(recover()); recoveredErr != nil {
|
2019-03-13 18:07:52 +00:00
|
|
|
err = recoveredErr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-05-13 13:28:09 +00:00
|
|
|
impl, err := s.getImpl(req.Plugin)
|
|
|
|
if err != nil {
|
2022-09-01 18:30:07 +00:00
|
|
|
return nil, common.NewGRPCError(err)
|
2018-05-13 13:28:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := impl.DeleteSnapshot(req.SnapshotID); err != nil {
|
2022-09-01 18:30:07 +00:00
|
|
|
return nil, common.NewGRPCError(err)
|
2017-11-13 23:31:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.Empty{}, nil
|
|
|
|
}
|
2017-11-29 17:23:21 +00:00
|
|
|
|
2019-03-27 18:22:04 +00:00
|
|
|
func (s *VolumeSnapshotterGRPCServer) GetVolumeID(ctx context.Context, req *proto.GetVolumeIDRequest) (response *proto.GetVolumeIDResponse, err error) {
|
2019-03-13 18:07:52 +00:00
|
|
|
defer func() {
|
2022-09-01 18:30:07 +00:00
|
|
|
if recoveredErr := common.HandlePanic(recover()); recoveredErr != nil {
|
2019-03-13 18:07:52 +00:00
|
|
|
err = recoveredErr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-05-13 13:28:09 +00:00
|
|
|
impl, err := s.getImpl(req.Plugin)
|
|
|
|
if err != nil {
|
2022-09-01 18:30:07 +00:00
|
|
|
return nil, common.NewGRPCError(err)
|
2018-05-13 13:28:09 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 17:23:21 +00:00
|
|
|
var pv unstructured.Unstructured
|
|
|
|
|
|
|
|
if err := json.Unmarshal(req.PersistentVolume, &pv); err != nil {
|
2022-09-01 18:30:07 +00:00
|
|
|
return nil, common.NewGRPCError(errors.WithStack(err))
|
2017-11-29 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-13 13:28:09 +00:00
|
|
|
volumeID, err := impl.GetVolumeID(&pv)
|
2017-11-29 17:23:21 +00:00
|
|
|
if err != nil {
|
2022-09-01 18:30:07 +00:00
|
|
|
return nil, common.NewGRPCError(err)
|
2017-11-29 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.GetVolumeIDResponse{VolumeID: volumeID}, nil
|
|
|
|
}
|
|
|
|
|
2019-03-27 18:22:04 +00:00
|
|
|
func (s *VolumeSnapshotterGRPCServer) SetVolumeID(ctx context.Context, req *proto.SetVolumeIDRequest) (response *proto.SetVolumeIDResponse, err error) {
|
2019-03-13 18:07:52 +00:00
|
|
|
defer func() {
|
2022-09-01 18:30:07 +00:00
|
|
|
if recoveredErr := common.HandlePanic(recover()); recoveredErr != nil {
|
2019-03-13 18:07:52 +00:00
|
|
|
err = recoveredErr
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2018-05-13 13:28:09 +00:00
|
|
|
impl, err := s.getImpl(req.Plugin)
|
|
|
|
if err != nil {
|
2022-09-01 18:30:07 +00:00
|
|
|
return nil, common.NewGRPCError(err)
|
2018-05-13 13:28:09 +00:00
|
|
|
}
|
|
|
|
|
2017-11-29 17:23:21 +00:00
|
|
|
var pv unstructured.Unstructured
|
|
|
|
if err := json.Unmarshal(req.PersistentVolume, &pv); err != nil {
|
2022-09-01 18:30:07 +00:00
|
|
|
return nil, common.NewGRPCError(errors.WithStack(err))
|
2017-11-29 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
2018-05-13 13:28:09 +00:00
|
|
|
updatedPV, err := impl.SetVolumeID(&pv, req.VolumeID)
|
2017-11-29 17:23:21 +00:00
|
|
|
if err != nil {
|
2022-09-01 18:30:07 +00:00
|
|
|
return nil, common.NewGRPCError(err)
|
2017-11-29 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updatedPVBytes, err := json.Marshal(updatedPV.UnstructuredContent())
|
|
|
|
if err != nil {
|
2022-09-01 18:30:07 +00:00
|
|
|
return nil, common.NewGRPCError(err)
|
2017-11-29 17:23:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &proto.SetVolumeIDResponse{PersistentVolume: updatedPVBytes}, nil
|
|
|
|
}
|