Skip to content

Commit 95c2c72

Browse files
fix(provider): aws-sd provider null pointer (#5404)
Signed-off-by: ivan katliarchuk <[email protected]>
1 parent 060ded1 commit 95c2c72

File tree

5 files changed

+584
-317
lines changed

5 files changed

+584
-317
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: 73 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 {
@@ -155,6 +158,11 @@ func (p *AWSSDProvider) Records(ctx context.Context) (endpoints []*endpoint.Endp
155158
continue
156159
}
157160

161+
if srv.Description == nil {
162+
log.Warnf("Skipping service %q as owner id not configured", *srv.Name)
163+
continue
164+
}
165+
158166
endpoints = append(endpoints, p.instancesToEndpoint(ns, srv, resp.Instances))
159167
}
160168
}
@@ -167,6 +175,7 @@ func (p *AWSSDProvider) instancesToEndpoint(ns *sdtypes.NamespaceSummary, srv *s
167175
recordName := *srv.Name + "." + *ns.Name
168176

169177
labels := endpoint.NewLabels()
178+
170179
labels[endpoint.AWSSDDescriptionLabel] = *srv.Description
171180

172181
newEndpoint := &endpoint.Endpoint{
@@ -288,7 +297,7 @@ func (p *AWSSDProvider) submitCreates(ctx context.Context, namespaces []*sdtypes
288297
if err != nil {
289298
return err
290299
}
291-
// update local list of services
300+
// update a local list of services
292301
services[*srv.Name] = srv
293302
} else if ch.RecordTTL.IsConfigured() && *srv.DnsConfig.DnsRecords[0].TTL != int64(ch.RecordTTL) {
294303
// update service when TTL differ
@@ -360,7 +369,7 @@ func (p *AWSSDProvider) ListNamespaces(ctx context.Context) ([]*sdtypes.Namespac
360369
return namespaces, nil
361370
}
362371

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

@@ -369,7 +378,7 @@ func (p *AWSSDProvider) ListServicesByNamespaceID(ctx context.Context, namespace
369378
Name: sdtypes.ServiceFilterNameNamespaceId,
370379
Values: []string{*namespaceID},
371380
}},
372-
MaxResults: aws.Int32(100),
381+
MaxResults: aws.Int32(maxResults),
373382
})
374383
for paginator.HasMorePages() {
375384
resp, err := paginator.NextPage(ctx)
@@ -412,32 +421,32 @@ func (p *AWSSDProvider) CreateService(ctx context.Context, namespaceID *string,
412421
ttl = int64(ep.RecordTTL)
413422
}
414423

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

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
434444
}
435445

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
438447
}
439448

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

@@ -448,45 +457,52 @@ func (p *AWSSDProvider) UpdateService(ctx context.Context, service *sdtypes.Serv
448457
ttl = int64(ep.RecordTTL)
449458
}
450459

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
467462
}
468463

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
470477
}
471478

472479
// DeleteService deletes empty Service from AWS API if its owner id match
473480
func (p *AWSSDProvider) DeleteService(ctx context.Context, service *sdtypes.Service) error {
474481
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
489495
}
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+
490506
return nil
491507
}
492508

@@ -619,7 +635,7 @@ func (p *AWSSDProvider) routingPolicyFromEndpoint(ep *endpoint.Endpoint) sdtypes
619635
return sdtypes.RoutingPolicyWeighted
620636
}
621637

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

0 commit comments

Comments
 (0)