Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
668 changes: 359 additions & 309 deletions api/ateapi/v1/ateapi.pb.go

Large diffs are not rendered by default.

32 changes: 29 additions & 3 deletions api/ateapi/v1/ateapi.proto
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,19 @@ message ResourceMetadata {
// +k8s:update=NoModify
// +k8s:update=NoUnset
google.protobuf.Timestamp delete_time = 7;

// `labels` identify the resource for its creator. They are immutable after
// creation. An update that changes or omits them returns `INVALID_ARGUMENT`.
// Keys in `ate.dev` and its subdomains are reserved. Keys and values together
// may not exceed 1024 bytes.
//
// +k8s:optional
// +k8s:maxProperties=16
// +k8s:eachKey=+k8s:format=k8s-label-key
// +k8s:eachVal=+k8s:format=k8s-label-value
// +k8s:customValidation # reserved keys and total size
// +k8s:immutable
map<string, string> labels = 8;
}

// ExternalVolumes is the external volume state recorded on an actor, wrapped
Expand Down Expand Up @@ -1214,6 +1227,19 @@ message ActorTemplate {

// +k8s:optional
ActorTemplateStatus status = 7;

// `actor_labels` are defaults for the `metadata.labels` of Actors created
// from this template. An Actor's own value wins on a key conflict. Changing
// an Actor's `actor_template` does not change its labels. These are separate
// from the template's own `metadata.labels`. Keys in `ate.dev` and its
// subdomains are reserved. Keys and values together may not exceed 1024 bytes.
//
// +k8s:optional
// +k8s:maxProperties=16
// +k8s:eachKey=+k8s:format=k8s-label-key
// +k8s:eachVal=+k8s:format=k8s-label-value
// +k8s:customValidation # reserved keys and total size
map<string, string> actor_labels = 8;
}

