|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 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 wrappers |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "time" |
| 22 | + |
| 23 | + log "github.com/sirupsen/logrus" |
| 24 | + |
| 25 | + "sigs.k8s.io/external-dns/endpoint" |
| 26 | + "sigs.k8s.io/external-dns/source" |
| 27 | +) |
| 28 | + |
| 29 | +type postProcessor struct { |
| 30 | + source source.Source |
| 31 | + cfg PostProcessorConfig |
| 32 | +} |
| 33 | + |
| 34 | +type PostProcessorConfig struct { |
| 35 | + ttl int64 |
| 36 | + isConfigured bool |
| 37 | +} |
| 38 | + |
| 39 | +type PostProcessorOption func(*PostProcessorConfig) |
| 40 | + |
| 41 | +func WithTTL(ttl time.Duration) PostProcessorOption { |
| 42 | + return func(cfg *PostProcessorConfig) { |
| 43 | + if int64(ttl.Seconds()) > 0 { |
| 44 | + cfg.isConfigured = true |
| 45 | + cfg.ttl = int64(ttl.Seconds()) |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func NewPostProcessor(source source.Source, opts ...PostProcessorOption) source.Source { |
| 51 | + cfg := PostProcessorConfig{} |
| 52 | + for _, opt := range opts { |
| 53 | + opt(&cfg) |
| 54 | + } |
| 55 | + return &postProcessor{source: source, cfg: cfg} |
| 56 | +} |
| 57 | + |
| 58 | +func (pp *postProcessor) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, error) { |
| 59 | + endpoints, err := pp.source.Endpoints(ctx) |
| 60 | + if err != nil { |
| 61 | + return nil, err |
| 62 | + } |
| 63 | + |
| 64 | + if !pp.cfg.isConfigured { |
| 65 | + return endpoints, nil |
| 66 | + } |
| 67 | + |
| 68 | + for _, ep := range endpoints { |
| 69 | + if ep == nil { |
| 70 | + continue |
| 71 | + } |
| 72 | + ep.WithMinTTL(pp.cfg.ttl) |
| 73 | + // Additional post-processing can be added here. |
| 74 | + } |
| 75 | + |
| 76 | + return endpoints, nil |
| 77 | +} |
| 78 | + |
| 79 | +func (pp *postProcessor) AddEventHandler(ctx context.Context, handler func()) { |
| 80 | + log.Debug("postProcessor: adding event handler") |
| 81 | + pp.source.AddEventHandler(ctx, handler) |
| 82 | +} |
0 commit comments