github.com/fafucoder/cilium@v1.6.11/pkg/service/store.go (about)

     1  // Copyright 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 service
    16  
    17  import (
    18  	"encoding/json"
    19  	"path"
    20  
    21  	"github.com/cilium/cilium/pkg/kvstore"
    22  	"github.com/cilium/cilium/pkg/kvstore/store"
    23  	"github.com/cilium/cilium/pkg/loadbalancer"
    24  )
    25  
    26  var (
    27  	// ServiceStorePrefix is the kvstore prefix of the shared store
    28  	//
    29  	// WARNING - STABLE API: Changing the structure or values of this will
    30  	// break backwards compatibility
    31  	ServiceStorePrefix = path.Join(kvstore.BaseKeyPrefix, "state", "services", "v1")
    32  )
    33  
    34  // PortConfiguration is the L4 port configuration of a frontend or backend. The
    35  // map is indexed by the name of the port and the value constains the L4 port
    36  // and protocol.
    37  type PortConfiguration map[string]*loadbalancer.L4Addr
    38  
    39  // DeepEquals returns true if both PortConfigurations are identical
    40  func (p PortConfiguration) DeepEquals(o PortConfiguration) bool {
    41  	if len(p) != len(o) {
    42  		return false
    43  	}
    44  
    45  	for portName1, port1 := range p {
    46  		port2, ok := o[portName1]
    47  
    48  		if !ok || !port1.Equals(port2) {
    49  			return false
    50  		}
    51  	}
    52  
    53  	return true
    54  }
    55  
    56  // ClusterService is the definition of a service in a cluster
    57  //
    58  // WARNING - STABLE API: Any change to this structure must be done in a
    59  // backwards compatible way.
    60  //
    61  // +k8s:deepcopy-gen=true
    62  type ClusterService struct {
    63  	// Cluster is the cluster name the service is configured in
    64  	Cluster string `json:"cluster"`
    65  
    66  	// Namespace is the cluster namespace the service is configured in
    67  	Namespace string `json:"namespace"`
    68  
    69  	// Name is the name of the service. It must be unique within the
    70  	// namespace of the cluster
    71  	Name string `json:"name"`
    72  
    73  	// Frontends is a map indexed by the frontend IP address
    74  	Frontends map[string]PortConfiguration `json:"frontends"`
    75  
    76  	// Backends is is map indexed by the backend IP address
    77  	Backends map[string]PortConfiguration `json:"backends"`
    78  
    79  	// Labels are the labels of the service
    80  	Labels map[string]string `json:"labels"`
    81  
    82  	// Selector is the label selector used to select backends
    83  	Selector map[string]string `json:"selector"`
    84  }
    85  
    86  func (s *ClusterService) String() string {
    87  	return s.Cluster + "/" + s.Namespace + "/" + s.Name
    88  }
    89  
    90  // NamespaceServiceName returns the namespace and service name
    91  func (s *ClusterService) NamespaceServiceName() string {
    92  	return s.Namespace + "/" + s.Name
    93  }
    94  
    95  // GetKeyName returns the kvstore key to be used for the global service
    96  func (s *ClusterService) GetKeyName() string {
    97  	// WARNING - STABLE API: Changing the structure of the key may break
    98  	// backwards compatibility
    99  	return path.Join(s.Cluster, s.Namespace, s.Name)
   100  }
   101  
   102  // DeepKeyCopy creates a deep copy of the LocalKey
   103  func (s *ClusterService) DeepKeyCopy() store.LocalKey {
   104  	return s.DeepCopy()
   105  }
   106  
   107  // Marshal returns the global service object as JSON byte slice
   108  func (s *ClusterService) Marshal() ([]byte, error) {
   109  	return json.Marshal(s)
   110  }
   111  
   112  // Unmarshal parses the JSON byte slice and updates the global service receiver
   113  func (s *ClusterService) Unmarshal(data []byte) error {
   114  	newService := NewClusterService("", "")
   115  
   116  	if err := json.Unmarshal(data, &newService); err != nil {
   117  		return err
   118  	}
   119  
   120  	*s = newService
   121  
   122  	return nil
   123  }
   124  
   125  // NewClusterService returns a new cluster service definition
   126  func NewClusterService(name, namespace string) ClusterService {
   127  	return ClusterService{
   128  		Name:      name,
   129  		Namespace: namespace,
   130  		Frontends: map[string]PortConfiguration{},
   131  		Backends:  map[string]PortConfiguration{},
   132  		Labels:    map[string]string{},
   133  		Selector:  map[string]string{},
   134  	}
   135  }