func()

in internal/manifests/collector/parser/receiver/receiver_otlp.go [55:116]


func (o *OTLPReceiverParser) Ports() ([]corev1.ServicePort, error) {
	ports := []corev1.ServicePort{}

	for _, protocol := range []struct {
		name         string
		defaultPorts []corev1.ServicePort
	}{
		{
			name: grpc,
			defaultPorts: []corev1.ServicePort{
				{
					Name:        naming.PortName(fmt.Sprintf("%s-grpc", o.name), defaultOTLPGRPCPort),
					Port:        defaultOTLPGRPCPort,
					TargetPort:  intstr.FromInt(int(defaultOTLPGRPCPort)),
					AppProtocol: &grpc,
				},
			},
		},
		{
			name: http,
			defaultPorts: []corev1.ServicePort{
				{
					Name:        naming.PortName(fmt.Sprintf("%s-http", o.name), defaultOTLPHTTPPort),
					Port:        defaultOTLPHTTPPort,
					TargetPort:  intstr.FromInt(int(defaultOTLPHTTPPort)),
					AppProtocol: &http,
				},
			},
		},
	} {
		// do we have the protocol specified at all?
		if receiverProtocol, ok := o.config[protocol.name]; ok {
			// we have the specified protocol, we definitely need a service port
			nameWithProtocol := fmt.Sprintf("%s-%s", o.name, protocol.name)
			var protocolPort *corev1.ServicePort

			// do we have a configuration block for the protocol?
			settings, ok := receiverProtocol.(map[interface{}]interface{})
			if ok {
				protocolPort = singlePortFromConfigEndpoint(o.logger, nameWithProtocol, settings)
			}

			// have we parsed a port based on the configuration block?
			// if not, we use the default port
			if protocolPort == nil {
				ports = append(ports, protocol.defaultPorts...)
			} else {
				// infer protocol and appProtocol from protocol.name
				if protocol.name == grpc {
					protocolPort.Protocol = corev1.ProtocolTCP
					protocolPort.AppProtocol = &grpc
				} else if protocol.name == http {
					protocolPort.Protocol = corev1.ProtocolTCP
					protocolPort.AppProtocol = &http
				}
				ports = append(ports, *protocolPort)
			}
		}
	}

	return ports, nil
}