github.com/cilium/cilium@v1.16.2/pkg/hubble/parser/agent/parser.go (about)

     1  // SPDX-License-Identifier: Apache-2.0
     2  // Copyright Authors of Hubble
     3  
     4  package agent
     5  
     6  import (
     7  	"encoding/json"
     8  	"fmt"
     9  
    10  	"google.golang.org/protobuf/types/known/timestamppb"
    11  	"google.golang.org/protobuf/types/known/wrapperspb"
    12  
    13  	flowpb "github.com/cilium/cilium/api/v1/flow"
    14  	monitorAPI "github.com/cilium/cilium/pkg/monitor/api"
    15  	"github.com/cilium/cilium/pkg/time"
    16  )
    17  
    18  func notifyTimeNotificationToProto(typ flowpb.AgentEventType, n monitorAPI.TimeNotification) *flowpb.AgentEvent {
    19  	var ts *timestamppb.Timestamp
    20  	if goTime, err := time.Parse(time.RFC3339Nano, n.Time); err == nil {
    21  		ts = timestamppb.New(goTime)
    22  	}
    23  	return &flowpb.AgentEvent{
    24  		Type: typ,
    25  		Notification: &flowpb.AgentEvent_AgentStart{
    26  			AgentStart: &flowpb.TimeNotification{
    27  				Time: ts,
    28  			},
    29  		},
    30  	}
    31  }
    32  
    33  func notifyPolicyNotificationToProto(typ flowpb.AgentEventType, n monitorAPI.PolicyUpdateNotification) *flowpb.AgentEvent {
    34  	return &flowpb.AgentEvent{
    35  		Type: typ,
    36  		Notification: &flowpb.AgentEvent_PolicyUpdate{
    37  			PolicyUpdate: &flowpb.PolicyUpdateNotification{
    38  				RuleCount: int64(n.RuleCount),
    39  				Labels:    n.Labels,
    40  				Revision:  n.Revision,
    41  			},
    42  		},
    43  	}
    44  }
    45  
    46  func notifyEndpointRegenNotificationToProto(typ flowpb.AgentEventType, n monitorAPI.EndpointRegenNotification) *flowpb.AgentEvent {
    47  	return &flowpb.AgentEvent{
    48  		Type: typ,
    49  		Notification: &flowpb.AgentEvent_EndpointRegenerate{
    50  			EndpointRegenerate: &flowpb.EndpointRegenNotification{
    51  				Id:     n.ID,
    52  				Labels: n.Labels,
    53  				Error:  n.Error,
    54  			},
    55  		},
    56  	}
    57  }
    58  
    59  func notifyEndpointUpdateNotificationToProto(typ flowpb.AgentEventType, n monitorAPI.EndpointNotification) *flowpb.AgentEvent {
    60  	return &flowpb.AgentEvent{
    61  		Type: typ,
    62  		Notification: &flowpb.AgentEvent_EndpointUpdate{
    63  			EndpointUpdate: &flowpb.EndpointUpdateNotification{
    64  				Id:        n.ID,
    65  				Labels:    n.Labels,
    66  				Error:     n.Error,
    67  				PodName:   n.PodName,
    68  				Namespace: n.Namespace,
    69  			},
    70  		},
    71  	}
    72  }
    73  func notifyIPCacheNotificationToProto(typ flowpb.AgentEventType, n monitorAPI.IPCacheNotification) *flowpb.AgentEvent {
    74  	var oldIdentity *wrapperspb.UInt32Value
    75  	if n.OldIdentity != nil {
    76  		oldIdentity = &wrapperspb.UInt32Value{
    77  			Value: *n.OldIdentity,
    78  		}
    79  	}
    80  	var hostIPString string
    81  	if n.HostIP != nil {
    82  		hostIPString = n.HostIP.String()
    83  	}
    84  	var oldHostIPString string
    85  	if n.OldHostIP != nil {
    86  		oldHostIPString = n.OldHostIP.String()
    87  	}
    88  	return &flowpb.AgentEvent{
    89  		Type: typ,
    90  		Notification: &flowpb.AgentEvent_IpcacheUpdate{
    91  			IpcacheUpdate: &flowpb.IPCacheNotification{
    92  				Cidr:        n.CIDR,
    93  				Identity:    n.Identity,
    94  				OldIdentity: oldIdentity,
    95  				HostIp:      hostIPString,
    96  				OldHostIp:   oldHostIPString,
    97  				EncryptKey:  uint32(n.EncryptKey),
    98  				Namespace:   n.Namespace,
    99  				PodName:     n.PodName,
   100  			},
   101  		},
   102  	}
   103  }
   104  
   105  func notifyServiceUpsertedToProto(typ flowpb.AgentEventType, n monitorAPI.ServiceUpsertNotification) *flowpb.AgentEvent {
   106  	feAddr := &flowpb.ServiceUpsertNotificationAddr{
   107  		Ip:   n.Frontend.IP.String(),
   108  		Port: uint32(n.Frontend.Port),
   109  	}
   110  	beAddrs := make([]*flowpb.ServiceUpsertNotificationAddr, 0, len(n.Backends))
   111  	for _, be := range n.Backends {
   112  		var ipStr string
   113  		if be.IP != nil {
   114  			ipStr = be.IP.String()
   115  		}
   116  		beAddrs = append(beAddrs, &flowpb.ServiceUpsertNotificationAddr{
   117  			Ip:   ipStr,
   118  			Port: uint32(be.Port),
   119  		})
   120  	}
   121  	return &flowpb.AgentEvent{
   122  		Type: typ,
   123  		Notification: &flowpb.AgentEvent_ServiceUpsert{
   124  			ServiceUpsert: &flowpb.ServiceUpsertNotification{
   125  				Id:               n.ID,
   126  				FrontendAddress:  feAddr,
   127  				BackendAddresses: beAddrs,
   128  				Type:             n.Type,
   129  				TrafficPolicy:    n.ExtTrafficPolicy,
   130  				ExtTrafficPolicy: n.ExtTrafficPolicy,
   131  				IntTrafficPolicy: n.IntTrafficPolicy,
   132  				Name:             n.Name,
   133  				Namespace:        n.Namespace,
   134  			},
   135  		},
   136  	}
   137  }
   138  
   139  func notifyServiceDeletedToProto(typ flowpb.AgentEventType, n monitorAPI.ServiceDeleteNotification) *flowpb.AgentEvent {
   140  	return &flowpb.AgentEvent{
   141  		Type: typ,
   142  		Notification: &flowpb.AgentEvent_ServiceDelete{
   143  			ServiceDelete: &flowpb.ServiceDeleteNotification{
   144  				Id: n.ID,
   145  			},
   146  		},
   147  	}
   148  }
   149  
   150  func notifyUnknownToProto(typ flowpb.AgentEventType, msg monitorAPI.AgentNotifyMessage) *flowpb.AgentEvent {
   151  	n, _ := json.Marshal(msg.Notification)
   152  	return &flowpb.AgentEvent{
   153  		Type: typ,
   154  		Notification: &flowpb.AgentEvent_Unknown{
   155  			Unknown: &flowpb.AgentEventUnknown{
   156  				Type:         fmt.Sprintf("%d", msg.Type),
   157  				Notification: string(n),
   158  			},
   159  		},
   160  	}
   161  }
   162  
   163  func NotifyMessageToProto(msg monitorAPI.AgentNotifyMessage) *flowpb.AgentEvent {
   164  	switch n := msg.Notification.(type) {
   165  	case monitorAPI.TimeNotification:
   166  		if msg.Type == monitorAPI.AgentNotifyStart {
   167  			return notifyTimeNotificationToProto(flowpb.AgentEventType_AGENT_STARTED, n)
   168  		}
   169  	case monitorAPI.PolicyUpdateNotification:
   170  		if msg.Type == monitorAPI.AgentNotifyPolicyUpdated {
   171  			return notifyPolicyNotificationToProto(flowpb.AgentEventType_POLICY_UPDATED, n)
   172  		} else if msg.Type == monitorAPI.AgentNotifyPolicyDeleted {
   173  			return notifyPolicyNotificationToProto(flowpb.AgentEventType_POLICY_DELETED, n)
   174  		}
   175  	case monitorAPI.EndpointRegenNotification:
   176  		if msg.Type == monitorAPI.AgentNotifyEndpointRegenerateSuccess {
   177  			return notifyEndpointRegenNotificationToProto(flowpb.AgentEventType_ENDPOINT_REGENERATE_SUCCESS, n)
   178  		} else if msg.Type == monitorAPI.AgentNotifyEndpointRegenerateFail {
   179  			return notifyEndpointRegenNotificationToProto(flowpb.AgentEventType_ENDPOINT_REGENERATE_FAILURE, n)
   180  		}
   181  	case monitorAPI.EndpointNotification:
   182  		if msg.Type == monitorAPI.AgentNotifyEndpointCreated {
   183  			return notifyEndpointUpdateNotificationToProto(flowpb.AgentEventType_ENDPOINT_CREATED, n)
   184  		} else if msg.Type == monitorAPI.AgentNotifyEndpointDeleted {
   185  			return notifyEndpointUpdateNotificationToProto(flowpb.AgentEventType_ENDPOINT_DELETED, n)
   186  		}
   187  	case monitorAPI.IPCacheNotification:
   188  		if msg.Type == monitorAPI.AgentNotifyIPCacheUpserted {
   189  			return notifyIPCacheNotificationToProto(flowpb.AgentEventType_IPCACHE_UPSERTED, n)
   190  		} else if msg.Type == monitorAPI.AgentNotifyIPCacheDeleted {
   191  			return notifyIPCacheNotificationToProto(flowpb.AgentEventType_IPCACHE_DELETED, n)
   192  		}
   193  	case monitorAPI.ServiceUpsertNotification:
   194  		if msg.Type == monitorAPI.AgentNotifyServiceUpserted {
   195  			return notifyServiceUpsertedToProto(flowpb.AgentEventType_SERVICE_UPSERTED, n)
   196  		}
   197  	case monitorAPI.ServiceDeleteNotification:
   198  		if msg.Type == monitorAPI.AgentNotifyServiceDeleted {
   199  			return notifyServiceDeletedToProto(flowpb.AgentEventType_SERVICE_DELETED, n)
   200  		}
   201  	}
   202  	return notifyUnknownToProto(flowpb.AgentEventType_AGENT_EVENT_UNKNOWN, msg)
   203  }