// Package schema provides kind-specific validators for the unified // *jobspec.WorkloadSpec introduced in P0b (REQ-064). Each workload kind // (Job, Service, DaemonSet per R-012) has different required fields; // this package exposes a Validator interface and a ValidatorFor // dispatcher so the emitter layer (REQ-074) and the lint engine // (REQ-084) can reject invalid specs before rendering. // // The validators operate purely on the *WorkloadSpec shape; they do no // I/O. Required-field violations return a structured error listing // every problem found (missing required fields, invalid combinations). package schema import ( "errors" "fmt" "strings" "git.cloudinit.dev/coreci/orca/internal/jobspec" ) // Validator validates a *jobspec.WorkloadSpec against a kind-specific // schema. Implementations are pure (no I/O) and return a clear error // listing every violation found. type Validator interface { Validate(spec *jobspec.WorkloadSpec) error } // JobValidator validates the Job workload kind (R-012). // // Rules: // - no service block required (Job has no Traefik route by default D-175) // - restart optional (defaults to never/on-failure when omitted) // - schedule optional (cron string) // - timeout optional // - ports optional // - count must be 1 (or unset → 1); count > 1 is an error for Job // (use a Service for replicas) // - no Traefik route (a ServiceBlock is rejected) type JobValidator struct{} // ServiceValidator validates the Service workload kind (R-012). // // Rules: // - ports required (at least one) // - count ≥ 1 // - restart required (mode must be service) // - update required // - runtime required // - service block implied (Traefik route YES) type ServiceValidator struct{} // DaemonSetValidator validates the DaemonSet workload kind (R-012). // // Rules: // - schedule block with mode (every-node/matching/mandatory) required // - no ports (no Traefik route by default D-175) // - no count (implicit = nodes matching condition) // - restart required type DaemonSetValidator struct{} // ValidatorFor returns the Validator for the given workload kind, or an // error for an unknown kind. kind must be one of Job, Service, // DaemonSet (R-012). func ValidatorFor(kind string) (Validator, error) { switch kind { case "Job": return JobValidator{}, nil case "Service": return ServiceValidator{}, nil case "DaemonSet": return DaemonSetValidator{}, nil default: return nil, fmt.Errorf("schema: unknown kind %q (want one of Job, Service, DaemonSet)", kind) } } // Validate validates a Job spec. See JobValidator for the rules. func (JobValidator) Validate(spec *jobspec.WorkloadSpec) error { if spec == nil { return errors.New("schema/Job: spec is nil") } var errs []string if strings.TrimSpace(spec.Name) == "" { errs = append(errs, "name is required") } if spec.Count != 0 && spec.Count != 1 { errs = append(errs, fmt.Sprintf("count must be 1 (or unset) for Job, got %d (use Service for replicas)", spec.Count)) } if spec.Service != nil { errs = append(errs, "service block (Traefik route) is not allowed for Job (D-175)") } return composeErrors("schema/Job", errs) } // Validate validates a Service spec. See ServiceValidator for the rules. func (ServiceValidator) Validate(spec *jobspec.WorkloadSpec) error { if spec == nil { return errors.New("schema/Service: spec is nil") } var errs []string if strings.TrimSpace(spec.Name) == "" { errs = append(errs, "name is required") } if len(spec.Ports) == 0 { errs = append(errs, "ports required (at least one)") } if spec.Count < 1 { errs = append(errs, fmt.Sprintf("count must be ≥ 1 for Service, got %d", spec.Count)) } if spec.Restart == nil { errs = append(errs, "restart block required for Service") } else if spec.Restart.Mode != "service" { errs = append(errs, fmt.Sprintf("restart mode must be %q for Service, got %q", "service", spec.Restart.Mode)) } if spec.Update == nil { errs = append(errs, "update block required for Service") } if spec.Runtime == nil { errs = append(errs, "runtime block required for Service") } return composeErrors("schema/Service", errs) } // Validate validates a DaemonSet spec. See DaemonSetValidator for the rules. func (DaemonSetValidator) Validate(spec *jobspec.WorkloadSpec) error { if spec == nil { return errors.New("schema/DaemonSet: spec is nil") } var errs []string if strings.TrimSpace(spec.Name) == "" { errs = append(errs, "name is required") } if spec.Schedule == nil { errs = append(errs, "schedule block required for DaemonSet") } else { switch spec.Schedule.Mode { case "every-node", "matching", "mandatory": case "": errs = append(errs, "schedule mode required for DaemonSet (one of every-node, matching, mandatory)") default: errs = append(errs, fmt.Sprintf("schedule mode %q invalid (want one of every-node, matching, mandatory)", spec.Schedule.Mode)) } } if len(spec.Ports) > 0 { errs = append(errs, "ports not allowed for DaemonSet (no Traefik route by default D-175)") } if spec.Count != 0 { errs = append(errs, fmt.Sprintf("count not allowed for DaemonSet (implicit = nodes matching condition), got %d", spec.Count)) } if spec.Restart == nil { errs = append(errs, "restart block required for DaemonSet") } return composeErrors("schema/DaemonSet", errs) } // composeErrors joins the per-field errors into a single error prefixed // by the validator name. Returns nil when there are no errors so the // caller can return the result directly. func composeErrors(name string, errs []string) error { if len(errs) == 0 { return nil } return fmt.Errorf("%s: %s", name, strings.Join(errs, "; ")) }