Skip to content

Commit 978dd38

Browse files
committed
Change GetSnapshotSecretReference not to rely on VolumeSnapshot struct
1 parent 0c69186 commit 978dd38

File tree

10 files changed

+26
-1212
lines changed

10 files changed

+26
-1212
lines changed

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ go 1.16
66
require (
77
github.com/container-storage-interface/spec v1.5.0
88
github.com/golang/protobuf v1.5.2
9-
github.com/kubernetes-csi/external-snapshotter/client/v4 v4.1.0
109
github.com/stretchr/testify v1.7.0
1110
golang.org/x/net v0.0.0-20210520170846-37e1c6afe023
1211
google.golang.org/grpc v1.38.0

go.sum

-36
Large diffs are not rendered by default.

params/snapshot.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import (
2020
"fmt"
2121
"strings"
2222

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

8180
resolvedNamespace, err := resolveTemplate(namespaceTemplate, namespaceParams)
@@ -95,9 +94,11 @@ func GetSnapshotSecretReference(secretParams SecretParamsMap, snapshotClassParam
9594
// Secret name template can make use of the VolumeSnapshotContent name, VolumeSnapshot name or namespace.
9695
// Note that VolumeSnapshot name and namespace are under the VolumeSnapshot user's control.
9796
nameParams := map[string]string{"volumesnapshotcontent.name": snapContentName}
98-
if snapshot != nil {
99-
nameParams["volumesnapshot.name"] = snapshot.Name
100-
nameParams["volumesnapshot.namespace"] = snapshot.Namespace
97+
if snapName != "" {
98+
nameParams["volumesnapshot.name"] = snapName
99+
}
100+
if snapNamespace != "" {
101+
nameParams["volumesnapshot.namespace"] = snapNamespace
101102
}
102103
resolvedName, err := resolveTemplate(nameTemplate, nameParams)
103104
if err != nil {

params/snapshot_test.go

+18-22
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,16 @@ import (
2020
"reflect"
2121
"testing"
2222

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

2826
func TestGetSnapshotSecretReference(t *testing.T) {
2927
testcases := map[string]struct {
3028
secretParams SecretParamsMap
3129
params map[string]string
3230
snapContentName string
33-
snapshot *crdv1.VolumeSnapshot
31+
snapNamespace string
32+
snapName string
3433
expectRef *v1.SecretReference
3534
expectErr bool
3635
}{
@@ -45,17 +44,19 @@ func TestGetSnapshotSecretReference(t *testing.T) {
4544
expectErr: true,
4645
},
4746
"simple - valid": {
48-
secretParams: SnapshotterSecretParams,
49-
params: map[string]string{prefixedSnapshotterSecretNameKey: "name", prefixedSnapshotterSecretNamespaceKey: "ns"},
50-
snapshot: &crdv1.VolumeSnapshot{},
51-
expectRef: &v1.SecretReference{Name: "name", Namespace: "ns"},
47+
secretParams: SnapshotterSecretParams,
48+
params: map[string]string{prefixedSnapshotterSecretNameKey: "name", prefixedSnapshotterSecretNamespaceKey: "ns"},
49+
snapNamespace: "",
50+
snapName: "",
51+
expectRef: &v1.SecretReference{Name: "name", Namespace: "ns"},
5252
},
5353
"simple - invalid name": {
54-
secretParams: SnapshotterSecretParams,
55-
params: map[string]string{prefixedSnapshotterSecretNameKey: "bad name", prefixedSnapshotterSecretNamespaceKey: "ns"},
56-
snapshot: &crdv1.VolumeSnapshot{},
57-
expectRef: nil,
58-
expectErr: true,
54+
secretParams: SnapshotterSecretParams,
55+
params: map[string]string{prefixedSnapshotterSecretNameKey: "bad name", prefixedSnapshotterSecretNamespaceKey: "ns"},
56+
snapNamespace: "",
57+
snapName: "",
58+
expectRef: nil,
59+
expectErr: true,
5960
},
6061
"template - invalid": {
6162
secretParams: SnapshotterSecretParams,
@@ -64,21 +65,16 @@ func TestGetSnapshotSecretReference(t *testing.T) {
6465
prefixedSnapshotterSecretNamespaceKey: "static-${volumesnapshotcontent.name}-${volumesnapshot.namespace}",
6566
},
6667
snapContentName: "snapcontentname",
67-
snapshot: &crdv1.VolumeSnapshot{
68-
ObjectMeta: metav1.ObjectMeta{
69-
Name: "snapshotname",
70-
Namespace: "snapshotnamespace",
71-
Annotations: map[string]string{"akey": "avalue"},
72-
},
73-
},
74-
expectRef: nil,
75-
expectErr: true,
68+
snapNamespace: "snapshotnamespace",
69+
snapName: "snapshotname",
70+
expectRef: nil,
71+
expectErr: true,
7672
},
7773
}
7874

7975
for k, tc := range testcases {
8076
t.Run(k, func(t *testing.T) {
81-
ref, err := GetSnapshotSecretReference(tc.secretParams, tc.params, tc.snapContentName, tc.snapshot)
77+
ref, err := GetSnapshotSecretReference(tc.secretParams, tc.params, tc.snapContentName, tc.snapNamespace, tc.snapName)
8278
if err != nil {
8379
if tc.expectErr {
8480
return

vendor/github.com/kubernetes-csi/external-snapshotter/client/v4/LICENSE

-201
This file was deleted.

vendor/github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1/doc.go

-20
This file was deleted.

0 commit comments

Comments
 (0)