github.com/darkowlzz/helm@v2.5.1-0.20171213183701-6707fe0468d4+incompatible/pkg/tiller/server.go (about) 1 /* 2 Copyright 2016 The Kubernetes Authors All rights reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package tiller 18 19 import ( 20 "fmt" 21 "log" 22 "strings" 23 24 goprom "github.com/grpc-ecosystem/go-grpc-prometheus" 25 "golang.org/x/net/context" 26 "google.golang.org/grpc" 27 "google.golang.org/grpc/metadata" 28 29 "k8s.io/helm/pkg/version" 30 ) 31 32 // maxMsgSize use 20MB as the default message size limit. 33 // grpc library default is 4MB 34 var maxMsgSize = 1024 * 1024 * 20 35 36 // DefaultServerOpts returns the set of default grpc ServerOption's that Tiller requires. 37 func DefaultServerOpts() []grpc.ServerOption { 38 return []grpc.ServerOption{ 39 grpc.MaxMsgSize(maxMsgSize), 40 grpc.UnaryInterceptor(newUnaryInterceptor()), 41 grpc.StreamInterceptor(newStreamInterceptor()), 42 } 43 } 44 45 // NewServer creates a new grpc server. 46 func NewServer(opts ...grpc.ServerOption) *grpc.Server { 47 return grpc.NewServer(append(DefaultServerOpts(), opts...)...) 48 } 49 50 func newUnaryInterceptor() grpc.UnaryServerInterceptor { 51 return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { 52 if err := checkClientVersion(ctx); err != nil { 53 // whitelist GetVersion() from the version check 54 if _, m := splitMethod(info.FullMethod); m != "GetVersion" { 55 log.Println(err) 56 return nil, err 57 } 58 } 59 return goprom.UnaryServerInterceptor(ctx, req, info, handler) 60 } 61 } 62 63 func newStreamInterceptor() grpc.StreamServerInterceptor { 64 return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { 65 if err := checkClientVersion(ss.Context()); err != nil { 66 log.Println(err) 67 return err 68 } 69 return goprom.StreamServerInterceptor(srv, ss, info, handler) 70 } 71 } 72 73 func splitMethod(fullMethod string) (string, string) { 74 if frags := strings.Split(fullMethod, "/"); len(frags) == 3 { 75 return frags[1], frags[2] 76 } 77 return "unknown", "unknown" 78 } 79 80 func versionFromContext(ctx context.Context) string { 81 if md, ok := metadata.FromIncomingContext(ctx); ok { 82 if v, ok := md["x-helm-api-client"]; ok && len(v) > 0 { 83 return v[0] 84 } 85 } 86 return "" 87 } 88 89 func checkClientVersion(ctx context.Context) error { 90 clientVersion := versionFromContext(ctx) 91 if !version.IsCompatible(clientVersion, version.GetVersion()) { 92 return fmt.Errorf("incompatible versions client[%s] server[%s]", clientVersion, version.GetVersion()) 93 } 94 return nil 95 }