// ResourceRequirements are the compute resources a workload may use.
Expand Down Expand Up @@ -2044,9 +2070,9 @@ message ListTagsResponse {
//
// +k8s:customValidation # metadata.atespace must match source_actor.atespace
message CreateTagRequest {
// The tag to create. metadata.atespace, metadata.name, scope and source_actor
// are honored; metadata.atespace must match source_actor's, and status is
// ignored on write.
// The tag to create. `metadata.atespace`, `metadata.name`, `metadata.labels`,
// `scope` and `source_actor` are honored. `metadata.atespace` must match
// `source_actor`'s, and `status` is ignored on write.
//
// +k8s:required
Tag tag = 1;
Expand Down
4 changes: 4 additions & 0 deletions cmd/ateapi/internal/apivalidation/actor_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ func ValidateCustom_Container_VolumeMounts(_ context.Context, _ operation.Operat
return resources.ValidateNestedMountPaths(fldPath, paths)
}

func ValidateCustom_ActorTemplate_ActorLabels(_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ map[string]string) field.ErrorList {
return validateLabels(fldPath, value)
}

func ValidateCustom_ActorMetadataItem_Path(_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ *string) field.ErrorList {
return resources.ValidateProjectedPath(fldPath, *value)
}
Expand Down
36 changes: 36 additions & 0 deletions cmd/ateapi/internal/apivalidation/actor_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,42 @@ func TestValidateCreateActorTemplateRequest(t *testing.T) {
field.Required(field.NewPath("actor_template", "resources", "limits", "cpu_millicores"), ""),
field.Required(field.NewPath("actor_template", "resources", "limits", "memory_bytes"), ""),
},
}, {
"valid actor_labels",
&ateapi.CreateActorTemplateRequest{ActorTemplate: validActorTemplate(func(tmpl *ateapi.ActorTemplate) {
tmpl.ActorLabels = map[string]string{"example.com/team": "payments"}
})},
nil,
}, {
"actor_labels at both limits",
&ateapi.CreateActorTemplateRequest{ActorTemplate: validActorTemplate(func(template *ateapi.ActorTemplate) {
template.ActorLabels = labelsOfBytes(0)
})},
nil,
}, {
"invalid actor_labels key",
&ateapi.CreateActorTemplateRequest{ActorTemplate: validActorTemplate(func(tmpl *ateapi.ActorTemplate) {
tmpl.ActorLabels = map[string]string{"bad key!": "1"}
})},
field.ErrorList{field.Invalid(field.NewPath("actor_template", "actor_labels"), "bad key!", "").WithOrigin("format=k8s-label-key")},
}, {
"too many actor_labels",
&ateapi.CreateActorTemplateRequest{ActorTemplate: validActorTemplate(func(tmpl *ateapi.ActorTemplate) {
tmpl.ActorLabels = selectorLabelsOfSize(17)
})},
field.ErrorList{field.TooMany(field.NewPath("actor_template", "actor_labels"), 17, 16).WithOrigin("maxProperties")},
}, {
"reserved actor_labels key",
&ateapi.CreateActorTemplateRequest{ActorTemplate: validActorTemplate(func(tmpl *ateapi.ActorTemplate) {
tmpl.ActorLabels = map[string]string{"ate.dev/agent": "a"}
})},
field.ErrorList{field.Invalid(field.NewPath("actor_template", "actor_labels"), "ate.dev/agent", "")},
}, {
"actor_labels over the size cap",
&ateapi.CreateActorTemplateRequest{ActorTemplate: validActorTemplate(func(tmpl *ateapi.ActorTemplate) {
tmpl.ActorLabels = labelsOfBytes(1)
})},
field.ErrorList{field.TooLong(field.NewPath("actor_template", "actor_labels"), 1025, 1024)},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
59 changes: 59 additions & 0 deletions cmd/ateapi/internal/apivalidation/actor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,34 @@ func TestValidateCreateActorRequest(t *testing.T) {
"invalid actor.source_tag.name",
validReq(validActor(withSourceTag("as", "invalid value"))),
field.ErrorList{field.Invalid(field.NewPath("actor", "source_tag", "name"), nil, "").WithOrigin("format=k8s-short-name")},
}, {
"valid labels",
validReq(validActor(withActorLabels(map[string]string{"example.com/agent": "billing"}))),
nil,
}, {
"labels at both limits",
validReq(validActor(withActorLabels(labelsOfBytes(0)))),
nil,
}, {
"invalid label key",
validReq(validActor(withActorLabels(map[string]string{"bad key!": "1"}))),
field.ErrorList{field.Invalid(field.NewPath("actor", "metadata", "labels"), "bad key!", "").WithOrigin("format=k8s-label-key")},
}, {
"invalid label value",
validReq(validActor(withActorLabels(map[string]string{"app": "not valid!"}))),
field.ErrorList{field.Invalid(field.NewPath("actor", "metadata", "labels").Key("app"), "not valid!", "").WithOrigin("format=k8s-label-value")},
}, {
"too many labels",
validReq(validActor(withActorLabels(selectorLabelsOfSize(17)))),
field.ErrorList{field.TooMany(field.NewPath("actor", "metadata", "labels"), 17, 16).WithOrigin("maxProperties")},
}, {
"reserved label key",
validReq(validActor(withActorLabels(map[string]string{"ate.dev/agent": "a"}))),
field.ErrorList{field.Invalid(field.NewPath("actor", "metadata", "labels"), "ate.dev/agent", "")},
}, {
"labels over the size cap",
validReq(validActor(withActorLabels(labelsOfBytes(1)))),
field.ErrorList{field.TooLong(field.NewPath("actor", "metadata", "labels"), 1025, 1024)},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -246,6 +274,33 @@ func TestValidateActorUpdate(t *testing.T) {
oldVal: validInput(withSourceTag("as1", "nm1")),
newVal: validOutput(withSourceTag("as2", "nm2")),
want: field.ErrorList{field.Invalid(field.NewPath("source_tag"), nil, "").WithOrigin("immutable")},
}, {
name: "keep actor.metadata.labels",
oldVal: validInput(withActorLabels(map[string]string{"app": "billing"})),
newVal: validOutput(withActorLabels(map[string]string{"app": "billing"})),
}, {
name: "nil labels equal empty labels",
oldVal: validInput(),
newVal: validOutput(withActorLabels(map[string]string{})),
}, {
name: "empty labels equal nil labels",
oldVal: validInput(withActorLabels(map[string]string{})),
newVal: validOutput(),
}, {
name: "add actor.metadata.labels",
oldVal: validInput(),
newVal: validOutput(withActorLabels(map[string]string{"app": "billing"})),
want: field.ErrorList{field.Invalid(field.NewPath("metadata", "labels"), nil, "").WithOrigin("immutable")},
}, {
name: "clear actor.metadata.labels",
oldVal: validInput(withActorLabels(map[string]string{"app": "billing"})),
newVal: validOutput(),
want: field.ErrorList{field.Invalid(field.NewPath("metadata", "labels"), nil, "").WithOrigin("immutable")},
}, {
name: "change actor.metadata.labels",
oldVal: validInput(withActorLabels(map[string]string{"app": "billing"})),
newVal: validOutput(withActorLabels(map[string]string{"app": "checkout"})),
want: field.ErrorList{field.Invalid(field.NewPath("metadata", "labels"), nil, "").WithOrigin("immutable")},
}, {
name: "set valid worker_selector",
oldVal: validInput(),
Expand Down Expand Up @@ -1085,6 +1140,10 @@ func withActorSourceTag(atespace, name string) func(*ateapi.Actor) {
return func(a *ateapi.Actor) { a.SourceTag = &ateapi.ObjectRef{Atespace: atespace, Name: name} }
}

func withActorLabels(labels map[string]string) func(*ateapi.Actor) {
return func(a *ateapi.Actor) { a.Metadata.Labels = labels }
}

// withActorWorkerAssignment returns a modifier func (see validActor) which sets
// the actor's worker_assignment to a valid value.
func withActorWorkerAssignment(mods ...func(*ateapi.WorkerAssignment)) func(*ateapi.ActorStatus) {
Expand Down
30 changes: 30 additions & 0 deletions cmd/ateapi/internal/apivalidation/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ package apivalidation
import (
"context"
"fmt"
"maps"
"net/netip"
"slices"
"strings"

"github.com/spiffe/go-spiffe/v2/spiffeid"
"go.ate.dev/substrate/api/ateapi/v1"
Expand All @@ -34,6 +37,10 @@ func ateDeepEqual[T any](a, b T) bool {
return resources.DeepEqual(a, b)
}

func ValidateCustom_ResourceMetadata_Labels(_ context.Context, _ operation.Operation, fldPath *field.Path, value, _ map[string]string) field.ErrorList {
return validateLabels(fldPath, value)
}

// validateSingletonName requires the name of a singleton subresource to be
// resources.SingletonName. An empty name is left to the required tag.
func validateSingletonName(fldPath *field.Path, name string) field.ErrorList {
Expand Down Expand Up @@ -115,6 +122,29 @@ func validateDualStackIPs(fldPath *field.Path, ips []string) field.ErrorList {
return errs
}

// maxLabelsSizeBytes bounds the keys and values of one label set together.
// Labels travel with every Get and List of their resource, so the per-entry
// formats alone would allow a set far larger than identification needs.
const maxLabelsSizeBytes = 1024

// validateLabels checks what the label tags cannot express: keys in ate.dev
// and its subdomains are reserved, and the set fits maxLabelsSizeBytes.
// Count and per-entry formats are enforced by tags.
func validateLabels(fldPath *field.Path, labels map[string]string) field.ErrorList {
var errs field.ErrorList
size := 0
for _, key := range slices.Sorted(maps.Keys(labels)) {
if strings.HasPrefix(key, "ate.dev/") || strings.Contains(key, ".ate.dev/") {
errs = append(errs, field.Invalid(fldPath, key, "ate.dev and its subdomains are reserved"))
}
size += len(key) + len(labels[key])
}
if size > maxLabelsSizeBytes {
errs = append(errs, field.TooLong(fldPath, size, maxLabelsSizeBytes))
}
return errs
}

// validateSPIFFEID checks that id is a SPIFFE ID. Presence and length are
// enforced by tags.
func validateSPIFFEID(fldPath *field.Path, id string) field.ErrorList {
Expand Down
48 changes: 48 additions & 0 deletions cmd/ateapi/internal/apivalidation/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"testing"

"go.ate.dev/substrate/internal/preview"
"k8s.io/apimachinery/pkg/api/operation"
"k8s.io/apimachinery/pkg/util/validation/field"
)

Expand Down Expand Up @@ -76,3 +77,50 @@ func assertValidateErr(t *testing.T, got field.ErrorList, want field.ErrorList)
t.Helper()
field.ErrorMatcher{}.ByType().ByField().ByOrigin().Test(t, want, got)
}

// labelsOfBytes returns 16 valid labels totaling 1024+extra key and value bytes.
func labelsOfBytes(extra int) map[string]string {
labels := make(map[string]string, 16)
for i := range 16 {
labels[fmt.Sprintf("k%03d", i)] = strings.Repeat("v", 60)
}
labels["k000"] = strings.Repeat("v", 60+extra)
return labels
}

func TestValidateCustom_ResourceMetadata_Labels(t *testing.T) {
fldPath := field.NewPath("metadata", "labels")
tests := []struct {
name string
labels map[string]string
want field.ErrorList
}{{
name: "none",
}, {
name: "valid",
labels: map[string]string{"app": "billing", "example.com/team": "payments"},
}, {
name: "ate.dev key is reserved",
labels: map[string]string{"ate.dev/agent": "a"},
want: field.ErrorList{field.Invalid(fldPath, "ate.dev/agent", "")},
}, {
name: "ate.dev subdomain key is reserved",
labels: map[string]string{"egress.ate.dev/agent": "a"},
want: field.ErrorList{field.Invalid(fldPath, "egress.ate.dev/agent", "")},
}, {
name: "a domain ending in ate.dev without the dot is not reserved",
labels: map[string]string{"private.dev/agent": "a", "notate.dev/agent": "a"},
}, {
name: "exactly the size cap",
labels: labelsOfBytes(0),
}, {
name: "over the size cap",
labels: labelsOfBytes(1),
want: field.ErrorList{field.TooLong(fldPath, 1025, 1024)},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assertValidateErr(t, ValidateCustom_ResourceMetadata_Labels(t.Context(), operation.Operation{Type: operation.Create}, fldPath, tt.labels, nil), tt.want)
})
}
}
88 changes: 88 additions & 0 deletions cmd/ateapi/internal/apivalidation/zz_generated.validation.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading