github.com/koderover/helm@v2.17.0+incompatible/pkg/tiller/server.go (about)

     1  /*
     2  Copyright The Helm Authors.
     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  const 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.MaxRecvMsgSize(maxMsgSize),
    40  		grpc.MaxSendMsgSize(maxMsgSize),
    41  		grpc.UnaryInterceptor(newUnaryInterceptor()),
    42  		grpc.StreamInterceptor(newStreamInterceptor()),
    43  	}
    44  }
    45  
    46  // NewServer creates a new grpc server.
    47  func NewServer(opts ...grpc.ServerOption) *grpc.Server {
    48  	return grpc.NewServer(append(DefaultServerOpts(), opts...)...)
    49  }
    50  
    51  func newUnaryInterceptor() grpc.UnaryServerInterceptor {
    52  	return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
    53  		if err := checkClientVersion(ctx); err != nil {
    54  			// whitelist GetVersion() from the version check
    55  			if _, m := splitMethod(info.FullMethod); m != "GetVersion" {
    56  				log.Println(err)
    57  				return nil, err
    58  			}
    59  		}
    60  		return goprom.UnaryServerInterceptor(ctx, req, info, handler)
    61  	}
    62  }
    63  
    64  func newStreamInterceptor() grpc.StreamServerInterceptor {
    65  	return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {
    66  		if err := checkClientVersion(ss.Context()); err != nil {
    67  			log.Println(err)
    68  			return err
    69  		}
    70  		return goprom.StreamServerInterceptor(srv, ss, info, handler)
    71  	}
    72  }
    73  
    74  func splitMethod(fullMethod string) (string, string) {
    75  	if frags := strings.Split(fullMethod, "/"); len(frags) == 3 {
    76  		return frags[1], frags[2]
    77  	}
    78  	return "unknown", "unknown"
    79  }
    80  
    81  func versionFromContext(ctx context.Context) string {
    82  	if md, ok := metadata.FromIncomingContext(ctx); ok {
    83  		if v, ok := md["x-helm-api-client"]; ok && len(v) > 0 {
    84  			return v[0]
    85  		}
    86  	}
    87  	return ""
    88  }
    89  
    90  func checkClientVersion(ctx context.Context) error {
    91  	clientVersion := versionFromContext(ctx)
    92  	if !version.IsCompatible(clientVersion, version.GetVersion()) {
    93  		return fmt.Errorf("incompatible versions client[%s] server[%s]", clientVersion, version.GetVersion())
    94  	}
    95  	return nil
    96  }