sigs.k8s.io/external-dns@v0.14.1/registry/aws_sd_registry.go (about)

     1  /*
     2  Copyright 2018 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 registry
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  
    23  	"sigs.k8s.io/external-dns/endpoint"
    24  	"sigs.k8s.io/external-dns/plan"
    25  	"sigs.k8s.io/external-dns/provider"
    26  )
    27  
    28  // AWSSDRegistry implements registry interface with ownership information associated via the Description field of SD Service
    29  type AWSSDRegistry struct {
    30  	provider provider.Provider
    31  	ownerID  string
    32  }
    33  
    34  // NewAWSSDRegistry returns implementation of registry for AWS SD
    35  func NewAWSSDRegistry(provider provider.Provider, ownerID string) (*AWSSDRegistry, error) {
    36  	if ownerID == "" {
    37  		return nil, errors.New("owner id cannot be empty")
    38  	}
    39  	return &AWSSDRegistry{
    40  		provider: provider,
    41  		ownerID:  ownerID,
    42  	}, nil
    43  }
    44  
    45  func (sdr *AWSSDRegistry) GetDomainFilter() endpoint.DomainFilter {
    46  	return sdr.provider.GetDomainFilter()
    47  }
    48  
    49  func (im *AWSSDRegistry) OwnerID() string {
    50  	return im.ownerID
    51  }
    52  
    53  // Records calls AWS SD API and expects AWS SD provider to provider Owner/Resource information as a serialized
    54  // value in the AWSSDDescriptionLabel value in the Labels map
    55  func (sdr *AWSSDRegistry) Records(ctx context.Context) ([]*endpoint.Endpoint, error) {
    56  	records, err := sdr.provider.Records(ctx)
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	for _, record := range records {
    62  		labels, err := endpoint.NewLabelsFromStringPlain(record.Labels[endpoint.AWSSDDescriptionLabel])
    63  		if err != nil {
    64  			// if we fail to parse the output then simply assume the endpoint is not managed by any instance of External DNS
    65  			record.Labels = endpoint.NewLabels()
    66  			continue
    67  		}
    68  		record.Labels = labels
    69  	}
    70  
    71  	return records, nil
    72  }
    73  
    74  // ApplyChanges filters out records not owned the External-DNS, additionally it adds the required label
    75  // inserted in the AWS SD instance as a CreateID field
    76  func (sdr *AWSSDRegistry) ApplyChanges(ctx context.Context, changes *plan.Changes) error {
    77  	filteredChanges := &plan.Changes{
    78  		Create:    changes.Create,
    79  		UpdateNew: endpoint.FilterEndpointsByOwnerID(sdr.ownerID, changes.UpdateNew),
    80  		UpdateOld: endpoint.FilterEndpointsByOwnerID(sdr.ownerID, changes.UpdateOld),
    81  		Delete:    endpoint.FilterEndpointsByOwnerID(sdr.ownerID, changes.Delete),
    82  	}
    83  
    84  	sdr.updateLabels(filteredChanges.Create)
    85  	sdr.updateLabels(filteredChanges.UpdateNew)
    86  	sdr.updateLabels(filteredChanges.UpdateOld)
    87  	sdr.updateLabels(filteredChanges.Delete)
    88  
    89  	return sdr.provider.ApplyChanges(ctx, filteredChanges)
    90  }
    91  
    92  func (sdr *AWSSDRegistry) updateLabels(endpoints []*endpoint.Endpoint) {
    93  	for _, ep := range endpoints {
    94  		if ep.Labels == nil {
    95  			ep.Labels = make(map[string]string)
    96  		}
    97  		ep.Labels[endpoint.OwnerLabelKey] = sdr.ownerID
    98  		ep.Labels[endpoint.AWSSDDescriptionLabel] = ep.Labels.SerializePlain(false)
    99  	}
   100  }
   101  
   102  // AdjustEndpoints modifies the endpoints as needed by the specific provider
   103  func (sdr *AWSSDRegistry) AdjustEndpoints(endpoints []*endpoint.Endpoint) ([]*endpoint.Endpoint, error) {
   104  	return sdr.provider.AdjustEndpoints(endpoints)
   105  }