|
1 | 1 | package validating |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | | - "encoding/json" |
6 | | - "net/http" |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
7 | 7 |
|
8 | | - "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 8 | + v1alpha1 "github.com/fluid-cloudnative/fluid/api/v1alpha1" |
| 9 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
9 | 10 | ) |
10 | 11 |
|
11 | 12 | // ValidatingHandler implements admission webhook for validating Fluid CRDs. |
12 | 13 | type ValidatingHandler struct { |
13 | | - decoder *admission.Decoder |
| 14 | + decoder *admission.Decoder |
14 | 15 | } |
15 | 16 |
|
16 | 17 | func NewValidatingHandler() *ValidatingHandler { |
17 | | - return &ValidatingHandler{} |
| 18 | + return &ValidatingHandler{} |
18 | 19 | } |
19 | 20 |
|
20 | 21 | func (h *ValidatingHandler) Handle(ctx context.Context, req admission.Request) admission.Response { |
21 | | - // Generic validation: ensure object contains metadata.name |
22 | | - var obj map[string]interface{} |
23 | | - if err := json.Unmarshal(req.Object.Raw, &obj); err != nil { |
24 | | - return admission.Errored(http.StatusBadRequest, err) |
25 | | - } |
26 | | - |
27 | | - metadata, ok := obj["metadata"].(map[string]interface{}) |
28 | | - if !ok { |
29 | | - return admission.Denied("metadata.name is required") |
30 | | - } |
31 | | - name, ok := metadata["name"].(string) |
32 | | - if !ok || name == "" { |
33 | | - return admission.Denied("metadata.name is required") |
34 | | - } |
35 | | - |
36 | | - // Passed basic validation |
37 | | - return admission.Allowed("validation passed") |
| 22 | + // Try to decode into a known type (Dataset) when possible |
| 23 | + var ds v1alpha1.Dataset |
| 24 | + if h.decoder != nil { |
| 25 | + if err := h.decoder.Decode(req, &ds); err == nil { |
| 26 | + // Perform Dataset-specific validations |
| 27 | + // Require either Mounts or Runtimes to be present |
| 28 | + if len(ds.Spec.Mounts) == 0 && len(ds.Spec.Runtimes) == 0 { |
| 29 | + return admission.Denied("dataset.spec must contain at least one mount or runtime") |
| 30 | + } |
| 31 | + |
| 32 | + // Validate mounts |
| 33 | + for _, m := range ds.Spec.Mounts { |
| 34 | + if m.MountPoint == "" { |
| 35 | + return admission.Denied("mount.mountPoint must not be empty") |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + // Validate runtimes |
| 40 | + for _, r := range ds.Spec.Runtimes { |
| 41 | + if r.Name == "" || r.Namespace == "" { |
| 42 | + return admission.Denied("runtime entries must include name and namespace") |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + return admission.Allowed("dataset validation passed") |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + // Fallback generic validation: ensure object contains metadata.name |
| 51 | + var obj map[string]interface{} |
| 52 | + if err := json.Unmarshal(req.Object.Raw, &obj); err != nil { |
| 53 | + return admission.Errored(http.StatusBadRequest, err) |
| 54 | + } |
| 55 | + |
| 56 | + metadata, ok := obj["metadata"].(map[string]interface{}) |
| 57 | + if !ok { |
| 58 | + return admission.Denied("metadata.name is required") |
| 59 | + } |
| 60 | + name, ok := metadata["name"].(string) |
| 61 | + if !ok || name == "" { |
| 62 | + return admission.Denied("metadata.name is required") |
| 63 | + } |
| 64 | + |
| 65 | + return admission.Allowed("validation passed") |
38 | 66 | } |
39 | 67 |
|
40 | 68 | func (h *ValidatingHandler) InjectDecoder(d *admission.Decoder) error { |
41 | | - h.decoder = d |
42 | | - return nil |
| 69 | + h.decoder = d |
| 70 | + return nil |
43 | 71 | } |
0 commit comments