Skip to content

Commit f583111

Browse files
committed
refactor: decouple CRD types from internal endpoint package
1 parent 72b1f30 commit f583111

File tree

4 files changed

+145
-27
lines changed

4 files changed

+145
-27
lines changed

apis/v1alpha1/dnsendpoint.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ package v1alpha1
1818

1919
import (
2020
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21-
22-
"sigs.k8s.io/external-dns/endpoint"
2321
)
2422

2523
// +genclient
@@ -51,7 +49,36 @@ type DNSEndpointList struct {
5149

5250
// DNSEndpointSpec defines the desired state of DNSEndpoint
5351
type DNSEndpointSpec struct {
54-
Endpoints []*endpoint.Endpoint `json:"endpoints,omitempty"`
52+
Endpoints []*Endpoint `json:"endpoints"`
53+
}
54+
55+
type ProviderSpecificProperty struct {
56+
Name string `json:"name,omitempty"`
57+
Value string `json:"value,omitempty"`
58+
}
59+
60+
// ProviderSpecific holds configuration which is specific to individual DNS providers
61+
type ProviderSpecific []ProviderSpecificProperty
62+
63+
// Endpoint is a high-level way of a connection between a service and an IP
64+
// +kubebuilder:object:generate=true
65+
type Endpoint struct {
66+
// The hostname of the DNS record
67+
DNSName string `json:"dnsName,omitempty"`
68+
// The targets the DNS record points to
69+
Targets []string `json:"targets,omitempty"`
70+
// RecordType type of record, e.g. CNAME, A, AAAA, SRV, TXT etc
71+
RecordType string `json:"recordType,omitempty"`
72+
// Identifier to distinguish multiple records with the same name and type (e.g. Route53 records with routing policies other than 'simple')
73+
SetIdentifier string `json:"setIdentifier,omitempty"`
74+
// TTL for the record
75+
RecordTTL int64 `json:"recordTTL,omitempty"`
76+
// Labels stores labels defined for the Endpoint
77+
// +optional
78+
Labels map[string]string `json:"labels,omitempty"`
79+
// ProviderSpecific stores provider specific config
80+
// +optional
81+
ProviderSpecific ProviderSpecific `json:"providerSpecific,omitempty"`
5582
}
5683

5784
// DNSEndpointStatus defines the observed state of DNSEndpoint

apis/v1alpha1/zz_generated.deepcopy.go

Lines changed: 68 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

source/crd.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,27 @@ func (cs *crdSource) AddEventHandler(_ context.Context, handler func()) {
160160
}
161161
}
162162

