Skip to content

Commit

Permalink
Enhanced Webhook Reconciliation Errors (#3180)
Browse files Browse the repository at this point in the history
* initial commit

* added enhanced reconciliation errors

* tests

* update

* tests

* add test

* changelog

* initial fixes

* linting

* changing to warnings

* shadow declarations

* fixing errors

* kubebuilder test

* kubebuilder testing

* fixing file

* unit test

* initial changes

* clean up

* merge conflicts

* Update apis/v1beta1/collector_webhook_test.go

Co-authored-by: Israel Blancas <iblancasa@gmail.com>

* fixed test

* fixed test

* unneeded code

* linting

* linting and unit tests

* reuse code

* Update apis/v1beta1/collector_webhook.go

Co-authored-by: Jacob Aronoff <jaronoff97@users.noreply.github.com>

* revert to not use getParams

* use config

* linting

* more linting

* validate as anon func

* linting

* linting

* testing without test

* added back test

* fix

* make generate

---------

Co-authored-by: Israel Blancas <iblancasa@gmail.com>
Co-authored-by: Jacob Aronoff <jaronoff97@users.noreply.github.com>
Co-authored-by: Jacob Aronoff <jacobaronoff45@gmail.com>
  • Loading branch information
4 people authored Sep 5, 2024
1 parent e023705 commit 5497a49
Show file tree
Hide file tree
Showing 9 changed files with 518 additions and 343 deletions.
16 changes: 16 additions & 0 deletions .chloggen/enhanced-webhook.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. operator, target allocator, github action)
component: operator

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Added reconciliation errors for webhook events. The webhooks run the manifest generators to check for any errors.

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

# (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:
44 changes: 34 additions & 10 deletions apis/v1beta1/collector_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type CollectorWebhook struct {
scheme *runtime.Scheme
reviewer *rbac.Reviewer
metrics *Metrics
bv BuildValidator
}

func (c CollectorWebhook) Default(_ context.Context, obj runtime.Object) error {
Expand Down Expand Up @@ -108,14 +109,17 @@ func (c CollectorWebhook) ValidateCreate(ctx context.Context, obj runtime.Object
return nil, fmt.Errorf("expected an OpenTelemetryCollector, received %T", obj)
}

warnings, err := c.validate(ctx, otelcol)
warnings, err := c.Validate(ctx, otelcol)
if err != nil {
return warnings, err
}
if c.metrics != nil {
c.metrics.create(ctx, otelcol)
}

if c.bv != nil {
newWarnings := c.bv(*otelcol)
warnings = append(warnings, newWarnings...)
}
return warnings, nil
}

Expand All @@ -133,7 +137,7 @@ func (c CollectorWebhook) ValidateUpdate(ctx context.Context, oldObj, newObj run
if otelcolOld.Spec.Mode != otelcol.Spec.Mode {
return admission.Warnings{}, fmt.Errorf("the OpenTelemetry Collector mode is set to %s, which does not support modification", otelcolOld.Spec.Mode)
}
warnings, err := c.validate(ctx, otelcol)
warnings, err := c.Validate(ctx, otelcol)
if err != nil {
return warnings, err
}
Expand All @@ -142,6 +146,10 @@ func (c CollectorWebhook) ValidateUpdate(ctx context.Context, oldObj, newObj run
c.metrics.update(ctx, otelcolOld, otelcol)
}

if c.bv != nil {
newWarnings := c.bv(*otelcol)
warnings = append(warnings, newWarnings...)
}
return warnings, nil
}

Expand All @@ -151,7 +159,7 @@ func (c CollectorWebhook) ValidateDelete(ctx context.Context, obj runtime.Object
return nil, fmt.Errorf("expected an OpenTelemetryCollector, received %T", obj)
}

warnings, err := c.validate(ctx, otelcol)
warnings, err := c.Validate(ctx, otelcol)
if err != nil {
return warnings, err
}
Expand All @@ -163,7 +171,7 @@ func (c CollectorWebhook) ValidateDelete(ctx context.Context, obj runtime.Object
return warnings, nil
}

func (c CollectorWebhook) validate(ctx context.Context, r *OpenTelemetryCollector) (admission.Warnings, error) {
func (c CollectorWebhook) Validate(ctx context.Context, r *OpenTelemetryCollector) (admission.Warnings, error) {
warnings := admission.Warnings{}

nullObjects := r.Spec.Config.nullObjects()
Expand Down Expand Up @@ -404,14 +412,30 @@ func checkAutoscalerSpec(autoscaler *AutoscalerSpec) error {
return nil
}

func SetupCollectorWebhook(mgr ctrl.Manager, cfg config.Config, reviewer *rbac.Reviewer, metrics *Metrics) error {
cvw := &CollectorWebhook{
reviewer: reviewer,
logger: mgr.GetLogger().WithValues("handler", "CollectorWebhook", "version", "v1beta1"),
scheme: mgr.GetScheme(),
// BuildValidator enables running the manifest generators for the collector reconciler
// +kubebuilder:object:generate=false
type BuildValidator func(c OpenTelemetryCollector) admission.Warnings

func NewCollectorWebhook(
logger logr.Logger,
scheme *runtime.Scheme,
cfg config.Config,
reviewer *rbac.Reviewer,
metrics *Metrics,
bv BuildValidator,
) *CollectorWebhook {
return &CollectorWebhook{
logger: logger,
scheme: scheme,
cfg: cfg,
reviewer: reviewer,
metrics: metrics,
bv: bv,
}
}

func SetupCollectorWebhook(mgr ctrl.Manager, cfg config.Config, reviewer *rbac.Reviewer, metrics *Metrics, bv BuildValidator) error {
cvw := NewCollectorWebhook(mgr.GetLogger().WithValues("handler", "CollectorWebhook", "version", "v1beta1"), mgr.GetScheme(), cfg, reviewer, metrics, bv)
return ctrl.NewWebhookManagedBy(mgr).
For(&OpenTelemetryCollector{}).
WithValidator(cvw).
Expand Down
Loading

0 comments on commit 5497a49

Please sign in to comment.