Skip to content

Commit f260e1c

Browse files
committed
main integration + first test review
1 parent 9dc1993 commit f260e1c

File tree

6 files changed

+55
-28
lines changed

6 files changed

+55
-28
lines changed

controller/execute.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,13 @@ func configureLogger(cfg *externaldns.Config) {
382382
// It initializes and returns a registry along with any error encountered during setup.
383383
// Supported registry types include: dynamodb, noop, txt, and aws-sd.
384384
func selectRegistry(cfg *externaldns.Config, p provider.Provider) (registry.Registry, error) {
385-
var r registry.Registry
385+
var reg registry.Registry
386386
var err error
387387
switch cfg.Registry {
388+
case "aws-sd":
389+
reg, err = registry.NewAWSSDRegistry(p, cfg.TXTOwnerID)
390+
case "crd":
391+
reg, err = registry.NewCRDRegistry(p, cfg.KubeConfig, cfg.APIServerURL, cfg.CRDAPIVersion, cfg.Namespace, cfg.TXTOwnerID, cfg.RequestTimeout, cfg.TXTCacheInterval)
388392
case "dynamodb":
389393
var dynamodbOpts []func(*dynamodb.Options)
390394
if cfg.AWSDynamoDBRegion != "" {
@@ -394,17 +398,16 @@ func selectRegistry(cfg *externaldns.Config, p provider.Provider) (registry.Regi
394398
},
395399
}
396400
}
397-
r, err = registry.NewDynamoDBRegistry(p, cfg.TXTOwnerID, dynamodb.NewFromConfig(aws.CreateDefaultV2Config(cfg), dynamodbOpts...), cfg.AWSDynamoDBTable, cfg.TXTPrefix, cfg.TXTSuffix, cfg.TXTWildcardReplacement, cfg.ManagedDNSRecordTypes, cfg.ExcludeDNSRecordTypes, []byte(cfg.TXTEncryptAESKey), cfg.TXTCacheInterval)
401+
reg, err = registry.NewDynamoDBRegistry(p, cfg.TXTOwnerID, dynamodb.NewFromConfig(aws.CreateDefaultV2Config(cfg), dynamodbOpts...), cfg.AWSDynamoDBTable, cfg.TXTPrefix, cfg.TXTSuffix, cfg.TXTWildcardReplacement, cfg.ManagedDNSRecordTypes, cfg.ExcludeDNSRecordTypes, []byte(cfg.TXTEncryptAESKey), cfg.TXTCacheInterval)
398402
case "noop":
399-
r, err = registry.NewNoopRegistry(p)
403+
reg, err = registry.NewNoopRegistry(p)
400404
case "txt":
401-
r, err = registry.NewTXTRegistry(p, cfg.TXTPrefix, cfg.TXTSuffix, cfg.TXTOwnerID, cfg.TXTCacheInterval, cfg.TXTWildcardReplacement, cfg.ManagedDNSRecordTypes, cfg.ExcludeDNSRecordTypes, cfg.TXTEncryptEnabled, []byte(cfg.TXTEncryptAESKey), cfg.TXTNewFormatOnly)
402-
case "aws-sd":
403-
r, err = registry.NewAWSSDRegistry(p, cfg.TXTOwnerID)
405+
reg, err = registry.NewTXTRegistry(p, cfg.TXTPrefix, cfg.TXTSuffix, cfg.TXTOwnerID, cfg.TXTCacheInterval, cfg.TXTWildcardReplacement, cfg.ManagedDNSRecordTypes, cfg.ExcludeDNSRecordTypes, cfg.TXTEncryptEnabled, []byte(cfg.TXTEncryptAESKey), cfg.TXTNewFormatOnly)
406+
404407
default:
405408
log.Fatalf("unknown registry: %s", cfg.Registry)
406409
}
407-
return r, err
410+
return reg, err
408411
}
409412

410413
// RegexDomainFilter overrides DomainFilter

