Skip to content

Commit

Permalink
cri/pkg: use our own interceptor grpc chain function
Browse files Browse the repository at this point in the history
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
fuweid authored and zhuangqh committed Feb 25, 2019
1 parent fc95f8f commit 465c1c6
Show file tree
Hide file tree
Showing 16 changed files with 82 additions and 972 deletions.
3 changes: 1 addition & 2 deletions cri/v1alpha2/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/alibaba/pouch/pkg/grpc/interceptor"
"github.com/alibaba/pouch/pkg/netutils"

"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/sirupsen/logrus"
"google.golang.org/grpc"
)
Expand All @@ -29,7 +28,7 @@ func NewService(cfg *config.Config, criMgr CriMgr) (*Service, error) {
config: cfg,
server: grpc.NewServer(
grpc.StreamInterceptor(metrics.GRPCMetrics.StreamServerInterceptor()),
grpc_middleware.WithUnaryServerChain(
interceptor.WithUnaryServerChain(
metrics.GRPCMetrics.UnaryServerInterceptor(),
interceptor.PayloadUnaryServerInterceptor(logEntry, criLogLevelDecider),
),
Expand Down
31 changes: 31 additions & 0 deletions pkg/grpc/interceptor/chain.go
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)
}
}
50 changes: 50 additions & 0 deletions pkg/grpc/interceptor/chain_test.go
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 vendor/github.com/grpc-ecosystem/go-grpc-middleware/CHANGELOG.md

This file was deleted.

This file was deleted.

166 changes: 0 additions & 166 deletions vendor/github.com/grpc-ecosystem/go-grpc-middleware/DOC.md

This file was deleted.

Loading

0 comments on commit 465c1c6

Please sign in to comment.