|
| 1 | +// Copyright The OpenTelemetry 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 receiver |
| 16 | + |
| 17 | +import ( |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "github.com/go-logr/logr" |
| 21 | + corev1 "k8s.io/api/core/v1" |
| 22 | + |
| 23 | + "github.com/open-telemetry/opentelemetry-operator/internal/manifests/collector/parser" |
| 24 | + "github.com/open-telemetry/opentelemetry-operator/internal/naming" |
| 25 | +) |
| 26 | + |
| 27 | +var _ parser.ComponentPortParser = &SyslogReceiverParser{} |
| 28 | + |
| 29 | +const parserNameSyslog = "__syslog" |
| 30 | + |
| 31 | +// SyslogReceiverParser parses the configuration for TCP log receivers. |
| 32 | +type SyslogReceiverParser struct { |
| 33 | + config map[interface{}]interface{} |
| 34 | + logger logr.Logger |
| 35 | + name string |
| 36 | +} |
| 37 | + |
| 38 | +// NewSyslogReceiverParser builds a new parser for TCP log receivers. |
| 39 | +func NewSyslogReceiverParser(logger logr.Logger, name string, config map[interface{}]interface{}) parser.ComponentPortParser { |
| 40 | + return &SyslogReceiverParser{ |
| 41 | + logger: logger, |
| 42 | + name: name, |
| 43 | + config: config, |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func (o *SyslogReceiverParser) Ports() ([]corev1.ServicePort, error) { |
| 48 | + var endpoint interface{} |
| 49 | + var endpointName string |
| 50 | + var protocol corev1.Protocol |
| 51 | + var c map[interface{}]interface{} |
| 52 | + |
| 53 | + // syslog receiver contains the endpoint |
| 54 | + // that needs to be exposed one level down inside config |
| 55 | + // i.e. either in tcp or udp section with field key |
| 56 | + // as `listen_address` |
| 57 | + if tcp, isTCP := o.config["tcp"]; isTCP && tcp != nil { |
| 58 | + c = tcp.(map[interface{}]interface{}) |
| 59 | + endpointName = "tcp" |
| 60 | + endpoint = getAddressFromConfig(o.logger, o.name, listenAddressKey, c) |
| 61 | + protocol = corev1.ProtocolTCP |
| 62 | + } else if udp, isUDP := o.config["udp"]; isUDP && udp != nil { |
| 63 | + c = udp.(map[interface{}]interface{}) |
| 64 | + endpointName = "udp" |
| 65 | + endpoint = getAddressFromConfig(o.logger, o.name, listenAddressKey, c) |
| 66 | + protocol = corev1.ProtocolUDP |
| 67 | + } |
| 68 | + |
| 69 | + switch e := endpoint.(type) { |
| 70 | + case nil: |
| 71 | + break |
| 72 | + case string: |
| 73 | + port, err := portFromEndpoint(e) |
| 74 | + if err != nil { |
| 75 | + o.logger.WithValues(listenAddressKey, e).Error(err, fmt.Sprintf("couldn't parse the %s endpoint's port", endpointName)) |
| 76 | + return nil, nil |
| 77 | + } |
| 78 | + |
| 79 | + return []corev1.ServicePort{{ |
| 80 | + Port: port, |
| 81 | + Name: naming.PortName(o.name, port), |
| 82 | + Protocol: protocol, |
| 83 | + }}, nil |
| 84 | + default: |
| 85 | + o.logger.WithValues(listenAddressKey, endpoint).Error(fmt.Errorf("unrecognized type %T of %s endpoint", endpoint, endpointName), |
| 86 | + "receiver's endpoint isn't a string") |
| 87 | + } |
| 88 | + |
| 89 | + return []corev1.ServicePort{}, nil |
| 90 | +} |
| 91 | + |
| 92 | +func (o *SyslogReceiverParser) ParserName() string { |
| 93 | + return parserNameSyslog |
| 94 | +} |
| 95 | + |
| 96 | +func init() { |
| 97 | + Register("syslog", NewSyslogReceiverParser) |
| 98 | +} |
0 commit comments