github.com/demonoid81/containerd@v1.3.4/leases/grpc.go (about) 1 /* 2 Copyright The containerd 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 leases 18 19 import ( 20 "context" 21 22 "google.golang.org/grpc/metadata" 23 ) 24 25 const ( 26 // GRPCHeader defines the header name for specifying a containerd lease. 27 GRPCHeader = "containerd-lease" 28 ) 29 30 func withGRPCLeaseHeader(ctx context.Context, lid string) context.Context { 31 // also store on the grpc headers so it gets picked up by any clients 32 // that are using this. 33 txheader := metadata.Pairs(GRPCHeader, lid) 34 md, ok := metadata.FromOutgoingContext(ctx) // merge with outgoing context. 35 if !ok { 36 md = txheader 37 } else { 38 // order ensures the latest is first in this list. 39 md = metadata.Join(txheader, md) 40 } 41 42 return metadata.NewOutgoingContext(ctx, md) 43 } 44 45 func fromGRPCHeader(ctx context.Context) (string, bool) { 46 // try to extract for use in grpc servers. 47 md, ok := metadata.FromIncomingContext(ctx) 48 if !ok { 49 return "", false 50 } 51 52 values := md[GRPCHeader] 53 if len(values) == 0 { 54 return "", false 55 } 56 57 return values[0], true 58 }