sigs.k8s.io/external-dns@v0.14.1/source/multisource.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  	"sigs.k8s.io/external-dns/endpoint"
    23  )
    24  
    25  // multiSource is a Source that merges the endpoints of its nested Sources.
    26  type multiSource struct {
    27  	children       []Source
    28  	defaultTargets []string
    29  }
    30  
    31  // Endpoints collects endpoints of all nested Sources and returns them in a single slice.
    32  func (ms *multiSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, error) {
    33  	result := []*endpoint.Endpoint{}
    34  
    35  	for _, s := range ms.children {
    36  		endpoints, err := s.Endpoints(ctx)
    37  		if err != nil {
    38  			return nil, err
    39  		}
    40  		if len(ms.defaultTargets) > 0 {
    41  			for i := range endpoints {
    42  				eps := endpointsForHostname(endpoints[i].DNSName, ms.defaultTargets, endpoints[i].RecordTTL, endpoints[i].ProviderSpecific, endpoints[i].SetIdentifier, "")
    43  				for _, ep := range eps {
    44  					ep.Labels = endpoints[i].Labels
    45  				}
    46  				result = append(result, eps...)
    47  			}
    48  		} else {
    49  			result = append(result, endpoints...)
    50  		}
    51  	}
    52  
    53  	return result, nil
    54  }
    55  
    56  func (ms *multiSource) AddEventHandler(ctx context.Context, handler func()) {
    57  	for _, s := range ms.children {
    58  		s.AddEventHandler(ctx, handler)
    59  	}
    60  }
    61  
    62  // NewMultiSource creates a new multiSource.
    63  func NewMultiSource(children []Source, defaultTargets []string) Source {
    64  	return &multiSource{children: children, defaultTargets: defaultTargets}
    65  }