-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Ambassador Host resources as sources
Ambassador can be configured with `Host` resources (based on the `Host` CRD) for defining the external DNS host name. This code adds a new source, `ambassador-host`, that looks for the `ambassador/ambassador` Service and and uses the `hostname` from the `Host` resource. Signed-off-by: Alvaro Saurin <alvaro.saurin@gmail.com>
- Loading branch information
Showing
6 changed files
with
319 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,241 @@ | ||
/* | ||
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 ( | ||
"context" | ||
"fmt" | ||
"sort" | ||
"strings" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
log "github.com/sirupsen/logrus" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
"k8s.io/client-go/dynamic" | ||
"k8s.io/client-go/dynamic/dynamicinformer" | ||
"k8s.io/client-go/informers" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/cache" | ||
|
||
"sigs.k8s.io/external-dns/endpoint" | ||
) | ||
|
||
// ambHostAnnotation is the annotation in the Host that maps to a Service | ||
const ambHostAnnotation = "external-dns.ambassador-service" | ||
|
||
// groupName is the group name for the Ambassador API | ||
const groupName = "getambassador.io" | ||
|
||
var schemeGroupVersion = schema.GroupVersion{Group: groupName, Version: "v2"} | ||
|
||
var ambHostGVR = schemeGroupVersion.WithResource("hosts") | ||
|
||
// ambassadorHostSource is an implementation of Source for Ambassador Host objects. | ||
// The IngressRoute implementation uses the spec.virtualHost.fqdn value for the hostname. | ||
// Use targetAnnotationKey to explicitly set Endpoint. | ||
type ambassadorHostSource struct { | ||
dynamicKubeClient dynamic.Interface | ||
kubeClient kubernetes.Interface | ||
namespace string | ||
ambassadorHostInformer informers.GenericInformer | ||
} | ||
|
||
// NewAmbassadorHostSource creates a new ambassadorHostSource with the given config. | ||
func NewAmbassadorHostSource( | ||
dynamicKubeClient dynamic.Interface, | ||
kubeClient kubernetes.Interface, | ||
namespace string) (Source, error) { | ||
var err error | ||
|
||
// Use shared informer to listen for add/update/delete of Host in the specified namespace. | ||
// Set resync period to 0, to prevent processing when nothing has changed. | ||
informerFactory := dynamicinformer.NewFilteredDynamicSharedInformerFactory(dynamicKubeClient, 0, namespace, nil) | ||
ambassadorHostInformer := informerFactory.ForResource(ambHostGVR) | ||
|
||
// Add default resource event handlers to properly initialize informer. | ||
ambassadorHostInformer.Informer().AddEventHandler( | ||
cache.ResourceEventHandlerFuncs{ | ||
AddFunc: func(obj interface{}) { | ||
}, | ||
}, | ||
) | ||
|
||
// TODO informer is not explicitly stopped since controller is not passing in its channel. | ||
informerFactory.Start(wait.NeverStop) | ||
|
||
// wait for the local cache to be populated. | ||
err = poll(time.Second, 60*time.Second, func() (bool, error) { | ||
return ambassadorHostInformer.Informer().HasSynced(), nil | ||
}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to sync cache: %v", err) | ||
} | ||
|
||
return &ambassadorHostSource{ | ||
dynamicKubeClient: dynamicKubeClient, | ||
kubeClient: kubeClient, | ||
namespace: namespace, | ||
ambassadorHostInformer: ambassadorHostInformer, | ||
}, nil | ||
} | ||
|
||
// Endpoints returns endpoint objects for each host-target combination that should be processed. | ||
// Retrieves all Hosts in the source's namespace(s). | ||
func (sc *ambassadorHostSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, error) { | ||
hosts, err := sc.ambassadorHostInformer.Lister().ByNamespace(sc.namespace).List(labels.Everything()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
endpoints := []*endpoint.Endpoint{} | ||
|
||
for _, host := range hosts { | ||
unstructuredHost, ok := host.(*unstructured.Unstructured) | ||
if !ok { | ||
return nil, errors.New("could not convert") | ||
} | ||
|
||
fullname := fmt.Sprintf("%s/%s", unstructuredHost.GetNamespace(), unstructuredHost.GetName()) | ||
|
||
annotations, found, err := parseAmbHostAnnotations(unstructuredHost) | ||
if err != nil { | ||
log.Warn(err) | ||
continue | ||
} | ||
if !found { | ||
log.Debugf("Host %s ignored: no annotations found", fullname) | ||
continue | ||
} | ||
|
||
// look for the "exernal-dns.ambassador-service" annotation. If it is not there then just ignore this `Host` | ||
service, found := annotations[ambHostAnnotation] | ||
if !found { | ||
log.Debugf("Host %s ignored: no annotation %q found", fullname, ambHostAnnotation) | ||
continue | ||
} | ||
|
||
targets, err := sc.targetsFromAmbassadorLoadBalancer(ctx, service) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
hostEndpoints, err := sc.endpointsFromHost(ctx, unstructuredHost, targets) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if len(hostEndpoints) == 0 { | ||
log.Debugf("No endpoints could be generated from Host %s", fullname) | ||
continue | ||
} | ||
|
||
log.Debugf("Endpoints generated from Host: %s: %v", fullname, hostEndpoints) | ||
endpoints = append(endpoints, hostEndpoints...) | ||
} | ||
|
||
for _, ep := range endpoints { | ||
sort.Sort(ep.Targets) | ||
} | ||
|
||
return endpoints, nil | ||
} | ||
|
||
// endpointsFromHost extracts the endpoints from a Host object | ||
func (sc *ambassadorHostSource) endpointsFromHost(ctx context.Context, host *unstructured.Unstructured, targets endpoint.Targets) ([]*endpoint.Endpoint, error) { | ||
var endpoints []*endpoint.Endpoint | ||
|
||
providerSpecific := endpoint.ProviderSpecific{} | ||
setIdentifier := "" | ||
|
||
annotations, found, err := parseAmbHostAnnotations(host) | ||
if err != nil { | ||
log.Warn(err) | ||
} | ||
if found { | ||
providerSpecific, setIdentifier = getProviderSpecificAnnotations(annotations) | ||
} | ||
|
||
ttl, err := getTTLFromAnnotations(annotations) | ||
if err != nil { | ||
log.Warn(err) | ||
} | ||
|
||
hostname, found, err := parseAmbHostHostname(host) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if found { | ||
endpoints = append(endpoints, endpointsForHostname(hostname, targets, ttl, providerSpecific, setIdentifier)...) | ||
} | ||
|
||
return endpoints, nil | ||
} | ||
|
||
func (sc *ambassadorHostSource) targetsFromAmbassadorLoadBalancer(ctx context.Context, service string) (targets endpoint.Targets, err error) { | ||
lbNamespace, lbName, err := parseAmbLoadBalancerService(service) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if svc, err := sc.kubeClient.CoreV1().Services(lbNamespace).Get(ctx, lbName, metav1.GetOptions{}); err != nil { | ||
log.Warn(err) | ||
} else { | ||
for _, lb := range svc.Status.LoadBalancer.Ingress { | ||
if lb.IP != "" { | ||
targets = append(targets, lb.IP) | ||
} | ||
if lb.Hostname != "" { | ||
targets = append(targets, lb.Hostname) | ||
} | ||
} | ||
} | ||
|
||
return | ||
} | ||
|
||
// parseAmbHostHostname returns the hostname in a `Host` | ||
func parseAmbHostHostname(host *unstructured.Unstructured) (string, bool, error) { | ||
return unstructured.NestedString(host.Object, "spec", "hostname") | ||
} | ||
|
||
// parseAmbHostAnnotations returns the annotations in a `Host` | ||
func parseAmbHostAnnotations(host *unstructured.Unstructured) (map[string]string, bool, error) { | ||
return unstructured.NestedStringMap(host.Object, "metadata", "annotations") | ||
} | ||
|
||
// parseAmbLoadBalancerService returns a name/namespace tuple from the annotation in a `Host` | ||
func parseAmbLoadBalancerService(service string) (namespace, name string, err error) { | ||
parts := strings.Split(service, "/") | ||
if len(parts) == 2 { | ||
namespace, name = parts[0], parts[1] | ||
} else { | ||
parts = strings.Split(service, ".") | ||
if len(parts) == 2 { | ||
name, namespace = parts[0], parts[1] | ||
} else { | ||
err = fmt.Errorf("invalid Ambassador load balancer service (namespace/name or name.namespace) found '%v'", service) | ||
} | ||
} | ||
|
||
return | ||
} | ||
|
||
func (sc *ambassadorHostSource) AddEventHandler(ctx context.Context, handler func()) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/* | ||
Copyright 2019 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 ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type AmbassadorSuite struct { | ||
suite.Suite | ||
} | ||
|
||
func TestAmbassadorSource(t *testing.T) { | ||
suite.Run(t, new(AmbassadorSuite)) | ||
t.Run("Interface", testAmbassadorSourceImplementsSource) | ||
} | ||
|
||
// testAmbassadorSourceImplementsSource tests that ambassadorHostSource is a valid Source. | ||
func testAmbassadorSourceImplementsSource(t *testing.T) { | ||
require.Implements(t, (*Source)(nil), new(ambassadorHostSource)) | ||
} |
Oops, something went wrong.