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
1,081 changes: 614 additions & 467 deletions api/ateapi/v1/ateapi.pb.go

Large diffs are not rendered by default.

52 changes: 51 additions & 1 deletion api/ateapi/v1/ateapi.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ service Control {
}

// Suspend a given actor to a new snapshot. A running actor is checkpointed
// on its worker. A SUSPENDED actor is returned as it is.
// on its worker. A SUSPENDED actor is returned as it is, unless a
// preconditions guard does not match.
rpc SuspendActor(SuspendActorRequest) returns (SuspendActorResponse) {
option (method).resource = "Actor";
}
Expand Down Expand Up @@ -1814,6 +1815,11 @@ message SuspendActorRequest {
//
// +k8s:optional
SuspendOptions options = 2;

// Act only if the actor still matches these guards.
//
// +k8s:optional
Preconditions preconditions = 3;
}

message SuspendActorResponse {
Expand Down Expand Up @@ -1852,6 +1858,11 @@ message ResumeActorRequest {
// +k8s:required
// +k8s:subfield(atespace)=+k8s:required
ObjectRef actor = 1;

// Act only if the actor still matches these guards.
//
// +k8s:optional
Preconditions preconditions = 2;
}

message ResumeActorResponse {
Expand All @@ -1868,6 +1879,11 @@ message RevertActorRequest {
// +k8s:required
// +k8s:subfield(atespace)=+k8s:required
ObjectRef actor = 1;

// Act only if the actor still matches these guards.
//
// +k8s:optional
Preconditions preconditions = 2;
}

// RevertActorResponse holds the reverted Actor.
Expand Down Expand Up @@ -2103,6 +2119,25 @@ message DeleteOptions {
string uid = 2;
}

// Preconditions are the optional guards a custom method request may carry.
// A guard that does not match the resource fails the request with `ABORTED`.
message Preconditions {
// Act only if the resource is still at this version, that is, unchanged
// since the caller read it. Zero skips the check.
//
// +k8s:optional
// +k8s:minimum=1
int64 version = 1;

// Act only if the resource still has this uid, that is, it is the one
// the caller read and not a later one created under the same name. Empty
// skips the check.
//
// +k8s:optional
// +k8s:format=k8s-uuid
string uid = 2;
}

// ListOptions are the pagination controls a List request may carry.
message ListOptions {
// Requested page size; the server may return fewer, or occasionally
Expand Down Expand Up @@ -2214,6 +2249,11 @@ message DrainWorkerRequest {
// +k8s:required
// +k8s:beta(since: "0.0")=+k8s:subfield(atespace)=+k8s:forbidden # TODO: get rid of beta prefix
ObjectRef worker = 1;

// Act only if the Worker still matches these guards.
//
// +k8s:optional
Preconditions preconditions = 2;
}

// DrainWorkerResponse holds the drained Worker.
Expand All @@ -2229,6 +2269,11 @@ message MarkWorkerAvailableRequest {
// +k8s:required
// +k8s:beta(since: "0.0")=+k8s:subfield(atespace)=+k8s:forbidden # TODO: get rid of beta prefix
ObjectRef worker = 1;

// Act only if the Worker still matches these guards.
//
// +k8s:optional
Preconditions preconditions = 2;
}

// MarkWorkerAvailableResponse holds the Worker marked ACTIVE.
Expand All @@ -2244,6 +2289,11 @@ message MarkWorkerUnavailableRequest {
// +k8s:required
// +k8s:beta(since: "0.0")=+k8s:subfield(atespace)=+k8s:forbidden # TODO: get rid of beta prefix
ObjectRef worker = 1;

// Act only if the Worker still matches these guards.
//
// +k8s:optional
Preconditions preconditions = 2;
}

// MarkWorkerUnavailableResponse holds the Worker marked UNAVAILABLE.
Expand Down
6 changes: 4 additions & 2 deletions api/ateapi/v1/ateapi_grpc.pb.go

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

84 changes: 84 additions & 0 deletions cmd/ateapi/internal/apivalidation/actor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,27 @@ func TestValidateResumeActorRequest(t *testing.T) {
"invalid actor.name",
&ateapi.ResumeActorRequest{Actor: &ateapi.ObjectRef{Atespace: "ns1", Name: "ID1"}},
field.ErrorList{field.Invalid(field.NewPath("actor", "name"), "ID1", "").WithOrigin("format=k8s-short-name")},
}, {
"valid preconditions",
&ateapi.ResumeActorRequest{
Actor: &ateapi.ObjectRef{Atespace: "ns1", Name: "id1"},
Preconditions: &ateapi.Preconditions{Uid: someActorUID, Version: 3},
},
nil,
}, {
"invalid preconditions.uid",
&ateapi.ResumeActorRequest{
Actor: &ateapi.ObjectRef{Atespace: "ns1", Name: "id1"},
Preconditions: &ateapi.Preconditions{Uid: "not-a-uuid"},
},
field.ErrorList{field.Invalid(field.NewPath("preconditions", "uid"), nil, "").WithOrigin("format=k8s-uuid")},
}, {
"negative preconditions.version",
&ateapi.ResumeActorRequest{
Actor: &ateapi.ObjectRef{Atespace: "ns1", Name: "id1"},
Preconditions: &ateapi.Preconditions{Version: -1},
},
field.ErrorList{field.Invalid(field.NewPath("preconditions", "version"), nil, "").WithOrigin("minimum")},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down Expand Up @@ -1024,6 +1045,27 @@ func TestValidateSuspendActorRequest(t *testing.T) {
Options: &ateapi.SuspendOptions{Mode: ateapi.DurabilityMode(3)},
},
field.ErrorList{field.Invalid(field.NewPath("options", "mode"), nil, "").WithOrigin("maximum")},
}, {
"valid preconditions",
&ateapi.SuspendActorRequest{
Actor: &ateapi.ObjectRef{Atespace: "ns1", Name: "id1"},
Preconditions: &ateapi.Preconditions{Uid: someActorUID, Version: 3},
},
nil,
}, {
"invalid preconditions.uid",
&ateapi.SuspendActorRequest{
Actor: &ateapi.ObjectRef{Atespace: "ns1", Name: "id1"},
Preconditions: &ateapi.Preconditions{Uid: "not-a-uuid"},
},
field.ErrorList{field.Invalid(field.NewPath("preconditions", "uid"), nil, "").WithOrigin("format=k8s-uuid")},
}, {
"negative preconditions.version",
&ateapi.SuspendActorRequest{
Actor: &ateapi.ObjectRef{Atespace: "ns1", Name: "id1"},
Preconditions: &ateapi.Preconditions{Version: -1},
},
field.ErrorList{field.Invalid(field.NewPath("preconditions", "version"), nil, "").WithOrigin("minimum")},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand All @@ -1032,6 +1074,48 @@ func TestValidateSuspendActorRequest(t *testing.T) {
}
}

func TestValidateRevertActorRequest(t *testing.T) {
tests := []struct {
name string
req *ateapi.RevertActorRequest
want field.ErrorList
}{{
"valid",
&ateapi.RevertActorRequest{Actor: &ateapi.ObjectRef{Atespace: "ns1", Name: "id1"}},
nil,
}, {
"missing actor",
&ateapi.RevertActorRequest{},
field.ErrorList{field.Required(field.NewPath("actor"), "")},
}, {
"valid preconditions",
&ateapi.RevertActorRequest{
Actor: &ateapi.ObjectRef{Atespace: "ns1", Name: "id1"},
Preconditions: &ateapi.Preconditions{Uid: someActorUID, Version: 3},
},
nil,
}, {
"invalid preconditions.uid",
&ateapi.RevertActorRequest{
Actor: &ateapi.ObjectRef{Atespace: "ns1", Name: "id1"},
Preconditions: &ateapi.Preconditions{Uid: "not-a-uuid"},
},
field.ErrorList{field.Invalid(field.NewPath("preconditions", "uid"), nil, "").WithOrigin("format=k8s-uuid")},
}, {
"negative preconditions.version",
&ateapi.RevertActorRequest{
Actor: &ateapi.ObjectRef{Atespace: "ns1", Name: "id1"},
Preconditions: &ateapi.Preconditions{Version: -1},
},
field.ErrorList{field.Invalid(field.NewPath("preconditions", "version"), nil, "").WithOrigin("minimum")},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assertValidateErr(t, ValidateRevertActorRequest(context.Background(), tt.req), tt.want)
})
}
}

// validActor returns a minimal Actor which should pass input validation.
func validActor(mods ...func(*ateapi.Actor)) *ateapi.Actor {
a := &ateapi.Actor{
Expand Down
59 changes: 31 additions & 28 deletions cmd/ateapi/internal/apivalidation/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1014,45 +1014,48 @@ func TestValidateExternalVolume_Update(t *testing.T) {
}
}

func TestValidateDeleteOptions(t *testing.T) {
valid := func(mutate ...func(*ateapi.DeleteOptions)) *ateapi.DeleteOptions {
tb := &ateapi.DeleteOptions{}
for _, m := range mutate {
m(tb)
}
return tb
// TestValidateGuards covers DeleteOptions and Preconditions, which carry the
// same optional guards.
func TestValidateGuards(t *testing.T) {
validators := map[string]func(version int64, uid string) field.ErrorList{
"DeleteOptions": func(version int64, uid string) field.ErrorList {
ctx := context.Background()
return Validate_DeleteOptions(ctx, MakeCreateOp(ctx), nil, &ateapi.DeleteOptions{Version: version, Uid: uid}, nil)
},
"Preconditions": func(version int64, uid string) field.ErrorList {
ctx := context.Background()
return Validate_Preconditions(ctx, MakeCreateOp(ctx), nil, &ateapi.Preconditions{Version: version, Uid: uid}, nil)
},
}

tests := []struct {
name string
obj *ateapi.DeleteOptions
want field.ErrorList
name string
version int64
uid string
want field.ErrorList
}{{
name: "valid",
obj: valid(), // all optional fields
name: "valid", // all optional fields
}, {
name: "valid version",
obj: valid(func(do *ateapi.DeleteOptions) { do.Version = 1 }),
want: nil,
name: "valid version",
version: 1,
}, {
name: "invalid version",
obj: valid(func(do *ateapi.DeleteOptions) { do.Version = -1 }),
want: field.ErrorList{field.Invalid(field.NewPath("version"), nil, "").WithOrigin("minimum")},
name: "invalid version",
version: -1,
want: field.ErrorList{field.Invalid(field.NewPath("version"), nil, "").WithOrigin("minimum")},
}, {
name: "valid uid",
obj: valid(func(do *ateapi.DeleteOptions) { do.Uid = "11111111-2222-3333-4444-555555555555" }),
want: nil,
uid: "11111111-2222-3333-4444-555555555555",
}, {
name: "invalid uid",
obj: valid(func(do *ateapi.DeleteOptions) { do.Uid = "not a uid" }),
uid: "not a uid",
want: field.ErrorList{field.Invalid(field.NewPath("uid"), nil, "").WithOrigin("format=k8s-uuid")},
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
op := MakeCreateOp(context.Background())
matcher := field.ErrorMatcher{}.ByType().ByField().ByOrigin()
matcher.Test(t, tt.want, Validate_DeleteOptions(context.Background(), op, nil, tt.obj, nil))
})
for msg, validate := range validators {
for _, tt := range tests {
t.Run(msg+"/"+tt.name, func(t *testing.T) {
matcher := field.ErrorMatcher{}.ByType().ByField().ByOrigin()
matcher.Test(t, tt.want, validate(tt.version, tt.uid))
})
}
}
}

Expand Down
Loading
Loading