generated from kubernetes/kubernetes-template-project
-
Notifications
You must be signed in to change notification settings - Fork 959
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate fields mount name and mount path in Dataset
Signed-off-by: ZhangXiaozheng <[email protected]>
- Loading branch information
Showing
7 changed files
with
437 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
Copyright 2024 The Fluid Author. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package validation | ||
|
||
import ( | ||
"strings" | ||
|
||
"github.com/fluid-cloudnative/fluid/api/v1alpha1" | ||
"k8s.io/apimachinery/pkg/util/validation" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
) | ||
|
||
func IsValidDataset(dataset v1alpha1.Dataset, enableMountValidation bool) error { | ||
if errs := validation.IsDNS1035Label(dataset.ObjectMeta.Name); len(dataset.ObjectMeta.Name) > 0 && len(errs) > 0 { | ||
return field.Invalid(field.NewPath("metadata").Child("name"), dataset.ObjectMeta.Name, strings.Join(errs, ",")) | ||
} | ||
|
||
// 0.1 Validate the mount name and mount path | ||
// Users can set the environment variable to 'false' to disable this validation | ||
// Default is true | ||
if !enableMountValidation { | ||
return nil | ||
} | ||
for _, mount := range dataset.Spec.Mounts { | ||
// The field mount.Name and mount.Path is optional | ||
// Empty name or path is allowed | ||
if len(mount.Name) != 0 { | ||
// If users set the mount.Name, it should comply with the DNS1035 rule. | ||
if errs := validation.IsDNS1035Label(mount.Name); len(errs) > 0 { | ||
return field.Invalid(field.NewPath("spec").Child("mounts").Child("name"), mount.Name, strings.Join(errs, ",")) | ||
} | ||
} | ||
if len(mount.Path) != 0 { | ||
// If users set the mount.Path, check it. | ||
if err := IsValidMountPath(mount.Path); err != nil { | ||
return field.Invalid(field.NewPath("spec").Child("mounts").Child("path"), mount.Path, err.Error()) | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
/* | ||
Copyright 2024 The Fluid Author. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package validation | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/fluid-cloudnative/fluid/api/v1alpha1" | ||
) | ||
|
||
const mountPoint1 string = "https://mirrors.bit.edu.cn/apache/spark/" | ||
const mountName1 string = "spark" | ||
|
||
const mountPoint2 string = "https://mirrors.bit.edu.cn/apache/flink/" | ||
const mountName2 string = "flink" | ||
|
||
const validMountPath1 string = "/test" | ||
const validMountPath2 string = "mnt/test" | ||
|
||
func TestIsValidDatasetWithValidDataset(t *testing.T) { | ||
type testCase struct { | ||
name string | ||
input v1alpha1.Dataset | ||
enableMountValidation bool | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
name: "validDatasetWithSingleMount", | ||
enableMountValidation: true, | ||
input: v1alpha1.Dataset{ | ||
Spec: v1alpha1.DatasetSpec{ | ||
Mounts: []v1alpha1.Mount{ | ||
{ | ||
MountPoint: mountPoint1, | ||
Name: mountName1, | ||
Path: validMountPath1, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "validDatasetWithMultiMount", | ||
enableMountValidation: true, | ||
input: v1alpha1.Dataset{ | ||
Spec: v1alpha1.DatasetSpec{ | ||
Mounts: []v1alpha1.Mount{ | ||
{ | ||
MountPoint: mountPoint1, | ||
Name: mountName1, | ||
}, | ||
{ | ||
MountPoint: mountPoint2, | ||
Name: mountName2, | ||
Path: validMountPath2, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "validDatasetWithDisableMountValidation", | ||
enableMountValidation: false, | ||
input: v1alpha1.Dataset{ | ||
Spec: v1alpha1.DatasetSpec{ | ||
Mounts: []v1alpha1.Mount{ | ||
{ | ||
MountPoint: mountPoint1, | ||
Path: "/${TEST}", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range testCases { | ||
got := IsValidDataset(test.input, test.enableMountValidation) | ||
if got != nil { | ||
t.Errorf("testcase %s failed, expect no error happened, but got an error: %s", test.name, got.Error()) | ||
} | ||
} | ||
} | ||
|
||
func TestIsValidDatasetWithInvalidDataset(t *testing.T) { | ||
type testCase struct { | ||
name string | ||
input v1alpha1.Dataset | ||
} | ||
|
||
testCases := []testCase{ | ||
{ | ||
name: "invalidDatasetMountName", | ||
input: v1alpha1.Dataset{ | ||
Spec: v1alpha1.DatasetSpec{ | ||
Mounts: []v1alpha1.Mount{ | ||
{ | ||
MountPoint: mountPoint1, | ||
Name: "$(cat /etc/passwd > /test.txt)", | ||
Path: validMountPath1, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "invalidDatasetMountPath", | ||
input: v1alpha1.Dataset{ | ||
Spec: v1alpha1.DatasetSpec{ | ||
Mounts: []v1alpha1.Mount{ | ||
{ | ||
MountPoint: mountPoint1, | ||
Name: mountName1, | ||
Path: "/$(cat /etc/passwd > /test.txt)", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "invalidDatasetMountPathInSecondMount", | ||
input: v1alpha1.Dataset{ | ||
Spec: v1alpha1.DatasetSpec{ | ||
Mounts: []v1alpha1.Mount{ | ||
{ | ||
MountPoint: mountPoint1, | ||
Name: mountName1, | ||
}, | ||
{ | ||
MountPoint: mountPoint2, | ||
Name: mountName2, | ||
Path: "/test/$(cat /etc/passwd > /test.txt)", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range testCases { | ||
got := IsValidDataset(test.input, true) | ||
if got == nil { | ||
t.Errorf("testcase %s failed, expect an error happened, but got no error", test.name) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.