Skip to content

Commit 11bd380

Browse files
committed
main integration + first test review
1 parent 0ca6e26 commit 11bd380

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
@@ -388,9 +388,13 @@ func configureLogger(cfg *externaldns.Config) {
388388
// It initializes and returns a registry along with any error encountered during setup.
389389
// Supported registry types include: dynamodb, noop, txt, and aws-sd.
390390
func selectRegistry(cfg *externaldns.Config, p provider.Provider) (registry.Registry, error) {
391-
var r registry.Registry
391+
var reg registry.Registry
392392
var err error
393393
switch cfg.Registry {
394+
case "aws-sd":
395+
reg, err = registry.NewAWSSDRegistry(p, cfg.TXTOwnerID)
396+
case "crd":
397+
reg, err = registry.NewCRDRegistry(p, cfg.KubeConfig, cfg.APIServerURL, cfg.CRDAPIVersion, cfg.Namespace, cfg.TXTOwnerID, cfg.RequestTimeout, cfg.TXTCacheInterval)
394398
case "dynamodb":
395399
var dynamodbOpts []func(*dynamodb.Options)
396400
if cfg.AWSDynamoDBRegion != "" {
@@ -400,17 +404,16 @@ func selectRegistry(cfg *externaldns.Config, p provider.Provider) (registry.Regi
400404
},
401405
}
402406
}
403-
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)
407+
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)
404408
case "noop":
405-
r, err = registry.NewNoopRegistry(p)
409+
reg, err = registry.NewNoopRegistry(p)
406410
case "txt":
407-
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)
408-
case "aws-sd":
409-
r, err = registry.NewAWSSDRegistry(p, cfg.TXTOwnerID)
411+
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)
412+
410413
default:
411414
log.Fatalf("unknown registry: %s", cfg.Registry)
412415
}
413-
return r, err
416+
return reg, err
414417
}
415418

416419
// RegexDomainFilter overrides DomainFilter

pkg/apis/externaldns/types.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ type Config struct {
158158
ExoscaleAPISecret string `secure:"yes"`
159159
ExoscaleAPIEnvironment string
160160
ExoscaleAPIZone string
161-
CRDSourceAPIVersion string
162-
CRDSourceKind string
161+
CRDAPIVersion string
162+
CRDKind string
163163
ServiceTypeFilter []string
164164
CFAPIEndpoint string
165165
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{},
@@ -459,8 +459,8 @@ func App(cfg *Config) *kingpin.Application {
459459
app.Flag("combine-fqdn-annotation", "Combine FQDN template and Annotations instead of overwriting").BoolVar(&cfg.CombineFQDNAndAnnotation)
460460
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")
461461
app.Flag("connector-source-server", "The server to connect for connector source, valid only when using connector source").Default(defaultConfig.ConnectorSourceServer).StringVar(&cfg.ConnectorSourceServer)
462-
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)
463-
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)
462+
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)
463+
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)
464464
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)
465465
app.Flag("exclude-record-types", "Record types to exclude from management; specify multiple times to exclude many; (optional)").Default().StringsVar(&cfg.ExcludeDNSRecordTypes)
466466
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
@@ -114,8 +114,8 @@ var (
114114
ExoscaleAPIZone: "ch-gva-2",
115115
ExoscaleAPIKey: "",
116116
ExoscaleAPISecret: "",
117-
CRDSourceAPIVersion: "externaldns.k8s.io/v1alpha1",
118-
CRDSourceKind: "DNSEndpoint",
117+
CRDAPIVersion: "externaldns.k8s.io/v1alpha1",
118+
CRDKind: "DNSEndpoint",
119119
TransIPAccountName: "",
120120
TransIPPrivateKeyFile: "",
121121
DigitalOceanAPIPageSize: 50,
@@ -229,8 +229,8 @@ var (
229229
ExoscaleAPIZone: "zone1",
230230
ExoscaleAPIKey: "1",
231231
ExoscaleAPISecret: "2",
232-
CRDSourceAPIVersion: "test.k8s.io/v1alpha1",
233-
CRDSourceKind: "Endpoint",
232+
CRDAPIVersion: "test.k8s.io/v1alpha1",
233+
CRDKind: "Endpoint",
234234
NS1Endpoint: "https://api.example.com/v1",
235235
NS1IgnoreSSL: true,
236236
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)