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

Improve probe parsing #3250

Merged
merged 20 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .chloggen/improve-probe-parsing.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 healthcheck parsing capabilities, allowing for future extensions to configure a healthcheck other than the v1 healthcheck extension.

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

# (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:
48 changes: 45 additions & 3 deletions apis/v1beta1/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (

"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/extensions"
"github.com/open-telemetry/opentelemetry-operator/internal/components/processors"
"github.com/open-telemetry/opentelemetry-operator/internal/components/receivers"
)
Expand All @@ -41,10 +42,11 @@ const (
KindReceiver ComponentKind = iota
KindExporter
KindProcessor
KindExtension
)

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

// AnyConfig represent parts of the config.
Expand Down Expand Up @@ -108,6 +110,7 @@ func (c *Config) GetEnabledComponents() map[ComponentKind]map[string]interface{}
KindReceiver: {},
KindProcessor: {},
KindExporter: {},
KindExtension: {},
}
for _, pipeline := range c.Service.Pipelines {
if pipeline == nil {
Expand All @@ -123,6 +126,9 @@ func (c *Config) GetEnabledComponents() map[ComponentKind]map[string]interface{}
toReturn[KindProcessor][componentId] = struct{}{}
}
}
for _, componentId := range c.Service.Extensions {
toReturn[KindExtension][componentId] = struct{}{}
}
return toReturn
}

Expand Down Expand Up @@ -162,6 +168,8 @@ func (c *Config) getRbacRulesForComponentKinds(logger logr.Logger, componentKind
} else {
cfg = *c.Processors
}
case KindExtension:
continue
}
for componentName := range enabledComponents[componentKind] {
// TODO: Clean up the naming here and make it simpler to use a retriever.
Expand Down Expand Up @@ -191,7 +199,9 @@ func (c *Config) getPortsForComponentKinds(logger logr.Logger, componentKinds ..
retriever = exporters.ParserFor
cfg = c.Exporters
case KindProcessor:
break
continue
case KindExtension:
continue
}
for componentName := range enabledComponents[componentKind] {
// TODO: Clean up the naming here and make it simpler to use a retriever.
Expand Down Expand Up @@ -227,6 +237,38 @@ func (c *Config) GetAllRbacRules(logger logr.Logger) ([]rbacv1.PolicyRule, error
return c.getRbacRulesForComponentKinds(logger, KindReceiver, KindExporter, KindProcessor)
}

// GetLivenessProbe gets the first enabled liveness probe. There should only ever be one extension enabled
// that provides the hinting for the liveness probe.
func (c *Config) GetLivenessProbe(logger logr.Logger) (*corev1.Probe, error) {
enabledComponents := c.GetEnabledComponents()
for componentName := range enabledComponents[KindExtension] {
// TODO: Clean up the naming here and make it simpler to use a retriever.
parser := extensions.ParserFor(componentName)
if probe, err := parser.GetLivenessProbe(logger, c.Extensions.Object[componentName]); err != nil {
return nil, err
} else if probe != nil {
return probe, nil
}
}
return nil, nil
}

// GetReadinessProbe gets the first enabled readiness probe. There should only ever be one extension enabled
// that provides the hinting for the readiness probe.
func (c *Config) GetReadinessProbe(logger logr.Logger) (*corev1.Probe, error) {
enabledComponents := c.GetEnabledComponents()
for componentName := range enabledComponents[KindExtension] {
// TODO: Clean up the naming here and make it simpler to use a retriever.
parser := extensions.ParserFor(componentName)
if probe, err := parser.GetReadinessProbe(logger, c.Extensions.Object[componentName]); err != nil {
return nil, err
} else if probe != nil {
return probe, nil
}
}
return nil, nil
}

// Yaml encodes the current object and returns it as a string.
func (c *Config) Yaml() (string, error) {
var buf bytes.Buffer
Expand Down Expand Up @@ -268,7 +310,7 @@ func (c *Config) nullObjects() []string {
}

type Service struct {
Extensions *[]string `json:"extensions,omitempty" yaml:"extensions,omitempty"`
Extensions []string `json:"extensions,omitempty" yaml:"extensions,omitempty"`
// +kubebuilder:pruning:PreserveUnknownFields
Telemetry *AnyConfig `json:"telemetry,omitempty" yaml:"telemetry,omitempty"`
// +kubebuilder:pruning:PreserveUnknownFields
Expand Down
14 changes: 13 additions & 1 deletion apis/v1beta1/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestConfigYaml(t *testing.T) {
},
},
Service: Service{
Extensions: &[]string{"addon"},
Extensions: []string{"addon"},
Telemetry: &AnyConfig{
Object: map[string]interface{}{
"insights": "yeah!",
Expand Down Expand Up @@ -304,6 +304,7 @@ func TestConfig_GetEnabledComponents(t *testing.T) {
"bar": struct{}{},
"count": struct{}{},
},
KindExtension: {},
},
},
{
Expand All @@ -321,6 +322,7 @@ func TestConfig_GetEnabledComponents(t *testing.T) {
KindExporter: {
"prometheus": struct{}{},
},
KindExtension: {},
},
},
{
Expand All @@ -339,6 +341,11 @@ func TestConfig_GetEnabledComponents(t *testing.T) {
"otlp": struct{}{},
"prometheus": struct{}{},
},
KindExtension: {
"health_check": struct{}{},
"pprof": struct{}{},
"zpages": struct{}{},
},
},
},
{
Expand All @@ -352,6 +359,9 @@ func TestConfig_GetEnabledComponents(t *testing.T) {
KindExporter: {
"otlp/auth": struct{}{},
},
KindExtension: {
"oauth2client": struct{}{},
},
},
},
{
Expand All @@ -365,6 +375,7 @@ func TestConfig_GetEnabledComponents(t *testing.T) {
KindExporter: {
"debug": struct{}{},
},
KindExtension: {},
},
},
{
Expand All @@ -374,6 +385,7 @@ func TestConfig_GetEnabledComponents(t *testing.T) {
KindReceiver: {},
KindProcessor: {},
KindExporter: {},
KindExtension: {},
},
},
}
Expand Down
8 changes: 2 additions & 6 deletions apis/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 30 additions & 9 deletions internal/components/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ import (
type ParserOption[ComponentConfigType any] func(*Settings[ComponentConfigType])

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

func NewEmptySettings[ComponentConfigType any]() *Settings[ComponentConfigType] {
Expand Down Expand Up @@ -104,13 +106,32 @@ func (b Builder[ComponentConfigType]) WithRbacGen(rbacGen RBACRuleGenerator[Comp
})
}

func (b Builder[ComponentConfigType]) WithLivenessGen(livenessGen ProbeGenerator[ComponentConfigType]) Builder[ComponentConfigType] {
return append(b, func(o *Settings[ComponentConfigType]) {
o.livenessGen = livenessGen
})
}

func (b Builder[ComponentConfigType]) WithReadinessGen(readinessGen ProbeGenerator[ComponentConfigType]) Builder[ComponentConfigType] {
return append(b, func(o *Settings[ComponentConfigType]) {
o.readinessGen = readinessGen
})
}

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

func (b Builder[ComponentConfigType]) MustBuild() *GenericParser[ComponentConfigType] {
Expand Down
Loading
Loading