github.com/aporeto-inc/trireme-lib@v10.358.0+incompatible/monitor/internal/kubernetes/kubernetes.go (about)

     1  // +build !windows
     2  
     3  package kubernetesmonitor
     4  
     5  import (
     6  	"context"
     7  	"time"
     8  
     9  	"go.uber.org/zap"
    10  	api "k8s.io/api/core/v1"
    11  	"k8s.io/apimachinery/pkg/labels"
    12  	kubecache "k8s.io/client-go/tools/cache"
    13  )
    14  
    15  // KubernetesPodNameIdentifier is the label used by Docker for the K8S pod name.
    16  const KubernetesPodNameIdentifier = "@usr:io.kubernetes.pod.name"
    17  
    18  // KubernetesPodNamespaceIdentifier is the label used by Docker for the K8S namespace.
    19  const KubernetesPodNamespaceIdentifier = "@usr:io.kubernetes.pod.namespace"
    20  
    21  // KubernetesContainerNameIdentifier is the label used by Docker for the K8S container name.
    22  const KubernetesContainerNameIdentifier = "@usr:io.kubernetes.container.name"
    23  
    24  // KubernetesInfraContainerName is the name of the infra POD.
    25  const KubernetesInfraContainerName = "POD"
    26  
    27  // UpstreamNameIdentifier is the identifier used to identify the nane on the resulting PU
    28  const UpstreamNameIdentifier = "k8s:name"
    29  
    30  // UpstreamNamespaceIdentifier is the identifier used to identify the nanespace on the resulting PU
    31  const UpstreamNamespaceIdentifier = "k8s:namespace"
    32  
    33  func (m *KubernetesMonitor) addPod(addedPod *api.Pod) error {
    34  	zap.L().Debug("pod added event", zap.String("name", addedPod.GetName()), zap.String("namespace", addedPod.GetNamespace()))
    35  
    36  	// This event is not needed as the trigger is the  DockerMonitor event
    37  	// The pod obejct is cached in order to reuse it and avoid an API request possibly laster on
    38  
    39  	return nil
    40  }
    41  
    42  func (m *KubernetesMonitor) deletePod(deletedPod *api.Pod) error {
    43  	zap.L().Debug("pod deleted event", zap.String("name", deletedPod.GetName()), zap.String("namespace", deletedPod.GetNamespace()))
    44  
    45  	return nil
    46  }
    47  
    48  func (m *KubernetesMonitor) updatePod(oldPod, updatedPod *api.Pod) error {
    49  	zap.L().Debug("pod modified event", zap.String("name", updatedPod.GetName()), zap.String("namespace", updatedPod.GetNamespace()))
    50  
    51  	if !isPolicyUpdateNeeded(oldPod, updatedPod) {
    52  		zap.L().Debug("no modified labels for Pod", zap.String("name", updatedPod.GetName()), zap.String("namespace", updatedPod.GetNamespace()))
    53  		return nil
    54  	}
    55  
    56  	// This event requires sending the Runtime upstream again.
    57  	// TODO: Use propagated context
    58  	return m.RefreshPUs(context.TODO(), updatedPod)
    59  }
    60  
    61  func (m *KubernetesMonitor) getPod(podNamespace, podName string) (*api.Pod, error) {
    62  	zap.L().Debug("no pod cached, querying Kubernetes API")
    63  
    64  	// TODO: Use cached Kube Store (from a shared informer)
    65  	return m.Pod(podName, podNamespace)
    66  }
    67  
    68  func isPolicyUpdateNeeded(oldPod, newPod *api.Pod) bool {
    69  	if !(oldPod.Status.PodIP == newPod.Status.PodIP) {
    70  		return true
    71  	}
    72  	if !labels.Equals(oldPod.GetLabels(), newPod.GetLabels()) {
    73  		return true
    74  	}
    75  	return false
    76  }
    77  
    78  // hasSynced sends an event on the Sync chan when the attachedController finished syncing.
    79  func hasSynced(sync chan struct{}, controller kubecache.Controller) {
    80  	for {
    81  		if controller.HasSynced() {
    82  			sync <- struct{}{}
    83  			return
    84  		}
    85  		<-time.After(100 * time.Millisecond)
    86  	}
    87  }