...

Source file src/github.com/datawire/ambassador/v2/pkg/agent/corestore.go

Documentation: github.com/datawire/ambassador/v2/pkg/agent

     1  package agent
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/datawire/ambassador/v2/pkg/kates"
     7  	v1 "k8s.io/api/core/v1"
     8  )
     9  
    10  const (
    11  	NamespaceKubeSystem = "kube-system"
    12  )
    13  
    14  // CoreSnapshot reflects the current state of the kates accumulators for the given
    15  // resource types.
    16  type CoreSnapshot struct {
    17  	Pods        []*kates.Pod
    18  	ConfigMaps  []*kates.ConfigMap
    19  	Deployments []*kates.Deployment
    20  	Endpoints   []*kates.Endpoints
    21  }
    22  
    23  // coreStore is used to store core k8s resources that are not handled by default
    24  // by Ambassador's snapshot.
    25  type coreStore struct {
    26  	podStore        *podStore
    27  	configMapStore  *configMapStore
    28  	deploymentStore *deploymentStore
    29  	endpointStore   *endpointStore
    30  }
    31  
    32  type configMapStore struct {
    33  	sotw map[string]*kates.ConfigMap
    34  }
    35  
    36  type deploymentStore struct {
    37  	sotw map[string]*kates.Deployment
    38  }
    39  
    40  type podStore struct {
    41  	sotw map[string]*kates.Pod
    42  }
    43  
    44  type endpointStore struct {
    45  	sotw map[string]*kates.Endpoints
    46  }
    47  
    48  // NewCoreStore will create a new coreStore with the given coreSnapshot.
    49  func NewCoreStore(snapshot *CoreSnapshot) *coreStore {
    50  	return &coreStore{
    51  		podStore:        NewPodStore(snapshot.Pods),
    52  		configMapStore:  NewConfigMapStore(snapshot.ConfigMaps),
    53  		deploymentStore: NewDeploymentStore(snapshot.Deployments),
    54  		endpointStore:   NewEndpointsStore(snapshot.Endpoints),
    55  	}
    56  }
    57  
    58  // NewPodStore will create a new podStore filtering out undesired resources.
    59  func NewPodStore(pods []*kates.Pod) *podStore {
    60  	sotw := make(map[string]*kates.Pod)
    61  	store := &podStore{sotw: sotw}
    62  
    63  	for _, pod := range pods {
    64  		if allowedNamespace(pod.GetNamespace()) && pod.Status.Phase != v1.PodSucceeded {
    65  			key := fmt.Sprintf("%s.%s", pod.GetName(), pod.GetNamespace())
    66  			store.sotw[key] = pod
    67  		}
    68  	}
    69  	return store
    70  }
    71  
    72  // NewConfigMapStore will create a new configMapStore filtering out undesired resources.
    73  func NewConfigMapStore(cms []*kates.ConfigMap) *configMapStore {
    74  	sotw := make(map[string]*kates.ConfigMap)
    75  	store := &configMapStore{sotw: sotw}
    76  
    77  	for _, cm := range cms {
    78  		if allowedNamespace(cm.GetNamespace()) {
    79  			key := fmt.Sprintf("%s.%s", cm.GetName(), cm.GetNamespace())
    80  			store.sotw[key] = cm
    81  		}
    82  	}
    83  	return store
    84  }
    85  
    86  // NewDeploymentStore will create a new deploymentStore filtering out undesired resources.
    87  func NewDeploymentStore(ds []*kates.Deployment) *deploymentStore {
    88  	sotw := make(map[string]*kates.Deployment)
    89  	store := &deploymentStore{sotw: sotw}
    90  
    91  	for _, d := range ds {
    92  		if allowedNamespace(d.GetNamespace()) {
    93  			key := fmt.Sprintf("%s.%s", d.GetName(), d.GetNamespace())
    94  			store.sotw[key] = d
    95  		}
    96  	}
    97  	return store
    98  }
    99  
   100  // NewEndpointsStore will create a new endpointStore filtering out undesired resources.
   101  func NewEndpointsStore(es []*kates.Endpoints) *endpointStore {
   102  	sotw := make(map[string]*kates.Endpoints)
   103  	store := &endpointStore{sotw: sotw}
   104  
   105  	for _, ep := range es {
   106  		if allowedNamespace(ep.GetNamespace()) {
   107  			key := fmt.Sprintf("%s.%s", ep.GetName(), ep.GetNamespace())
   108  			store.sotw[key] = ep
   109  		}
   110  	}
   111  	return store
   112  }
   113  
   114  // StateOfWorld returns the current state of all pods from the allowed namespaces.
   115  func (store *podStore) StateOfWorld() []*kates.Pod {
   116  	pods := []*kates.Pod{}
   117  	for _, v := range store.sotw {
   118  		pods = append(pods, v)
   119  	}
   120  	return pods
   121  }
   122  
   123  func (store *endpointStore) StateOfWorld() []*kates.Endpoints {
   124  	eps := []*kates.Endpoints{}
   125  	for _, ep := range store.sotw {
   126  		eps = append(eps, ep)
   127  	}
   128  	return eps
   129  }
   130  
   131  // StateOfWorld returns the current state of all configmaps from the allowed namespaces.
   132  func (store *configMapStore) StateOfWorld() []*kates.ConfigMap {
   133  	configs := []*kates.ConfigMap{}
   134  	for _, v := range store.sotw {
   135  		configs = append(configs, v)
   136  	}
   137  	return configs
   138  }
   139  
   140  // StateOfWorld returns the current state of all deployments from the allowed namespaces.
   141  func (store *deploymentStore) StateOfWorld() []*kates.Deployment {
   142  	deployments := []*kates.Deployment{}
   143  	for _, v := range store.sotw {
   144  		deployments = append(deployments, v)
   145  	}
   146  	return deployments
   147  }
   148  
   149  // allowedNamespace will check if resources from the given namespace
   150  // should be reported to Ambassador Cloud.
   151  func allowedNamespace(namespace string) bool {
   152  	if namespace != NamespaceKubeSystem {
   153  		return true
   154  	}
   155  	return false
   156  }
   157  

View as plain text