sigs.k8s.io/external-dns@v0.14.1/source/dedupsource.go (about)

     1  /*
     2  Copyright 2017 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 source
    18  
    19  import (
    20  	"context"
    21  
    22  	log "github.com/sirupsen/logrus"
    23  
    24  	"sigs.k8s.io/external-dns/endpoint"
    25  )
    26  
    27  // dedupSource is a Source that removes duplicate endpoints from its wrapped source.
    28  type dedupSource struct {
    29  	source Source
    30  }
    31  
    32  // NewDedupSource creates a new dedupSource wrapping the provided Source.
    33  func NewDedupSource(source Source) Source {
    34  	return &dedupSource{source: source}
    35  }
    36  
    37  // Endpoints collects endpoints from its wrapped source and returns them without duplicates.
    38  func (ms *dedupSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, error) {
    39  	result := []*endpoint.Endpoint{}
    40  	collected := map[string]bool{}
    41  
    42  	endpoints, err := ms.source.Endpoints(ctx)
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  
    47  	for _, ep := range endpoints {
    48  		identifier := ep.DNSName + " / " + ep.SetIdentifier + " / " + ep.Targets.String()
    49  
    50  		if _, ok := collected[identifier]; ok {
    51  			log.Debugf("Removing duplicate endpoint %s", ep)
    52  			continue
    53  		}
    54  
    55  		collected[identifier] = true
    56  		result = append(result, ep)
    57  	}
    58  
    59  	return result, nil
    60  }
    61  
    62  func (ms *dedupSource) AddEventHandler(ctx context.Context, handler func()) {
    63  	ms.source.AddEventHandler(ctx, handler)
    64  }