mirror of
https://github.com/kubernetes-sigs/external-dns.git
synced 2025-08-06 01:26:59 +02:00
Merge pull request #4120 from fad3t/feat-amb-provider-specific
feat(ambassador): add support for provider specific annotations
This commit is contained in:
commit
3a2da65e36
@ -6,7 +6,7 @@ The following table documents which sources support which annotations:
|
|||||||
|
|
||||||
| Source | controller | hostname | internal-hostname | target | ttl | (provider-specific) |
|
| Source | controller | hostname | internal-hostname | target | ttl | (provider-specific) |
|
||||||
|--------------|------------|----------|-------------------|---------|---------|---------------------|
|
|--------------|------------|----------|-------------------|---------|---------|---------------------|
|
||||||
| Ambassador | | | | Yes | Yes | |
|
| Ambassador | | | | Yes | Yes | Yes |
|
||||||
| Connector | | | | | | |
|
| Connector | | | | | | |
|
||||||
| Contour | Yes | Yes[^1] | | Yes | Yes | Yes |
|
| Contour | Yes | Yes[^1] | | Yes | Yes | Yes |
|
||||||
| CloudFoundry | | | | | | |
|
| CloudFoundry | | | | | | |
|
||||||
|
@ -166,13 +166,10 @@ func (sc *ambassadorHostSource) Endpoints(ctx context.Context) ([]*endpoint.Endp
|
|||||||
// endpointsFromHost extracts the endpoints from a Host object
|
// endpointsFromHost extracts the endpoints from a Host object
|
||||||
func (sc *ambassadorHostSource) endpointsFromHost(ctx context.Context, host *ambassador.Host, targets endpoint.Targets) ([]*endpoint.Endpoint, error) {
|
func (sc *ambassadorHostSource) endpointsFromHost(ctx context.Context, host *ambassador.Host, targets endpoint.Targets) ([]*endpoint.Endpoint, error) {
|
||||||
var endpoints []*endpoint.Endpoint
|
var endpoints []*endpoint.Endpoint
|
||||||
|
annotations := host.Annotations
|
||||||
providerSpecific := endpoint.ProviderSpecific{}
|
|
||||||
setIdentifier := ""
|
|
||||||
|
|
||||||
resource := fmt.Sprintf("host/%s/%s", host.Namespace, host.Name)
|
resource := fmt.Sprintf("host/%s/%s", host.Namespace, host.Name)
|
||||||
|
providerSpecific, setIdentifier := getProviderSpecificAnnotations(annotations)
|
||||||
annotations := host.Annotations
|
|
||||||
ttl := getTTLFromAnnotations(annotations, resource)
|
ttl := getTTLFromAnnotations(annotations, resource)
|
||||||
|
|
||||||
if host.Spec != nil {
|
if host.Spec != nil {
|
||||||
|
@ -17,19 +17,26 @@ limitations under the License.
|
|||||||
package source
|
package source
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
ambassador "github.com/datawire/ambassador/pkg/api/getambassador.io/v2"
|
ambassador "github.com/datawire/ambassador/pkg/api/getambassador.io/v2"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
"github.com/stretchr/testify/suite"
|
"github.com/stretchr/testify/suite"
|
||||||
"golang.org/x/net/context"
|
"golang.org/x/net/context"
|
||||||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
fakeDynamic "k8s.io/client-go/dynamic/fake"
|
fakeDynamic "k8s.io/client-go/dynamic/fake"
|
||||||
fakeKube "k8s.io/client-go/kubernetes/fake"
|
fakeKube "k8s.io/client-go/kubernetes/fake"
|
||||||
|
"sigs.k8s.io/external-dns/endpoint"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const defaultAmbassadorNamespace = "ambassador"
|
||||||
|
const defaultAmbassadorServiceName = "ambassador"
|
||||||
|
|
||||||
type AmbassadorSuite struct {
|
type AmbassadorSuite struct {
|
||||||
suite.Suite
|
suite.Suite
|
||||||
}
|
}
|
||||||
@ -45,60 +52,284 @@ func testAmbassadorSourceImplementsSource(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestAmbassadorHostSource(t *testing.T) {
|
func TestAmbassadorHostSource(t *testing.T) {
|
||||||
fakeKubernetesClient := fakeKube.NewSimpleClientset()
|
t.Parallel()
|
||||||
|
|
||||||
ambassadorScheme := runtime.NewScheme()
|
hostAnnotation := fmt.Sprintf("%s/%s", defaultAmbassadorNamespace, defaultAmbassadorServiceName)
|
||||||
|
|
||||||
ambassador.AddToScheme(ambassadorScheme)
|
for _, ti := range []struct {
|
||||||
|
title string
|
||||||
|
host ambassador.Host
|
||||||
|
service v1.Service
|
||||||
|
expected []*endpoint.Endpoint
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
title: "Simple host",
|
||||||
|
host: ambassador.Host{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "basic-host",
|
||||||
|
Annotations: map[string]string{
|
||||||
|
ambHostAnnotation: hostAnnotation,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Spec: &ambassador.HostSpec{
|
||||||
|
Hostname: "www.example.org",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
service: v1.Service{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: defaultAmbassadorServiceName,
|
||||||
|
},
|
||||||
|
Status: v1.ServiceStatus{
|
||||||
|
LoadBalancer: v1.LoadBalancerStatus{
|
||||||
|
Ingress: []v1.LoadBalancerIngress{{
|
||||||
|
IP: "1.1.1.1",
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: []*endpoint.Endpoint{
|
||||||
|
{
|
||||||
|
DNSName: "www.example.org",
|
||||||
|
RecordType: endpoint.RecordTypeA,
|
||||||
|
Targets: endpoint.Targets{"1.1.1.1"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, {
|
||||||
|
title: "Service with load balancer hostname",
|
||||||
|
host: ambassador.Host{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "basic-host",
|
||||||
|
Annotations: map[string]string{
|
||||||
|
ambHostAnnotation: hostAnnotation,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Spec: &ambassador.HostSpec{
|
||||||
|
Hostname: "www.example.org",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
service: v1.Service{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: defaultAmbassadorServiceName,
|
||||||
|
},
|
||||||
|
Status: v1.ServiceStatus{
|
||||||
|
LoadBalancer: v1.LoadBalancerStatus{
|
||||||
|
Ingress: []v1.LoadBalancerIngress{{
|
||||||
|
Hostname: "dns.google",
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: []*endpoint.Endpoint{
|
||||||
|
{
|
||||||
|
DNSName: "www.example.org",
|
||||||
|
RecordType: endpoint.RecordTypeCNAME,
|
||||||
|
Targets: endpoint.Targets{"dns.google"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, {
|
||||||
|
title: "Service with external IP",
|
||||||
|
host: ambassador.Host{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "service-external-ip",
|
||||||
|
Annotations: map[string]string{
|
||||||
|
ambHostAnnotation: hostAnnotation,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Spec: &ambassador.HostSpec{
|
||||||
|
Hostname: "www.example.org",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
service: v1.Service{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: defaultAmbassadorServiceName,
|
||||||
|
},
|
||||||
|
Spec: v1.ServiceSpec{
|
||||||
|
ExternalIPs: []string{"2.2.2.2"},
|
||||||
|
},
|
||||||
|
Status: v1.ServiceStatus{
|
||||||
|
LoadBalancer: v1.LoadBalancerStatus{
|
||||||
|
Ingress: []v1.LoadBalancerIngress{{
|
||||||
|
IP: "1.1.1.1",
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: []*endpoint.Endpoint{
|
||||||
|
{
|
||||||
|
DNSName: "www.example.org",
|
||||||
|
RecordType: endpoint.RecordTypeA,
|
||||||
|
Targets: endpoint.Targets{"2.2.2.2"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, {
|
||||||
|
title: "Host with target annotation",
|
||||||
|
host: ambassador.Host{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "basic-host",
|
||||||
|
Annotations: map[string]string{
|
||||||
|
ambHostAnnotation: hostAnnotation,
|
||||||
|
targetAnnotationKey: "3.3.3.3",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Spec: &ambassador.HostSpec{
|
||||||
|
Hostname: "www.example.org",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
service: v1.Service{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: defaultAmbassadorServiceName,
|
||||||
|
},
|
||||||
|
Status: v1.ServiceStatus{
|
||||||
|
LoadBalancer: v1.LoadBalancerStatus{
|
||||||
|
Ingress: []v1.LoadBalancerIngress{{
|
||||||
|
IP: "1.1.1.1",
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: []*endpoint.Endpoint{
|
||||||
|
{
|
||||||
|
DNSName: "www.example.org",
|
||||||
|
RecordType: endpoint.RecordTypeA,
|
||||||
|
Targets: endpoint.Targets{"3.3.3.3"},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, {
|
||||||
|
title: "Host with TTL annotation",
|
||||||
|
host: ambassador.Host{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "basic-host",
|
||||||
|
Annotations: map[string]string{
|
||||||
|
ambHostAnnotation: hostAnnotation,
|
||||||
|
ttlAnnotationKey: "180",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Spec: &ambassador.HostSpec{
|
||||||
|
Hostname: "www.example.org",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
service: v1.Service{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: defaultAmbassadorServiceName,
|
||||||
|
},
|
||||||
|
Status: v1.ServiceStatus{
|
||||||
|
LoadBalancer: v1.LoadBalancerStatus{
|
||||||
|
Ingress: []v1.LoadBalancerIngress{{
|
||||||
|
IP: "1.1.1.1",
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: []*endpoint.Endpoint{
|
||||||
|
{
|
||||||
|
DNSName: "www.example.org",
|
||||||
|
RecordType: endpoint.RecordTypeA,
|
||||||
|
Targets: endpoint.Targets{"1.1.1.1"},
|
||||||
|
RecordTTL: 180,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, {
|
||||||
|
title: "Host with provider specific annotation",
|
||||||
|
host: ambassador.Host{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "basic-host",
|
||||||
|
Annotations: map[string]string{
|
||||||
|
ambHostAnnotation: hostAnnotation,
|
||||||
|
CloudflareProxiedKey: "true",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Spec: &ambassador.HostSpec{
|
||||||
|
Hostname: "www.example.org",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
service: v1.Service{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: defaultAmbassadorServiceName,
|
||||||
|
},
|
||||||
|
Status: v1.ServiceStatus{
|
||||||
|
LoadBalancer: v1.LoadBalancerStatus{
|
||||||
|
Ingress: []v1.LoadBalancerIngress{{
|
||||||
|
IP: "1.1.1.1",
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: []*endpoint.Endpoint{
|
||||||
|
{
|
||||||
|
DNSName: "www.example.org",
|
||||||
|
RecordType: endpoint.RecordTypeA,
|
||||||
|
Targets: endpoint.Targets{"1.1.1.1"},
|
||||||
|
ProviderSpecific: endpoint.ProviderSpecific{{
|
||||||
|
Name: "external-dns.alpha.kubernetes.io/cloudflare-proxied",
|
||||||
|
Value: "true",
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}, {
|
||||||
|
title: "Host with missing Ambassador annotation",
|
||||||
|
host: ambassador.Host{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "basic-host",
|
||||||
|
},
|
||||||
|
Spec: &ambassador.HostSpec{
|
||||||
|
Hostname: "www.example.org",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
service: v1.Service{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: defaultAmbassadorServiceName,
|
||||||
|
},
|
||||||
|
Status: v1.ServiceStatus{
|
||||||
|
LoadBalancer: v1.LoadBalancerStatus{
|
||||||
|
Ingress: []v1.LoadBalancerIngress{{
|
||||||
|
IP: "1.1.1.1",
|
||||||
|
}},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: []*endpoint.Endpoint{},
|
||||||
|
},
|
||||||
|
} {
|
||||||
|
ti := ti
|
||||||
|
t.Run(ti.title, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
fakeDynamicClient := fakeDynamic.NewSimpleDynamicClient(ambassadorScheme)
|
fakeKubernetesClient := fakeKube.NewSimpleClientset()
|
||||||
|
ambassadorScheme := runtime.NewScheme()
|
||||||
|
ambassador.AddToScheme(ambassadorScheme)
|
||||||
|
fakeDynamicClient := fakeDynamic.NewSimpleDynamicClient(ambassadorScheme)
|
||||||
|
|
||||||
ctx := context.Background()
|
namespace := "default"
|
||||||
|
|
||||||
namespace := "test"
|
// Create Ambassador service
|
||||||
|
_, err := fakeKubernetesClient.CoreV1().Services(defaultAmbassadorNamespace).Create(context.Background(), &ti.service, metav1.CreateOptions{})
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
host, err := createAmbassadorHost("test-host", "test-service")
|
// Create host resource
|
||||||
if err != nil {
|
host, err := createAmbassadorHost(&ti.host)
|
||||||
t.Fatalf("could not create host resource: %v", err)
|
assert.NoError(t, err)
|
||||||
}
|
|
||||||
|
|
||||||
{
|
_, err = fakeDynamicClient.Resource(ambHostGVR).Namespace(namespace).Create(context.Background(), host, metav1.CreateOptions{})
|
||||||
_, err := fakeDynamicClient.Resource(ambHostGVR).Namespace(namespace).Create(ctx, host, v1.CreateOptions{})
|
assert.NoError(t, err)
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("could not create host: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ambassadorSource, err := NewAmbassadorHostSource(ctx, fakeDynamicClient, fakeKubernetesClient, namespace)
|
source, err := NewAmbassadorHostSource(context.TODO(), fakeDynamicClient, fakeKubernetesClient, namespace)
|
||||||
if err != nil {
|
assert.NoError(t, err)
|
||||||
t.Fatalf("could not create ambassador source: %v", err)
|
assert.NotNil(t, source)
|
||||||
}
|
|
||||||
|
|
||||||
{
|
endpoints, err := source.Endpoints(context.Background())
|
||||||
_, err := ambassadorSource.Endpoints(ctx)
|
assert.NoError(t, err)
|
||||||
if err != nil {
|
// Validate returned endpoints against expected endpoints.
|
||||||
t.Fatalf("could not collect ambassador source endpoints: %v", err)
|
validateEndpoints(t, endpoints, ti.expected)
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func createAmbassadorHost(name, ambassadorService string) (*unstructured.Unstructured, error) {
|
func createAmbassadorHost(host *ambassador.Host) (*unstructured.Unstructured, error) {
|
||||||
host := &ambassador.Host{
|
|
||||||
ObjectMeta: v1.ObjectMeta{
|
|
||||||
Name: name,
|
|
||||||
Annotations: map[string]string{
|
|
||||||
ambHostAnnotation: ambassadorService,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
obj := &unstructured.Unstructured{}
|
obj := &unstructured.Unstructured{}
|
||||||
uc, _ := newUnstructuredConverter()
|
uc, _ := newUnstructuredConverter()
|
||||||
err := uc.scheme.Convert(host, obj, nil)
|
err := uc.scheme.Convert(host, obj, nil)
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return obj, nil
|
return obj, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestParseAmbLoadBalancerService tests our parsing of Ambassador service info.
|
// TestParseAmbLoadBalancerService tests our parsing of Ambassador service info.
|
||||||
@ -126,7 +357,6 @@ func TestParseAmbLoadBalancerService(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
errstr = err.Error()
|
errstr = err.Error()
|
||||||
}
|
}
|
||||||
|
|
||||||
if v.ns != ns {
|
if v.ns != ns {
|
||||||
t.Errorf("%s: got ns \"%s\", wanted \"%s\"", v.input, ns, v.ns)
|
t.Errorf("%s: got ns \"%s\", wanted \"%s\"", v.input, ns, v.ns)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user