Skip to content

Commit 1005327

Browse files
authored
Put Worker.actor fields into an "Assignment" message (agent-substrate#363)
This is a cleanup I noticed as I was reviewing other PRs. I tried to keep the commits clean, so you can see them one by one.
1 parent 4aedeab commit 1005327

25 files changed

Lines changed: 1425 additions & 374 deletions

File tree

.golangci.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,10 @@ linters:
5353
- (k8s.io/client-go/tools/cache.SharedIndexInformer).AddIndexers
5454

5555
misspell:
56-
mode: default
56+
locale: US
57+
mode: restricted # comments only
58+
ignore-rules:
59+
- cancelled
5760

5861
exclusions:
5962
# Built-in presets that suppress the most common idiomatic false

cmd/ateapi/internal/controlapi/create_actor.go

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ import (
1818
"context"
1919
"errors"
2020
"fmt"
21-
"strings"
2221

2322
"github.com/agent-substrate/substrate/cmd/ateapi/internal/store"
2423
"github.com/agent-substrate/substrate/internal/resources"
2524
"github.com/agent-substrate/substrate/pkg/proto/ateapipb"
2625
"google.golang.org/grpc/codes"
2726
"google.golang.org/grpc/status"
2827
k8serrors "k8s.io/apimachinery/pkg/api/errors"
29-
"k8s.io/apimachinery/pkg/util/validation"
28+
"k8s.io/apimachinery/pkg/api/validate/content"
29+
"k8s.io/apimachinery/pkg/util/validation/field"
3030
)
3131

3232
func (s *Service) CreateActor(ctx context.Context, req *ateapipb.CreateActorRequest) (*ateapipb.CreateActorResponse, error) {
@@ -79,42 +79,59 @@ func (s *Service) CreateActor(ctx context.Context, req *ateapipb.CreateActorRequ
7979
}
8080

8181
func validateCreateActorRequest(req *ateapipb.CreateActorRequest) error {
82-
if req.GetActorTemplateNamespace() == "" {
83-
return status.Error(codes.InvalidArgument, "actor_template_namespace is required")
84-
}
85-
if req.GetActorTemplateName() == "" {
86-
return status.Error(codes.InvalidArgument, "actor_template_name is required")
87-
}
88-
if req.GetActorRef().GetName() == "" {
89-
return status.Error(codes.InvalidArgument, "actor_id is required")
82+
var fldPath *field.Path
83+
var errs field.ErrorList
84+
85+
if val, fldPath := req.ActorTemplateNamespace, fldPath.Child("actor_template_namespace"); val == "" {
86+
errs = append(errs, field.Required(fldPath, ""))
87+
} else {
88+
for _, msg := range content.IsDNS1123Label(val) {
89+
errs = append(errs, field.Invalid(fldPath, val, msg))
90+
}
9091
}
91-
if err := resources.ValidateActorID(req.GetActorRef().GetName()); err != nil {
92-
return status.Error(codes.InvalidArgument, err.Error())
92+
93+
if val, fldPath := req.ActorTemplateName, fldPath.Child("actor_template_name"); val == "" {
94+
errs = append(errs, field.Required(fldPath, ""))
95+
} else {
96+
for _, msg := range content.IsDNS1123Subdomain(val) {
97+
errs = append(errs, field.Invalid(fldPath, val, msg))
98+
}
9399
}
94-
if req.GetActorRef().GetAtespace() == "" {
95-
return status.Error(codes.InvalidArgument, "atespace is required")
100+
101+
if val, fldPath := req.ActorRef, fldPath.Child("actor_ref"); val == nil {
102+
errs = append(errs, field.Required(fldPath, ""))
103+
} else {
104+
errs = append(errs, resources.ValidateActorRef(val, fldPath)...)
96105
}
97-
if err := resources.ValidateAtespace(req.GetActorRef().GetAtespace()); err != nil {
98-
return status.Error(codes.InvalidArgument, err.Error())
106+
107+
if val := req.WorkerSelector; val != nil {
108+
errs = append(errs, validateSelector(val, fldPath.Child("worker_selector"))...)
99109
}
100-
if err := validateSelector(req.GetWorkerSelector()); err != nil {
101-
return status.Error(codes.InvalidArgument, err.Error())
110+
111+
if len(errs) > 0 {
112+
return status.Error(codes.InvalidArgument, errs.ToAggregate().Error())
102113
}
103114
return nil
104115
}
105116

106-
func validateSelector(sel *ateapipb.Selector) error {
107-
const maxSelectorMatchLabels = 10
108-
if n := len(sel.GetMatchLabels()); n > maxSelectorMatchLabels {
109-
return fmt.Errorf("worker_selector has %d match_labels entries, exceeding the limit of %d", n, maxSelectorMatchLabels)
110-
}
111-
for k, v := range sel.GetMatchLabels() {
112-
if errs := validation.IsQualifiedName(k); len(errs) > 0 {
113-
return fmt.Errorf("invalid worker_selector label key %q: %s", k, strings.Join(errs, "; "))
117+
func validateSelector(sel *ateapipb.Selector, fldPath *field.Path) field.ErrorList {
118+
var errs field.ErrorList
119+
120+
if sel.MatchLabels != nil {
121+
const maxSelectorMatchLabels = 10
122+
if n := len(sel.MatchLabels); n > maxSelectorMatchLabels {
123+
return field.ErrorList{field.TooMany(fldPath.Child("match_labels"), n, maxSelectorMatchLabels)}
114124
}
115-
if errs := validation.IsValidLabelValue(v); len(errs) > 0 {
116-
return fmt.Errorf("invalid worker_selector label value %q for key %q: %s", v, k, strings.Join(errs, "; "))
125+
126+
for k, v := range sel.MatchLabels {
127+
for _, msg := range content.IsLabelKey(k) {
128+
errs = append(errs, field.Invalid(fldPath.Child("match_labels").Key(k), k, msg))
129+
}
130+
for _, msg := range content.IsLabelValue(v) {
131+
errs = append(errs, field.Invalid(fldPath.Child("match_labels").Key(k), v, msg))
132+
}
117133
}
118134
}
119-
return nil
135+
136+
return errs
120137
}

cmd/ateapi/internal/controlapi/delete_actor.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"github.com/agent-substrate/substrate/pkg/proto/ateapipb"
2525
"google.golang.org/grpc/codes"
2626
"google.golang.org/grpc/status"
27+
"k8s.io/apimachinery/pkg/util/validation/field"
2728
)
2829

2930
func (s *Service) DeleteActor(ctx context.Context, req *ateapipb.DeleteActorRequest) (*ateapipb.DeleteActorResponse, error) {
@@ -52,14 +53,17 @@ func (s *Service) DeleteActor(ctx context.Context, req *ateapipb.DeleteActorRequ
5253
}
5354

5455
func validateDeleteActorRequest(req *ateapipb.DeleteActorRequest) error {
55-
if req.GetActorRef().GetName() == "" {
56-
return status.Error(codes.InvalidArgument, "actor_id is required")
57-
}
58-
if err := resources.ValidateActorID(req.GetActorRef().GetName()); err != nil {
59-
return status.Error(codes.InvalidArgument, err.Error())
56+
var fldPath *field.Path
57+
var errs field.ErrorList
58+
59+
if val, fldPath := req.ActorRef, fldPath.Child("actor_ref"); val == nil {
60+
errs = append(errs, field.Required(fldPath, ""))
61+
} else {
62+
errs = append(errs, resources.ValidateActorRef(val, fldPath)...)
6063
}
61-
if req.GetActorRef().GetAtespace() == "" {
62-
return status.Error(codes.InvalidArgument, "atespace is required")
64+
65+
if len(errs) > 0 {
66+
return status.Error(codes.InvalidArgument, errs.ToAggregate().Error())
6367
}
6468
return nil
6569
}

0 commit comments

Comments
 (0)