@@ -2,17 +2,20 @@ package aws
22
33import (
44 "fmt"
5+ "strings"
56 "sync"
67
78 "github.com/sirupsen/logrus"
89
910 "github.com/openshift/cluster-ingress-operator/pkg/dns"
1011
1112 "github.com/aws/aws-sdk-go/aws"
13+ "github.com/aws/aws-sdk-go/aws/arn"
1214 "github.com/aws/aws-sdk-go/aws/credentials"
1315 "github.com/aws/aws-sdk-go/aws/ec2metadata"
1416 "github.com/aws/aws-sdk-go/aws/session"
1517 "github.com/aws/aws-sdk-go/service/elb"
18+ "github.com/aws/aws-sdk-go/service/resourcegroupstaggingapi"
1619 "github.com/aws/aws-sdk-go/service/route53"
1720)
1821
@@ -33,11 +36,16 @@ var _ dns.Manager = &Manager{}
3336type Manager struct {
3437 elb * elb.ELB
3538 route53 * route53.Route53
39+ tags * resourcegroupstaggingapi.ResourceGroupsTaggingAPI
3640
3741 config Config
38- lock sync.RWMutex
39- // zones is a cache of all known DNS zones and their associated tags.
40- zones []* zoneWithTags
42+
43+ // lock protects access to everything below.
44+ lock sync.RWMutex
45+ // publicZoneID is the public zone shared by all clusters.
46+ publicZoneID string
47+ // privateZoneID is the private zone owned by the cluster.
48+ privateZoneID string
4149 // updatedRecords is a cache of records which have been created during the
4250 // life of this manager. The key is zoneID+domain+target. This is a quick hack
4351 // to minimize AWS API calls for now.
@@ -87,85 +95,74 @@ func NewManager(config Config) (*Manager, error) {
8795 return & Manager {
8896 elb : elb .New (sess , aws .NewConfig ().WithRegion (region )),
8997 route53 : route53 .New (sess ),
90-
98+ // TODO: This API will only return hostedzone resources (which are global)
99+ // when the region is forced to us-east-1. We don't yet understand why.
100+ tags : resourcegroupstaggingapi .New (sess , aws .NewConfig ().WithRegion ("us-east-1" )),
91101 config : config ,
92- zones : []* zoneWithTags {},
93102 updatedRecords : map [string ]struct {}{},
94103 }, nil
95104}
96105
97- type zoneWithTags struct {
98- zone * route53.HostedZone
99- tags []* route53.Tag
100- }
101-
102- // updateZoneCache caches all zones and their tags.
103- func (m * Manager ) updateZoneCache () error {
106+ // discoverZones finds the public and private zones to manage, setting the
107+ // privateZoneID and publicZoneID fields. An error is returned if either zone
108+ // can't be found.
109+ func (m * Manager ) discoverZones () error {
104110 m .lock .Lock ()
105111 defer m .lock .Unlock ()
106112
107- // There's no reason to update this more than once as we're only interested in
108- // the zones scoped to this cluster.
109- if len (m .zones ) > 0 {
110- return nil
111- }
112-
113- // Find and cache all zones and their tags.
114- // TODO: probably only need to store the two zones we care about.
115- zones := []* zoneWithTags {}
116- listedAllTags := true
117- f := func (resp * route53.ListHostedZonesOutput , lastPage bool ) (shouldContinue bool ) {
118- for _ , zone := range resp .HostedZones {
119- tagsList , err := m .route53 .ListTagsForResource (& route53.ListTagsForResourceInput {
120- ResourceType : aws .String ("hostedzone" ),
121- ResourceId : zone .Id ,
122- })
123- if err != nil {
124- logrus .Errorf ("failed to list tags for hosted zone %s: %v" , zone .Id , err )
125- listedAllTags = false
126- return false
113+ // Find the public zone, which is the non-private zone whose domain matches
114+ // the cluster base domain.
115+ if len (m .publicZoneID ) == 0 {
116+ f := func (resp * route53.ListHostedZonesOutput , lastPage bool ) (shouldContinue bool ) {
117+ for _ , zone := range resp .HostedZones {
118+ if aws .StringValue (zone .Name ) == m .config .BaseDomain && ! aws .BoolValue (zone .Config .PrivateZone ) {
119+ m .publicZoneID = aws .StringValue (zone .Id )
120+ return false
121+ }
127122 }
128- zones = append ( zones , & zoneWithTags { zone : zone , tags : tagsList . ResourceTagSet . Tags })
123+ return true
129124 }
130- return true
131- }
132- err := m .route53 .ListHostedZonesPages (& route53.ListHostedZonesInput {}, f )
133- if err != nil {
134- return fmt .Errorf ("failed to list hosted zones: %v" , err )
135- }
136- if ! listedAllTags {
137- return fmt .Errorf ("failed to list all hosted zone tags" )
138- }
139-
140- m .zones = zones
141- return nil
142- }
143-
144- // findClusterPublicZone finds the public zone with the base domain from config.
145- func (m * Manager ) findClusterPublicZone () * route53.HostedZone {
146- m .lock .Lock ()
147- defer m .lock .Unlock ()
148- for _ , zwt := range m .zones {
149- if aws .StringValue (zwt .zone .Name ) == m .config .BaseDomain && ! aws .BoolValue (zwt .zone .Config .PrivateZone ) {
150- return zwt .zone
125+ err := m .route53 .ListHostedZonesPages (& route53.ListHostedZonesInput {}, f )
126+ if err != nil {
127+ return fmt .Errorf ("failed to list hosted zones: %v" , err )
151128 }
129+ if len (m .publicZoneID ) == 0 {
130+ return fmt .Errorf ("couldn't find public hosted zone for %q" , m .config .BaseDomain )
131+ }
132+ logrus .Infof ("using public zone %s" , m .publicZoneID )
152133 }
153- return nil
154- }
155134
156- // findClusterPrivateZone finds the private zone with the base domain and
157- // cluster ID from config.
158- func (m * Manager ) findClusterPrivateZone () * route53.HostedZone {
159- m .lock .Lock ()
160- defer m .lock .Unlock ()
161- for _ , zwt := range m .zones {
162- if aws .StringValue (zwt .zone .Name ) == m .config .BaseDomain && aws .BoolValue (zwt .zone .Config .PrivateZone ) {
163- for _ , tag := range zwt .tags {
164- if aws .StringValue (tag .Key ) == "openshiftClusterID" && aws .StringValue (tag .Value ) == m .config .ClusterID {
165- return zwt .zone
166- }
135+ // Find the private zone, which is the private zone whose openshiftClusterID tag
136+ // value matches the cluster ID.
137+ if len (m .privateZoneID ) == 0 {
138+ zones , err := m .tags .GetResources (& resourcegroupstaggingapi.GetResourcesInput {
139+ ResourceTypeFilters : []* string {aws .String ("route53:hostedzone" )},
140+ TagFilters : []* resourcegroupstaggingapi.TagFilter {
141+ {
142+ Key : aws .String ("openshiftClusterID" ),
143+ Values : []* string {aws .String (m .config .ClusterID )},
144+ },
145+ },
146+ })
147+ if err != nil {
148+ return fmt .Errorf ("failed to get tagged resources: %v" , err )
149+ }
150+ for _ , zone := range zones .ResourceTagMappingList {
151+ zoneARN , err := arn .Parse (aws .StringValue (zone .ResourceARN ))
152+ if err != nil {
153+ return fmt .Errorf ("failed to parse hostedzone ARN %q: %v" , aws .StringValue (zone .ResourceARN ), err )
167154 }
155+ elems := strings .Split (zoneARN .Resource , "/" )
156+ if len (elems ) != 2 || elems [0 ] != "hostedzone" {
157+ return fmt .Errorf ("got unexpected resource ARN: %v" , zoneARN )
158+ }
159+ m .privateZoneID = elems [1 ]
160+ break
168161 }
162+ if len (m .privateZoneID ) == 0 {
163+ return fmt .Errorf ("couldn't find private hosted zone for cluster id %q" , m .config .ClusterID )
164+ }
165+ logrus .Infof ("using private zone %s" , m .privateZoneID )
169166 }
170167 return nil
171168}
@@ -182,18 +179,9 @@ func (m *Manager) EnsureAlias(domain, target string) error {
182179 }
183180
184181 // Ensure the zone cache is up to date.
185- err := m .updateZoneCache ()
182+ err := m .discoverZones ()
186183 if err != nil {
187- return fmt .Errorf ("failed to update hosted zone cache: %v" , err )
188- }
189-
190- publicZone := m .findClusterPublicZone ()
191- privateZone := m .findClusterPrivateZone ()
192- if publicZone == nil {
193- return fmt .Errorf ("failed to find public zone for cluster" )
194- }
195- if privateZone == nil {
196- return fmt .Errorf ("failed to find private zone for cluster" )
184+ return fmt .Errorf ("failed to discover hosted zones: %v" , err )
197185 }
198186
199187 // Find the target hosted zone of the load balancer attached to the service.
@@ -219,7 +207,7 @@ func (m *Manager) EnsureAlias(domain, target string) error {
219207 // TODO: handle the caching/diff detection in a better way.
220208 m .lock .Lock ()
221209 defer m .lock .Unlock ()
222- for _ , zoneID := range []string {aws . StringValue ( publicZone . Id ), aws . StringValue ( privateZone . Id ) } {
210+ for _ , zoneID := range []string {m . publicZoneID , m . privateZoneID } {
223211 key := zoneID + domain + target
224212 // Only process these once, for now.
225213 if _ , exists := m .updatedRecords [key ]; exists {
0 commit comments