@@ -21,7 +21,6 @@ import (
2121 "reflect"
2222
2323 volumesnapshotv1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1"
24- volumesnapshotv1beta1 "github.com/kubernetes-csi/external-snapshotter/client/v6/apis/volumesnapshot/v1beta1"
2524 storagelisters "github.com/kubernetes-csi/external-snapshotter/client/v6/listers/volumesnapshot/v1"
2625 "github.com/kubernetes-csi/external-snapshotter/v6/pkg/utils"
2726 v1 "k8s.io/api/admission/v1"
@@ -31,12 +30,8 @@ import (
3130)
3231
3332var (
34- // SnapshotV1Beta1GVR is GroupVersionResource for v1beta1 VolumeSnapshots
35- SnapshotV1Beta1GVR = metav1.GroupVersionResource {Group : volumesnapshotv1beta1 .GroupName , Version : "v1beta1" , Resource : "volumesnapshots" }
3633 // SnapshotV1GVR is GroupVersionResource for v1 VolumeSnapshots
3734 SnapshotV1GVR = metav1.GroupVersionResource {Group : volumesnapshotv1 .GroupName , Version : "v1" , Resource : "volumesnapshots" }
38- // SnapshotContentV1Beta1GVR is GroupVersionResource for v1beta1 VolumeSnapshotContents
39- SnapshotContentV1Beta1GVR = metav1.GroupVersionResource {Group : volumesnapshotv1beta1 .GroupName , Version : "v1beta1" , Resource : "volumesnapshotcontents" }
4035 // SnapshotContentV1GVR is GroupVersionResource for v1 VolumeSnapshotContents
4136 SnapshotContentV1GVR = metav1.GroupVersionResource {Group : volumesnapshotv1 .GroupName , Version : "v1" , Resource : "volumesnapshotcontents" }
4237 // SnapshotContentV1GVR is GroupVersionResource for v1 VolumeSnapshotContents
@@ -77,18 +72,6 @@ func (a admitter) Admit(ar v1.AdmissionReview) *v1.AdmissionResponse {
7772
7873 deserializer := codecs .UniversalDeserializer ()
7974 switch ar .Request .Resource {
80- case SnapshotV1Beta1GVR :
81- snapshot := & volumesnapshotv1beta1.VolumeSnapshot {}
82- if _ , _ , err := deserializer .Decode (raw , nil , snapshot ); err != nil {
83- klog .Error (err )
84- return toV1AdmissionResponse (err )
85- }
86- oldSnapshot := & volumesnapshotv1beta1.VolumeSnapshot {}
87- if _ , _ , err := deserializer .Decode (oldRaw , nil , oldSnapshot ); err != nil {
88- klog .Error (err )
89- return toV1AdmissionResponse (err )
90- }
91- return decideSnapshotV1beta1 (snapshot , oldSnapshot , isUpdate )
9275 case SnapshotV1GVR :
9376 snapshot := & volumesnapshotv1.VolumeSnapshot {}
9477 if _ , _ , err := deserializer .Decode (raw , nil , snapshot ); err != nil {
@@ -101,18 +84,6 @@ func (a admitter) Admit(ar v1.AdmissionReview) *v1.AdmissionResponse {
10184 return toV1AdmissionResponse (err )
10285 }
10386 return decideSnapshotV1 (snapshot , oldSnapshot , isUpdate )
104- case SnapshotContentV1Beta1GVR :
105- snapcontent := & volumesnapshotv1beta1.VolumeSnapshotContent {}
106- if _ , _ , err := deserializer .Decode (raw , nil , snapcontent ); err != nil {
107- klog .Error (err )
108- return toV1AdmissionResponse (err )
109- }
110- oldSnapcontent := & volumesnapshotv1beta1.VolumeSnapshotContent {}
111- if _ , _ , err := deserializer .Decode (oldRaw , nil , oldSnapcontent ); err != nil {
112- klog .Error (err )
113- return toV1AdmissionResponse (err )
114- }
115- return decideSnapshotContentV1beta1 (snapcontent , oldSnapcontent , isUpdate )
11687 case SnapshotContentV1GVR :
11788 snapcontent := & volumesnapshotv1.VolumeSnapshotContent {}
11889 if _ , _ , err := deserializer .Decode (raw , nil , snapcontent ); err != nil {
@@ -138,44 +109,12 @@ func (a admitter) Admit(ar v1.AdmissionReview) *v1.AdmissionResponse {
138109 }
139110 return decideSnapshotClassV1 (snapClass , oldSnapClass , a .lister )
140111 default :
141- err := fmt .Errorf ("expect resource to be %s or %s" , SnapshotV1Beta1GVR , SnapshotContentV1Beta1GVR )
112+ err := fmt .Errorf ("expect resource to be %s, %s or %s" , SnapshotV1GVR , SnapshotContentV1GVR , SnapshotClassV1GVR )
142113 klog .Error (err )
143114 return toV1AdmissionResponse (err )
144115 }
145116}
146117
147- func decideSnapshotV1beta1 (snapshot , oldSnapshot * volumesnapshotv1beta1.VolumeSnapshot , isUpdate bool ) * v1.AdmissionResponse {
148- reviewResponse := & v1.AdmissionResponse {
149- Allowed : true ,
150- Result : & metav1.Status {},
151- }
152-
153- if isUpdate {
154- // if it is an UPDATE and oldSnapshot is not valid, then don't enforce strict validation
155- // This allows no-op updates to occur on snapshot resources which fail strict validation
156- // Which allows the remover of finalizers and therefore deletion of this object
157- // Don't rely on the pointers to be nil, because the deserialization method will convert it to
158- // The empty struct value. Instead check the operation type.
159- if err := ValidateV1Beta1Snapshot (oldSnapshot ); err != nil {
160- return reviewResponse
161- }
162-
163- // if it is an UPDATE and oldSnapshot is valid, check immutable fields
164- if err := checkSnapshotImmutableFieldsV1beta1 (snapshot , oldSnapshot ); err != nil {
165- reviewResponse .Allowed = false
166- reviewResponse .Result .Message = err .Error ()
167- return reviewResponse
168- }
169- }
170- // Enforce strict validation for CREATE requests. Immutable checks don't apply for CREATE requests.
171- // Enforce strict validation for UPDATE requests where old is valid and passes immutability check.
172- if err := ValidateV1Beta1Snapshot (snapshot ); err != nil {
173- reviewResponse .Allowed = false
174- reviewResponse .Result .Message = err .Error ()
175- }
176- return reviewResponse
177- }
178-
179118func decideSnapshotV1 (snapshot , oldSnapshot * volumesnapshotv1.VolumeSnapshot , isUpdate bool ) * v1.AdmissionResponse {
180119 reviewResponse := & v1.AdmissionResponse {
181120 Allowed : true ,
@@ -199,38 +138,6 @@ func decideSnapshotV1(snapshot, oldSnapshot *volumesnapshotv1.VolumeSnapshot, is
199138 return reviewResponse
200139}
201140
202- func decideSnapshotContentV1beta1 (snapcontent , oldSnapcontent * volumesnapshotv1beta1.VolumeSnapshotContent , isUpdate bool ) * v1.AdmissionResponse {
203- reviewResponse := & v1.AdmissionResponse {
204- Allowed : true ,
205- Result : & metav1.Status {},
206- }
207-
208- if isUpdate {
209- // if it is an UPDATE and oldSnapcontent is not valid, then don't enforce strict validation
210- // This allows no-op updates to occur on snapshot resources which fail strict validation
211- // Which allows the remover of finalizers and therefore deletion of this object
212- // Don't rely on the pointers to be nil, because the deserialization method will convert it to
213- // The empty struct value. Instead check the operation type.
214- if err := ValidateV1Beta1SnapshotContent (oldSnapcontent ); err != nil {
215- return reviewResponse
216- }
217-
218- // if it is an UPDATE and oldSnapcontent is valid, check immutable fields
219- if err := checkSnapshotContentImmutableFieldsV1beta1 (snapcontent , oldSnapcontent ); err != nil {
220- reviewResponse .Allowed = false
221- reviewResponse .Result .Message = err .Error ()
222- return reviewResponse
223- }
224- }
225- // Enforce strict validation for all CREATE requests. Immutable checks don't apply for CREATE requests.
226- // Enforce strict validation for UPDATE requests where old is valid and passes immutability check.
227- if err := ValidateV1Beta1SnapshotContent (snapcontent ); err != nil {
228- reviewResponse .Allowed = false
229- reviewResponse .Result .Message = err .Error ()
230- }
231- return reviewResponse
232- }
233-
234141func decideSnapshotContentV1 (snapcontent , oldSnapcontent * volumesnapshotv1.VolumeSnapshotContent , isUpdate bool ) * v1.AdmissionResponse {
235142 reviewResponse := & v1.AdmissionResponse {
236143 Allowed : true ,
@@ -298,27 +205,6 @@ func strPtrDereference(s *string) string {
298205 return * s
299206}
300207
301- func checkSnapshotImmutableFieldsV1beta1 (snapshot , oldSnapshot * volumesnapshotv1beta1.VolumeSnapshot ) error {
302- if snapshot == nil {
303- return fmt .Errorf ("VolumeSnapshot is nil" )
304- }
305- if oldSnapshot == nil {
306- return fmt .Errorf ("old VolumeSnapshot is nil" )
307- }
308-
309- source := snapshot .Spec .Source
310- oldSource := oldSnapshot .Spec .Source
311-
312- if ! reflect .DeepEqual (source .PersistentVolumeClaimName , oldSource .PersistentVolumeClaimName ) {
313- return fmt .Errorf ("Spec.Source.PersistentVolumeClaimName is immutable but was changed from %s to %s" , strPtrDereference (oldSource .PersistentVolumeClaimName ), strPtrDereference (source .PersistentVolumeClaimName ))
314- }
315- if ! reflect .DeepEqual (source .VolumeSnapshotContentName , oldSource .VolumeSnapshotContentName ) {
316- return fmt .Errorf ("Spec.Source.VolumeSnapshotContentName is immutable but was changed from %s to %s" , strPtrDereference (oldSource .VolumeSnapshotContentName ), strPtrDereference (source .VolumeSnapshotContentName ))
317- }
318-
319- return nil
320- }
321-
322208func checkSnapshotImmutableFieldsV1 (snapshot , oldSnapshot * volumesnapshotv1.VolumeSnapshot ) error {
323209 if snapshot == nil {
324210 return fmt .Errorf ("VolumeSnapshot is nil" )
@@ -340,26 +226,6 @@ func checkSnapshotImmutableFieldsV1(snapshot, oldSnapshot *volumesnapshotv1.Volu
340226 return nil
341227}
342228
343- func checkSnapshotContentImmutableFieldsV1beta1 (snapcontent , oldSnapcontent * volumesnapshotv1beta1.VolumeSnapshotContent ) error {
344- if snapcontent == nil {
345- return fmt .Errorf ("VolumeSnapshotContent is nil" )
346- }
347- if oldSnapcontent == nil {
348- return fmt .Errorf ("old VolumeSnapshotContent is nil" )
349- }
350-
351- source := snapcontent .Spec .Source
352- oldSource := oldSnapcontent .Spec .Source
353-
354- if ! reflect .DeepEqual (source .VolumeHandle , oldSource .VolumeHandle ) {
355- return fmt .Errorf ("Spec.Source.VolumeHandle is immutable but was changed from %s to %s" , strPtrDereference (oldSource .VolumeHandle ), strPtrDereference (source .VolumeHandle ))
356- }
357- if ! reflect .DeepEqual (source .SnapshotHandle , oldSource .SnapshotHandle ) {
358- return fmt .Errorf ("Spec.Source.SnapshotHandle is immutable but was changed from %s to %s" , strPtrDereference (oldSource .SnapshotHandle ), strPtrDereference (source .SnapshotHandle ))
359- }
360- return nil
361- }
362-
363229func checkSnapshotContentImmutableFieldsV1 (snapcontent , oldSnapcontent * volumesnapshotv1.VolumeSnapshotContent ) error {
364230 if snapcontent == nil {
365231 return fmt .Errorf ("VolumeSnapshotContent is nil" )
0 commit comments