Skip to content

Commit

Permalink
Change GetSnapshotSecretReference not to rely on VolumeSnapshot struct
Browse files Browse the repository at this point in the history
  • Loading branch information
mkimuram committed Jul 28, 2021
1 parent 0c69186 commit 978dd38
Show file tree
Hide file tree
Showing 10 changed files with 26 additions and 1,212 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ go 1.16
require (
github.com/container-storage-interface/spec v1.5.0
github.com/golang/protobuf v1.5.2
github.com/kubernetes-csi/external-snapshotter/client/v4 v4.1.0
github.com/stretchr/testify v1.7.0
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023
google.golang.org/grpc v1.38.0
Expand Down
36 changes: 0 additions & 36 deletions go.sum

Large diffs are not rendered by default.

15 changes: 8 additions & 7 deletions params/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"fmt"
"strings"

crdv1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -57,7 +56,7 @@ var (
// - the nameTemplate or namespaceTemplate contains a token that cannot be resolved
// - the resolved name is not a valid secret name
// - the resolved namespace is not a valid namespace name
func GetSnapshotSecretReference(secretParams SecretParamsMap, snapshotClassParams map[string]string, snapContentName string, snapshot *crdv1.VolumeSnapshot) (*v1.SecretReference, error) {
func GetSnapshotSecretReference(secretParams SecretParamsMap, snapshotClassParams map[string]string, snapContentName string, snapNamespace string, snapName string) (*v1.SecretReference, error) {
nameTemplate, namespaceTemplate, err := VerifyAndGetSecretNameAndNamespaceTemplate(secretParams, snapshotClassParams)
if err != nil {
return nil, fmt.Errorf("failed to get name and namespace template from params: %v", err)
Expand All @@ -74,8 +73,8 @@ func GetSnapshotSecretReference(secretParams SecretParamsMap, snapshotClassParam
namespaceParams := map[string]string{"volumesnapshotcontent.name": snapContentName}
// snapshot may be nil when resolving create/delete snapshot secret names because the
// snapshot may or may not exist at delete time
if snapshot != nil {
namespaceParams["volumesnapshot.namespace"] = snapshot.Namespace
if snapNamespace != "" {
namespaceParams["volumesnapshot.namespace"] = snapNamespace
}

resolvedNamespace, err := resolveTemplate(namespaceTemplate, namespaceParams)
Expand All @@ -95,9 +94,11 @@ func GetSnapshotSecretReference(secretParams SecretParamsMap, snapshotClassParam
// Secret name template can make use of the VolumeSnapshotContent name, VolumeSnapshot name or namespace.
// Note that VolumeSnapshot name and namespace are under the VolumeSnapshot user's control.
nameParams := map[string]string{"volumesnapshotcontent.name": snapContentName}
if snapshot != nil {
nameParams["volumesnapshot.name"] = snapshot.Name
nameParams["volumesnapshot.namespace"] = snapshot.Namespace
if snapName != "" {
nameParams["volumesnapshot.name"] = snapName
}
if snapNamespace != "" {
nameParams["volumesnapshot.namespace"] = snapNamespace
}
resolvedName, err := resolveTemplate(nameTemplate, nameParams)
if err != nil {
Expand Down
40 changes: 18 additions & 22 deletions params/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@ import (
"reflect"
"testing"

crdv1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestGetSnapshotSecretReference(t *testing.T) {
testcases := map[string]struct {
secretParams SecretParamsMap
params map[string]string
snapContentName string
snapshot *crdv1.VolumeSnapshot
snapNamespace string
snapName string
expectRef *v1.SecretReference
expectErr bool
}{
Expand All @@ -45,17 +44,19 @@ func TestGetSnapshotSecretReference(t *testing.T) {
expectErr: true,
},
"simple - valid": {
secretParams: SnapshotterSecretParams,
params: map[string]string{prefixedSnapshotterSecretNameKey: "name", prefixedSnapshotterSecretNamespaceKey: "ns"},
snapshot: &crdv1.VolumeSnapshot{},
expectRef: &v1.SecretReference{Name: "name", Namespace: "ns"},
secretParams: SnapshotterSecretParams,
params: map[string]string{prefixedSnapshotterSecretNameKey: "name", prefixedSnapshotterSecretNamespaceKey: "ns"},
snapNamespace: "",
snapName: "",
expectRef: &v1.SecretReference{Name: "name", Namespace: "ns"},
},
"simple - invalid name": {
secretParams: SnapshotterSecretParams,
params: map[string]string{prefixedSnapshotterSecretNameKey: "bad name", prefixedSnapshotterSecretNamespaceKey: "ns"},
snapshot: &crdv1.VolumeSnapshot{},
expectRef: nil,
expectErr: true,
secretParams: SnapshotterSecretParams,
params: map[string]string{prefixedSnapshotterSecretNameKey: "bad name", prefixedSnapshotterSecretNamespaceKey: "ns"},
snapNamespace: "",
snapName: "",
expectRef: nil,
expectErr: true,
},
"template - invalid": {
secretParams: SnapshotterSecretParams,
Expand All @@ -64,21 +65,16 @@ func TestGetSnapshotSecretReference(t *testing.T) {
prefixedSnapshotterSecretNamespaceKey: "static-${volumesnapshotcontent.name}-${volumesnapshot.namespace}",
},
snapContentName: "snapcontentname",
snapshot: &crdv1.VolumeSnapshot{
ObjectMeta: metav1.ObjectMeta{
Name: "snapshotname",
Namespace: "snapshotnamespace",
Annotations: map[string]string{"akey": "avalue"},
},
},
expectRef: nil,
expectErr: true,
snapNamespace: "snapshotnamespace",
snapName: "snapshotname",
expectRef: nil,
expectErr: true,
},
}

for k, tc := range testcases {
t.Run(k, func(t *testing.T) {
ref, err := GetSnapshotSecretReference(tc.secretParams, tc.params, tc.snapContentName, tc.snapshot)
ref, err := GetSnapshotSecretReference(tc.secretParams, tc.params, tc.snapContentName, tc.snapNamespace, tc.snapName)
if err != nil {
if tc.expectErr {
return
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 978dd38

Please sign in to comment.