external-dns/source/ingress.go
Yerken 25eef9159c [PR-156 follow-up] Generate endpoints hostnames if go-template is specified (#160)
* add --fqdn-template

* add missing ,

* gofmt

* no endpoint creation on empty fqdntemplate

* improve test coverage

* gofmt simple on service_test.go and ingress_test.go

* import package order changed

* gofmt

* refactor to generate template in the source init

* refactor for err handling

* fix service tests

* fix wrong check, check for priorities, mate > template

* fix tests, check for controller annotation in the right place

* add to changelog

* add flag description, improve testing, reorganize imports

* review changes: log the error, use text/template, change func interface
2017-04-18 18:13:08 +02:00

134 lines
3.7 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 (
"bytes"
"strings"
"text/template"
log "github.com/Sirupsen/logrus"
"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
fqdntemplate *template.Template
}
// NewIngressSource creates a new ingressSource with the given client and namespace scope.
func NewIngressSource(client kubernetes.Interface, namespace string, fqdntemplate string) (Source, error) {
var tmpl *template.Template
var err error
if fqdntemplate != "" {
tmpl, err = template.New("endpoint").Funcs(template.FuncMap{
"trimPrefix": strings.TrimPrefix,
}).Parse(fqdntemplate)
if err != nil {
return nil, err
}
}
return &ingressSource{
client: client,
namespace: namespace,
fqdntemplate: tmpl,
}, nil
}
// 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 {
// Check controller annotation to see if we are responsible.
controller, ok := ing.Annotations[controllerAnnotationKey]
if ok && controller != controllerAnnotationValue { //TODO(ideahitme): log the skip
continue
}
ingEndpoints := endpointsFromIngress(&ing)
// apply template if host is missing on ingress
if len(ingEndpoints) == 0 && sc.fqdntemplate != nil {
ingEndpoints = sc.endpointsFromTemplate(&ing)
}
endpoints = append(endpoints, ingEndpoints...)
}
return endpoints, nil
}
func (sc *ingressSource) endpointsFromTemplate(ing *v1beta1.Ingress) []*endpoint.Endpoint {
var endpoints []*endpoint.Endpoint
var buf bytes.Buffer
err := sc.fqdntemplate.Execute(&buf, ing)
if err != nil {
log.Errorf("failed to apply template: %v", err)
return nil
}
hostname := buf.String()
for _, lb := range ing.Status.LoadBalancer.Ingress {
if lb.IP != "" {
endpoints = append(endpoints, endpoint.NewEndpoint(hostname, lb.IP, ""))
}
if lb.Hostname != "" {
endpoints = append(endpoints, endpoint.NewEndpoint(hostname, lb.Hostname, ""))
}
}
return endpoints
}
// endpointsFromIngress extracts the endpoints from ingress object
func endpointsFromIngress(ing *v1beta1.Ingress) []*endpoint.Endpoint {
var endpoints []*endpoint.Endpoint
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(rule.Host, lb.IP, ""))
}
if lb.Hostname != "" {
endpoints = append(endpoints, endpoint.NewEndpoint(rule.Host, lb.Hostname, ""))
}
}
}
return endpoints
}