-
Notifications
You must be signed in to change notification settings - Fork 440
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
Changes from 6 commits
6dfaae8
b8c8fb1
43c8a48
91cf382
60b5a9d
a3e0806
21ffd38
5c498fc
c8c5d47
b6f8027
cd909d0
c7b4b2e
849b4cc
014d433
bdc2a29
d38ea2c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ import ( | |
|
||
"github.com/go-logr/logr" | ||
corev1 "k8s.io/api/core/v1" | ||
rbacv1 "k8s.io/api/rbac/v1" | ||
"k8s.io/apimachinery/pkg/util/intstr" | ||
|
||
"github.com/open-telemetry/opentelemetry-operator/internal/naming" | ||
|
@@ -34,37 +35,44 @@ var ( | |
PortNotFoundErr = errors.New("port should not be empty") | ||
) | ||
|
||
// 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, o *Option, config T) ([]corev1.ServicePort, error) | ||
|
||
type PortRetriever interface { | ||
GetPortNum() (int32, error) | ||
GetPortNumOrDefault(logr.Logger, int32) int32 | ||
} | ||
|
||
type Option struct { | ||
// 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) | ||
jaronoff97 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// 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) | ||
type PortBuilderOption[T any] func(*Option[T]) | ||
|
||
type Option[T any] struct { | ||
protocol corev1.Protocol | ||
appProtocol *string | ||
targetPort intstr.IntOrString | ||
nodePort int32 | ||
name string | ||
port int32 | ||
portParser PortParser[T] | ||
rbacGen RBACRuleGenerator[T] | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note: I need the generics for the parsing stuff, but it really crowds the type signatures. (see the receivers/helpers.go file) I could remove the functions from the options struct, and put it in the constructor, but that feels counter intuitive to the options struct. |
||
|
||
func NewOption(name string, port int32) *Option { | ||
return &Option{ | ||
func NewOption[T any](name string, port int32) *Option[T] { | ||
return &Option[T]{ | ||
name: name, | ||
port: port, | ||
} | ||
} | ||
|
||
func (o *Option) Apply(opts ...PortBuilderOption) { | ||
func (o *Option[T]) Apply(opts ...PortBuilderOption[T]) { | ||
for _, opt := range opts { | ||
opt(o) | ||
} | ||
} | ||
|
||
func (o *Option) GetServicePort() *corev1.ServicePort { | ||
func (o *Option[T]) GetServicePort() *corev1.ServicePort { | ||
return &corev1.ServicePort{ | ||
Name: naming.PortName(o.name, o.port), | ||
Port: o.port, | ||
|
@@ -75,22 +83,32 @@ func (o *Option) GetServicePort() *corev1.ServicePort { | |
} | ||
} | ||
|
||
type PortBuilderOption func(*Option) | ||
func WithRBACRuleGenerator[T any](r RBACRuleGenerator[T]) PortBuilderOption[T] { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TBH I don't feel confortable with everything returing "PortBuilder". For some components we don't have to build anything port related (some processors) but we need to get RBAC. I was not sure about this in the other PR but I though you were thinking about something different and didn't raise my concerns. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think i should just rename this to not be "PortBuilderOption" but rather "ParserOption" which is more accurate. |
||
return func(opt *Option[T]) { | ||
opt.rbacGen = r | ||
} | ||
} | ||
|
||
func WithTargetPort(targetPort int32) PortBuilderOption { | ||
return func(opt *Option) { | ||
func WithPortParser[T any](p PortParser[T]) PortBuilderOption[T] { | ||
return func(opt *Option[T]) { | ||
opt.portParser = p | ||
} | ||
} | ||
|
||
func WithTargetPort[T any](targetPort int32) PortBuilderOption[T] { | ||
return func(opt *Option[T]) { | ||
opt.targetPort = intstr.FromInt32(targetPort) | ||
} | ||
} | ||
|
||
func WithAppProtocol(proto *string) PortBuilderOption { | ||
return func(opt *Option) { | ||
func WithAppProtocol[T any](proto *string) PortBuilderOption[T] { | ||
return func(opt *Option[T]) { | ||
opt.appProtocol = proto | ||
} | ||
} | ||
|
||
func WithProtocol(proto corev1.Protocol) PortBuilderOption { | ||
return func(opt *Option) { | ||
func WithProtocol[T any](proto corev1.Protocol) PortBuilderOption[T] { | ||
return func(opt *Option[T]) { | ||
opt.protocol = proto | ||
} | ||
} | ||
|
@@ -137,6 +155,9 @@ type Parser interface { | |
// 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) | ||
|
||
// ParserType returns the type of this parser | ||
ParserType() string | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ import ( | |
"github.com/go-logr/logr" | ||
"github.com/mitchellh/mapstructure" | ||
corev1 "k8s.io/api/core/v1" | ||
rbacv1 "k8s.io/api/rbac/v1" | ||
) | ||
|
||
var ( | ||
|
@@ -30,8 +31,20 @@ var ( | |
// functionality to idempotent functions. | ||
type GenericParser[T any] struct { | ||
name string | ||
option *Option[T] | ||
portParser PortParser[T] | ||
option *Option | ||
rbacGen RBACRuleGenerator[T] | ||
} | ||
|
||
func (g *GenericParser[T]) GetRBACRules(logger logr.Logger, config interface{}) ([]rbacv1.PolicyRule, error) { | ||
if g.rbacGen == nil { | ||
return nil, nil | ||
} | ||
var parsed T | ||
if err := mapstructure.Decode(config, &parsed); err != nil { | ||
return nil, err | ||
} | ||
Comment on lines
+44
to
+46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. By using generics here, we can let mapstructure decode take care of all the marshalling for us into the exact right type – no more map assertions! |
||
return g.rbacGen(logger, parsed) | ||
} | ||
|
||
func (g *GenericParser[T]) Ports(logger logr.Logger, name string, config interface{}) ([]corev1.ServicePort, error) { | ||
|
@@ -42,7 +55,7 @@ func (g *GenericParser[T]) Ports(logger logr.Logger, name string, config interfa | |
if err := mapstructure.Decode(config, &parsed); err != nil { | ||
return nil, err | ||
} | ||
return g.portParser(logger, name, g.option, parsed) | ||
return g.portParser(logger, name, g.option.GetServicePort(), parsed) | ||
} | ||
|
||
func (g *GenericParser[T]) ParserType() string { | ||
|
@@ -53,8 +66,8 @@ func (g *GenericParser[T]) ParserName() string { | |
return fmt.Sprintf("__%s", g.name) | ||
} | ||
|
||
func NewGenericParser[T any](name string, port int32, parser PortParser[T], opts ...PortBuilderOption) *GenericParser[T] { | ||
o := NewOption(name, port) | ||
func NewGenericParser[T any](name string, port int32, opts ...PortBuilderOption[T]) *GenericParser[T] { | ||
o := NewOption[T](name, port) | ||
o.Apply(opts...) | ||
return &GenericParser[T]{name: name, portParser: parser, option: o} | ||
return &GenericParser[T]{name: name, portParser: o.portParser, rbacGen: o.rbacGen, option: o} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved this to builder.go