163+
func fromCRDEndpoint(crdEp *apiv1alpha1.Endpoint) *endpoint.Endpoint {
164+
//TODO:
165+
p := endpoint.ProviderSpecific{}
166+
for _, ps := range crdEp.ProviderSpecific {
167+
p = append(p, endpoint.ProviderSpecificProperty{
168+
Name: ps.Name,
169+
Value: ps.Value,
170+
})
171+
}
172+
ep := &endpoint.Endpoint{
173+
DNSName: crdEp.DNSName,
174+
Targets: crdEp.Targets,
175+
RecordType: crdEp.RecordType,
176+
SetIdentifier: crdEp.SetIdentifier,
177+
RecordTTL: endpoint.TTL(crdEp.RecordTTL),
178+
Labels: crdEp.Labels,
179+
ProviderSpecific: p,
180+
}
181+
return ep
182+
}
183+
163184
// Endpoints returns endpoint objects.
164185
func (cs *crdSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, error) {
165186
endpoints := []*endpoint.Endpoint{}
@@ -181,7 +202,8 @@ func (cs *crdSource) Endpoints(ctx context.Context) ([]*endpoint.Endpoint, error
181202

182203
for _, dnsEndpoint := range result.Items {
183204
var crdEndpoints []*endpoint.Endpoint
184-
for _, ep := range dnsEndpoint.Spec.Endpoints {
205+
for _, crdEp := range dnsEndpoint.Spec.Endpoints {
206+
ep := fromCRDEndpoint(crdEp)
185207
if (ep.RecordType == endpoint.RecordTypeCNAME || ep.RecordType == endpoint.RecordTypeA || ep.RecordType == endpoint.RecordTypeAAAA) && len(ep.Targets) < 1 {
186208
log.Debugf("Endpoint %s with DNSName %s has an empty list of targets, allowing it to pass through for default-targets processing", dnsEndpoint.Name, ep.DNSName)
187209
}

source/crd_test.go

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func objBody(codec runtime.Encoder, obj runtime.Object) io.ReadCloser {
6262
return io.NopCloser(bytes.NewReader([]byte(runtime.EncodeOrDie(codec, obj))))
6363
}
6464

65-
func fakeRESTClient(endpoints []*endpoint.Endpoint, apiVersion, kind, namespace, name string, annotations map[string]string, labels map[string]string, _ *testing.T) rest.Interface {
65+
func fakeRESTClient(endpoints []*apiv1alpha1.Endpoint, apiVersion, kind, namespace, name string, annotations map[string]string, labels map[string]string, _ *testing.T) rest.Interface {
6666
groupVersion, _ := schema.ParseGroupVersion(apiVersion)
6767
scheme := runtime.NewScheme()
6868
_ = addKnownTypes(scheme, groupVersion)
@@ -143,7 +143,7 @@ func testCRDSourceEndpoints(t *testing.T) {
143143
apiVersion string
144144
registeredKind string
145145
kind string
146-
endpoints []*endpoint.Endpoint
146+
endpoints []*apiv1alpha1.Endpoint
147147
expectEndpoints bool
148148
expectError bool
149149
annotationFilter string
@@ -157,7 +157,7 @@ func testCRDSourceEndpoints(t *testing.T) {
157157
apiVersion: "blah.k8s.io/v1alpha1",
158158
registeredKind: "DNSEndpoint",
159159
kind: "DNSEndpoint",
160-
endpoints: []*endpoint.Endpoint{
160+
endpoints: []*apiv1alpha1.Endpoint{
161161
{
162162
DNSName: "abc.example.org",
163163
Targets: endpoint.Targets{"1.2.3.4"},
@@ -174,7 +174,7 @@ func testCRDSourceEndpoints(t *testing.T) {
174174
apiVersion: "test.k8s.io/v1alpha1",
175175
registeredKind: "DNSEndpoint",
176176
kind: "JustEndpoint",
177-
endpoints: []*endpoint.Endpoint{
177+
endpoints: []*apiv1alpha1.Endpoint{
178178
{
179179
DNSName: "abc.example.org",
180180
Targets: endpoint.Targets{"1.2.3.4"},
@@ -193,7 +193,7 @@ func testCRDSourceEndpoints(t *testing.T) {
193193
kind: "DNSEndpoint",
194194
namespace: "foo",
195195
registeredNamespace: "foo",
196-
endpoints: []*endpoint.Endpoint{
196+
endpoints: []*apiv1alpha1.Endpoint{
197197
{
198198
DNSName: "abc.example.org",
199199
Targets: endpoint.Targets{"1.2.3.4"},
@@ -212,7 +212,7 @@ func testCRDSourceEndpoints(t *testing.T) {
212212
kind: "DNSEndpoint",
213213
namespace: "foo",
214214
registeredNamespace: "bar",
215-
endpoints: []*endpoint.Endpoint{
215+
endpoints: []*apiv1alpha1.Endpoint{
216216
{
217217
DNSName: "abc.example.org",
218218
Targets: endpoint.Targets{"1.2.3.4"},
@@ -231,7 +231,7 @@ func testCRDSourceEndpoints(t *testing.T) {
231231
kind: "DNSEndpoint",
232232
namespace: "foo",
233233
registeredNamespace: "foo",
234-
endpoints: []*endpoint.Endpoint{
234+
endpoints: []*apiv1alpha1.Endpoint{
235235
{
236236
DNSName: "no-targets.example.org",
237237
Targets: endpoint.Targets{},
@@ -250,7 +250,7 @@ func testCRDSourceEndpoints(t *testing.T) {
250250
kind: "DNSEndpoint",
251251
namespace: "foo",
252252
registeredNamespace: "foo",
253-
endpoints: []*endpoint.Endpoint{
253+
endpoints: []*apiv1alpha1.Endpoint{
254254
{
255255
DNSName: "abc.example.org",
256256
Targets: endpoint.Targets{"1.2.3.4"},
@@ -269,7 +269,7 @@ func testCRDSourceEndpoints(t *testing.T) {
269269
kind: "DNSEndpoint",
270270
namespace: "foo",
271271
registeredNamespace: "foo",
272-
endpoints: []*endpoint.Endpoint{
272+
endpoints: []*apiv1alpha1.Endpoint{
273273
{
274274
DNSName: "abc.example.org",
275275
Targets: endpoint.Targets{"1.2.3.4"},
@@ -296,7 +296,7 @@ func testCRDSourceEndpoints(t *testing.T) {
296296
registeredNamespace: "foo",
297297
annotations: map[string]string{"test": "that"},
298298
annotationFilter: "test=filter_something_else",
299-
endpoints: []*endpoint.Endpoint{
299+
endpoints: []*apiv1alpha1.Endpoint{
300300
{
301301
DNSName: "abc.example.org",
302302
Targets: endpoint.Targets{"1.2.3.4"},
@@ -317,7 +317,7 @@ func testCRDSourceEndpoints(t *testing.T) {
317317
registeredNamespace: "foo",
318318
annotations: map[string]string{"test": "that"},
319319
annotationFilter: "test=that",
320-
endpoints: []*endpoint.Endpoint{
320+
endpoints: []*apiv1alpha1.Endpoint{
321321
{
322322
DNSName: "abc.example.org",
323323
Targets: endpoint.Targets{"1.2.3.4"},
@@ -338,7 +338,7 @@ func testCRDSourceEndpoints(t *testing.T) {
338338
registeredNamespace: "foo",
339339
labels: map[string]string{"test": "that"},
340340
labelFilter: "test=filter_something_else",
341-
endpoints: []*endpoint.Endpoint{
341+
endpoints: []*apiv1alpha1.Endpoint{
342342
{
343343
DNSName: "abc.example.org",
344344
Targets: endpoint.Targets{"1.2.3.4"},
@@ -359,7 +359,7 @@ func testCRDSourceEndpoints(t *testing.T) {
359359
registeredNamespace: "foo",
360360
labels: map[string]string{"test": "that"},
361361
labelFilter: "test=that",
362-
endpoints: []*endpoint.Endpoint{
362+
endpoints: []*apiv1alpha1.Endpoint{
363363
{
364364
DNSName: "abc.example.org",
365365
Targets: endpoint.Targets{"1.2.3.4"},
@@ -380,7 +380,7 @@ func testCRDSourceEndpoints(t *testing.T) {
380380
registeredNamespace: "foo",
381381
labels: map[string]string{"test": "that"},
382382
labelFilter: "test=that",
383-
endpoints: []*endpoint.Endpoint{
383+
endpoints: []*apiv1alpha1.Endpoint{
384384
{
385385
DNSName: "abc.example.org",
386386
Targets: endpoint.Targets{"ns1.k8s.io", "ns2.k8s.io"},
@@ -401,7 +401,7 @@ func testCRDSourceEndpoints(t *testing.T) {
401401
registeredNamespace: "foo",
402402
labels: map[string]string{"test": "that"},
403403
labelFilter: "test=that",
404-
endpoints: []*endpoint.Endpoint{
404+
endpoints: []*apiv1alpha1.Endpoint{
405405
{
406406
DNSName: "_svc._tcp.example.org",
407407
Targets: endpoint.Targets{"0 0 80 abc.example.org", "0 0 80 def.example.org"},
@@ -422,7 +422,7 @@ func testCRDSourceEndpoints(t *testing.T) {
422422
registeredNamespace: "foo",
423423
labels: map[string]string{"test": "that"},
424424
labelFilter: "test=that",
425-
endpoints: []*endpoint.Endpoint{
425+
endpoints: []*apiv1alpha1.Endpoint{
426426
{
427427
DNSName: "example.org",
428428
Targets: endpoint.Targets{`100 10 "S" "SIP+D2U" "!^.*$!sip:[email protected]!" _sip._udp.example.org.`, `102 10 "S" "SIP+D2T" "!^.*$!sip:[email protected]!" _sip._tcp.example.org.`},
@@ -443,7 +443,7 @@ func testCRDSourceEndpoints(t *testing.T) {
443443
registeredNamespace: "foo",
444444
labels: map[string]string{"test": "that"},
445445
labelFilter: "test=that",
446-
endpoints: []*endpoint.Endpoint{
446+
endpoints: []*apiv1alpha1.Endpoint{
447447
{
448448
DNSName: "example.org",
449449
Targets: endpoint.Targets{"foo.example.org."},
@@ -464,7 +464,7 @@ func testCRDSourceEndpoints(t *testing.T) {
464464
registeredNamespace: "foo",
465465
labels: map[string]string{"test": "that"},
466466
labelFilter: "test=that",
467-
endpoints: []*endpoint.Endpoint{
467+
endpoints: []*apiv1alpha1.Endpoint{
468468
{
469469
DNSName: "example.org",
470470
Targets: endpoint.Targets{`100 10 "S" "SIP+D2U" "!^.*$!sip:[email protected]!" _sip._udp.example.org`, `102 10 "S" "SIP+D2T" "!^.*$!sip:[email protected]!" _sip._tcp.example.org`},
@@ -513,7 +513,11 @@ func testCRDSourceEndpoints(t *testing.T) {
513513
}
514514

515515
// Validate received endpoints against expected endpoints.
516-
validateEndpoints(t, receivedEndpoints, ti.endpoints)
516+
endpoints := make([]*endpoint.Endpoint, 0, len(ti.endpoints))
517+
for _, ep := range ti.endpoints {
518+
endpoints = append(endpoints, fromCRDEndpoint(ep))
519+
}
520+
validateEndpoints(t, receivedEndpoints, endpoints)
517521

518522
for _, e := range receivedEndpoints {
519523
// TODO: at the moment not all sources apply ResourceLabelKey
@@ -754,7 +758,7 @@ func generateTestFixtureDNSEndpointsByType(namespace string, typeCounts map[stri
754758
Namespace: namespace,
755759
},
756760
Spec: apiv1alpha1.DNSEndpointSpec{
757-
Endpoints: []*endpoint.Endpoint{
761+
Endpoints: []*apiv1alpha1.Endpoint{
758762
{
759763
DNSName: strings.ToLower(fmt.Sprintf("%s-%d.example.com", rt, idx)),
760764
RecordType: rt,

0 commit comments

Comments
 (0)