-
Notifications
You must be signed in to change notification settings - Fork 950
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cri/pkg: use our own interceptor grpc chain function
The grpc middleware uses difference protobuf version from containerd@v1.2. In order to align with containerd, we should use our own interceptor grpc chain function. Signed-off-by: Wei Fu <fuweid89@gmail.com>
- Loading branch information
Showing
16 changed files
with
82 additions
and
972 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package interceptor | ||
|
||
import ( | ||
"context" | ||
|
||
"google.golang.org/grpc" | ||
) | ||
|
||
// WithUnaryServerChain creates a single interceptor out of a chain of many interceptors. | ||
func WithUnaryServerChain(interceptors ...grpc.UnaryServerInterceptor) grpc.ServerOption { | ||
return grpc.UnaryInterceptor(chainUnaryServer(interceptors...)) | ||
} | ||
|
||
// chainUnaryServer creates a single interceptor out of a chain of many interceptors. | ||
// | ||
// Execution is done in left-to-right order. | ||
func chainUnaryServer(interceptors ...grpc.UnaryServerInterceptor) grpc.UnaryServerInterceptor { | ||
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { | ||
buildChain := func(current grpc.UnaryServerInterceptor, next grpc.UnaryHandler) grpc.UnaryHandler { | ||
return func(currentCtx context.Context, currentReq interface{}) (interface{}, error) { | ||
return current(currentCtx, currentReq, info, next) | ||
} | ||
} | ||
|
||
chain := handler | ||
for i := len(interceptors) - 1; i >= 0; i-- { | ||
chain = buildChain(interceptors[i], chain) | ||
} | ||
return chain(ctx, req) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package interceptor | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"google.golang.org/grpc" | ||
) | ||
|
||
func TestChainUnaryServer(t *testing.T) { | ||
type key int | ||
|
||
var ( | ||
serviceName = "Foo.UnaryMethod" | ||
theUnaryInfo = &grpc.UnaryServerInfo{FullMethod: serviceName} | ||
|
||
keyFirst key = 1 | ||
keySecond key = 2 | ||
valueFirst = "I'm first" | ||
valueSecond = "I'm second" | ||
|
||
input = "input" | ||
result = "result" | ||
) | ||
|
||
first := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { | ||
assert.Equal(t, ctx.Value(keyFirst), nil, "there should be no first value") | ||
assert.Equal(t, info, theUnaryInfo) | ||
return handler(context.WithValue(ctx, keyFirst, valueFirst), req) | ||
} | ||
|
||
second := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { | ||
assert.Equal(t, ctx.Value(keyFirst), valueFirst, "there should be first value") | ||
assert.Equal(t, ctx.Value(keySecond), nil, "there should be no second value") | ||
assert.Equal(t, info, theUnaryInfo) | ||
return handler(context.WithValue(ctx, keySecond, valueSecond), req) | ||
} | ||
|
||
handler := func(ctx context.Context, req interface{}) (interface{}, error) { | ||
assert.Equal(t, req, input, "check the input") | ||
assert.Equal(t, ctx.Value(keyFirst), valueFirst, "there should be first value") | ||
assert.Equal(t, ctx.Value(keySecond), valueSecond, "there should be second value") | ||
return result, nil | ||
} | ||
|
||
output, err := chainUnaryServer(first, second)(context.TODO(), input, theUnaryInfo, handler) | ||
assert.Equal(t, err, nil) | ||
assert.Equal(t, output, result) | ||
} |
30 changes: 0 additions & 30 deletions
30
vendor/github.com/grpc-ecosystem/go-grpc-middleware/CHANGELOG.md
This file was deleted.
Oops, something went wrong.
20 changes: 0 additions & 20 deletions
20
vendor/github.com/grpc-ecosystem/go-grpc-middleware/CONTRIBUTING.md
This file was deleted.
Oops, something went wrong.
166 changes: 0 additions & 166 deletions
166
vendor/github.com/grpc-ecosystem/go-grpc-middleware/DOC.md
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.