-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New functions to pre-provision VolumeSnapshot and VolumeSnapshotConte…
…nt (#1282) * Add the new CreateCSISnapshotStatuc and DeleteCSISnapshotContent functions The new 'create' function allows user to pre-provision a pair of VolumeSnapshot and VolumeSnapshotContent resources, which aren't associated with any existing PersistentVolumeClaim resource. See https://kubernetes.io/docs/concepts/storage/volume-snapshots/ for the difference between dynamic and pre-provisioned snapshots. Signed-off-by: Ivan Sim <ivan.sim@kasten.io> * Update gitignore Signed-off-by: Ivan Sim <ivan.sim@kasten.io> * Update controller RBAC to manage snapshot class Signed-off-by: Ivan Sim <ivan.sim@kasten.io> * Update documentation Signed-off-by: Ivan Sim <ivan.sim@kasten.io> * Address Vivek's feedback Signed-off-by: Ivan Sim <ivan.sim@kasten.io> * Add suffix to distinguish input/output Variables Co-authored-by: Pavan Navarathna <pavan@kasten.io> * Add newline to method signature Co-authored-by: Pavan Navarathna <pavan@kasten.io> * Update documentation Signed-off-by: Ivan Sim <ivan.sim@kasten.io> * Change testSuite fields to local constants Signed-off-by: Ivan Sim <ivan.sim@kasten.io> * Fix gofmt issues from GH assisted-commit Signed-off-by: Ivan Sim <ivan.sim@kasten.io> Co-authored-by: Pavan Navarathna <pavan@kasten.io> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
428c402
commit e3e762e
Showing
8 changed files
with
561 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ | |
/.vendor | ||
/bin | ||
/dist | ||
**/*.swp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
// Copyright 2022 The Kanister Authors. | ||
// | ||
// 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. | ||
|
||
package function | ||
|
||
import ( | ||
"context" | ||
|
||
v1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1" | ||
|
||
kanister "github.com/kanisterio/kanister/pkg" | ||
"github.com/kanisterio/kanister/pkg/kube" | ||
"github.com/kanisterio/kanister/pkg/kube/snapshot" | ||
"github.com/kanisterio/kanister/pkg/param" | ||
) | ||
|
||
func init() { | ||
_ = kanister.Register(&createCSISnapshotStaticFunc{}) | ||
} | ||
|
||
var ( | ||
_ kanister.Func = (*createCSISnapshotStaticFunc)(nil) | ||
) | ||
|
||
const ( | ||
// CreateCSISnapshotStaticFuncName gives the name of the function | ||
CreateCSISnapshotStaticFuncName = "CreateCSISnapshotStatic" | ||
|
||
// CreateCSISnapshotStaticNameArg provides name of the new VolumeSnapshot | ||
CreateCSISnapshotStaticNameArg = "name" | ||
|
||
// CreateCSISnapshotStaticNamespaceArg specifies the namespace of the new VolumeSnapshot | ||
CreateCSISnapshotStaticNamespaceArg = "namespace" | ||
|
||
// CreateCSISnapshotStaticDriverArg specifies the CSI driver used in the new VolumeSnapshotContent | ||
CreateCSISnapshotStaticDriverArg = "driver" | ||
|
||
// CreateCSISnapshotStaticSnapshotHandleArg specifies the UID of the backend storage snapshot used in the new VolumeSnapshotContent | ||
CreateCSISnapshotStaticSnapshotHandleArg = "snapshotHandle" | ||
|
||
// CreateCSISnapshotStaticSnapshotClassArg specifies the name of the VolumeSnapshotClass | ||
CreateCSISnapshotStaticSnapshotClassArg = "snapshotClass" | ||
|
||
// CreateCSISnapshotStaticOutputRestoreSize gives the storage size required for PV/PVC restoration | ||
CreateCSISnapshotStaticOutputRestoreSize = "restoreSize" | ||
|
||
// CreateCSISnapshotStaticOutputSnapshotContentName provides the name of dynamically provisioned VolumeSnapshotContent | ||
CreateCSISnapshotStaticOutputSnapshotContentName = "snapshotContent" | ||
) | ||
|
||
type createCSISnapshotStaticFunc struct{} | ||
|
||
func (*createCSISnapshotStaticFunc) Name() string { | ||
return CreateCSISnapshotStaticFuncName | ||
} | ||
|
||
func (*createCSISnapshotStaticFunc) Exec(ctx context.Context, tp param.TemplateParams, args map[string]interface{}) (map[string]interface{}, error) { | ||
var ( | ||
name, namespace string | ||
driver, snapshotHandle, snapshotClass string | ||
) | ||
|
||
if err := Arg(args, CreateCSISnapshotStaticNameArg, &name); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := Arg(args, CreateCSISnapshotStaticNamespaceArg, &namespace); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := Arg(args, CreateCSISnapshotStaticDriverArg, &driver); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := Arg(args, CreateCSISnapshotStaticSnapshotHandleArg, &snapshotHandle); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := Arg(args, CreateCSISnapshotStaticSnapshotClassArg, &snapshotClass); err != nil { | ||
return nil, err | ||
} | ||
|
||
kubeCli, err := kube.NewClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
dynCli, err := kube.NewDynamicClient() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
snapshotter, err := snapshot.NewSnapshotter(kubeCli, dynCli) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
// waitForReady is set to true by default because snapshot information is needed as output artifacts | ||
waitForReady := true | ||
vs, err := createCSISnapshotStatic(ctx, snapshotter, name, namespace, driver, snapshotHandle, snapshotClass, waitForReady) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return map[string]interface{}{ | ||
CreateCSISnapshotStaticNameArg: name, | ||
CreateCSISnapshotStaticNamespaceArg: namespace, | ||
CreateCSISnapshotStaticOutputRestoreSize: vs.Status.RestoreSize.String(), | ||
CreateCSISnapshotStaticOutputSnapshotContentName: vs.Status.BoundVolumeSnapshotContentName, | ||
}, nil | ||
} | ||
|
||
func (*createCSISnapshotStaticFunc) RequiredArgs() []string { | ||
return []string{ | ||
CreateCSISnapshotStaticNameArg, | ||
CreateCSISnapshotStaticNamespaceArg, | ||
CreateCSISnapshotStaticDriverArg, | ||
CreateCSISnapshotStaticSnapshotHandleArg, | ||
CreateCSISnapshotStaticSnapshotClassArg, | ||
} | ||
} | ||
|
||
func (*createCSISnapshotStaticFunc) Arguments() []string { | ||
return []string{ | ||
CreateCSISnapshotStaticNameArg, | ||
CreateCSISnapshotStaticNamespaceArg, | ||
CreateCSISnapshotStaticDriverArg, | ||
CreateCSISnapshotStaticSnapshotHandleArg, | ||
CreateCSISnapshotStaticSnapshotClassArg, | ||
} | ||
} | ||
|
||
func createCSISnapshotStatic( | ||
ctx context.Context, | ||
snapshotter snapshot.Snapshotter, | ||
name, namespace, driver, snapshotHandle, snapshotClass string, | ||
wait bool, | ||
) (*v1.VolumeSnapshot, error) { | ||
source := &snapshot.Source{ | ||
Handle: snapshotHandle, | ||
Driver: driver, | ||
VolumeSnapshotClassName: snapshotClass, | ||
} | ||
if err := snapshotter.CreateFromSource(ctx, source, name, namespace, wait); err != nil { | ||
return nil, err | ||
} | ||
|
||
return snapshotter.Get(ctx, name, namespace) | ||
} |
Oops, something went wrong.