Skip to content

Commit

Permalink
Migrate to use new parsers (#3069)
Browse files Browse the repository at this point in the history
* Simplified parsers for 95% of use cases

* tons o tests

* temp

* known receivers

* lint

* nothin

* redone with many tests

* add interfaces for getting

* update to use it everywhere

* migrate to use

* oop

* fix tests

* sort the things

* updates from feedback
  • Loading branch information
jaronoff97 committed Jul 8, 2024
1 parent e4398f8 commit 1289c63
Show file tree
Hide file tree
Showing 24 changed files with 767 additions and 478 deletions.
16 changes: 16 additions & 0 deletions .chloggen/migrate-to-use.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action)
component: collector

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Improves the performance of port and configuration parsing in the operator

# One or more tracking issues related to the change
issues: [2603]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
79 changes: 66 additions & 13 deletions apis/v1beta1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,24 @@ import (
"strconv"
"strings"

"github.com/go-logr/logr"
"gopkg.in/yaml.v3"
corev1 "k8s.io/api/core/v1"

"github.com/open-telemetry/opentelemetry-operator/internal/components"
"github.com/open-telemetry/opentelemetry-operator/internal/components/exporters"
"github.com/open-telemetry/opentelemetry-operator/internal/components/receivers"
)

type ComponentType int
type ComponentKind int

const (
ComponentTypeReceiver ComponentType = iota
ComponentTypeExporter
ComponentTypeProcessor
KindReceiver ComponentKind = iota
KindExporter
KindProcessor
)

func (c ComponentType) String() string {
func (c ComponentKind) String() string {
return [...]string{"receiver", "exporter", "processor"}[c]
}

Expand Down Expand Up @@ -95,24 +101,24 @@ type Pipeline struct {
}

// GetEnabledComponents constructs a list of enabled components by component type.
func (c *Config) GetEnabledComponents() map[ComponentType]map[string]interface{} {
toReturn := map[ComponentType]map[string]interface{}{
ComponentTypeReceiver: {},
ComponentTypeProcessor: {},
ComponentTypeExporter: {},
func (c *Config) GetEnabledComponents() map[ComponentKind]map[string]interface{} {
toReturn := map[ComponentKind]map[string]interface{}{
KindReceiver: {},
KindProcessor: {},
KindExporter: {},
}
for _, pipeline := range c.Service.Pipelines {
if pipeline == nil {
continue
}
for _, componentId := range pipeline.Receivers {
toReturn[ComponentTypeReceiver][componentId] = struct{}{}
toReturn[KindReceiver][componentId] = struct{}{}
}
for _, componentId := range pipeline.Exporters {
toReturn[ComponentTypeExporter][componentId] = struct{}{}
toReturn[KindExporter][componentId] = struct{}{}
}
for _, componentId := range pipeline.Processors {
toReturn[ComponentTypeProcessor][componentId] = struct{}{}
toReturn[KindProcessor][componentId] = struct{}{}
}
}
return toReturn
Expand All @@ -133,6 +139,53 @@ type Config struct {
Service Service `json:"service" yaml:"service"`
}

// getPortsForComponentKinds gets the ports for the given ComponentKind(s).
func (c *Config) getPortsForComponentKinds(logger logr.Logger, componentKinds ...ComponentKind) ([]corev1.ServicePort, error) {
var ports []corev1.ServicePort
enabledComponents := c.GetEnabledComponents()
for _, componentKind := range componentKinds {
var retriever components.ParserRetriever
var cfg AnyConfig
switch componentKind {
case KindReceiver:
retriever = receivers.ReceiverFor
cfg = c.Receivers
case KindExporter:
retriever = exporters.ParserFor
cfg = c.Exporters
case KindProcessor:
break
}
for componentName := range enabledComponents[componentKind] {
// TODO: Clean up the naming here and make it simpler to use a retriever.
parser := retriever(componentName)
if parsedPorts, err := parser.Ports(logger, componentName, cfg.Object[componentName]); err != nil {
return nil, err
} else {
ports = append(ports, parsedPorts...)
}
}
}

sort.Slice(ports, func(i, j int) bool {
return ports[i].Name < ports[j].Name
})

return ports, nil
}

func (c *Config) GetReceiverPorts(logger logr.Logger) ([]corev1.ServicePort, error) {
return c.getPortsForComponentKinds(logger, KindReceiver)
}

func (c *Config) GetExporterPorts(logger logr.Logger) ([]corev1.ServicePort, error) {
return c.getPortsForComponentKinds(logger, KindExporter)
}

func (c *Config) GetAllPorts(logger logr.Logger) ([]corev1.ServicePort, error) {
return c.getPortsForComponentKinds(logger, KindReceiver, KindExporter)
}

// Yaml encodes the current object and returns it as a string.
func (c *Config) Yaml() (string, error) {
var buf bytes.Buffer
Expand Down
Loading

0 comments on commit 1289c63

Please sign in to comment.