github.com/fafucoder/cilium@v1.6.11/pkg/endpoint/id/id.go (about)

     1  // Copyright 2016-2017 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 id
    16  
    17  import (
    18  	"fmt"
    19  	"net"
    20  	"strconv"
    21  	"strings"
    22  )
    23  
    24  // PrefixType describes the type of endpoint identifier
    25  type PrefixType string
    26  
    27  func (s PrefixType) String() string { return string(s) }
    28  
    29  const (
    30  	// CiliumLocalIdPrefix is a numeric identifier with local scope. It has
    31  	// no cluster wide meaning and is only unique in the scope of a single
    32  	// agent. An endpoint is guaranteed to always have a local scope identifier.
    33  	CiliumLocalIdPrefix PrefixType = "cilium-local"
    34  
    35  	// CiliumGlobalIdPrefix is an endpoint identifier with global scope.
    36  	// This addressing mechanism is currently unused.
    37  	CiliumGlobalIdPrefix PrefixType = "cilium-global"
    38  
    39  	// ContainerIdPrefix is used to address an endpoint via its primary
    40  	// container ID. The container ID is specific to the container runtime
    41  	// in use. Only the primary container that defines the networking scope
    42  	// can be used to address an endpoint.
    43  	ContainerIdPrefix PrefixType = "container-id"
    44  
    45  	// DockerEndpointPrefix is used to address an endpoint via the Docker
    46  	// endpoint ID. This method is only possible if the endpoint was
    47  	// created via the cilium-docker plugin and the container is backed by
    48  	// the libnetwork abstraction.
    49  	DockerEndpointPrefix PrefixType = "docker-endpoint"
    50  
    51  	// ContainerNamePrefix is used to address the endpoint via the
    52  	// container's name. This addressing mechanism depends on the container
    53  	// runtime. Only the primary container that the networking scope can be
    54  	// used to address an endpoint.
    55  	ContainerNamePrefix PrefixType = "container-name"
    56  
    57  	// PodNamePrefix is used to address an endpoint via the Kubernetes pod
    58  	// name. This addressing only works if the endpoint represents as
    59  	// Kubernetes pod.
    60  	PodNamePrefix PrefixType = "pod-name"
    61  
    62  	// IPv4Prefix is used to address an endpoint via the endpoint's IPv4
    63  	// address.
    64  	IPv4Prefix PrefixType = "ipv4"
    65  
    66  	// IPv6Prefix is the prefix used to refer to an endpoint via IPv6 address
    67  	IPv6Prefix PrefixType = "ipv6"
    68  )
    69  
    70  // NewCiliumID returns a new endpoint identifier of type CiliumLocalIdPrefix
    71  func NewCiliumID(id int64) string {
    72  	return fmt.Sprintf("%s:%d", CiliumLocalIdPrefix, id)
    73  }
    74  
    75  // NewID returns a new endpoint identifier
    76  func NewID(prefix PrefixType, id string) string {
    77  	return string(prefix) + ":" + id
    78  }
    79  
    80  // NewIPPrefixID returns an identifier based on the IP address specified
    81  func NewIPPrefixID(ip net.IP) string {
    82  	if ip.To4() != nil {
    83  		return NewID(IPv4Prefix, ip.String())
    84  	}
    85  
    86  	return NewID(IPv6Prefix, ip.String())
    87  }
    88  
    89  // splitID splits ID into prefix and id. No validation is performed on prefix.
    90  func splitID(id string) (PrefixType, string) {
    91  	if idx := strings.Index(id, ":"); idx > -1 {
    92  		return PrefixType(id[:idx]), id[idx+1:]
    93  	}
    94  
    95  	// default prefix
    96  	return CiliumLocalIdPrefix, id
    97  }
    98  
    99  // ParseCiliumID parses id as cilium endpoint id and returns numeric portion.
   100  func ParseCiliumID(id string) (int64, error) {
   101  	prefix, id := splitID(id)
   102  	if prefix != CiliumLocalIdPrefix {
   103  		return 0, fmt.Errorf("not a cilium identifier")
   104  	}
   105  	n, err := strconv.ParseInt(id, 0, 64)
   106  	if err != nil {
   107  		return 0, fmt.Errorf("invalid numeric cilium id: %s", err)
   108  	}
   109  	return n, nil
   110  }
   111  
   112  // Parse parses a string as an endpoint identified consists of an optional
   113  // prefix [prefix:] followed by the identifier.
   114  func Parse(id string) (PrefixType, string, error) {
   115  	prefix, id := splitID(id)
   116  	switch prefix {
   117  	case CiliumLocalIdPrefix, CiliumGlobalIdPrefix, ContainerIdPrefix, DockerEndpointPrefix, ContainerNamePrefix, PodNamePrefix, IPv4Prefix, IPv6Prefix:
   118  		return prefix, id, nil
   119  	}
   120  
   121  	return "", "", fmt.Errorf("unknown endpoint ID prefix \"%s\"", prefix)
   122  }