Skip to content

Commit 1b67782

Browse files
committed
Added validations to reject invalid spec
Signed-off-by: VershaAgrawal <[email protected]>
1 parent a351ab6 commit 1b67782

File tree

3 files changed

+136
-0
lines changed

3 files changed

+136
-0
lines changed

pkg/admission/test/unit/admission_unit_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,31 @@ var _ = Describe("BackingStore admission unit tests", func() {
3333

3434
Describe("Validate create operations", func() {
3535
Describe("General backingstore validations", func() {
36+
Context("Invalid spec for declared type", func() {
37+
It("Should Deny", func() {
38+
bs.Spec = nbv1.BackingStoreSpec{
39+
Type: nbv1.StoreTypeAWSS3,
40+
}
41+
err = validations.ValidateBSInValidSpec(*bs)
42+
Ω(err).Should(HaveOccurred())
43+
Expect(err.Error()).To(Equal("AWSS3 spec must be provided for aws-s3 type BackingStore"))
44+
})
45+
It("Should Allow", func() {
46+
bs.Spec = nbv1.BackingStoreSpec{
47+
Type: nbv1.StoreTypeAWSS3,
48+
AWSS3: &nbv1.AWSS3Spec{
49+
TargetBucket: "some-target-bucket",
50+
Secret: corev1.SecretReference{
51+
Name: "secret-name",
52+
Namespace: "test",
53+
},
54+
},
55+
}
56+
err = validations.ValidateBSInValidSpec(*bs)
57+
Ω(err).ShouldNot(HaveOccurred())
58+
})
59+
})
60+
3661
Context("Empty secret name", func() {
3762
It("Should Deny", func() {
3863
bs.Spec = nbv1.BackingStoreSpec{
@@ -384,6 +409,31 @@ var _ = Describe("NamespaceStore admission unit tests", func() {
384409

385410
Describe("Validate create operations", func() {
386411
Describe("General namespacestore validations", func() {
412+
Context("Invalid spec for declared type", func() {
413+
It("Should Deny", func() {
414+
ns.Spec = nbv1.NamespaceStoreSpec{
415+
Type: nbv1.NSStoreTypeAWSS3,
416+
}
417+
err = validations.ValidateNSInValidSpec(*ns)
418+
Ω(err).Should(HaveOccurred())
419+
Expect(err.Error()).To(Equal("AWSS3 spec must be provided for aws-s3 type Namespacestore"))
420+
})
421+
It("Should Allow", func() {
422+
ns.Spec = nbv1.NamespaceStoreSpec{
423+
Type: nbv1.NSStoreTypeAWSS3,
424+
AWSS3: &nbv1.AWSS3Spec{
425+
TargetBucket: "some-target-bucket",
426+
Secret: corev1.SecretReference{
427+
Name: "secret-name",
428+
Namespace: "test",
429+
},
430+
},
431+
}
432+
err = validations.ValidateNSInValidSpec(*ns)
433+
Ω(err).ShouldNot(HaveOccurred())
434+
})
435+
})
436+
387437
Context("Empty secret name", func() {
388438
It("Should Deny", func() {
389439
ns.Spec = nbv1.NamespaceStoreSpec{

pkg/validations/backingstore_validations.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ const (
1818

1919
// ValidateBackingStore validates create validations on resource Backinstore
2020
func ValidateBackingStore(bs nbv1.BackingStore) error {
21+
//Ensure that the Spec contains the expected sub-spec for the declared type
22+
if err := ValidateBSInValidSpec(bs); err != nil {
23+
return err
24+
}
25+
2126
if err := ValidateBSEmptySecretName(bs); err != nil {
2227
return err
2328
}
@@ -52,6 +57,41 @@ func ValidateBackingStore(bs nbv1.BackingStore) error {
5257
return nil
5358
}
5459

60+
// ValidateBSInValidSpec validates that the backingstore spec contains the expected sub-spec for the declared type
61+
func ValidateBSInValidSpec(bs nbv1.BackingStore) error {
62+
switch bs.Spec.Type {
63+
case nbv1.StoreTypeAWSS3:
64+
if bs.Spec.AWSS3 == nil {
65+
return util.ValidationError{Msg: "AWSS3 spec must be provided for aws-s3 type BackingStore"}
66+
}
67+
case nbv1.StoreTypeS3Compatible:
68+
if bs.Spec.S3Compatible == nil {
69+
return util.ValidationError{Msg: "S3Compatible spec must be provided for s3-compatible type BackingStore"}
70+
}
71+
case nbv1.StoreTypeIBMCos:
72+
if bs.Spec.IBMCos == nil {
73+
return util.ValidationError{Msg: "IBMCos spec must be provided for ibm-cos type BackingStore"}
74+
}
75+
case nbv1.StoreTypeAzureBlob:
76+
if bs.Spec.AzureBlob == nil {
77+
return util.ValidationError{Msg: "AzureBlob spec must be provided for azure-blob type BackingStore"}
78+
}
79+
case nbv1.StoreTypeGoogleCloudStorage:
80+
if bs.Spec.GoogleCloudStorage == nil {
81+
return util.ValidationError{Msg: "GoogleCloudStorage spec must be provided for google-cloud-storage type BackingStore"}
82+
}
83+
case nbv1.StoreTypePVPool:
84+
if bs.Spec.PVPool == nil {
85+
return util.ValidationError{Msg: "PVPool spec must be provided for pv-pool type BackingStore"}
86+
}
87+
default:
88+
return util.ValidationError{
89+
Msg: "Invalid Backingstore type, please provide a valid Backingstore type",
90+
}
91+
}
92+
return nil
93+
}
94+
5595
// ValidateBSEmptySecretName validates a secret name is provided for cloud backingstores
5696
func ValidateBSEmptySecretName(bs nbv1.BackingStore) error {
5797
switch bs.Spec.Type {

pkg/validations/namespacestore_validations.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ const (
1919

2020
// ValidateNamespaceStore validates namespacestore configuration
2121
func ValidateNamespaceStore(nsStore *nbv1.NamespaceStore) error {
22+
// ensure that the Spec contains the expected sub-spec for the declared type
23+
if err := ValidateNSInValidSpec(*nsStore); err != nil {
24+
return err
25+
}
26+
2227
if err := ValidateNSEmptySecretName(*nsStore); err != nil {
2328
return err
2429
}
@@ -52,6 +57,47 @@ func ValidateNamespaceStore(nsStore *nbv1.NamespaceStore) error {
5257
}
5358
}
5459

60+
// ValidateNSInValidSpec ensures that the namespacestore spec contains the expected sub-spec for the declared type
61+
func ValidateNSInValidSpec(nsStore nbv1.NamespaceStore) error {
62+
switch nsStore.Spec.Type {
63+
64+
case nbv1.NSStoreTypeNSFS:
65+
if nsStore.Spec.NSFS == nil {
66+
return util.ValidationError{Msg: "NSFS spec must be provided for nsfs type Namespacestore"}
67+
}
68+
69+
case nbv1.NSStoreTypeAWSS3:
70+
if nsStore.Spec.AWSS3 == nil {
71+
return util.ValidationError{Msg: "AWSS3 spec must be provided for aws-s3 type Namespacestore"}
72+
}
73+
74+
case nbv1.NSStoreTypeS3Compatible:
75+
if nsStore.Spec.S3Compatible == nil {
76+
return util.ValidationError{Msg: "S3Compatible spec must be provided for s3-compatible type Namespacestore"}
77+
}
78+
79+
case nbv1.NSStoreTypeIBMCos:
80+
if nsStore.Spec.IBMCos == nil {
81+
return util.ValidationError{Msg: "IBMCos spec must be provided for ibm-cos type Namespacestore"}
82+
}
83+
84+
case nbv1.NSStoreTypeAzureBlob:
85+
if nsStore.Spec.AzureBlob == nil {
86+
return util.ValidationError{Msg: "AzureBlob spec must be provided for azure-blob type Namespacestore"}
87+
}
88+
89+
case nbv1.NSStoreTypeGoogleCloudStorage:
90+
if nsStore.Spec.GoogleCloudStorage == nil {
91+
return util.ValidationError{Msg: "GoogleCloudStorage spec must be provided for google-cloud-storage type Namespacestore"}
92+
}
93+
default:
94+
return util.ValidationError{
95+
Msg: "Invalid Namespacestore type, please provide a valid Namespacestore type",
96+
}
97+
}
98+
return nil
99+
}
100+
55101
// ValidateNsStoreNSFS validates namespacestore nsfs type configuration
56102
func ValidateNsStoreNSFS(nsStore *nbv1.NamespaceStore) error {
57103
nsfs := nsStore.Spec.NSFS

0 commit comments

Comments
 (0)