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

Uses the new parsing structure for RBAC parsing #3206

Merged
merged 16 commits into from
Sep 16, 2024
Merged
62 changes: 31 additions & 31 deletions internal/components/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,30 +23,30 @@ import (
"github.com/open-telemetry/opentelemetry-operator/internal/naming"
)

type ParserOption[T any] func(*Option[T])
type ParserOption[ComponentConfigType any] func(*Settings[ComponentConfigType])

type Option[T any] struct {
type Settings[ComponentConfigType any] struct {
protocol corev1.Protocol
appProtocol *string
targetPort intstr.IntOrString
nodePort int32
name string
port int32
portParser PortParser[T]
rbacGen RBACRuleGenerator[T]
portParser PortParser[ComponentConfigType]
rbacGen RBACRuleGenerator[ComponentConfigType]
}

func NewEmptyOption[T any]() *Option[T] {
return &Option[T]{}
func NewEmptySettings[ComponentConfigType any]() *Settings[ComponentConfigType] {
return &Settings[ComponentConfigType]{}
}

func (o *Option[T]) Apply(opts ...ParserOption[T]) {
func (o *Settings[ComponentConfigType]) Apply(opts ...ParserOption[ComponentConfigType]) {
for _, opt := range opts {
opt(o)
}
}

func (o *Option[T]) GetServicePort() *corev1.ServicePort {
func (o *Settings[ComponentConfigType]) GetServicePort() *corev1.ServicePort {
return &corev1.ServicePort{
Name: naming.PortName(o.name, o.port),
Port: o.port,
Expand All @@ -57,63 +57,63 @@ func (o *Option[T]) GetServicePort() *corev1.ServicePort {
}
}

type Builder[T any] []ParserOption[T]
type Builder[ComponentConfigType any] []ParserOption[ComponentConfigType]

func NewBuilder[T any]() Builder[T] {
return []ParserOption[T]{}
func NewBuilder[ComponentConfigType any]() Builder[ComponentConfigType] {
return []ParserOption[ComponentConfigType]{}
}

func (b Builder[T]) WithProtocol(protocol corev1.Protocol) Builder[T] {
return append(b, func(o *Option[T]) {
func (b Builder[ComponentConfigType]) WithProtocol(protocol corev1.Protocol) Builder[ComponentConfigType] {
return append(b, func(o *Settings[ComponentConfigType]) {
o.protocol = protocol
})
}
func (b Builder[T]) WithAppProtocol(appProtocol *string) Builder[T] {
return append(b, func(o *Option[T]) {
func (b Builder[ComponentConfigType]) WithAppProtocol(appProtocol *string) Builder[ComponentConfigType] {
return append(b, func(o *Settings[ComponentConfigType]) {
o.appProtocol = appProtocol
})
}
func (b Builder[T]) WithTargetPort(targetPort int32) Builder[T] {
return append(b, func(o *Option[T]) {
func (b Builder[ComponentConfigType]) WithTargetPort(targetPort int32) Builder[ComponentConfigType] {
return append(b, func(o *Settings[ComponentConfigType]) {
o.targetPort = intstr.FromInt32(targetPort)
})
}
func (b Builder[T]) WithNodePort(nodePort int32) Builder[T] {
return append(b, func(o *Option[T]) {
func (b Builder[ComponentConfigType]) WithNodePort(nodePort int32) Builder[ComponentConfigType] {
return append(b, func(o *Settings[ComponentConfigType]) {
o.nodePort = nodePort
})
}
func (b Builder[T]) WithName(name string) Builder[T] {
return append(b, func(o *Option[T]) {
func (b Builder[ComponentConfigType]) WithName(name string) Builder[ComponentConfigType] {
return append(b, func(o *Settings[ComponentConfigType]) {
o.name = name
})
}
func (b Builder[T]) WithPort(port int32) Builder[T] {
return append(b, func(o *Option[T]) {
func (b Builder[ComponentConfigType]) WithPort(port int32) Builder[ComponentConfigType] {
return append(b, func(o *Settings[ComponentConfigType]) {
o.port = port
})
}
func (b Builder[T]) WithPortParser(portParser PortParser[T]) Builder[T] {
return append(b, func(o *Option[T]) {
func (b Builder[ComponentConfigType]) WithPortParser(portParser PortParser[ComponentConfigType]) Builder[ComponentConfigType] {
return append(b, func(o *Settings[ComponentConfigType]) {
o.portParser = portParser
})
}
func (b Builder[T]) WithRbacGen(rbacGen RBACRuleGenerator[T]) Builder[T] {
return append(b, func(o *Option[T]) {
func (b Builder[ComponentConfigType]) WithRbacGen(rbacGen RBACRuleGenerator[ComponentConfigType]) Builder[ComponentConfigType] {
return append(b, func(o *Settings[ComponentConfigType]) {
o.rbacGen = rbacGen
})
}

func (b Builder[T]) Build() (*GenericParser[T], error) {
o := NewEmptyOption[T]()
func (b Builder[ComponentConfigType]) Build() (*GenericParser[ComponentConfigType], error) {
o := NewEmptySettings[ComponentConfigType]()
o.Apply(b...)
if len(o.name) == 0 {
return nil, fmt.Errorf("invalid option struct, no name specified")
}
return &GenericParser[T]{name: o.name, portParser: o.portParser, rbacGen: o.rbacGen, option: o}, nil
return &GenericParser[ComponentConfigType]{name: o.name, portParser: o.portParser, rbacGen: o.rbacGen, option: o}, nil
}

func (b Builder[T]) MustBuild() *GenericParser[T] {
func (b Builder[ComponentConfigType]) MustBuild() *GenericParser[ComponentConfigType] {
if p, err := b.Build(); err != nil {
panic(err)
} else {
Expand Down
10 changes: 5 additions & 5 deletions internal/components/component.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ type PortRetriever interface {
GetPortNumOrDefault(logr.Logger, int32) int32
}

// PortParser is a function that returns a list of servicePorts given a config of type T.
type PortParser[T any] func(logger logr.Logger, name string, defaultPort *corev1.ServicePort, config T) ([]corev1.ServicePort, error)
// 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 T
// It's expected that type T is the configuration used by a parser.
type RBACRuleGenerator[T any] func(logger logr.Logger, config T) ([]rbacv1.PolicyRule, 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)

// ComponentType returns the type for a given component name.
// components have a name like:
Expand Down
2 changes: 1 addition & 1 deletion internal/components/generic_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var (
// functionality to idempotent functions.
type GenericParser[T any] struct {
name string
option *Option[T]
option *Settings[T]
swiatekm marked this conversation as resolved.
Show resolved Hide resolved
portParser PortParser[T]
rbacGen RBACRuleGenerator[T]
}
Expand Down
8 changes: 4 additions & 4 deletions internal/components/multi_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (m *MultiPortReceiver) GetRBACRules(logr.Logger, interface{}) ([]rbacv1.Pol
return nil, nil
}

type MultiPortBuilder[T any] []Builder[T]
type MultiPortBuilder[ComponentConfigType any] []Builder[ComponentConfigType]

func NewMultiPortReceiverBuilder(name string) MultiPortBuilder[*MultiProtocolEndpointConfig] {
return append(MultiPortBuilder[*MultiProtocolEndpointConfig]{}, NewBuilder[*MultiProtocolEndpointConfig]().WithName(name))
Expand All @@ -86,11 +86,11 @@ func NewProtocolBuilder(name string, port int32) Builder[*MultiProtocolEndpointC
return NewBuilder[*MultiProtocolEndpointConfig]().WithName(name).WithPort(port)
}

func (mp MultiPortBuilder[T]) AddPortMapping(builder Builder[T]) MultiPortBuilder[T] {
func (mp MultiPortBuilder[ComponentConfigType]) AddPortMapping(builder Builder[ComponentConfigType]) MultiPortBuilder[ComponentConfigType] {
return append(mp, builder)
}

func (mp MultiPortBuilder[T]) Build() (*MultiPortReceiver, error) {
func (mp MultiPortBuilder[ComponentConfigType]) Build() (*MultiPortReceiver, error) {
if len(mp) < 1 {
return nil, fmt.Errorf("must provide at least one port mapping")
}
Expand All @@ -108,7 +108,7 @@ func (mp MultiPortBuilder[T]) Build() (*MultiPortReceiver, error) {
return multiReceiver, nil
}

func (mp MultiPortBuilder[T]) MustBuild() *MultiPortReceiver {
func (mp MultiPortBuilder[ComponentConfigType]) MustBuild() *MultiPortReceiver {
if p, err := mp.Build(); err != nil {
panic(err)
} else {
Expand Down
Loading