From 6bf1c2bcb54881673a694ba923ff64314d11db56 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Mar=C3=A7al?= Date: Thu, 18 Jul 2019 10:11:34 +0200 Subject: [PATCH] Added "external-dns/resource=..." field to CRDs usefull to solve conflits Added warning when CR has a target with a "." in the end, which would make external-dns contantly add and remove the record MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: João Marçal --- source/crd.go | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/source/crd.go b/source/crd.go index 60fbe7b9a..cbab76efe 100644 --- a/source/crd.go +++ b/source/crd.go @@ -110,6 +110,7 @@ func NewCRDSource(crdClient rest.Interface, namespace, kind string, scheme *runt // Endpoints returns endpoint objects. func (cs *crdSource) Endpoints() ([]*endpoint.Endpoint, error) { endpoints := []*endpoint.Endpoint{} + crdEndpoints := []*endpoint.Endpoint{} result, err := cs.List(&metav1.ListOptions{}) if err != nil { @@ -118,14 +119,35 @@ func (cs *crdSource) Endpoints() ([]*endpoint.Endpoint, error) { for _, dnsEndpoint := range result.Items { // Make sure that all endpoints have targets for A or CNAME type - for _, endpoint := range dnsEndpoint.Spec.Endpoints { - if (endpoint.RecordType == "CNAME" || endpoint.RecordType == "A") && len(endpoint.Targets) < 1 { - log.Warnf("Endpoint %s with DNSName %s has an empty list of targets", dnsEndpoint.ObjectMeta.Name, endpoint.DNSName) + crdEndpoints = []*endpoint.Endpoint{} + for _, ep := range dnsEndpoint.Spec.Endpoints { + if (ep.RecordType == "CNAME" || ep.RecordType == "A") && len(ep.Targets) < 1 { + log.Warnf("Endpoint %s with DNSName %s has an empty list of targets", dnsEndpoint.ObjectMeta.Name, ep.DNSName) continue } - endpoints = append(endpoints, endpoint) + + illegalTarget := false + for _, target := range ep.Targets{ + if strings.HasSuffix(target, "."){ + illegalTarget = true + break + } + } + if illegalTarget{ + log.Warnf("Endpoint %s with DNSName %s has Target illegal target. The subdomain must consist of lower case alphanumeric characters, '-' or '.', and must start and end with an alphanumeric character (e.g. 'example.com')", dnsEndpoint.ObjectMeta.Name, ep.DNSName) + continue + } + + if ep.Labels == nil{ + ep.Labels = endpoint.NewLabels() + } + + crdEndpoints = append(crdEndpoints, ep) } + cs.setResourceLabel(&dnsEndpoint, crdEndpoints) + endpoints = append(endpoints, crdEndpoints...) + if dnsEndpoint.Status.ObservedGeneration == dnsEndpoint.Generation { continue } @@ -141,6 +163,12 @@ func (cs *crdSource) Endpoints() ([]*endpoint.Endpoint, error) { return endpoints, nil } +func (cs *crdSource) setResourceLabel(crd *endpoint.DNSEndpoint, endpoints []*endpoint.Endpoint) { + for _, ep := range endpoints { + ep.Labels[endpoint.ResourceLabelKey] = fmt.Sprintf("crd/%s/%s", crd.ObjectMeta.Namespace, crd.ObjectMeta.Name) + } +} + func (cs *crdSource) List(opts *metav1.ListOptions) (result *endpoint.DNSEndpointList, err error) { result = &endpoint.DNSEndpointList{} err = cs.crdClient.Get().