github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/operators/kubeipresolver/kubeipresolver.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 kubeipresolver provides an operator that enriches events by looking
    16  // up IP addresses in Kubernetes resources such as pods and services.
    17  package kubeipresolver
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"github.com/inspektor-gadget/inspektor-gadget/pkg/gadgets"
    23  	"github.com/inspektor-gadget/inspektor-gadget/pkg/operators"
    24  	"github.com/inspektor-gadget/inspektor-gadget/pkg/operators/common"
    25  	"github.com/inspektor-gadget/inspektor-gadget/pkg/params"
    26  	"github.com/inspektor-gadget/inspektor-gadget/pkg/types"
    27  )
    28  
    29  const (
    30  	OperatorName = "KubeIPResolver"
    31  )
    32  
    33  type KubeIPResolverInterface interface {
    34  	GetEndpoints() []*types.L3Endpoint
    35  }
    36  
    37  type KubeIPResolver struct{}
    38  
    39  func (k *KubeIPResolver) Name() string {
    40  	return OperatorName
    41  }
    42  
    43  func (k *KubeIPResolver) Description() string {
    44  	return "KubeIPResolver resolves IP addresses to pod and service names"
    45  }
    46  
    47  func (k *KubeIPResolver) GlobalParamDescs() params.ParamDescs {
    48  	return nil
    49  }
    50  
    51  func (k *KubeIPResolver) ParamDescs() params.ParamDescs {
    52  	return nil
    53  }
    54  
    55  func (k *KubeIPResolver) Dependencies() []string {
    56  	return nil
    57  }
    58  
    59  func (k *KubeIPResolver) CanOperateOn(gadget gadgets.GadgetDesc) bool {
    60  	_, hasIPResolverInterface := gadget.EventPrototype().(KubeIPResolverInterface)
    61  	return hasIPResolverInterface
    62  }
    63  
    64  func (k *KubeIPResolver) Init(params *params.Params) error {
    65  	return nil
    66  }
    67  
    68  func (k *KubeIPResolver) Close() error {
    69  	return nil
    70  }
    71  
    72  func (k *KubeIPResolver) Instantiate(gadgetCtx operators.GadgetContext, gadgetInstance any, params *params.Params) (operators.OperatorInstance, error) {
    73  	k8sInventory, err := common.GetK8sInventoryCache()
    74  	if err != nil {
    75  		return nil, fmt.Errorf("creating k8s inventory cache: %w", err)
    76  	}
    77  
    78  	return &KubeIPResolverInstance{
    79  		gadgetCtx:      gadgetCtx,
    80  		k8sInventory:   k8sInventory,
    81  		gadgetInstance: gadgetInstance,
    82  	}, nil
    83  }
    84  
    85  type KubeIPResolverInstance struct {
    86  	gadgetCtx      operators.GadgetContext
    87  	k8sInventory   common.K8sInventoryCache
    88  	gadgetInstance any
    89  }
    90  
    91  func (m *KubeIPResolverInstance) Name() string {
    92  	return "KubeIPResolverInstance"
    93  }
    94  
    95  func (m *KubeIPResolverInstance) PreGadgetRun() error {
    96  	m.k8sInventory.Start()
    97  	return nil
    98  }
    99  
   100  func (m *KubeIPResolverInstance) PostGadgetRun() error {
   101  	m.k8sInventory.Stop()
   102  	return nil
   103  }
   104  
   105  func (m *KubeIPResolverInstance) enrich(ev any) {
   106  	endpoints := ev.(KubeIPResolverInterface).GetEndpoints()
   107  	for _, endpoint := range endpoints {
   108  		endpoint.Kind = types.EndpointKindRaw
   109  
   110  		pod := m.k8sInventory.GetPodByIp(endpoint.Addr)
   111  		if pod != nil {
   112  			if pod.Spec.HostNetwork {
   113  				continue
   114  			}
   115  			endpoint.Kind = types.EndpointKindPod
   116  			endpoint.Name = pod.Name
   117  			endpoint.Namespace = pod.Namespace
   118  			endpoint.PodLabels = pod.Labels
   119  			continue
   120  		}
   121  
   122  		svc := m.k8sInventory.GetSvcByIp(endpoint.Addr)
   123  		if svc != nil {
   124  			endpoint.Kind = types.EndpointKindService
   125  			endpoint.Name = svc.Name
   126  			endpoint.Namespace = svc.Namespace
   127  			endpoint.PodLabels = svc.Labels
   128  		}
   129  	}
   130  }
   131  
   132  func (m *KubeIPResolverInstance) EnrichEvent(ev any) error {
   133  	m.enrich(ev)
   134  	return nil
   135  }
   136  
   137  func init() {
   138  	operators.Register(&KubeIPResolver{})
   139  }