forked from open-telemetry/opentelemetry-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsingle_endpoint.go
111 lines (95 loc) · 4.28 KB
/
single_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
// 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"
"strings"
"github.com/go-logr/logr"
"github.com/mitchellh/mapstructure"
corev1 "k8s.io/api/core/v1"
"github.com/open-telemetry/opentelemetry-operator/internal/naming"
)
var (
_ Parser = &GenericParser[*SingleEndpointConfig]{}
)
// SingleEndpointConfig represents the minimal struct for a given YAML configuration input containing either
// endpoint or listen_address.
type SingleEndpointConfig struct {
Endpoint string `mapstructure:"endpoint,omitempty" yaml:"endpoint,omitempty"`
ListenAddress string `mapstructure:"listen_address,omitempty" yaml:"listen_address,omitempty"`
}
func (g *SingleEndpointConfig) GetPortNumOrDefault(logger logr.Logger, p int32) int32 {
num, err := g.GetPortNum()
if err != nil {
logger.V(3).Info("no port set, using default: %d", p)
return p
}
return num
}
// GetPortNum attempts to get the port for the given config. If it cannot, the UnsetPort and the given missingPortError
// are returned.
func (g *SingleEndpointConfig) GetPortNum() (int32, error) {
if len(g.Endpoint) > 0 {
return PortFromEndpoint(g.Endpoint)
} else if len(g.ListenAddress) > 0 {
return PortFromEndpoint(g.ListenAddress)
}
return UnsetPort, PortNotFoundErr
}
func ParseSingleEndpointSilent(logger logr.Logger, name string, defaultPort *corev1.ServicePort, singleEndpointConfig *SingleEndpointConfig) ([]corev1.ServicePort, error) {
return internalParseSingleEndpoint(logger, name, true, defaultPort, singleEndpointConfig)
}
func ParseSingleEndpoint(logger logr.Logger, name string, defaultPort *corev1.ServicePort, singleEndpointConfig *SingleEndpointConfig) ([]corev1.ServicePort, error) {
return internalParseSingleEndpoint(logger, name, false, defaultPort, singleEndpointConfig)
}
func internalParseSingleEndpoint(logger logr.Logger, name string, failSilently bool, defaultPort *corev1.ServicePort, singleEndpointConfig *SingleEndpointConfig) ([]corev1.ServicePort, error) {
if singleEndpointConfig == nil {
return nil, nil
}
if _, err := singleEndpointConfig.GetPortNum(); err != nil && defaultPort.Port == UnsetPort {
if failSilently {
logger.WithValues("receiver", defaultPort.Name).V(4).Info("couldn't parse the endpoint's port and no default port set", "error", err)
err = nil
} else {
logger.WithValues("receiver", defaultPort.Name).Error(err, "couldn't parse the endpoint's port and no default port set")
}
return []corev1.ServicePort{}, err
}
port := singleEndpointConfig.GetPortNumOrDefault(logger, defaultPort.Port)
svcPort := defaultPort
svcPort.Name = naming.PortName(name, port)
return []corev1.ServicePort{ConstructServicePort(svcPort, port)}, nil
}
func NewSinglePortParserBuilder(name string, port int32) Builder[*SingleEndpointConfig] {
return NewBuilder[*SingleEndpointConfig]().WithPort(port).WithName(name).WithPortParser(ParseSingleEndpoint).WithDefaultsApplier(AddressDefaulter).WithDefaultRecAddress("0.0.0.0")
}
func NewSilentSinglePortParserBuilder(name string, port int32) Builder[*SingleEndpointConfig] {
return NewBuilder[*SingleEndpointConfig]().WithPort(port).WithName(name).WithPortParser(ParseSingleEndpointSilent).WithDefaultsApplier(AddressDefaulter).WithDefaultRecAddress("0.0.0.0")
}
func AddressDefaulter(logger logr.Logger, defaultRecAddr string, port int32, config *SingleEndpointConfig) (map[string]interface{}, error) {
if config == nil {
config = &SingleEndpointConfig{}
}
if config.Endpoint == "" {
config.Endpoint = fmt.Sprintf("%s:%d", defaultRecAddr, port)
} else {
v := strings.Split(config.Endpoint, ":")
if len(v) < 2 || v[0] == "" {
config.Endpoint = fmt.Sprintf("%s:%s", defaultRecAddr, v[len(v)-1])
}
}
res := make(map[string]interface{})
err := mapstructure.Decode(config, &res)
return res, err
}