pkg/apis/externaldns/types.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ type Config struct {
161161
ExoscaleAPISecret string `secure:"yes"`
162162
ExoscaleAPIEnvironment string
163163
ExoscaleAPIZone string
164-
CRDSourceAPIVersion string
165-
CRDSourceKind string
164+
CRDAPIVersion string
165+
CRDKind string
166166
ServiceTypeFilter []string
167167
CFAPIEndpoint string
168168
CFUsername string
@@ -261,8 +261,8 @@ var defaultConfig = &Config{
261261
Compatibility: "",
262262
ConnectorSourceServer: "localhost:8080",
263263
CoreDNSPrefix: "/skydns/",
264-
CRDSourceAPIVersion: "externaldns.k8s.io/v1alpha1",
265-
CRDSourceKind: "DNSEndpoint",
264+
CRDAPIVersion: "externaldns.k8s.io/v1alpha1",
265+
CRDKind: "DNSEndpoint",
266266
DefaultTargets: []string{},
267267
DigitalOceanAPIPageSize: 50,
268268
DomainFilter: []string{},
@@ -455,8 +455,8 @@ func App(cfg *Config) *kingpin.Application {
455455
app.Flag("combine-fqdn-annotation", "Combine FQDN template and Annotations instead of overwriting (default: false)").BoolVar(&cfg.CombineFQDNAndAnnotation)
456456
app.Flag("compatibility", "Process annotation semantics from legacy implementations (optional, options: mate, molecule, kops-dns-controller)").Default(defaultConfig.Compatibility).EnumVar(&cfg.Compatibility, "", "mate", "molecule", "kops-dns-controller")
457457
app.Flag("connector-source-server", "The server to connect for connector source, valid only when using connector source").Default(defaultConfig.ConnectorSourceServer).StringVar(&cfg.ConnectorSourceServer)
458-
app.Flag("crd-source-apiversion", "API version of the CRD for crd source, e.g. `externaldns.k8s.io/v1alpha1`, valid only when using crd source").Default(defaultConfig.CRDSourceAPIVersion).StringVar(&cfg.CRDSourceAPIVersion)
459-
app.Flag("crd-source-kind", "Kind of the CRD for the crd source in API group and version specified by crd-source-apiversion").Default(defaultConfig.CRDSourceKind).StringVar(&cfg.CRDSourceKind)
458+
app.Flag("crd-source-apiversion", "API version of the CRD for crd source, e.g. `externaldns.k8s.io/v1alpha1`, valid only when using crd source").Default(defaultConfig.CRDAPIVersion).StringVar(&cfg.CRDAPIVersion)
459+
app.Flag("crd-source-kind", "Kind of the CRD for the crd source in API group and version specified by crd-source-apiversion").Default(defaultConfig.CRDKind).StringVar(&cfg.CRDKind)
460460
app.Flag("default-targets", "Set globally default host/IP that will apply as a target instead of source addresses. Specify multiple times for multiple targets (optional)").StringsVar(&cfg.DefaultTargets)
461461
app.Flag("exclude-record-types", "Record types to exclude from management; specify multiple times to exclude many; (optional)").Default().StringsVar(&cfg.ExcludeDNSRecordTypes)
462462
app.Flag("exclude-target-net", "Exclude target nets (optional)").StringsVar(&cfg.ExcludeTargetNets)

pkg/apis/externaldns/types_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ var (
115115
ExoscaleAPIZone: "ch-gva-2",
116116
ExoscaleAPIKey: "",
117117
ExoscaleAPISecret: "",
118-
CRDSourceAPIVersion: "externaldns.k8s.io/v1alpha1",
119-
CRDSourceKind: "DNSEndpoint",
118+
CRDAPIVersion: "externaldns.k8s.io/v1alpha1",
119+
CRDKind: "DNSEndpoint",
120120
TransIPAccountName: "",
121121
TransIPPrivateKeyFile: "",
122122
DigitalOceanAPIPageSize: 50,
@@ -227,8 +227,8 @@ var (
227227
ExoscaleAPIZone: "zone1",
228228
ExoscaleAPIKey: "1",
229229
ExoscaleAPISecret: "2",
230-
CRDSourceAPIVersion: "test.k8s.io/v1alpha1",
231-
CRDSourceKind: "Endpoint",
230+
CRDAPIVersion: "test.k8s.io/v1alpha1",
231+
CRDKind: "Endpoint",
232232
NS1Endpoint: "https://api.example.com/v1",
233233
NS1IgnoreSSL: true,
234234
TransIPAccountName: "transip",

registry/crd.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"sigs.k8s.io/external-dns/endpoint"
3838
"sigs.k8s.io/external-dns/plan"
3939
"sigs.k8s.io/external-dns/provider"
40+
"sigs.k8s.io/external-dns/source"
4041
)
4142

4243
const (
@@ -202,7 +203,10 @@ func NewCRDClientForAPIVersionKind(client kubernetes.Interface, kubeConfig, apiS
202203
}
203204

204205
// NewCRDRegistry returns new CRDRegistry object
205-
func NewCRDRegistry(provider provider.Provider, crdClient CRDClient, ownerID string, cacheInterval time.Duration, namespace string) (*CRDRegistry, error) {
206+
func NewCRDRegistry(provider provider.Provider, kubeConfig, apiServerURL, apiVersion, namespace, ownerID string, cacheInterval, apiServerTimeOut time.Duration) (*CRDRegistry, error) {
207+
var err error
208+
var k8sClient kubernetes.Interface
209+
206210
if ownerID == "" {
207211
return nil, errors.New("owner id cannot be empty")
208212
}
@@ -212,6 +216,27 @@ func NewCRDRegistry(provider provider.Provider, crdClient CRDClient, ownerID str
212216
namespace = "default"
213217
}
214218

219+
// new Singleton because the user may want to store this registry on a
220+
// remote (and shared) cluster between multiple external-dns instances
221+
clientGenerator := &source.SingletonClientGenerator{
222+
KubeConfig: kubeConfig,
223+
APIServerURL: apiServerURL,
224+
// If update events are enabled, disable timeout.
225+
RequestTimeout: func() time.Duration {
226+
return apiServerTimeOut
227+
}(),
228+
}
229+
230+
k8sClient, err = clientGenerator.KubeClient()
231+
if err != nil {
232+
return nil, fmt.Errorf("unable to create kubeclient: %s", err)
233+
}
234+
235+
crdClient, err := NewCRDClientForAPIVersionKind(k8sClient, kubeConfig, apiServerURL, apiVersion)
236+
if err != nil {
237+
return nil, fmt.Errorf("unable to create crdclient: %s", err)
238+
}
239+
215240
return &CRDRegistry{
216241
client: crdClient,
217242
namespace: namespace,

registry/crd_test.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,14 @@ func testCRDSourceImplementsSource(t *testing.T) {
6767
}
6868

6969
func testConstructor(t *testing.T) {
70-
_, err := NewCRDRegistry(nil, nil, "", time.Second, "default")
71-
if err == nil {
72-
t.Error("Expected a new registry to return an error when no ownerID are specified")
73-
}
70+
_, err := NewCRDRegistry(nil, "", "", "v1", "", "", time.Second, time.Second)
71+
assert.Error(t, err, "Expected a new registry to return an error when no ownerID are specified")
7472

75-
_, err = NewCRDRegistry(nil, nil, "ownerID", time.Second, "namespace")
76-
if err != nil {
77-
t.Error("Expected registry to be initialized without error when providing an owner id and a namespace", err)
78-
}
73+
_, err = NewCRDRegistry(nil, "/dev/null", "", "v1", "default", "ownerID", time.Second, time.Second)
74+
assert.Error(t, err, err.Error()+"Expected a new registry to return an error when there is no kubeconfig")
75+
76+
_, err = NewCRDRegistry(nil, "", "####", "v1", "default", "ownerID", time.Second, time.Second)
77+
assert.Error(t, err, err.Error()+"Expected a new registry to return an error when there is an invalid url")
7978
}
8079

8180
func testRecords(t *testing.T) {

source/store.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,8 @@ func NewSourceConfig(cfg *externaldns.Config) *Config {
111111
PublishHostIP: cfg.PublishHostIP,
112112
AlwaysPublishNotReadyAddresses: cfg.AlwaysPublishNotReadyAddresses,
113113
ConnectorServer: cfg.ConnectorSourceServer,
114-
CRDSourceAPIVersion: cfg.CRDSourceAPIVersion,
115-
CRDSourceKind: cfg.CRDSourceKind,
114+
CRDSourceAPIVersion: cfg.CRDAPIVersion,
115+
CRDSourceKind: cfg.CRDKind,
116116
KubeConfig: cfg.KubeConfig,
117117
APIServerURL: cfg.APIServerURL,
118118
ServiceTypeFilter: cfg.ServiceTypeFilter,

0 commit comments

Comments
 (0)