sigs.k8s.io/external-dns@v0.14.1/provider/provider.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 provider
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"net"
    23  	"strings"
    24  
    25  	"sigs.k8s.io/external-dns/endpoint"
    26  	"sigs.k8s.io/external-dns/plan"
    27  )
    28  
    29  // SoftError is an error, that provider will only log as error instead
    30  // of fatal. It is meant for error propagation from providers to tell
    31  // that this is a transient error.
    32  var SoftError error = errors.New("soft error")
    33  
    34  // NewSoftError creates a SoftError from the given error
    35  func NewSoftError(err error) error {
    36  	return errors.Join(SoftError, err)
    37  }
    38  
    39  // Provider defines the interface DNS providers should implement.
    40  type Provider interface {
    41  	Records(ctx context.Context) ([]*endpoint.Endpoint, error)
    42  	ApplyChanges(ctx context.Context, changes *plan.Changes) error
    43  	// AdjustEndpoints canonicalizes a set of candidate endpoints.
    44  	// It is called with a set of candidate endpoints obtained from the various sources.
    45  	// It returns a set modified as required by the provider. The provider is responsible for
    46  	// adding, removing, and modifying the ProviderSpecific properties to match
    47  	// the endpoints that the provider returns in `Records` so that the change plan will not have
    48  	// unnecessary (potentially failing) changes. It may also modify other fields, add, or remove
    49  	// Endpoints. It is permitted to modify the supplied endpoints.
    50  	AdjustEndpoints(endpoints []*endpoint.Endpoint) ([]*endpoint.Endpoint, error)
    51  	GetDomainFilter() endpoint.DomainFilter
    52  }
    53  
    54  type BaseProvider struct{}
    55  
    56  func (b BaseProvider) AdjustEndpoints(endpoints []*endpoint.Endpoint) ([]*endpoint.Endpoint, error) {
    57  	return endpoints, nil
    58  }
    59  
    60  func (b BaseProvider) GetDomainFilter() endpoint.DomainFilter {
    61  	return endpoint.DomainFilter{}
    62  }
    63  
    64  type contextKey struct {
    65  	name string
    66  }
    67  
    68  func (k *contextKey) String() string { return "provider context value " + k.name }
    69  
    70  // RecordsContextKey is a context key. It can be used during ApplyChanges
    71  // to access previously cached records. The associated value will be of
    72  // type []*endpoint.Endpoint.
    73  var RecordsContextKey = &contextKey{"records"}
    74  
    75  // EnsureTrailingDot ensures that the hostname receives a trailing dot if it hasn't already.
    76  func EnsureTrailingDot(hostname string) string {
    77  	if net.ParseIP(hostname) != nil {
    78  		return hostname
    79  	}
    80  
    81  	return strings.TrimSuffix(hostname, ".") + "."
    82  }
    83  
    84  // Difference tells which entries need to be respectively
    85  // added, removed, or left untouched for "current" to be transformed to "desired"
    86  func Difference(current, desired []string) ([]string, []string, []string) {
    87  	add, remove, leave := []string{}, []string{}, []string{}
    88  	index := make(map[string]struct{}, len(current))
    89  	for _, x := range current {
    90  		index[x] = struct{}{}
    91  	}
    92  	for _, x := range desired {
    93  		if _, found := index[x]; found {
    94  			leave = append(leave, x)
    95  			delete(index, x)
    96  		} else {
    97  			add = append(add, x)
    98  			delete(index, x)
    99  		}
   100  	}
   101  	for x := range index {
   102  		remove = append(remove, x)
   103  	}
   104  	return add, remove, leave
   105  }