forked from open-telemetry/opentelemetry-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmulti_endpoint.go
168 lines (144 loc) · 5.37 KB
/
multi_endpoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package components
import (
"fmt"
"github.com/go-logr/logr"
"github.com/mitchellh/mapstructure"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"github.com/open-telemetry/opentelemetry-operator/internal/naming"
)
var _ Parser = &MultiPortReceiver{}
// MultiProtocolEndpointConfig represents the minimal struct for a given YAML configuration input containing a map to
// a struct with either endpoint or listen_address.
type MultiProtocolEndpointConfig struct {
Protocols map[string]*SingleEndpointConfig `mapstructure:"protocols"`
}
// MultiPortOption allows the setting of options for a MultiPortReceiver.
type MultiPortOption func(parser *MultiPortReceiver)
// MultiPortReceiver is a special parser for components with endpoints for each protocol.
type MultiPortReceiver struct {
name string
defaultRecAddr string
addrMappings map[string]string
portMappings map[string]*corev1.ServicePort
}
func (m *MultiPortReceiver) Ports(logger logr.Logger, name string, config interface{}) ([]corev1.ServicePort, error) {
multiProtoEndpointCfg := &MultiProtocolEndpointConfig{}
if err := mapstructure.Decode(config, multiProtoEndpointCfg); err != nil {
return nil, err
}
var ports []corev1.ServicePort
for protocol, ec := range multiProtoEndpointCfg.Protocols {
if defaultSvc, ok := m.portMappings[protocol]; ok {
port := defaultSvc.Port
if ec != nil {
port = ec.GetPortNumOrDefault(logger, port)
}
defaultSvc.Name = naming.PortName(fmt.Sprintf("%s-%s", name, protocol), port)
ports = append(ports, ConstructServicePort(defaultSvc, port))
} else {
return nil, fmt.Errorf("unknown protocol set: %s", protocol)
}
}
return ports, nil
}
func (m *MultiPortReceiver) ParserType() string {
return ComponentType(m.name)
}
func (m *MultiPortReceiver) ParserName() string {
return fmt.Sprintf("__%s", m.name)
}
func (m *MultiPortReceiver) GetDefaultConfig(logger logr.Logger, config interface{}) (interface{}, error) {
multiProtoEndpointCfg := &MultiProtocolEndpointConfig{}
if err := mapstructure.Decode(config, multiProtoEndpointCfg); err != nil {
return nil, err
}
defaultedConfig := map[string]interface{}{}
for protocol, ec := range multiProtoEndpointCfg.Protocols {
if defaultSvc, ok := m.portMappings[protocol]; ok {
port := defaultSvc.Port
if ec != nil {
port = ec.GetPortNumOrDefault(logger, port)
}
addr := m.defaultRecAddr
if defaultAddr, ok := m.addrMappings[protocol]; ok {
addr = defaultAddr
}
conf, err := AddressDefaulter(logger, addr, port, ec)
if err != nil {
return nil, err
}
defaultedConfig[protocol] = conf
} else {
return nil, fmt.Errorf("unknown protocol set: %s", protocol)
}
}
return map[string]interface{}{
"protocols": defaultedConfig,
}, nil
}
func (m *MultiPortReceiver) GetLivenessProbe(logger logr.Logger, config interface{}) (*corev1.Probe, error) {
return nil, nil
}
func (m *MultiPortReceiver) GetReadinessProbe(logger logr.Logger, config interface{}) (*corev1.Probe, error) {
return nil, nil
}
func (m *MultiPortReceiver) GetRBACRules(logr.Logger, interface{}) ([]rbacv1.PolicyRule, error) {
return nil, nil
}
func (m *MultiPortReceiver) GetEnvironmentVariables(logger logr.Logger, config interface{}) ([]corev1.EnvVar, error) {
return nil, nil
}
type MultiPortBuilder[ComponentConfigType any] []Builder[ComponentConfigType]
func NewMultiPortReceiverBuilder(name string) MultiPortBuilder[*MultiProtocolEndpointConfig] {
return append(MultiPortBuilder[*MultiProtocolEndpointConfig]{}, NewBuilder[*MultiProtocolEndpointConfig]().WithName(name).WithDefaultRecAddress("0.0.0.0"))
}
func NewProtocolBuilder(name string, port int32) Builder[*MultiProtocolEndpointConfig] {
return NewBuilder[*MultiProtocolEndpointConfig]().WithName(name).WithPort(port).WithDefaultRecAddress("0.0.0.0")
}
func (mp MultiPortBuilder[ComponentConfigType]) AddPortMapping(builder Builder[ComponentConfigType]) MultiPortBuilder[ComponentConfigType] {
return append(mp, builder)
}
func (mp MultiPortBuilder[ComponentConfigType]) Build() (*MultiPortReceiver, error) {
if len(mp) < 1 {
return nil, fmt.Errorf("must provide at least one port mapping")
}
mb := mp[0].MustBuild()
multiReceiver := &MultiPortReceiver{
name: mb.name,
defaultRecAddr: mb.settings.defaultRecAddr,
addrMappings: map[string]string{},
portMappings: map[string]*corev1.ServicePort{},
}
for _, bu := range mp[1:] {
built, err := bu.Build()
if err != nil {
return nil, err
}
if built.settings != nil {
multiReceiver.portMappings[built.name] = built.settings.GetServicePort()
multiReceiver.addrMappings[built.name] = built.settings.defaultRecAddr
}
}
return multiReceiver, nil
}
func (mp MultiPortBuilder[ComponentConfigType]) MustBuild() *MultiPortReceiver {
if p, err := mp.Build(); err != nil {
panic(err)
} else {
return p
}
}