Skip to content

Commit 5497a49

Browse files
taniyourstrulyiblancasajaronoff97
authored
Enhanced Webhook Reconciliation Errors (#3180)
* 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 <[email protected]> * fixed test * fixed test * unneeded code * linting * linting and unit tests * reuse code * Update apis/v1beta1/collector_webhook.go Co-authored-by: Jacob Aronoff <[email protected]> * 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 <[email protected]> Co-authored-by: Jacob Aronoff <[email protected]> Co-authored-by: Jacob Aronoff <[email protected]>
1 parent e023705 commit 5497a49

File tree

9 files changed

+518
-343
lines changed

9 files changed

+518
-343
lines changed

.chloggen/enhanced-webhook.yaml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
2+
change_type: enhancement
3+
4+
# The name of the component, or a single word describing the area of concern, (e.g. operator, target allocator, github action)
5+
component: operator
6+
7+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
8+
note: Added reconciliation errors for webhook events. The webhooks run the manifest generators to check for any errors.
9+
10+
# One or more tracking issues related to the change
11+
issues: [2399]
12+
13+
# (Optional) One or more lines of additional information to render under the primary note.
14+
# These lines will be padded with 2 spaces and then inserted directly into the document.
15+
# Use pipe (|) for multiline entries.
16+
subtext:

apis/v1beta1/collector_webhook.go

+34-10
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ type CollectorWebhook struct {
4747
scheme *runtime.Scheme
4848
reviewer *rbac.Reviewer
4949
metrics *Metrics
50+
bv BuildValidator
5051
}
5152

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

111-
warnings, err := c.validate(ctx, otelcol)
112+
warnings, err := c.Validate(ctx, otelcol)
112113
if err != nil {
113114
return warnings, err
114115
}
115116
if c.metrics != nil {
116117
c.metrics.create(ctx, otelcol)
117118
}
118-
119+
if c.bv != nil {
120+
newWarnings := c.bv(*otelcol)
121+
warnings = append(warnings, newWarnings...)
122+
}
119123
return warnings, nil
120124
}
121125

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

149+
if c.bv != nil {
150+
newWarnings := c.bv(*otelcol)
151+
warnings = append(warnings, newWarnings...)
152+
}
145153
return warnings, nil
146154
}
147155

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

154-
warnings, err := c.validate(ctx, otelcol)
162+
warnings, err := c.Validate(ctx, otelcol)
155163
if err != nil {
156164
return warnings, err
157165
}
@@ -163,7 +171,7 @@ func (c CollectorWebhook) ValidateDelete(ctx context.Context, obj runtime.Object
163171
return warnings, nil
164172
}
165173

166-
func (c CollectorWebhook) validate(ctx context.Context, r *OpenTelemetryCollector) (admission.Warnings, error) {
174+
func (c CollectorWebhook) Validate(ctx context.Context, r *OpenTelemetryCollector) (admission.Warnings, error) {
167175
warnings := admission.Warnings{}
168176

169177
nullObjects := r.Spec.Config.nullObjects()
@@ -404,14 +412,30 @@ func checkAutoscalerSpec(autoscaler *AutoscalerSpec) error {
404412
return nil
405413
}
406414

407-
func SetupCollectorWebhook(mgr ctrl.Manager, cfg config.Config, reviewer *rbac.Reviewer, metrics *Metrics) error {
408-
cvw := &CollectorWebhook{
409-
reviewer: reviewer,
410-
logger: mgr.GetLogger().WithValues("handler", "CollectorWebhook", "version", "v1beta1"),
411-
scheme: mgr.GetScheme(),
415+
// BuildValidator enables running the manifest generators for the collector reconciler
416+
// +kubebuilder:object:generate=false
417+
type BuildValidator func(c OpenTelemetryCollector) admission.Warnings
418+
419+
func NewCollectorWebhook(
420+
logger logr.Logger,
421+
scheme *runtime.Scheme,
422+
cfg config.Config,
423+
reviewer *rbac.Reviewer,
424+
metrics *Metrics,
425+
bv BuildValidator,
426+
) *CollectorWebhook {
427+
return &CollectorWebhook{
428+
logger: logger,
429+
scheme: scheme,
412430
cfg: cfg,
431+
reviewer: reviewer,
413432
metrics: metrics,
433+
bv: bv,
414434
}
435+
}
436+
437+
func SetupCollectorWebhook(mgr ctrl.Manager, cfg config.Config, reviewer *rbac.Reviewer, metrics *Metrics, bv BuildValidator) error {
438+
cvw := NewCollectorWebhook(mgr.GetLogger().WithValues("handler", "CollectorWebhook", "version", "v1beta1"), mgr.GetScheme(), cfg, reviewer, metrics, bv)
415439
return ctrl.NewWebhookManagedBy(mgr).
416440
For(&OpenTelemetryCollector{}).
417441
WithValidator(cvw).

0 commit comments

Comments
 (0)