Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate to use new parsers #3069

Merged
merged 17 commits into from
Jul 8, 2024
Merged
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am just curious why kind?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was per @swiatekm-sumo comment here #3069 (comment)


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
Loading