github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/operators/kubenameresolver/kubenameresolver.go (about)

     1  // Copyright 2023 The Inspektor Gadget authors
     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 kubenameresolver provides an operator that enriches events by looking
    16  // up the pod name and namespace and enriches it with its ip information. It is
    17  // currently used by the following gadgets:
    18  // - trace network
    19  package kubenameresolver
    20  
    21  import (
    22  	"fmt"
    23  	"strings"
    24  
    25  	"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets"
    26  	"github.com/inspektor-gadget/inspektor-gadget/pkg/operators"
    27  	"github.com/inspektor-gadget/inspektor-gadget/pkg/operators/common"
    28  	"github.com/inspektor-gadget/inspektor-gadget/pkg/operators/kubemanager"
    29  	"github.com/inspektor-gadget/inspektor-gadget/pkg/params"
    30  )
    31  
    32  const (
    33  	OperatorName = "KubeNameResolver"
    34  )
    35  
    36  type KubeNameResolverInterface interface {
    37  	SetLocalPodDetails(owner, hostIP, podIP string, labels map[string]string)
    38  }
    39  
    40  type KubeNameResolver struct{}
    41  
    42  func (k *KubeNameResolver) Name() string {
    43  	return OperatorName
    44  }
    45  
    46  func (k *KubeNameResolver) Description() string {
    47  	return "KubeNameResolver resolves pod name/namespace to IP addresses"
    48  }
    49  
    50  func (k *KubeNameResolver) GlobalParamDescs() params.ParamDescs {
    51  	return nil
    52  }
    53  
    54  func (k *KubeNameResolver) ParamDescs() params.ParamDescs {
    55  	return nil
    56  }
    57  
    58  func (k *KubeNameResolver) Dependencies() []string {
    59  	return []string{kubemanager.OperatorName}
    60  }
    61  
    62  func (k *KubeNameResolver) CanOperateOn(gadget gadgets.GadgetDesc) bool {
    63  	km := kubemanager.KubeManager{}
    64  	if !km.CanOperateOn(gadget) {
    65  		return false
    66  	}
    67  	_, hasNameResolverInterface := gadget.EventPrototype().(KubeNameResolverInterface)
    68  	return hasNameResolverInterface
    69  }
    70  
    71  func (k *KubeNameResolver) Init(params *params.Params) error {
    72  	return nil
    73  }
    74  
    75  func (k *KubeNameResolver) Close() error {
    76  	return nil
    77  }
    78  
    79  func (k *KubeNameResolver) Instantiate(gadgetCtx operators.GadgetContext, gadgetInstance any, params *params.Params) (operators.OperatorInstance, error) {
    80  	k8sInventory, err := common.GetK8sInventoryCache()
    81  	if err != nil {
    82  		return nil, fmt.Errorf("creating k8s inventory cache: %w", err)
    83  	}
    84  
    85  	return &KubeNameResolverInstance{
    86  		gadgetCtx:      gadgetCtx,
    87  		k8sInventory:   k8sInventory,
    88  		gadgetInstance: gadgetInstance,
    89  	}, nil
    90  }
    91  
    92  type KubeNameResolverInstance struct {
    93  	gadgetCtx      operators.GadgetContext
    94  	k8sInventory   common.K8sInventoryCache
    95  	gadgetInstance any
    96  }
    97  
    98  func (m *KubeNameResolverInstance) Name() string {
    99  	return "KubeNameResolverInstance"
   100  }
   101  
   102  func (m *KubeNameResolverInstance) PreGadgetRun() error {
   103  	m.k8sInventory.Start()
   104  	return nil
   105  }
   106  
   107  func (m *KubeNameResolverInstance) PostGadgetRun() error {
   108  	m.k8sInventory.Stop()
   109  	return nil
   110  }
   111  
   112  func (m *KubeNameResolverInstance) enrich(ev any) {
   113  	kubeNameResolver, _ := ev.(KubeNameResolverInterface)
   114  	containerInfo, _ := ev.(operators.ContainerInfoGetters)
   115  
   116  	pod := m.k8sInventory.GetPodByName(containerInfo.GetNamespace(), containerInfo.GetPod())
   117  	if pod != nil {
   118  		owner := ""
   119  		// When the pod belongs to Deployment, ReplicaSet or DaemonSet, find the
   120  		// shorter name without the random suffix. That will be used to
   121  		// generate the network policy name.
   122  		if pod.OwnerReferences != nil {
   123  			nameItems := strings.Split(pod.Name, "-")
   124  			if len(nameItems) > 2 {
   125  				owner = strings.Join(nameItems[:len(nameItems)-2], "-")
   126  			}
   127  		}
   128  		kubeNameResolver.SetLocalPodDetails(owner, pod.Status.HostIP, pod.Status.PodIP, pod.Labels)
   129  	}
   130  }
   131  
   132  func (m *KubeNameResolverInstance) EnrichEvent(ev any) error {
   133  	m.enrich(ev)
   134  	return nil
   135  }
   136  
   137  func init() {
   138  	operators.Register(&KubeNameResolver{})
   139  }