external-dns/source/ingress.go
Martin Linkhorst b0f437a438 support hostnames as endpoint targets (CNAME support) (#122)
* feat(aws): support hostnames as endpoint targets

* docs: describe how to run ExternalDNS on AWS

* docs: update changelog with CNAME feature

* docs: update changelog to include AWS documentation

* fix(aws): test that updating records removes the old value

* feat(google): add CNAME support to Google provider

* fix(source): sanitize source and target hostnames

* docs: update changelog to include latest changes

* docs(aws): mention that ExternalDNS takes full ownership of a hosted zone

* fix(aws): switch route53 tests to use endpoint pointers

* docs: add TODO to remove record filtering once ownership is in place
2017-04-05 14:58:10 +02:00

84 lines
2.6 KiB
Go

/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package source
import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/pkg/api/v1"
"k8s.io/client-go/pkg/apis/extensions/v1beta1"
"github.com/kubernetes-incubator/external-dns/endpoint"
)
// ingressSource is an implementation of Source for Kubernetes ingress objects.
// Ingress implementation will use the spec.rules.host value for the hostname
// Ingress annotations are ignored
type ingressSource struct {
client kubernetes.Interface
namespace string
}
// NewIngressSource creates a new ingressSource with the given client and namespace scope.
func NewIngressSource(client kubernetes.Interface, namespace string) Source {
return &ingressSource{client: client, namespace: namespace}
}
// Endpoints returns endpoint objects for each host-target combination that should be processed.
// Retrieves all ingress resources on all namespaces
func (sc *ingressSource) Endpoints() ([]*endpoint.Endpoint, error) {
ingresses, err := sc.client.Extensions().Ingresses(sc.namespace).List(v1.ListOptions{})
if err != nil {
return nil, err
}
endpoints := []*endpoint.Endpoint{}
for _, ing := range ingresses.Items {
ingEndpoints := endpointsFromIngress(&ing)
endpoints = append(endpoints, ingEndpoints...)
}
return endpoints, nil
}
// endpointsFromIngress extracts the endpoints from ingress object
func endpointsFromIngress(ing *v1beta1.Ingress) []*endpoint.Endpoint {
var endpoints []*endpoint.Endpoint
// Check controller annotation to see if we are responsible.
controller, exists := ing.Annotations[controllerAnnotationKey]
if exists && controller != controllerAnnotationValue {
return endpoints
}
for _, rule := range ing.Spec.Rules {
if rule.Host == "" {
continue
}
for _, lb := range ing.Status.LoadBalancer.Ingress {
if lb.IP != "" {
endpoints = append(endpoints, endpoint.NewEndpoint(sanitizeHostname(rule.Host), lb.IP))
}
if lb.Hostname != "" {
endpoints = append(endpoints, endpoint.NewEndpoint(sanitizeHostname(rule.Host), sanitizeHostname(lb.Hostname)))
}
}
}
return endpoints
}