external-dns/source/wrappers/post_processor.go
ivan katliarchuk 1e85a23a63
feat(source/min-ttl): added min-ttl support
Signed-off-by: ivan katliarchuk <ivan.katliarchuk@gmail.com>
2025-07-11 14:49:45 +01:00

77 lines
1.7 KiB
Go

/*
Copyright 2025 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package wrappers
import (
"context"
"time"
"sigs.k8s.io/external-dns/endpoint"
"sigs.k8s.io/external-dns/source"
)
type postProcessor struct {
source source.Source
cfg PostProcessorConfig
}
type PostProcessorConfig struct {
ttl int64
isConfigured bool
}
type PostProcessorOption func(*PostProcessorConfig)
func WithTTL(ttl time.Duration) PostProcessorOption {
return func(cfg *PostProcessorConfig) {
if int64(ttl.Seconds()) > 0 {
cfg.isConfigured = true
cfg.ttl = int64(ttl.Seconds())
}
}
}
func NewPostProcessor(source source.Source, opts ...PostProcessorOption) source.Source {
cfg := PostProcessorConfig{}
for _, opt := range opts {
opt(&cfg)
}
return &postProcessor{source: source, cfg: cfg}
}
func (pp *postProcessor) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, error) {
endpoints, err := pp.source.Endpoints(ctx)
if err != nil {
return nil, err
}
if !pp.cfg.isConfigured {
return endpoints, nil
}
for _, ep := range endpoints {
ep.WithMinTTL(pp.cfg.ttl)
// Additional post-processing can be added here.
}
return endpoints, nil
}
func (pp *postProcessor) AddEventHandler(_ context.Context, handler func()) {
}