@@ -37,6 +37,9 @@ import (
37
37
const (
38
38
defaultTTL = 300
39
39
40
+ // https://github.com/aws/aws-sdk-go-v2/blob/cf8509382340d6afdc93612550d56d685181bbb3/service/servicediscovery/api_op_ListServices.go#L42
41
+ maxResults = 100
42
+
40
43
sdNamespaceTypePublic = "public"
41
44
sdNamespaceTypePrivate = "private"
42
45
@@ -117,7 +120,7 @@ func newSdNamespaceFilter(namespaceTypeConfig string) sdtypes.NamespaceFilter {
117
120
}
118
121
}
119
122
120
- // awsTags converts user supplied tags to AWS format
123
+ // awsTags converts user- supplied tags to AWS format
121
124
func awsTags (tags map [string ]string ) []sdtypes.Tag {
122
125
awsTags := make ([]sdtypes.Tag , 0 , len (tags ))
123
126
for k , v := range tags {
@@ -155,6 +158,11 @@ func (p *AWSSDProvider) Records(ctx context.Context) (endpoints []*endpoint.Endp
155
158
continue
156
159
}
157
160
161
+ if srv .Description == nil {
162
+ log .Warnf ("Skipping service %q as owner id not configured" , * srv .Name )
163
+ continue
164
+ }
165
+
158
166
endpoints = append (endpoints , p .instancesToEndpoint (ns , srv , resp .Instances ))
159
167
}
160
168
}
@@ -167,6 +175,7 @@ func (p *AWSSDProvider) instancesToEndpoint(ns *sdtypes.NamespaceSummary, srv *s
167
175
recordName := * srv .Name + "." + * ns .Name
168
176
169
177
labels := endpoint .NewLabels ()
178
+
170
179
labels [endpoint .AWSSDDescriptionLabel ] = * srv .Description
171
180
172
181
newEndpoint := & endpoint.Endpoint {
@@ -288,7 +297,7 @@ func (p *AWSSDProvider) submitCreates(ctx context.Context, namespaces []*sdtypes
288
297
if err != nil {
289
298
return err
290
299
}
291
- // update local list of services
300
+ // update a local list of services
292
301
services [* srv .Name ] = srv
293
302
} else if ch .RecordTTL .IsConfigured () && * srv .DnsConfig .DnsRecords [0 ].TTL != int64 (ch .RecordTTL ) {
294
303
// update service when TTL differ
@@ -360,7 +369,7 @@ func (p *AWSSDProvider) ListNamespaces(ctx context.Context) ([]*sdtypes.Namespac
360
369
return namespaces , nil
361
370
}
362
371
363
- // ListServicesByNamespaceID returns list of services in given namespace.
372
+ // ListServicesByNamespaceID returns a list of services in a given namespace.
364
373
func (p * AWSSDProvider ) ListServicesByNamespaceID (ctx context.Context , namespaceID * string ) (map [string ]* sdtypes.Service , error ) {
365
374
services := make ([]sdtypes.ServiceSummary , 0 )
366
375
@@ -369,7 +378,7 @@ func (p *AWSSDProvider) ListServicesByNamespaceID(ctx context.Context, namespace
369
378
Name : sdtypes .ServiceFilterNameNamespaceId ,
370
379
Values : []string {* namespaceID },
371
380
}},
372
- MaxResults : aws .Int32 (100 ),
381
+ MaxResults : aws .Int32 (maxResults ),
373
382
})
374
383
for paginator .HasMorePages () {
375
384
resp , err := paginator .NextPage (ctx )
@@ -412,32 +421,32 @@ func (p *AWSSDProvider) CreateService(ctx context.Context, namespaceID *string,
412
421
ttl = int64 (ep .RecordTTL )
413
422
}
414
423
415
- if ! p .dryRun {
416
- out , err := p .client .CreateService (ctx , & sd.CreateServiceInput {
417
- Name : srvName ,
418
- Description : aws .String (ep .Labels [endpoint .AWSSDDescriptionLabel ]),
419
- DnsConfig : & sdtypes.DnsConfig {
420
- RoutingPolicy : routingPolicy ,
421
- DnsRecords : []sdtypes.DnsRecord {{
422
- Type : srvType ,
423
- TTL : aws .Int64 (ttl ),
424
- }},
425
- },
426
- NamespaceId : namespaceID ,
427
- Tags : p .tags ,
428
- })
429
- if err != nil {
430
- return nil , err
431
- }
424
+ if p .dryRun {
425
+ // return a mock service summary in case of a dry run
426
+ return & sdtypes.Service {Id : aws .String ("dry-run-service" ), Name : aws .String ("dry-run-service" )}, nil
427
+ }
432
428
433
- return out .Service , nil
429
+ out , err := p .client .CreateService (ctx , & sd.CreateServiceInput {
430
+ Name : srvName ,
431
+ Description : aws .String (ep .Labels [endpoint .AWSSDDescriptionLabel ]),
432
+ DnsConfig : & sdtypes.DnsConfig {
433
+ RoutingPolicy : routingPolicy ,
434
+ DnsRecords : []sdtypes.DnsRecord {{
435
+ Type : srvType ,
436
+ TTL : aws .Int64 (ttl ),
437
+ }},
438
+ },
439
+ NamespaceId : namespaceID ,
440
+ Tags : p .tags ,
441
+ })
442
+ if err != nil {
443
+ return nil , err
434
444
}
435
445
436
- // return mock service summary in case of dry run
437
- return & sdtypes.Service {Id : aws .String ("dry-run-service" ), Name : aws .String ("dry-run-service" )}, nil
446
+ return out .Service , nil
438
447
}
439
448
440
- // UpdateService updates the specified service with information from provided endpoint.
449
+ // UpdateService updates the specified service with information from the provided endpoint.
441
450
func (p * AWSSDProvider ) UpdateService (ctx context.Context , service * sdtypes.Service , ep * endpoint.Endpoint ) error {
442
451
log .Infof ("Updating service \" %s\" " , * service .Name )
443
452
@@ -448,45 +457,52 @@ func (p *AWSSDProvider) UpdateService(ctx context.Context, service *sdtypes.Serv
448
457
ttl = int64 (ep .RecordTTL )
449
458
}
450
459
451
- if ! p .dryRun {
452
- _ , err := p .client .UpdateService (ctx , & sd.UpdateServiceInput {
453
- Id : service .Id ,
454
- Service : & sdtypes.ServiceChange {
455
- Description : aws .String (ep .Labels [endpoint .AWSSDDescriptionLabel ]),
456
- DnsConfig : & sdtypes.DnsConfigChange {
457
- DnsRecords : []sdtypes.DnsRecord {{
458
- Type : srvType ,
459
- TTL : aws .Int64 (ttl ),
460
- }},
461
- },
462
- },
463
- })
464
- if err != nil {
465
- return err
466
- }
460
+ if p .dryRun {
461
+ return nil
467
462
}
468
463
469
- return nil
464
+ _ , err := p .client .UpdateService (ctx , & sd.UpdateServiceInput {
465
+ Id : service .Id ,
466
+ Service : & sdtypes.ServiceChange {
467
+ Description : aws .String (ep .Labels [endpoint .AWSSDDescriptionLabel ]),
468
+ DnsConfig : & sdtypes.DnsConfigChange {
469
+ DnsRecords : []sdtypes.DnsRecord {{
470
+ Type : srvType ,
471
+ TTL : aws .Int64 (ttl ),
472
+ }},
473
+ },
474
+ },
475
+ })
476
+ return err
470
477
}
471
478
472
479
// DeleteService deletes empty Service from AWS API if its owner id match
473
480
func (p * AWSSDProvider ) DeleteService (ctx context.Context , service * sdtypes.Service ) error {
474
481
log .Debugf ("Check if service \" %s\" owner id match and it can be deleted" , * service .Name )
475
- if ! p .dryRun && p .cleanEmptyService {
476
- // convert ownerID string to service description format
477
- label := endpoint .NewLabels ()
478
- label [endpoint .OwnerLabelKey ] = p .ownerID
479
- label [endpoint .AWSSDDescriptionLabel ] = label .SerializePlain (false )
480
-
481
- if strings .HasPrefix (* service .Description , label [endpoint .AWSSDDescriptionLabel ]) {
482
- log .Infof ("Deleting service \" %s\" " , * service .Name )
483
- _ , err := p .client .DeleteService (ctx , & sd.DeleteServiceInput {
484
- Id : aws .String (* service .Id ),
485
- })
486
- return err
487
- }
488
- log .Debugf ("Skipping service removal %s because owner id does not match, found: \" %s\" , required: \" %s\" " , * service .Name , * service .Description , label [endpoint .AWSSDDescriptionLabel ])
482
+
483
+ if p .dryRun || ! p .cleanEmptyService {
484
+ return nil
485
+ }
486
+
487
+ // convert ownerID string to the service description format
488
+ label := endpoint .NewLabels ()
489
+ label [endpoint .OwnerLabelKey ] = p .ownerID
490
+ label [endpoint .AWSSDDescriptionLabel ] = label .SerializePlain (false )
491
+
492
+ if service .Description == nil {
493
+ log .Debugf ("Skipping service removal %q because owner id (service.Description) not set, when should be %q" , * service .Name , label [endpoint .AWSSDDescriptionLabel ])
494
+ return nil
489
495
}
496
+
497
+ if strings .HasPrefix (* service .Description , label [endpoint .AWSSDDescriptionLabel ]) {
498
+ log .Infof ("Deleting service \" %s\" " , * service .Name )
499
+ _ , err := p .client .DeleteService (ctx , & sd.DeleteServiceInput {
500
+ Id : aws .String (* service .Id ),
501
+ })
502
+ return err
503
+ }
504
+ log .Debugf ("Skipping service removal %q because owner id does not match, found: %q, required: %q" , * service .Name , * service .Description , label [endpoint .AWSSDDescriptionLabel ])
505
+
490
506
return nil
491
507
}
492
508
@@ -619,7 +635,7 @@ func (p *AWSSDProvider) routingPolicyFromEndpoint(ep *endpoint.Endpoint) sdtypes
619
635
return sdtypes .RoutingPolicyWeighted
620
636
}
621
637
622
- // determine service type (A, AAAA, CNAME) from given endpoint
638
+ // determine the service type (A, AAAA, CNAME) from a given endpoint
623
639
func (p * AWSSDProvider ) serviceTypeFromEndpoint (ep * endpoint.Endpoint ) sdtypes.RecordType {
624
640
switch ep .RecordType {
625
641
case endpoint .RecordTypeCNAME :
0 commit comments