Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(route53): optimize hosted zones retrieval #130

Open
wants to merge 1 commit into
base: release-0.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions internal/cloud_providers/aws/aws-route53.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/aws/aws-sdk-go/service/route53"
)

const ()

type AWSRoute53Connection struct {
client *route53.Route53
}
Expand All @@ -29,28 +27,44 @@ func WithRoute53() AWSConnectionOption {
}
}

// getRoute53HostedZones get a list of every Hosted Zone on the Route53 service
func (c *AWSRoute53Connection) GetRoute53HostedZones() ([]*route53.HostedZone, error) {
type HostedZone struct {
Zone *route53.HostedZone
Tags []*route53.Tag
}

// GetZonesWithTags retrieves all hosted zones and their associated tags from AWS Route53
func (c *AWSRoute53Connection) GetZonesWithTags() ([]HostedZone, error) {
// Route 53 returns up to 100 items in each response.
// If you have a lot of hosted zones, use the MaxItems parameter to list them in groups of up to 100.
// https://pkg.go.dev/github.com/aws/aws-sdk-go/service/route53#Route53.ListHostedZonesByName
input := route53.ListHostedZonesByNameInput{}
result, err := c.client.ListHostedZonesByName(&input)
if err != nil {
return nil, err
}
return result.HostedZones, nil
}
zonesWithTags := make([]HostedZone, 0, len(result.HostedZones))

for _, zone := range result.HostedZones {
hztype := route53.TagResourceTypeHostedzone
tags, err := c.client.ListTagsForResource(&route53.ListTagsForResourceInput{
ResourceType: &hztype,
ResourceId: aws.String(*zone.Id),
})
if err != nil {
continue
}
zonesWithTags = append(zonesWithTags, HostedZone{
Zone: zone,
Tags: tags.ResourceTagSet.Tags,
})

// CheckIfHostedZoneBelongsToCluster returns true or false if the hosted zone is associated to a cluster Ingress (routers)
func (c *AWSRoute53Connection) CheckIfHostedZoneBelongsToCluster(cluster *inventory.Cluster, hostedZone *route53.HostedZone) bool {
hztype := route53.TagResourceTypeHostedzone
output, err := c.client.ListTagsForResource(&route53.ListTagsForResourceInput{
ResourceType: &hztype,
ResourceId: aws.String(*hostedZone.Id),
})
if err != nil {
return false
}
return zonesWithTags, nil
}

for _, tag := range output.ResourceTagSet.Tags {
// ZoneBelongsToCluster returns true or false if the hosted zone is associated to a cluster Ingress (routers)
func (c *AWSRoute53Connection) ZoneBelongsToCluster(cluster *inventory.Cluster, zoneWithTags HostedZone) bool {
for _, tag := range zoneWithTags.Tags {
if strings.Contains(*(tag.Key), cluster.Name) {
return true
}
Expand All @@ -73,11 +87,9 @@ func (c *AWSRoute53Connection) GetHostedZoneRecords(hostedZoneID string) ([]*rou
records = append(records, page.ResourceRecordSets...)
return !lastPage // Continue if there are more record pages
})

if err != nil {
return nil, fmt.Errorf("Error getting the DNS registries: %w", err)
}

return records, nil

}
15 changes: 9 additions & 6 deletions internal/stocker/aws_stocker_route53.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package stocker

import (
"strings"
"time"

"github.com/RHEcosystemAppEng/cluster-iq/internal/inventory"
"github.com/aws/aws-sdk-go/aws"
Expand Down Expand Up @@ -50,20 +51,22 @@ func (s *AWSStocker) getConsoleLinkOfCluster(cluster *inventory.Cluster, hostedZ

// FindOpenshiftConsoleURLs iterates every Cluster and every Route53 HostedZone for looking for the corresponding URLs for the OCP console
func (s *AWSStocker) FindOpenshiftConsoleURLs() error {
hostedZones, err := s.conn.Route53.GetRoute53HostedZones()
start := time.Now()
hostedZones, err := s.conn.Route53.GetZonesWithTags()
if err != nil {
return err
}

for i, cluster := range s.Account.Clusters {
for _, hostedZone := range hostedZones {
// Checking if the current hosted zone belongs to the current cluster
if s.conn.Route53.CheckIfHostedZoneBelongsToCluster(cluster, hostedZone) {
s.logger.Debug("Found Hosted Zone for Cluster", zap.String("hosted_zone_id", *hostedZone.Id), zap.String("cluster_id", cluster.ID))
s.Account.Clusters[i].ConsoleLink = s.getConsoleLinkOfCluster(cluster, hostedZone)
if s.conn.Route53.ZoneBelongsToCluster(cluster, hostedZone) {
s.logger.Debug("Found Hosted Zone for Cluster", zap.String("hosted_zone_id", *hostedZone.Zone.Name), zap.String("cluster_id", cluster.ID))

s.Account.Clusters[i].ConsoleLink = s.getConsoleLinkOfCluster(cluster, hostedZone.Zone)
}
}
}

s.logger.Debug("Finished finding OpenShift console URLs",
zap.Duration("duration", time.Since(start)))
return nil
}