Skip to content

Commit 918ccc5

Browse files
committed
Signed-off-by: Jan Jansen <jan.jansen@gdata.de>
1 parent f306b22 commit 918ccc5

File tree

2 files changed

+136
-83
lines changed

2 files changed

+136
-83
lines changed

coredns.go

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,21 @@ import (
3030
log "github.com/sirupsen/logrus"
3131
"go.etcd.io/etcd/api/v3/mvccpb"
3232
etcdcv3 "go.etcd.io/etcd/client/v3"
33+
3334
"sigs.k8s.io/external-dns/pkg/tlsutils"
3435

3536
"sigs.k8s.io/external-dns/endpoint"
3637
"sigs.k8s.io/external-dns/plan"
3738
"sigs.k8s.io/external-dns/provider"
3839
)
3940

40-
func init() {
41-
rand.Seed(time.Now().UnixNano())
42-
}
43-
4441
const (
4542
priority = 10 // default priority when nothing is set
4643
etcdTimeout = 5 * time.Second
4744

48-
randomPrefixLabel = "prefix"
49-
providerSpecificGroup = "webhook/coredns-group"
45+
randomPrefixLabel = "prefix"
46+
providerSpecificGroup = "webhook/coredns-group"
47+
providerSpecificGroup2 = "coredns/group"
5048
)
5149

