mirror of
https://github.com/agent-substrate/substrate.git
synced 2026-11-11 10:23:17 +08:00
Fixes #1507 Golden snapshots currently remain owned by the temporary golden actor, so another resume/suspend cycle or actor deletion can collect a snapshot still referenced by its template. The controller now copies the warmed snapshot into a published tag, deletes the golden actor, and records the tag reference on the template. Interrupted tag creation and cleanup remain retryable; template deletion cleans up both resources. `CreateActor` resolves an explicit `sourceTag` or the template's golden tag into the actor's initial snapshot. Actors created before the golden tag is ready retain their cold-boot behavior. The golden tag uses the template UID as its name in `ate-golden`. The proto replaces `golden_snapshot` with `golden_tag` at field 1, without backward compatibility. This PR is based directly on `main` and does not depend on #1521. Follow-up recommendation: move the create → resume → wait → suspend → tag → delete sequence into a golden-template workflow using the existing workflow conventions. The reconciler now coordinates multiple recoverable steps; it could retain scheduling and retries while delegating that sequence to the workflow. This refactor is outside this PR. - [x] Tests pass - [x] Appropriate changes to documentation are included in the PR Validation: full `env -u NO_COLOR make verify` passed after rebasing onto `main`. After the final proto field-number change, bindings were regenerated and the control API unit/functional tests plus proto-format and Go-format checks passed. --------- Signed-off-by: Eitan Yarmush <eitan.yarmush@solo.io>
511 lines
21 KiB
Go
511 lines
21 KiB
Go
// Copyright 2026 Google LLC
|
|
//
|
|
// 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 controlapi
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/distribution/reference"
|
|
|
|
"github.com/agent-substrate/substrate/cmd/ateapi/internal/store"
|
|
"github.com/agent-substrate/substrate/internal/resources"
|
|
"github.com/agent-substrate/substrate/internal/volumepath"
|
|
"github.com/agent-substrate/substrate/pkg/proto/ateapipb"
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
"google.golang.org/protobuf/proto"
|
|
"k8s.io/apimachinery/pkg/api/operation"
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
|
"k8s.io/apimachinery/pkg/util/sets"
|
|
"k8s.io/apimachinery/pkg/util/validation/field"
|
|
)
|
|
|
|
func (s *RPCService) CreateActorTemplate(ctx context.Context, req *ateapipb.CreateActorTemplateRequest) (*ateapipb.ActorTemplate, error) {
|
|
// First scrub any fields that users are not allowed to set, then fill the
|
|
// defaults so validation sees the final resource state.
|
|
in := req.GetActorTemplate()
|
|
if in != nil { // otherwise validation will flag it
|
|
scrubResourceMetadataForCreate(in.Metadata)
|
|
in.Status = nil
|
|
defaultActorTemplate(in)
|
|
}
|
|
|
|
// Validate the request, including the object within it.
|
|
if errs := validateCreateActorTemplateRequest(ctx, req); len(errs) > 0 {
|
|
return nil, toGRPCStatusError(errs)
|
|
}
|
|
|
|
// config_name is required; the declarative validation has already
|
|
// rejected an empty one.
|
|
if _, err := resolveTemplateSandboxConfig(s.sandboxConfigLister, in.GetSandboxConfig()); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
templateRef := resources.ActorTemplateRefFromActorTemplate(in)
|
|
|
|
stored, err := s.impl.CreateActorTemplate(ctx, in)
|
|
if err != nil {
|
|
if errors.Is(err, store.ErrAlreadyExists) {
|
|
return nil, status.Errorf(codes.AlreadyExists, "ActorTemplate %s already exists", templateRef)
|
|
}
|
|
if errors.Is(err, store.ErrFailedPrecondition) {
|
|
return nil, status.Error(codes.FailedPrecondition, err.Error())
|
|
}
|
|
return nil, fmt.Errorf("while recording actor template: %w", err)
|
|
}
|
|
|
|
return stored, nil
|
|
}
|
|
|
|
func (s *ServiceImpl) CreateActorTemplate(ctx context.Context, inTemplate *ateapipb.ActorTemplate) (*ateapipb.ActorTemplate, error) {
|
|
// Build the stored object: status is server-owned and starts empty.
|
|
outTemplate := proto.Clone(inTemplate).(*ateapipb.ActorTemplate)
|
|
outTemplate.Status = &ateapipb.ActorTemplateStatus{}
|
|
|
|
// Validate the final value before storing it.
|
|
if errs := validateActorTemplateUpdate(ctx, field.NewPath("actor_template"), outTemplate, inTemplate); len(errs) > 0 {
|
|
return nil, toGRPCInternalError(errs)
|
|
}
|
|
|
|
return s.store.CreateActorTemplate(ctx, outTemplate)
|
|
}
|
|
|
|
func validateCreateActorTemplateRequest(ctx context.Context, req *ateapipb.CreateActorTemplateRequest) field.ErrorList {
|
|
// Call the generated validation.
|
|
op := operation.Operation{Type: operation.Create}
|
|
return Validate_CreateActorTemplateRequest(ctx, op, nil, req, nil)
|
|
}
|
|
|
|
func validateActorTemplateUpdate(ctx context.Context, fldPath *field.Path, newVal, oldVal *ateapipb.ActorTemplate) field.ErrorList {
|
|
// Call the generated validation.
|
|
op := operation.Operation{Type: operation.Update}
|
|
return Validate_ActorTemplate(ctx, op, fldPath, newVal, oldVal)
|
|
}
|
|
|
|
func (s *RPCService) GetActorTemplate(ctx context.Context, req *ateapipb.GetActorTemplateRequest) (*ateapipb.ActorTemplate, error) {
|
|
if errs := validateGetActorTemplateRequest(ctx, req); len(errs) > 0 {
|
|
return nil, toGRPCStatusError(errs)
|
|
}
|
|
|
|
templateRef := resources.ActorTemplateRefFromObjectRef(req.GetActorTemplate())
|
|
template, err := s.impl.GetActorTemplate(ctx, templateRef)
|
|
if errors.Is(err, store.ErrNotFound) {
|
|
return nil, status.Errorf(codes.NotFound, "ActorTemplate %s not found", templateRef)
|
|
} else if err != nil {
|
|
return nil, fmt.Errorf("while getting actor template from DB: %w", err)
|
|
}
|
|
|
|
return template, nil
|
|
}
|
|
|
|
func (s *ServiceImpl) GetActorTemplate(ctx context.Context, templateRef resources.ActorTemplateRef) (*ateapipb.ActorTemplate, error) {
|
|
// TODO: implement this
|
|
return s.store.GetActorTemplate(ctx, templateRef)
|
|
}
|
|
|
|
func validateGetActorTemplateRequest(ctx context.Context, req *ateapipb.GetActorTemplateRequest) field.ErrorList {
|
|
// Call the generated validation.
|
|
op := operation.Operation{Type: operation.Create}
|
|
return Validate_GetActorTemplateRequest(ctx, op, nil, req, nil)
|
|
}
|
|
|
|
func (s *RPCService) ListActorTemplates(ctx context.Context, req *ateapipb.ListActorTemplatesRequest) (*ateapipb.ListActorTemplatesResponse, error) {
|
|
if errs := validateListActorTemplatesRequest(ctx, req); len(errs) > 0 {
|
|
return nil, toGRPCStatusError(errs)
|
|
}
|
|
|
|
page, err := s.impl.ListActorTemplates(ctx, req.GetAtespace(), store.ListOptions{PageSize: effectivePageSize(req.GetPageSize()), PageToken: req.GetPageToken()})
|
|
if err != nil {
|
|
return nil, mapListError(fmt.Errorf("while listing actor templates in db: %w", err))
|
|
}
|
|
return &ateapipb.ListActorTemplatesResponse{
|
|
ActorTemplates: page.Items,
|
|
NextPageToken: page.NextPageToken,
|
|
}, nil
|
|
}
|
|
|
|
func (s *ServiceImpl) ListActorTemplates(ctx context.Context, atespace string, opts store.ListOptions) (store.ListResponse[*ateapipb.ActorTemplate], error) {
|
|
// TODO: implement this
|
|
return s.store.ListActorTemplates(ctx, atespace, opts)
|
|
}
|
|
|
|
func validateListActorTemplatesRequest(ctx context.Context, req *ateapipb.ListActorTemplatesRequest) field.ErrorList {
|
|
// Call the generated validation.
|
|
op := operation.Operation{Type: operation.Create}
|
|
return Validate_ListActorTemplatesRequest(ctx, op, nil, req, nil)
|
|
}
|
|
|
|
func (s *RPCService) DeleteActorTemplate(ctx context.Context, req *ateapipb.DeleteActorTemplateRequest) (*ateapipb.ActorTemplate, error) {
|
|
if errs := validateDeleteActorTemplateRequest(ctx, req); len(errs) > 0 {
|
|
return nil, toGRPCStatusError(errs)
|
|
}
|
|
|
|
templateRef := resources.ActorTemplateRefFromObjectRef(req.GetActorTemplate())
|
|
// Serialize cleanup against golden actor/tag creation by the reconciler.
|
|
ctx, lease, err := acquireLease(ctx, s.impl, "lease:actortemplate:"+templateRef.Atespace+":"+templateRef.Name, "ActorTemplate "+templateRef.String())
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer lease.Close()
|
|
tmpl, err := s.impl.GetActorTemplate(ctx, templateRef)
|
|
if errors.Is(err, store.ErrNotFound) {
|
|
return nil, status.Errorf(codes.NotFound, "ActorTemplate %s not found", templateRef)
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
goldenRef := &ateapipb.ObjectRef{Atespace: resources.GoldenActorAtespace, Name: tmpl.GetMetadata().GetUid()}
|
|
if _, err := s.DeleteActor(ctx, &ateapipb.DeleteActorRequest{Actor: goldenRef, AnyState: true}); err != nil && status.Code(err) != codes.NotFound {
|
|
return nil, fmt.Errorf("while deleting golden actor: %w", err)
|
|
}
|
|
if _, err := s.DeleteTag(ctx, &ateapipb.DeleteTagRequest{Tag: goldenRef}); err != nil && status.Code(err) != codes.NotFound {
|
|
return nil, fmt.Errorf("while deleting golden tag: %w", err)
|
|
}
|
|
deleted, err := s.impl.DeleteActorTemplate(ctx, templateRef)
|
|
if err != nil {
|
|
if errors.Is(err, store.ErrNotFound) {
|
|
return nil, status.Errorf(codes.NotFound, "ActorTemplate %s not found", templateRef)
|
|
}
|
|
if errors.Is(err, store.ErrFailedPrecondition) {
|
|
return nil, status.Error(codes.FailedPrecondition, err.Error())
|
|
}
|
|
return nil, fmt.Errorf("while deleting actor template from DB: %w", err)
|
|
}
|
|
|
|
return deleted, nil
|
|
}
|
|
|
|
func (s *ServiceImpl) DeleteActorTemplate(ctx context.Context, templateRef resources.ActorTemplateRef) (*ateapipb.ActorTemplate, error) {
|
|
// TODO: implement this
|
|
return s.store.DeleteActorTemplate(ctx, templateRef)
|
|
}
|
|
|
|
func validateDeleteActorTemplateRequest(ctx context.Context, req *ateapipb.DeleteActorTemplateRequest) field.ErrorList {
|
|
// Call the generated validation.
|
|
op := operation.Operation{Type: operation.Create}
|
|
return Validate_DeleteActorTemplateRequest(ctx, op, nil, req, nil)
|
|
}
|
|
|
|
func (s *ServiceImpl) UpdateActorTemplate(ctx context.Context, templateRef resources.ActorTemplateRef, precondition store.Precondition, mutate func(dbTemplate *ateapipb.ActorTemplate) error) (*ateapipb.ActorTemplate, error) {
|
|
// ActorTemplates are immutable to clients: there is no update RPC, and
|
|
// the only writer is the template reconciler, which updates status
|
|
// against the store directly. The store enforces metadata immutability,
|
|
// so this layer has nothing to add.
|
|
return s.store.UpdateActorTemplate(ctx, templateRef, precondition, mutate)
|
|
}
|
|
|
|
// httpGetPathRE constrains readyz paths to RFC 3986 path-segment
|
|
// characters only, with well-formed percent-escapes, and no query string
|
|
// or fragment.
|
|
var httpGetPathRE = regexp.MustCompile(`^/([A-Za-z0-9\-._~!$&'()*+,;=:@/]|%[0-9A-Fa-f]{2})*$`)
|
|
|
|
func ValidateCustom_HTTPGetAction_Path(_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *string) field.ErrorList {
|
|
if !httpGetPathRE.MatchString(*value) {
|
|
return field.ErrorList{field.Invalid(fldPath, *value, "must be a URL path starting with '/', using only RFC 3986 path-segment characters, without query or fragment")}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// mountPathBadSegmentRE matches '.' or '..' path segments.
|
|
var mountPathBadSegmentRE = regexp.MustCompile(`(^|/)[.][.]?(/|$)`)
|
|
|
|
// ValidateCustom_VolumeMount_MountPath requires a clean absolute Unix path
|
|
// that starts with '/', is not '/', and contains no ':', '.' or '..'
|
|
// segments, '//', trailing '/', or control characters.
|
|
func ValidateCustom_VolumeMount_MountPath(_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *string) field.ErrorList {
|
|
p := *value
|
|
bad := !strings.HasPrefix(p, "/") || len(p) == 1 ||
|
|
strings.HasSuffix(p, "/") || strings.Contains(p, "//") ||
|
|
strings.Contains(p, ":") || mountPathBadSegmentRE.MatchString(p)
|
|
if !bad {
|
|
for _, r := range p {
|
|
if r < 0x20 || r == 0x7f {
|
|
bad = true
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if bad {
|
|
return field.ErrorList{field.Invalid(fldPath, p, "must be a clean absolute Unix path: must start with '/', not be '/', and contain no ':', '..', '.', '//', trailing '/', or control characters")}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// validateProjectedPath applies the projected-path rule shared with atelet,
|
|
// which re-checks it before writing to the host.
|
|
func validateProjectedPath(fldPath *field.Path, p string) field.ErrorList {
|
|
if err := volumepath.ValidateProjected(p); err != nil {
|
|
return field.ErrorList{field.Invalid(fldPath, p, err.Error())}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ValidateCustom_ActorMetadataItem_Path(_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *string) field.ErrorList {
|
|
return validateProjectedPath(fldPath, *value)
|
|
}
|
|
|
|
func ValidateCustom_TrustBundleDataSource_Path(_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *string) field.ErrorList {
|
|
return validateProjectedPath(fldPath, *value)
|
|
}
|
|
|
|
// ValidateCustom_SystemInfoVolumeSource_DataSources requires every projected
|
|
// file path to be unique across all data sources: atelet writes them in
|
|
// order into one tree, so a repeated path silently clobbers the earlier file.
|
|
func ValidateCustom_SystemInfoVolumeSource_DataSources(_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ []*ateapipb.SystemInfoDataSource) field.ErrorList {
|
|
var errs field.ErrorList
|
|
seen := sets.New[string]()
|
|
for i, ds := range value {
|
|
switch {
|
|
case ds == nil:
|
|
case ds.TrustBundle != nil:
|
|
if seen.Has(ds.TrustBundle.Path) {
|
|
errs = append(errs, field.Duplicate(fldPath.Index(i).Child("trust_bundle", "path"), ds.TrustBundle.Path))
|
|
}
|
|
seen.Insert(ds.TrustBundle.Path)
|
|
case ds.ActorMetadata != nil:
|
|
for j, item := range ds.ActorMetadata.Items {
|
|
if item == nil {
|
|
continue
|
|
}
|
|
if seen.Has(item.Path) {
|
|
errs = append(errs, field.Duplicate(fldPath.Index(i).Child("actor_metadata", "items").Index(j).Child("path"), item.Path))
|
|
}
|
|
seen.Insert(item.Path)
|
|
}
|
|
}
|
|
}
|
|
return errs
|
|
}
|
|
|
|
// validatePinnedImage requires a well-formed OCI image reference pinned by
|
|
// digest (e.g. "name@sha256:..."): changing the image content under a fixed
|
|
// reference invalidates snapshots. It parses with the same grammar the
|
|
// container runtimes use, so a malformed digest is rejected rather than
|
|
// treated as pinned.
|
|
func validatePinnedImage(fldPath *field.Path, value string) field.ErrorList {
|
|
if value == "" {
|
|
return nil // required is enforced by tags
|
|
}
|
|
ref, err := reference.ParseNormalizedNamed(value)
|
|
if err != nil {
|
|
return field.ErrorList{field.Invalid(fldPath, value, fmt.Sprintf("must be a well-formed image reference: %v", err))}
|
|
}
|
|
if _, ok := ref.(reference.Digested); !ok {
|
|
return field.ErrorList{field.Invalid(fldPath, value, "must be pinned by digest (changing the image invalidates snapshots)")}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ValidateCustom_ImageVolumeSource_Reference(_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *string) field.ErrorList {
|
|
return validatePinnedImage(fldPath, *value)
|
|
}
|
|
|
|
func ValidateCustom_Container_Image(_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *string) field.ErrorList {
|
|
return validatePinnedImage(fldPath, *value)
|
|
}
|
|
|
|
func ValidateCustom_ExternalVolumeTemplate_Capacity(_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *string) field.ErrorList {
|
|
if _, err := resource.ParseQuantity(*value); err != nil {
|
|
return field.ErrorList{field.Invalid(fldPath, *value, fmt.Sprintf("must be a Kubernetes resource quantity: %v", err))}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// cpuLimitMax bounds cpu limits: they must be less than 1000 cores.
|
|
var cpuLimitMax = resource.MustParse("1k")
|
|
|
|
// ValidateCustom_Resources_Limits validates the resource limits: only cpu
|
|
// and memory limits are supported, each quantity must be greater than zero,
|
|
// and the cpu limit must be less than 1000 cores. Presence and uniqueness
|
|
// of names are enforced by tags.
|
|
func ValidateCustom_Resources_Limits(_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ []*ateapipb.Limits) field.ErrorList {
|
|
var errs field.ErrorList
|
|
for i, limit := range value {
|
|
if limit == nil {
|
|
continue
|
|
}
|
|
if limit.Name != "cpu" && limit.Name != "memory" {
|
|
errs = append(errs, field.NotSupported(fldPath.Index(i).Child("name"), limit.Name, []string{"cpu", "memory"}))
|
|
continue
|
|
}
|
|
if limit.Quantity == "" {
|
|
continue // required is enforced by tags
|
|
}
|
|
q, err := resource.ParseQuantity(limit.Quantity)
|
|
if err != nil {
|
|
errs = append(errs, field.Invalid(fldPath.Index(i).Child("quantity"), limit.Quantity, fmt.Sprintf("must be a Kubernetes resource quantity: %v", err)))
|
|
continue
|
|
}
|
|
if q.Sign() <= 0 {
|
|
errs = append(errs, field.Invalid(fldPath.Index(i).Child("quantity"), limit.Quantity, "must be greater than zero"))
|
|
}
|
|
if limit.Name == "cpu" && q.Cmp(cpuLimitMax) >= 0 {
|
|
errs = append(errs, field.Invalid(fldPath.Index(i).Child("quantity"), limit.Quantity, "cpu limit must be less than 1000 cores"))
|
|
}
|
|
}
|
|
return errs
|
|
}
|
|
|
|
// ValidateCustom_SnapshotsConfig_StorageLocation ensures an
|
|
// ActorTemplate's snapshotsConfig.location is a well-formed
|
|
// URI with a bucket, so a bad location fails fast.
|
|
func ValidateCustom_SnapshotsConfig_StorageLocation(_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *string) field.ErrorList {
|
|
if err := resources.ValidateSnapshotLocation(*value); err != nil {
|
|
return field.ErrorList{field.Invalid(fldPath, *value, err.Error())}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ValidateCustom_SnapshotsConfig requires on_commit to be a subset of on_pause.
|
|
func ValidateCustom_SnapshotsConfig(_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *ateapipb.SnapshotsConfig) field.ErrorList {
|
|
if value.GetOnPause() == ateapipb.SnapshotContentScope_SNAPSHOT_CONTENT_SCOPE_DATA &&
|
|
value.GetOnCommit() != ateapipb.SnapshotContentScope_SNAPSHOT_CONTENT_SCOPE_DATA {
|
|
return field.ErrorList{field.Invalid(fldPath.Child("on_commit"), value.GetOnCommit().String(), "must be a subset of on_pause")}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// envVarNameRE constrains env var names to any printable ASCII character
|
|
// except '='.
|
|
var envVarNameRE = regexp.MustCompile(`^[ -<>-~]+$`)
|
|
|
|
func ValidateCustom_EnvVar_Name(_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *string) field.ErrorList {
|
|
if !envVarNameRE.MatchString(*value) {
|
|
return field.ErrorList{field.Invalid(fldPath, *value, "may contain any printable ASCII character except '='")}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// capabilityRE constrains Linux capability names: uppercase, without the
|
|
// "CAP_" prefix (which is added when the OCI spec is written; the prefixed
|
|
// spelling would silently grant nothing).
|
|
var capabilityRE = regexp.MustCompile(`^[A-Z][A-Z0-9_]*$`)
|
|
|
|
func validateCapabilities(fldPath *field.Path, caps []string, allowAll bool) field.ErrorList {
|
|
var errs field.ErrorList
|
|
for i, c := range caps {
|
|
p := fldPath.Index(i)
|
|
switch {
|
|
case c == "ALL" && !allowAll:
|
|
errs = append(errs, field.Invalid(p, c, "add does not accept 'ALL'; name the individual capabilities the container needs"))
|
|
case c == "ALL":
|
|
case len(c) > 63:
|
|
errs = append(errs, field.TooLong(p, nil, 63))
|
|
case strings.HasPrefix(c, "CAP_"):
|
|
errs = append(errs, field.Invalid(p, c, "must be named without the 'CAP_' prefix (e.g. 'NET_BIND_SERVICE')"))
|
|
case !capabilityRE.MatchString(c):
|
|
errs = append(errs, field.Invalid(p, c, "must be an uppercase capability name like 'NET_BIND_SERVICE'"))
|
|
}
|
|
}
|
|
return errs
|
|
}
|
|
|
|
func ValidateCustom_Capabilities_Add(_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ []string) field.ErrorList {
|
|
return validateCapabilities(fldPath, value, false)
|
|
}
|
|
|
|
func ValidateCustom_Capabilities_Drop(_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ []string) field.ErrorList {
|
|
return validateCapabilities(fldPath, value, true)
|
|
}
|
|
|
|
// actorTemplateGetter is the storage subset template resolution needs.
|
|
type actorTemplateGetter interface {
|
|
GetActorTemplate(ctx context.Context, templateRef resources.ActorTemplateRef) (*ateapipb.ActorTemplate, error)
|
|
}
|
|
|
|
// errActorTemplateNotFound matches (via errors.Is) resolution failures where
|
|
// the actor names a template that does not exist. Most callers return the
|
|
// error as is — it already carries FailedPrecondition — while delete
|
|
// tolerates it and cleans up without the template.
|
|
var errActorTemplateNotFound = status.New(codes.FailedPrecondition, "actor template not found").Err()
|
|
|
|
// resolveActorTemplate resolves the substrate ActorTemplate the actor's
|
|
// actor_template ref names. A missing template surfaces as
|
|
// errActorTemplateNotFound.
|
|
func resolveActorTemplate(ctx context.Context, st actorTemplateGetter, actor *ateapipb.Actor) (*ateapipb.ActorTemplate, error) {
|
|
templateRef := resources.ActorTemplateRefFromObjectRef(actor.GetActorTemplate())
|
|
template, err := st.GetActorTemplate(ctx, templateRef)
|
|
if errors.Is(err, store.ErrNotFound) {
|
|
return nil, fmt.Errorf("%w; ObjectRef: %s ", errActorTemplateNotFound, templateRef)
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("while getting ActorTemplate: %w", err)
|
|
}
|
|
return template, nil
|
|
}
|
|
|
|
// actorTemplateObjectRef returns a fresh copy of the actor's template
|
|
// reference — fresh so records built from it never alias the actor message.
|
|
func actorTemplateObjectRef(actor *ateapipb.Actor) *ateapipb.ObjectRef {
|
|
ref := actor.GetActorTemplate()
|
|
if ref == nil {
|
|
return nil
|
|
}
|
|
return &ateapipb.ObjectRef{Atespace: ref.GetAtespace(), Name: ref.GetName()}
|
|
}
|
|
|
|
// ValidateCustom_Container_VolumeMounts rejects mounts that nest under one
|
|
// another.
|
|
func ValidateCustom_Container_VolumeMounts(_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ []*ateapipb.VolumeMount) field.ErrorList {
|
|
var errs field.ErrorList
|
|
for i, m := range value {
|
|
path := m.GetMountPath()
|
|
if path == "" {
|
|
continue // required is enforced by tags
|
|
}
|
|
// Nested mounts are unsupported (volumes cannot mount onto
|
|
// other volumes).
|
|
for j := 0; j < i; j++ {
|
|
prior := value[j].GetMountPath()
|
|
if prior == "" || prior == path {
|
|
continue
|
|
}
|
|
if strings.HasPrefix(path, prior+"/") || strings.HasPrefix(prior, path+"/") {
|
|
errs = append(errs, field.Invalid(fldPath.Index(i).Child("mount_path"), path,
|
|
fmt.Sprintf("must not nest under or over another mount (%q)", prior)))
|
|
}
|
|
}
|
|
}
|
|
return errs
|
|
}
|
|
|
|
// ValidateCustom_CreateActorTemplateRequest_ActorTemplate rejects container
|
|
// volume mounts that reference volumes the template does not declare.
|
|
func ValidateCustom_CreateActorTemplateRequest_ActorTemplate(_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *ateapipb.ActorTemplate) field.ErrorList {
|
|
declared := make(map[string]bool, len(value.GetVolumes()))
|
|
for _, vol := range value.GetVolumes() {
|
|
declared[vol.GetName()] = true
|
|
}
|
|
var errs field.ErrorList
|
|
for i, ctr := range value.GetContainers() {
|
|
for j, mount := range ctr.GetVolumeMounts() {
|
|
name := mount.GetName()
|
|
if name == "" {
|
|
continue // required is enforced by tags
|
|
}
|
|
if !declared[name] {
|
|
errs = append(errs, field.Invalid(
|
|
fldPath.Child("containers").Index(i).Child("volume_mounts").Index(j).Child("name"),
|
|
name, "must reference a volume declared in the template"))
|
|
}
|
|
}
|
|
}
|
|
return errs
|
|
}
|