|
| 1 | +/* |
| 2 | +Copyright 2024. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package v1 |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/dataflow-operator/dataflow/pkg/providers" |
| 21 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 22 | + "sigs.k8s.io/controller-runtime/pkg/webhook/admission" |
| 23 | +) |
| 24 | + |
| 25 | +func validateErrors(errors *ErrorSinkSpec, f *field.Path) field.ErrorList { |
| 26 | + var all field.ErrorList |
| 27 | + if errors == nil { |
| 28 | + return all |
| 29 | + } |
| 30 | + all = append(all, validateSink(&errors.SinkSpec, f)...) |
| 31 | + if errors.AckPolicy != "" && |
| 32 | + errors.AckPolicy != ErrorAckPolicyAfterWrite && |
| 33 | + errors.AckPolicy != ErrorAckPolicyNever && |
| 34 | + errors.AckPolicy != ErrorAckPolicyAfterMainSinkSuccess { |
| 35 | + all = append(all, field.NotSupported(f.Child("ackPolicy"), errors.AckPolicy, []string{ |
| 36 | + ErrorAckPolicyAfterWrite, |
| 37 | + ErrorAckPolicyNever, |
| 38 | + ErrorAckPolicyAfterMainSinkSuccess, |
| 39 | + })) |
| 40 | + } |
| 41 | + return all |
| 42 | +} |
| 43 | + |
| 44 | +func validateIdempotency(spec *DataFlowSpec, f *field.Path) field.ErrorList { |
| 45 | + var all field.ErrorList |
| 46 | + if spec == nil || !isPollingSourceType(spec.Source.Type) { |
| 47 | + return all |
| 48 | + } |
| 49 | + if sinkIsIdempotent(&spec.Sink) { |
| 50 | + return all |
| 51 | + } |
| 52 | + msg := "polling source with non-idempotent main sink may produce duplicates on restart; enable upsertMode on the sink or set strictIdempotency: false to accept this warning" |
| 53 | + if StrictIdempotencyEnabled(spec) { |
| 54 | + all = append(all, field.Invalid(f.Child("sink"), spec.Sink.Type, |
| 55 | + "strictIdempotency is enabled but main sink is not idempotent (enable upsertMode for postgresql/trino/clickhouse sinks)")) |
| 56 | + } else { |
| 57 | + _ = msg // warnings emitted via WarnDataFlowSpec |
| 58 | + } |
| 59 | + return all |
| 60 | +} |
| 61 | + |
| 62 | +// WarnDataFlowSpec returns admission warnings for non-fatal spec issues. |
| 63 | +func WarnDataFlowSpec(spec *DataFlowSpec) admission.Warnings { |
| 64 | + var warnings admission.Warnings |
| 65 | + if spec == nil { |
| 66 | + return warnings |
| 67 | + } |
| 68 | + if isPollingSourceType(spec.Source.Type) && !sinkIsIdempotent(&spec.Sink) && !StrictIdempotencyEnabled(spec) { |
| 69 | + warnings = append(warnings, |
| 70 | + "polling source with non-idempotent main sink may produce duplicates on restart; enable sink.config.upsertMode or set strictIdempotency: true to reject at admission") |
| 71 | + } |
| 72 | + return warnings |
| 73 | +} |
| 74 | + |
| 75 | +func isPollingSourceType(sourceType string) bool { |
| 76 | + return providers.SourceSupportsCheckpoint(sourceType) |
| 77 | +} |
| 78 | + |
| 79 | +func sinkIsIdempotent(sink *SinkSpec) bool { |
| 80 | + if sink == nil || sink.Config == nil || len(sink.Config.Raw) == 0 { |
| 81 | + return false |
| 82 | + } |
| 83 | + switch sink.Type { |
| 84 | + case "postgresql": |
| 85 | + cfg, err := sink.GetPostgreSQLConfig() |
| 86 | + if err != nil || cfg == nil { |
| 87 | + return false |
| 88 | + } |
| 89 | + return cfg.UpsertMode != nil && *cfg.UpsertMode |
| 90 | + case "clickhouse": |
| 91 | + cfg, err := sink.GetClickHouseConfig() |
| 92 | + if err != nil || cfg == nil { |
| 93 | + return false |
| 94 | + } |
| 95 | + return cfg.UpsertMode != nil && *cfg.UpsertMode |
| 96 | + case "trino": |
| 97 | + cfg, err := sink.GetTrinoConfig() |
| 98 | + if err != nil || cfg == nil { |
| 99 | + return false |
| 100 | + } |
| 101 | + return cfg.UpsertMode != nil && *cfg.UpsertMode |
| 102 | + default: |
| 103 | + return false |
| 104 | + } |
| 105 | +} |
0 commit comments