The Web UI and messaging channels now read and write Archie's runtime settings through one versioned, audited control-plane API hosted on the State Store's existing gRPC server: catalog, query, command, watch. Each feature owns its resource, validation and commands; storage only writes versions and audit records. Workflows move with it. Definitions become portable YAML compiled through a closed, typed step registry and stored as a control-plane resource, so the yaegi interpretation paths (internal/gate, wfeval, wfextract, gateeval, gateextract, skillbuild) are deleted rather than carried. The UI process reads definitions as data: the JSON projection lives in internal/domain/workflow/task so a dashboard decodes the stored collection without linking the workflow engine, which cmd/archie-ui's deletion gate forbids. Shipped definitions ride the catalog descriptor rather than a bespoke REST route, keeping the API to its four operations. archie-messaging gains -state-store-target/-state-store-token and reads channel settings from the database, failing closed when the State Store is unreachable: the database is authoritative for those settings.
132 lines
4.5 KiB
Go
132 lines
4.5 KiB
Go
// Package controlplane owns control-plane resource registration, dispatch, and validation.
|
|
package controlplane
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"time"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
"google.golang.org/grpc/status"
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
|
|
"github.com/samcharles93/archie-core/internal/store"
|
|
)
|
|
|
|
var (
|
|
ErrValidation = errors.New("control-plane validation")
|
|
ErrNotFound = errors.New("control-plane resource not found")
|
|
ErrVersionConflict = errors.New("control-plane version conflict")
|
|
ErrUnavailable = errors.New("control-plane unavailable")
|
|
)
|
|
|
|
type resourceStore interface {
|
|
Resource(context.Context, string) (store.Resource, error)
|
|
PutResource(context.Context, store.ResourceWrite) (store.Resource, error)
|
|
}
|
|
|
|
type Server struct {
|
|
pb.UnimplementedControlPlaneServiceServer
|
|
store resourceStore
|
|
definitions map[string]Definition
|
|
ordered []Definition
|
|
}
|
|
|
|
func NewServer(resources resourceStore) *Server {
|
|
definitions := builtinDefinitions()
|
|
byKind := make(map[string]Definition, len(definitions))
|
|
for _, definition := range definitions {
|
|
byKind[definition.Kind] = definition
|
|
}
|
|
return &Server{store: resources, definitions: byKind, ordered: definitions}
|
|
}
|
|
|
|
func (s *Server) Catalog(context.Context, *pb.CatalogRequest) (*pb.CatalogResponse, error) {
|
|
descriptors := make([]*pb.ResourceDescriptor, 0, len(s.ordered)+len(domainManagedDescriptors()))
|
|
for _, definition := range s.ordered {
|
|
descriptors = append(descriptors, definition.Descriptor())
|
|
}
|
|
descriptors = append(descriptors, domainManagedDescriptors()...)
|
|
return &pb.CatalogResponse{Resources: descriptors}, nil
|
|
}
|
|
|
|
func (s *Server) Query(ctx context.Context, request *pb.QueryRequest) (*pb.QueryResponse, error) {
|
|
if _, ok := s.definitions[request.GetKind()]; !ok {
|
|
return nil, status.Error(codes.NotFound, "resource not found")
|
|
}
|
|
resource, err := s.store.Resource(ctx, request.Kind)
|
|
if err != nil {
|
|
return nil, mapError(err)
|
|
}
|
|
return &pb.QueryResponse{Resource: resourceProto(resource)}, nil
|
|
}
|
|
|
|
func (s *Server) Command(ctx context.Context, request *pb.CommandRequest) (*pb.CommandResponse, error) {
|
|
definition, ok := s.definitions[request.GetKind()]
|
|
if !ok || request.GetCommand() != "replace" {
|
|
return nil, status.Error(codes.NotFound, "resource or command not found")
|
|
}
|
|
if request.GetActor() == "" || request.GetSource() == "" || request.GetRequestId() == "" {
|
|
return nil, status.Error(codes.InvalidArgument, "actor, source, and request ID are required")
|
|
}
|
|
value, err := definition.Decode(request.ValueJson)
|
|
if err != nil {
|
|
return nil, mapError(err)
|
|
}
|
|
resource, err := s.store.PutResource(ctx, store.ResourceWrite{Kind: request.Kind, Value: value, Actor: request.Actor, Source: request.Source, RequestID: request.RequestId, ExpectedVersion: request.ExpectedVersion, At: time.Now().UTC()})
|
|
if err != nil {
|
|
return nil, mapError(err)
|
|
}
|
|
return &pb.CommandResponse{Resource: resourceProto(resource)}, nil
|
|
}
|
|
|
|
func (s *Server) Watch(request *pb.WatchRequest, stream pb.ControlPlaneService_WatchServer) error {
|
|
if _, ok := s.definitions[request.GetKind()]; !ok {
|
|
return status.Error(codes.NotFound, "resource not found")
|
|
}
|
|
version := request.AfterVersion
|
|
ticker := time.NewTicker(250 * time.Millisecond)
|
|
defer ticker.Stop()
|
|
for {
|
|
resource, err := s.store.Resource(stream.Context(), request.Kind)
|
|
if err == nil && resource.Version > version {
|
|
if err := stream.Send(&pb.WatchResponse{Resource: resourceProto(resource)}); err != nil {
|
|
return err
|
|
}
|
|
version = resource.Version
|
|
} else if err != nil && !errors.Is(err, store.ErrResourceNotFound) {
|
|
return mapError(err)
|
|
}
|
|
select {
|
|
case <-stream.Context().Done():
|
|
return stream.Context().Err()
|
|
case <-ticker.C:
|
|
}
|
|
}
|
|
}
|
|
|
|
func resourceProto(resource store.Resource) *pb.Resource {
|
|
return &pb.Resource{Kind: resource.Kind, Version: resource.Version, ValueJson: resource.Value, UpdatedAt: timestamp(resource.At)}
|
|
}
|
|
|
|
func timestamp(value time.Time) *timestamppb.Timestamp {
|
|
if value.IsZero() {
|
|
return nil
|
|
}
|
|
return timestamppb.New(value)
|
|
}
|
|
|
|
func mapError(err error) error {
|
|
switch {
|
|
case errors.Is(err, ErrValidation):
|
|
return status.Error(codes.InvalidArgument, err.Error())
|
|
case errors.Is(err, store.ErrResourceNotFound):
|
|
return status.Error(codes.NotFound, err.Error())
|
|
case errors.Is(err, store.ErrResourceVersionConflict):
|
|
return status.Error(codes.Aborted, err.Error())
|
|
default:
|
|
return status.Error(codes.Unavailable, err.Error())
|
|
}
|
|
}
|