|
| 1 | +/* |
| 2 | +Copyright 2021 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package webhook |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + |
| 22 | + crdv1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1" |
| 23 | + crdv1beta1 "github.com/kubernetes-csi/external-snapshotter/client/v4/apis/volumesnapshot/v1beta1" |
| 24 | +) |
| 25 | + |
| 26 | +// ValidateV1Snapshot performs additional strict validation. |
| 27 | +// Do NOT rely on this function to fully validate snapshot objects. |
| 28 | +// This function will only check the additional rules provided by the webhook. |
| 29 | +func ValidateV1Snapshot(snapshot *crdv1.VolumeSnapshot) error { |
| 30 | + if snapshot == nil { |
| 31 | + return fmt.Errorf("VolumeSnapshot is nil") |
| 32 | + } |
| 33 | + |
| 34 | + vscname := snapshot.Spec.VolumeSnapshotClassName |
| 35 | + if vscname != nil && *vscname == "" { |
| 36 | + return fmt.Errorf("Spec.VolumeSnapshotClassName must not be the empty string") |
| 37 | + } |
| 38 | + return nil |
| 39 | +} |
| 40 | + |
| 41 | +// ValidateV1Beta1Snapshot performs additional strict validation. |
| 42 | +// Do NOT rely on this function to fully validate snapshot objects. |
| 43 | +// This function will only check the additional rules provided by the webhook. |
| 44 | +func ValidateV1Beta1Snapshot(snapshot *crdv1beta1.VolumeSnapshot) error { |
| 45 | + if snapshot == nil { |
| 46 | + return fmt.Errorf("VolumeSnapshot is nil") |
| 47 | + } |
| 48 | + |
| 49 | + source := snapshot.Spec.Source |
| 50 | + |
| 51 | + if source.PersistentVolumeClaimName != nil && source.VolumeSnapshotContentName != nil { |
| 52 | + return fmt.Errorf("only one of Spec.Source.PersistentVolumeClaimName = %s and Spec.Source.VolumeSnapshotContentName = %s should be set", *source.PersistentVolumeClaimName, *source.VolumeSnapshotContentName) |
| 53 | + } |
| 54 | + if source.PersistentVolumeClaimName == nil && source.VolumeSnapshotContentName == nil { |
| 55 | + return fmt.Errorf("one of Spec.Source.PersistentVolumeClaimName and Spec.Source.VolumeSnapshotContentName should be set") |
| 56 | + } |
| 57 | + vscname := snapshot.Spec.VolumeSnapshotClassName |
| 58 | + if vscname != nil && *vscname == "" { |
| 59 | + return fmt.Errorf("Spec.VolumeSnapshotClassName must not be the empty string") |
| 60 | + } |
| 61 | + return nil |
| 62 | +} |
| 63 | + |
| 64 | +// ValidateV1SnapshotContent performs additional strict validation. |
| 65 | +// Do NOT rely on this function to fully validate snapshot content objects. |
| 66 | +// This function will only check the additional rules provided by the webhook. |
| 67 | +func ValidateV1SnapshotContent(snapcontent *crdv1.VolumeSnapshotContent) error { |
| 68 | + if snapcontent == nil { |
| 69 | + return fmt.Errorf("VolumeSnapshotContent is nil") |
| 70 | + } |
| 71 | + |
| 72 | + vsref := snapcontent.Spec.VolumeSnapshotRef |
| 73 | + |
| 74 | + if vsref.Name == "" || vsref.Namespace == "" { |
| 75 | + return fmt.Errorf("both Spec.VolumeSnapshotRef.Name = %s and Spec.VolumeSnapshotRef.Namespace = %s must be set", vsref.Name, vsref.Namespace) |
| 76 | + } |
| 77 | + |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +// ValidateV1Beta1SnapshotContent performs additional strict validation. |
| 82 | +// Do NOT rely on this function to fully validate snapshot content objects. |
| 83 | +// This function will only check the additional rules provided by the webhook. |
| 84 | +func ValidateV1Beta1SnapshotContent(snapcontent *crdv1beta1.VolumeSnapshotContent) error { |
| 85 | + if snapcontent == nil { |
| 86 | + return fmt.Errorf("VolumeSnapshotContent is nil") |
| 87 | + } |
| 88 | + |
| 89 | + source := snapcontent.Spec.Source |
| 90 | + |
| 91 | + if source.VolumeHandle != nil && source.SnapshotHandle != nil { |
| 92 | + return fmt.Errorf("only one of Spec.Source.VolumeHandle = %s and Spec.Source.SnapshotHandle = %s should be set", *source.VolumeHandle, *source.SnapshotHandle) |
| 93 | + } |
| 94 | + if source.VolumeHandle == nil && source.SnapshotHandle == nil { |
| 95 | + return fmt.Errorf("one of Spec.Source.VolumeHandle and Spec.Source.SnapshotHandle should be set") |
| 96 | + } |
| 97 | + |
| 98 | + vsref := snapcontent.Spec.VolumeSnapshotRef |
| 99 | + |
| 100 | + if vsref.Name == "" || vsref.Namespace == "" { |
| 101 | + return fmt.Errorf("both Spec.VolumeSnapshotRef.Name = %s and Spec.VolumeSnapshotRef.Namespace = %s must be set", vsref.Name, vsref.Namespace) |
| 102 | + } |
| 103 | + |
| 104 | + return nil |
| 105 | +} |
0 commit comments