github.phpd.cn/cilium/cilium@v1.6.12/pkg/proxy/epinfo.go (about)

     1  // Copyright 2016-2018 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package proxy
    16  
    17  import (
    18  	"net"
    19  
    20  	"github.com/cilium/cilium/pkg/endpointmanager"
    21  	"github.com/cilium/cilium/pkg/identity"
    22  	"github.com/cilium/cilium/pkg/identity/cache"
    23  	"github.com/cilium/cilium/pkg/proxy/accesslog"
    24  	"github.com/cilium/cilium/pkg/proxy/logger"
    25  )
    26  
    27  var (
    28  	// DefaultEndpointInfoRegistry is the default instance implementing the
    29  	// EndpointInfoRegistry interface.
    30  	DefaultEndpointInfoRegistry logger.EndpointInfoRegistry = &defaultEndpointInfoRegistry{}
    31  )
    32  
    33  // defaultEndpointInfoRegistry is the default implementation of the
    34  // EndpointInfoRegistry interface.
    35  type defaultEndpointInfoRegistry struct{}
    36  
    37  func (r *defaultEndpointInfoRegistry) FillEndpointIdentityByID(id identity.NumericIdentity, info *accesslog.EndpointInfo) bool {
    38  	identity := cache.LookupIdentityByID(id)
    39  	if identity == nil {
    40  		return false
    41  	}
    42  
    43  	info.Identity = uint64(id)
    44  	info.Labels = identity.Labels.GetModel()
    45  	info.LabelsSHA256 = identity.GetLabelsSHA256()
    46  
    47  	return true
    48  }
    49  
    50  func (r *defaultEndpointInfoRegistry) FillEndpointIdentityByIP(ip net.IP, info *accesslog.EndpointInfo) bool {
    51  	ep := endpointmanager.LookupIP(ip)
    52  	if ep == nil {
    53  		return false
    54  	}
    55  
    56  	if err := ep.RLockAlive(); err != nil {
    57  		ep.LogDisconnectedMutexAction(err, "before FillEndpointIdentityByIP")
    58  		return false
    59  	}
    60  
    61  	info.ID = uint64(ep.ID)
    62  	info.Identity = uint64(ep.GetIdentityLocked())
    63  	info.Labels = ep.GetLabels()
    64  	info.LabelsSHA256 = ep.GetLabelsSHA()
    65  
    66  	ep.RUnlock()
    67  	return true
    68  }