5250
type CoreDNSConfig struct {
@@ -66,6 +64,7 @@ type coreDNSProvider struct {
6664
client coreDNSClient
6765
dryRun bool
6866
CoreDNSConfig
67+
strictlyOwned bool
6968
}
7069

7170
// Service represents CoreDNS etcd record
@@ -93,13 +92,13 @@ type Service struct {
9392
// Etcd key where we found this service and ignored from json un-/marshaling
9493
Key string `json:"-"`
9594

96-
// OwnedBy is used to prevent service to be added by different external-dns (only used by external-dns)
97-
OwnedBy string `json:"ownedby,omitempty"`
95+
// Owner is used to prevent service to be added by different external-dns (only used by external-dns)
96+
Owner string `json:"owner,omitempty"`
9897
}
9998

10099
type etcdClient struct {
101100
client *etcdcv3.Client
102-
ownerID string
101+
owner string
103102
strictlyOwned bool
104103
}
105104

@@ -123,7 +122,7 @@ func (c etcdClient) GetServices(ctx context.Context, prefix string) ([]*Service,
123122
if err != nil {
124123
return nil, err
125124
}
126-
if c.strictlyOwned && svc.OwnedBy != c.ownerID {
125+
if c.strictlyOwned && svc.Owner != c.owner {
127126
continue
128127
}
129128
b := Service{
@@ -156,7 +155,7 @@ func (c etcdClient) SaveService(ctx context.Context, service *Service) error {
156155
defer cancel()
157156

158157
// check only for empty OwnedBy
159-
if c.strictlyOwned && service.OwnedBy != c.ownerID {
158+
if c.strictlyOwned && service.Owner != c.owner {
160159
r, err := c.client.Get(ctx, service.Key)
161160
if err != nil {
162161
return fmt.Errorf("etcd get %q: %w", service.Key, err)
@@ -167,11 +166,11 @@ func (c etcdClient) SaveService(ctx context.Context, service *Service) error {
167166
if err != nil {
168167
return fmt.Errorf("failed to unmarshal value for key %q: %w", service.Key, err)
169168
}
170-
if svc.OwnedBy != c.ownerID {
169+
if svc.Owner != c.owner {
171170
return fmt.Errorf("key %q is not owned by this provider", service.Key)
172171
}
173172
}
174-
service.OwnedBy = c.ownerID
173+
service.Owner = c.owner
175174
}
176175

177176
value, err := json.Marshal(&service)
@@ -200,7 +199,7 @@ func (c etcdClient) DeleteService(ctx context.Context, key string) error {
200199
if err != nil {
201200
return err
202201
}
203-
if svc.OwnedBy != c.ownerID {
202+
if svc.Owner != c.owner {
204203
continue
205204
}
206205

@@ -234,9 +233,10 @@ func getETCDConfig() (*etcdcv3.Config, error) {
234233
firstURL := strings.ToLower(etcdURLs[0])
235234
etcdUsername := os.Getenv("ETCD_USERNAME")
236235
etcdPassword := os.Getenv("ETCD_PASSWORD")
237-
if strings.HasPrefix(firstURL, "http://") {
236+
switch {
237+
case strings.HasPrefix(firstURL, "http://"):
238238
return &etcdcv3.Config{Endpoints: etcdURLs, Username: etcdUsername, Password: etcdPassword}, nil
239-
} else if strings.HasPrefix(firstURL, "https://") {
239+
case strings.HasPrefix(firstURL, "https://"):
240240
tlsConfig, err := tlsutils.CreateTLSConfig("ETCD")
241241
if err != nil {
242242
return nil, err
@@ -248,13 +248,13 @@ func getETCDConfig() (*etcdcv3.Config, error) {
248248
Username: etcdUsername,
249249
Password: etcdPassword,
250250
}, nil
251-
} else {
251+
default:
252252
return nil, errors.New("etcd URLs must start with either http:// or https://")
253253
}
254254
}
255255

256256
// the newETCDClient is an etcd client constructor
257-
func newETCDClient(ownerID string, strictlyOwned bool) (coreDNSClient, error) {
257+
func newETCDClient(owner string, strictlyOwned bool) (coreDNSClient, error) {
258258
cfg, err := getETCDConfig()
259259
if err != nil {
260260
return nil, err
@@ -263,12 +263,12 @@ func newETCDClient(ownerID string, strictlyOwned bool) (coreDNSClient, error) {
263263
if err != nil {
264264
return nil, err
265265
}
266-
return etcdClient{c, ownerID, strictlyOwned}, nil
266+
return etcdClient{c, owner, strictlyOwned}, nil
267267
}
268268

269269
// NewCoreDNSProvider is a CoreDNS provider constructor
270-
func NewCoreDNSProvider(config CoreDNSConfig, ownerID string, strictlyOwned, dryRun bool) (provider.Provider, error) {
271-
client, err := newETCDClient(ownerID, strictlyOwned)
270+
func NewCoreDNSProvider(config CoreDNSConfig, owner string, strictlyOwned, dryRun bool) (provider.Provider, error) {
271+
client, err := newETCDClient(owner, strictlyOwned)
272272
if err != nil {
273273
return nil, err
274274
}
@@ -277,6 +277,7 @@ func NewCoreDNSProvider(config CoreDNSConfig, ownerID string, strictlyOwned, dry
277277
client: client,
278278
dryRun: dryRun,
279279
CoreDNSConfig: config,
280+
strictlyOwned: strictlyOwned,
280281
}, nil
281282
}
282283

@@ -333,9 +334,13 @@ func (p coreDNSProvider) Records(ctx context.Context) ([]*endpoint.Endpoint, err
333334
)
334335
if service.Group != "" {
335336
ep.WithProviderSpecific(providerSpecificGroup, service.Group)
337+
ep.WithProviderSpecific(providerSpecificGroup2, service.Group)
336338
}
337339
log.Debugf("Creating new ep (%s) with new service host (%s)", ep, service.Host)
338340
}
341+
if p.strictlyOwned {
342+
ep.Labels[endpoint.OwnerLabelKey] = service.Owner
343+
}
339344
ep.Labels["originalText"] = service.Text
340345
ep.Labels[randomPrefixLabel] = prefix
341346
ep.Labels[service.Host] = prefix
@@ -347,6 +352,9 @@ func (p coreDNSProvider) Records(ctx context.Context) ([]*endpoint.Endpoint, err
347352
endpoint.RecordTypeTXT,
348353
service.Text,
349354
)
355+
if p.strictlyOwned {
356+
ep.Labels[endpoint.OwnerLabelKey] = service.Owner
357+
}
350358
ep.Labels[randomPrefixLabel] = prefix
351359
result = append(result, ep)
352360
}
@@ -420,11 +428,13 @@ func (p coreDNSProvider) createServicesForEndpoint(ctx context.Context, dnsName
420428
prefix = fmt.Sprintf("%08x", rand.Int31())
421429
log.Infof("Generating new prefix: (%s)", prefix)
422430
}
423-
424431
group := ""
425432
if prop, ok := ep.GetProviderSpecificProperty(providerSpecificGroup); ok {
426433
group = prop
427434
}
435+
if prop, ok := ep.GetProviderSpecificProperty(providerSpecificGroup2); ok {
436+
group = prop
437+
}
428438
service := Service{
429439
Host: target,
430440
Text: ep.Labels["originalText"],

0 commit comments

Comments
 (0)