@@ -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 {
@@ -140,6 +143,10 @@ func (p *AWSSDProvider) Records(ctx context.Context) (endpoints []*endpoint.Endp
140
143
}
141
144
142
145
for _ , srv := range services {
146
+ if srv .Description == nil {
147
+ log .Warnf ("Skipping service %q as owner id not configured" , * srv .Name )
148
+ continue
149
+ }
143
150
resp , err := p .client .DiscoverInstances (ctx , & sd.DiscoverInstancesInput {
144
151
NamespaceName : ns .Name ,
145
152
ServiceName : srv .Name ,
@@ -167,6 +174,7 @@ func (p *AWSSDProvider) instancesToEndpoint(ns *sdtypes.NamespaceSummary, srv *s
167
174
recordName := * srv .Name + "." + * ns .Name
168
175
169
176
labels := endpoint .NewLabels ()
177
+
170
178
labels [endpoint .AWSSDDescriptionLabel ] = * srv .Description
171
179
172
180
newEndpoint := & endpoint.Endpoint {
@@ -288,7 +296,7 @@ func (p *AWSSDProvider) submitCreates(ctx context.Context, namespaces []*sdtypes
288
296
if err != nil {
289
297
return err
290
298
}
291
- // update local list of services
299
+ // update a local list of services
292
300
services [* srv .Name ] = srv
293
301
} else if ch .RecordTTL .IsConfigured () && * srv .DnsConfig .DnsRecords [0 ].TTL != int64 (ch .RecordTTL ) {
294
302
// update service when TTL differ
@@ -360,7 +368,7 @@ func (p *AWSSDProvider) ListNamespaces(ctx context.Context) ([]*sdtypes.Namespac
360
368
return namespaces , nil
361
369
}
362
370
363
- // ListServicesByNamespaceID returns list of services in given namespace.
371
+ // ListServicesByNamespaceID returns a list of services in a given namespace.
364
372
func (p * AWSSDProvider ) ListServicesByNamespaceID (ctx context.Context , namespaceID * string ) (map [string ]* sdtypes.Service , error ) {
365
373
services := make ([]sdtypes.ServiceSummary , 0 )
366
374
@@ -369,7 +377,7 @@ func (p *AWSSDProvider) ListServicesByNamespaceID(ctx context.Context, namespace
369
377
Name : sdtypes .ServiceFilterNameNamespaceId ,
370
378
Values : []string {* namespaceID },
371
379
}},
372
- MaxResults : aws .Int32 (100 ),
380
+ MaxResults : aws .Int32 (maxResults ),
373
381
})
374
382
for paginator .HasMorePages () {
375
383
resp , err := paginator .NextPage (ctx )
@@ -412,32 +420,32 @@ func (p *AWSSDProvider) CreateService(ctx context.Context, namespaceID *string,
412
420
ttl = int64 (ep .RecordTTL )
413
421
}
414
422
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
- }
423
+ if p .dryRun {
424
+ // return a mock service summary in case of a dry run
425
+ return & sdtypes.Service {Id : aws .String ("dry-run-service" ), Name : aws .String ("dry-run-service" )}, nil
426
+ }
432
427
433
- return out .Service , nil
428
+ out , err := p .client .CreateService (ctx , & sd.CreateServiceInput {
429
+ Name : srvName ,
430
+ Description : aws .String (ep .Labels [endpoint .AWSSDDescriptionLabel ]),
431
+ DnsConfig : & sdtypes.DnsConfig {
432
+ RoutingPolicy : routingPolicy ,
433
+ DnsRecords : []sdtypes.DnsRecord {{
434
+ Type : srvType ,
435
+ TTL : aws .Int64 (ttl ),
436
+ }},
437
+ },
438
+ NamespaceId : namespaceID ,
439
+ Tags : p .tags ,
440
+ })
441
+ if err != nil {
442
+ return nil , err
434
443
}
435
444
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
445
+ return out .Service , nil
438
446
}
439
447
440
- // UpdateService updates the specified service with information from provided endpoint.
448
+ // UpdateService updates the specified service with information from the provided endpoint.
441
449
func (p * AWSSDProvider ) UpdateService (ctx context.Context , service * sdtypes.Service , ep * endpoint.Endpoint ) error {
442
450
log .Infof ("Updating service \" %s\" " , * service .Name )
443
451
@@ -448,45 +456,52 @@ func (p *AWSSDProvider) UpdateService(ctx context.Context, service *sdtypes.Serv
448
456
ttl = int64 (ep .RecordTTL )
449
457
}
450
458
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
- }
459
+ if p .dryRun {
460
+ return nil
467
461
}
468
462
469
- return nil
463
+ _ , err := p .client .UpdateService (ctx , & sd.UpdateServiceInput {
464
+ Id : service .Id ,
465
+ Service : & sdtypes.ServiceChange {
466
+ Description : aws .String (ep .Labels [endpoint .AWSSDDescriptionLabel ]),
467
+ DnsConfig : & sdtypes.DnsConfigChange {
468
+ DnsRecords : []sdtypes.DnsRecord {{
469
+ Type : srvType ,
470
+ TTL : aws .Int64 (ttl ),
471
+ }},
472
+ },
473
+ },
474
+ })
475
+ return err
470
476
}
471
477
472
478
// DeleteService deletes empty Service from AWS API if its owner id match
473
479
func (p * AWSSDProvider ) DeleteService (ctx context.Context , service * sdtypes.Service ) error {
474
480
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 ])
481
+
482
+ if p .dryRun || ! p .cleanEmptyService {
483
+ return nil
484
+ }
485
+
486
+ // convert ownerID string to the service description format
487
+ label := endpoint .NewLabels ()
488
+ label [endpoint .OwnerLabelKey ] = p .ownerID
489
+ label [endpoint .AWSSDDescriptionLabel ] = label .SerializePlain (false )
490
+
491
+ if service .Description == nil {
492
+ log .Debugf ("Skipping service removal %q because owner id (service.Description) not set, when should be %q" , * service .Name , label [endpoint .AWSSDDescriptionLabel ])
493
+ return nil
489
494
}
495
+
496
+ if strings .HasPrefix (* service .Description , label [endpoint .AWSSDDescriptionLabel ]) {
497
+ log .Infof ("Deleting service \" %s\" " , * service .Name )
498
+ _ , err := p .client .DeleteService (ctx , & sd.DeleteServiceInput {
499
+ Id : aws .String (* service .Id ),
500
+ })
501
+ return err
502
+ }
503
+ log .Debugf ("Skipping service removal %q because owner id does not match, found: %q, required: %q" , * service .Name , * service .Description , label [endpoint .AWSSDDescriptionLabel ])
504
+
490
505
return nil
491
506
}
492
507
@@ -619,7 +634,7 @@ func (p *AWSSDProvider) routingPolicyFromEndpoint(ep *endpoint.Endpoint) sdtypes
619
634
return sdtypes .RoutingPolicyWeighted
620
635
}
621
636
622
- // determine service type (A, AAAA, CNAME) from given endpoint
637
+ // determine the service type (A, AAAA, CNAME) from a given endpoint
623
638
func (p * AWSSDProvider ) serviceTypeFromEndpoint (ep * endpoint.Endpoint ) sdtypes.RecordType {
624
639
switch ep .RecordType {
625
640
case endpoint .RecordTypeCNAME :
0 commit comments