func()

in pkg/plugins/runtime/k8s/controllers/outbound_converter.go [40:116]


func (p *PodConverter) OutboundInterfacesFor(
	ctx context.Context,
	pod *kube_core.Pod,
	others []*mesh_k8s.Dataplane,
	reachableServices []string,
) ([]*mesh_proto.Dataplane_Networking_Outbound, error) {
	var outbounds []*mesh_proto.Dataplane_Networking_Outbound

	reachableServicesMap := map[string]bool{}
	for _, service := range reachableServices {
		reachableServicesMap[service] = true
	}

	var dataplanes []*core_mesh.DataplaneResource
	for _, other := range others {
		dp := core_mesh.NewDataplaneResource()
		if err := p.ResourceConverter.ToCoreResource(other, dp); err != nil {
			converterLog.Error(err, "failed to parse Dataplane", "dataplane", other.Spec)
			continue // one invalid Dataplane definition should not break the entire mesh
		}
		dataplanes = append(dataplanes, dp)
	}

	endpoints := endpointsByService(dataplanes)
	for _, serviceTag := range endpoints.Services() {
		service, port, err := k8sService(ctx, serviceTag, p.ServiceGetter)
		if err != nil {
			converterLog.Error(err, "could not get K8S Service for service tag")
			continue // one invalid Dataplane definition should not break the entire mesh
		}
		if len(reachableServices) > 0 && !reachableServicesMap[serviceTag] {
			continue // ignore generating outbound if reachable services are defined and this one is not on the list
		}

		// Do not generate outbounds for service-less
		if isServiceLess(port) {
			continue
		}

		// Do not generate hostnames for ExternalName Service
		if isExternalNameService(service) {
			converterLog.V(1).Info(
				"ignoring outbound generation for unsupported ExternalName Service",
				"name", service.GetName(),
				"namespace", service.GetNamespace(),
			)
			continue
		}

		if isHeadlessService(service) {
			// Generate outbound listeners for every endpoint of services.
			for _, endpoint := range endpoints[serviceTag] {
				if endpoint.Address == pod.Status.PodIP {
					continue // ignore generating outbound for itself, otherwise we've got a conflict with inbound
				}
				outbounds = append(outbounds, &mesh_proto.Dataplane_Networking_Outbound{
					Address: endpoint.Address,
					Port:    endpoint.Port,
					Tags: map[string]string{
						mesh_proto.ServiceTag:  serviceTag,
						mesh_proto.InstanceTag: endpoint.Instance,
					},
				})
			}
		} else {
			// generate outbound based on ClusterIP
			outbounds = append(outbounds, &mesh_proto.Dataplane_Networking_Outbound{
				Address: service.Spec.ClusterIP,
				Port:    port,
				Tags: map[string]string{
					mesh_proto.ServiceTag: serviceTag,
				},
			})
		}
	}
	return outbounds, nil
}