Skip to content

Commit

Permalink
fix review findings
Browse files Browse the repository at this point in the history
  • Loading branch information
sbueringer committed Jun 2, 2022
1 parent b580913 commit 93a57d0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 21 deletions.
7 changes: 4 additions & 3 deletions internal/runtime/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,14 +204,13 @@ func (c *client) CallAllExtensions(ctx context.Context, hook runtimecatalog.Hook
}

// Aggregate all responses into a single response.
// Note: we only get here if all the extension handlers succeeded.
aggregateSuccessfulResponses(response, responses)

if err != nil {
return errors.Wrap(err, "failed to aggregate responses")
}
return nil
}

// aggregateSuccessfulResponses aggregates all successful responses into a single response.
func aggregateSuccessfulResponses(aggregatedResponse runtimehooksv1.ResponseObject, responses []runtimehooksv1.ResponseObject) {
// At this point the Status should always be ResponseStatusSuccess and the Message should be empty.
// So let's set those values to avoid keeping values that could have been set by the caller of CallAllExtensions.
Expand All @@ -223,6 +222,8 @@ func aggregateSuccessfulResponses(aggregatedResponse runtimehooksv1.ResponseObje
return
}

// Note: as all responses have the same type we can assume now that
// they all implement the RetryResponseObject interface.
aggregatedRetryableResponse := aggregatedResponse.(runtimehooksv1.RetryResponseObject)

for _, resp := range responses {
Expand Down
50 changes: 32 additions & 18 deletions internal/runtime/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,11 +391,25 @@ func TestClient_CallExtension(t *testing.T) {
testServer: testServerConfig{
start: false,
},
args: args{
hook: fakev1alpha1.FakeHook,
name: "valid-extension",
request: &fakev1alpha1.SecondFakeRequest{},
response: &fakev1alpha1.SecondFakeResponse{},
},
wantErr: true,
},
{
name: "should fail when hook GVH does not match the registered ExtensionHandler",
registeredExtensionConfigs: []runtimev1.ExtensionConfig{validExtensionHandlerWithFailPolicy},
testServer: testServerConfig{
start: false,
},
args: args{
hook: fakev1alpha1.SecondFakeHook,
name: "valid-extension",
request: &fakev1alpha1.FakeRequest{},
response: &fakev1alpha1.FakeResponse{},
request: &fakev1alpha1.SecondFakeRequest{},
response: &fakev1alpha1.SecondFakeResponse{},
},
wantErr: true,
},
Expand Down Expand Up @@ -612,7 +626,7 @@ func Test_client_CallAllExtensions(t *testing.T) {
wantErr: true,
},
{
name: "should succeed when no corresponding hook is registered",
name: "should succeed when no ExtensionHandlers are registered for the hook",
registeredExtensionConfigs: []runtimev1.ExtensionConfig{},
testServer: testServerConfig{
start: false,
Expand Down Expand Up @@ -735,31 +749,31 @@ func Test_aggregateResponses(t *testing.T) {
}{
{
name: "Aggregate response if there is only one response",
aggregateResponse: fakeResponse(),
aggregateResponse: fakeSuccessResponse(),
responses: []runtimehooksv1.ResponseObject{
fakeResponse(),
fakeSuccessResponse(),
},
want: fakeResponse(),
want: fakeSuccessResponse(),
},
{
name: "Aggregate retry response if there is only one response",
aggregateResponse: fakeRetryableResponse(0),
aggregateResponse: fakeRetryableSuccessResponse(0),
responses: []runtimehooksv1.ResponseObject{
fakeRetryableResponse(5),
fakeRetryableSuccessResponse(5),
},
want: fakeRetryableResponse(5),
want: fakeRetryableSuccessResponse(5),
},
{
name: "Aggregate retry responses to lowest non-zero retryAfterSeconds value",
aggregateResponse: fakeRetryableResponse(0),
aggregateResponse: fakeRetryableSuccessResponse(0),
responses: []runtimehooksv1.ResponseObject{
fakeRetryableResponse(0),
fakeRetryableResponse(1),
fakeRetryableResponse(5),
fakeRetryableResponse(4),
fakeRetryableResponse(3),
fakeRetryableSuccessResponse(0),
fakeRetryableSuccessResponse(1),
fakeRetryableSuccessResponse(5),
fakeRetryableSuccessResponse(4),
fakeRetryableSuccessResponse(3),
},
want: fakeRetryableResponse(1),
want: fakeRetryableSuccessResponse(1),
},
}
for _, tt := range tests {
Expand Down Expand Up @@ -839,7 +853,7 @@ func registry(configs []runtimev1.ExtensionConfig) runtimeregistry.ExtensionRegi
return registry
}

func fakeResponse() *fakev1alpha1.FakeResponse {
func fakeSuccessResponse() *fakev1alpha1.FakeResponse {
return &fakev1alpha1.FakeResponse{
TypeMeta: metav1.TypeMeta{
Kind: "FakeResponse",
Expand All @@ -852,7 +866,7 @@ func fakeResponse() *fakev1alpha1.FakeResponse {
}
}

func fakeRetryableResponse(retryAfterSeconds int32) *fakev1alpha1.RetryableFakeResponse {
func fakeRetryableSuccessResponse(retryAfterSeconds int32) *fakev1alpha1.RetryableFakeResponse {
return &fakev1alpha1.RetryableFakeResponse{
TypeMeta: metav1.TypeMeta{
Kind: "FakeResponse",
Expand Down

0 comments on commit 93a57d0

Please sign in to comment.