forked from open-telemetry/opentelemetry-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomponent.go
151 lines (122 loc) · 5.32 KB
/
component.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
// 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 (
"errors"
"regexp"
"strconv"
"strings"
"github.com/go-logr/logr"
corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/util/intstr"
)
var (
GrpcProtocol = "grpc"
HttpProtocol = "http"
UnsetPort int32 = 0
PortNotFoundErr = errors.New("port should not be empty")
)
type PortRetriever interface {
GetPortNum() (int32, error)
GetPortNumOrDefault(logr.Logger, int32) int32
}
// PortParser is a function that returns a list of servicePorts given a config of type Config.
type PortParser[ComponentConfigType any] func(logger logr.Logger, name string, defaultPort *corev1.ServicePort, config ComponentConfigType) ([]corev1.ServicePort, error)
// RBACRuleGenerator is a function that generates a list of RBAC Rules given a configuration of type Config
// It's expected that type Config is the configuration used by a parser.
type RBACRuleGenerator[ComponentConfigType any] func(logger logr.Logger, config ComponentConfigType) ([]rbacv1.PolicyRule, error)
// ProbeGenerator is a function that generates a valid probe for a container given Config
// It's expected that type Config is the configuration used by a parser.
type ProbeGenerator[ComponentConfigType any] func(logger logr.Logger, config ComponentConfigType) (*corev1.Probe, error)
// EnvVarGenerator is a function that generates a list of environment variables for a given config.
// It's expected that type Config is the configuration used by a parser.
type EnvVarGenerator[ComponentConfigType any] func(logger logr.Logger, config ComponentConfigType) ([]corev1.EnvVar, error)
// Defaulter is a function that applies given defaults to the passed Config.
// It's expected that type Config is the configuration used by a parser.
type Defaulter[ComponentConfigType any] func(logger logr.Logger, defaultAddr string, defaultPort int32, config ComponentConfigType) (map[string]interface{}, error)
// ComponentType returns the type for a given component name.
// components have a name like:
// - mycomponent/custom
// - mycomponent
// we extract the "mycomponent" part and see if we have a parser for the component.
func ComponentType(name string) string {
if strings.Contains(name, "/") {
return name[:strings.Index(name, "/")]
}
return name
}
func PortFromEndpoint(endpoint string) (int32, error) {
var err error
var port int64
r := regexp.MustCompile(":[0-9]+")
if r.MatchString(endpoint) {
portStr := r.FindString(endpoint)
cleanedPortStr := strings.Replace(portStr, ":", "", -1)
port, err = strconv.ParseInt(cleanedPortStr, 10, 32)
if err != nil {
return UnsetPort, err
}
}
if port == 0 {
return UnsetPort, PortNotFoundErr
}
return int32(port), err
}
type ParserRetriever func(string) Parser
type Parser interface {
// GetDefaultConfig returns a config with set default values.
// NOTE: Config merging must be done by the caller if desired.
GetDefaultConfig(logger logr.Logger, config interface{}) (interface{}, error)
// Ports returns the service ports parsed based on the component's configuration where name is the component's name
// of the form "name" or "type/name"
Ports(logger logr.Logger, name string, config interface{}) ([]corev1.ServicePort, error)
// GetRBACRules returns the rbac rules for this component
GetRBACRules(logger logr.Logger, config interface{}) ([]rbacv1.PolicyRule, error)
// GetLivenessProbe returns a liveness probe set for the collector
GetLivenessProbe(logger logr.Logger, config interface{}) (*corev1.Probe, error)
// GetEnvironmentVariables returns a list of environment variables for the collector
GetEnvironmentVariables(logger logr.Logger, config interface{}) ([]corev1.EnvVar, error)
// GetReadinessProbe returns a readiness probe set for the collector
GetReadinessProbe(logger logr.Logger, config interface{}) (*corev1.Probe, error)
// ParserType returns the type of this parser
ParserType() string
// ParserName is an internal name for the parser
ParserName() string
}
func ConstructServicePort(current *corev1.ServicePort, port int32) corev1.ServicePort {
svc := corev1.ServicePort{
Name: current.Name,
Port: port,
NodePort: current.NodePort,
AppProtocol: current.AppProtocol,
Protocol: current.Protocol,
}
if port > 0 && current.TargetPort.IntValue() > 0 {
svc.TargetPort = intstr.FromInt32(port)
}
return svc
}
func GetPortsForConfig(logger logr.Logger, config map[string]interface{}, retriever ParserRetriever) ([]corev1.ServicePort, error) {
var ports []corev1.ServicePort
for componentName, componentDef := range config {
parser := retriever(componentName)
if parsedPorts, err := parser.Ports(logger, componentName, componentDef); err != nil {
return nil, err
} else {
ports = append(ports, parsedPorts...)
}
}
return ports, nil
}