Skip to content

Commit 0a67905

Browse files
fix(provider): aws-sd provider null pointer
1 parent 4ea5f9d commit 0a67905

File tree

4 files changed

+577
-312
lines changed

4 files changed

+577
-312
lines changed

endpoint/labels.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,8 @@ func NewLabelsFromStringPlain(labelText string) (Labels, error) {
9090
func NewLabelsFromString(labelText string, aesKey []byte) (Labels, error) {
9191
if len(aesKey) != 0 {
9292
decryptedText, encryptionNonce, err := DecryptText(strings.Trim(labelText, "\""), aesKey)
93-
// in case if we have decryption error, just try process original text
94-
// decryption errors should be ignored here, because we can already have plain-text labels in registry
93+
// in case if we have a decryption error, try process original text
94+
// decryption errors should be ignored here, because we can already have plain-text labels in the registry
9595
if err == nil {
9696
labels, err := NewLabelsFromStringPlain(decryptedText)
9797
if err == nil {

provider/awssd/aws_sd.go

Lines changed: 72 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ import (
3737
const (
3838
defaultTTL = 300
3939

40+
// https://github.com/aws/aws-sdk-go-v2/blob/cf8509382340d6afdc93612550d56d685181bbb3/service/servicediscovery/api_op_ListServices.go#L42
41+
maxResults = 100
42+
4043
sdNamespaceTypePublic = "public"
4144
sdNamespaceTypePrivate = "private"
4245

@@ -117,7 +120,7 @@ func newSdNamespaceFilter(namespaceTypeConfig string) sdtypes.NamespaceFilter {
117120
}
118121
}
119122

120-
// awsTags converts user supplied tags to AWS format
123+
// awsTags converts user-supplied tags to AWS format
121124
func awsTags(tags map[string]string) []sdtypes.Tag {
122125
awsTags := make([]sdtypes.Tag, 0, len(tags))
123126
for k, v := range tags {
@@ -140,6 +143,10 @@ func (p *AWSSDProvider) Records(ctx context.Context) (endpoints []*endpoint.Endp
140143
}
141144

142145
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+
}
143150
resp, err := p.client.DiscoverInstances(ctx, &sd.DiscoverInstancesInput{
144151
NamespaceName: ns.Name,
145152
ServiceName: srv.Name,
@@ -167,6 +174,7 @@ func (p *AWSSDProvider) instancesToEndpoint(ns *sdtypes.NamespaceSummary, srv *s
167174
recordName := *srv.Name + "." + *ns.Name
168175

169176
labels := endpoint.NewLabels()
177+
170178
labels[endpoint.AWSSDDescriptionLabel] = *srv.Description
171179

172180
newEndpoint := &endpoint.Endpoint{
@@ -288,7 +296,7 @@ func (p *AWSSDProvider) submitCreates(ctx context.Context, namespaces []*sdtypes
288296
if err != nil {
289297
return err
290298
}
291-
// update local list of services
299+
// update a local list of services
292300
services[*srv.Name] = srv
293301
} else if ch.RecordTTL.IsConfigured() && *srv.DnsConfig.DnsRecords[0].TTL != int64(ch.RecordTTL) {
294302
// update service when TTL differ
@@ -360,7 +368,7 @@ func (p *AWSSDProvider) ListNamespaces(ctx context.Context) ([]*sdtypes.Namespac
360368
return namespaces, nil
361369
}
362370

363-
// ListServicesByNamespaceID returns list of services in given namespace.
371+
// ListServicesByNamespaceID returns a list of services in a given namespace.
364372
func (p *AWSSDProvider) ListServicesByNamespaceID(ctx context.Context, namespaceID *string) (map[string]*sdtypes.Service, error) {
365373
services := make([]sdtypes.ServiceSummary, 0)
366374

@@ -369,7 +377,7 @@ func (p *AWSSDProvider) ListServicesByNamespaceID(ctx context.Context, namespace
369377
Name: sdtypes.ServiceFilterNameNamespaceId,
370378
Values: []string{*namespaceID},
371379
}},
372-
MaxResults: aws.Int32(100),
380+
MaxResults: aws.Int32(maxResults),
373381
})
374382
for paginator.HasMorePages() {
375383
resp, err := paginator.NextPage(ctx)
@@ -412,32 +420,32 @@ func (p *AWSSDProvider) CreateService(ctx context.Context, namespaceID *string,
412420
ttl = int64(ep.RecordTTL)
413421
}
414422

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+
}
432427

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
434443
}
435444

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
438446
}
439447

440-
// UpdateService updates the specified service with information from provided endpoint.
448+
// UpdateService updates the specified service with information from the provided endpoint.
441449
func (p *AWSSDProvider) UpdateService(ctx context.Context, service *sdtypes.Service, ep *endpoint.Endpoint) error {
442450
log.Infof("Updating service \"%s\"", *service.Name)
443451

@@ -448,45 +456,52 @@ func (p *AWSSDProvider) UpdateService(ctx context.Context, service *sdtypes.Serv
448456
ttl = int64(ep.RecordTTL)
449457
}
450458

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
467461
}
468462

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
470476
}
471477

472478
// DeleteService deletes empty Service from AWS API if its owner id match
473479
func (p *AWSSDProvider) DeleteService(ctx context.Context, service *sdtypes.Service) error {
474480
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
489494
}
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+
490505
return nil
491506
}
492507

@@ -619,7 +634,7 @@ func (p *AWSSDProvider) routingPolicyFromEndpoint(ep *endpoint.Endpoint) sdtypes
619634
return sdtypes.RoutingPolicyWeighted
620635
}
621636

622-
// determine service type (A, AAAA, CNAME) from given endpoint
637+
// determine the service type (A, AAAA, CNAME) from a given endpoint
623638
func (p *AWSSDProvider) serviceTypeFromEndpoint(ep *endpoint.Endpoint) sdtypes.RecordType {
624639
switch ep.RecordType {
625640
case endpoint.RecordTypeCNAME:

0 commit comments

Comments
 (0)