Skip to content

Commit 8fee5d3

Browse files
committed
ingress: Minor refactor
1 parent 8bf3956 commit 8fee5d3

File tree

2 files changed

+42
-25
lines changed

2 files changed

+42
-25
lines changed

custom-domain/dstack-ingress/scripts/dns_providers/base.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,40 @@ def create_caa_record(self, zone_id: str, caa_record: CAARecord) -> bool:
136136
"""
137137
pass
138138

139+
def set_a_record(
140+
self, zone_id: str, name: str, ip_address: str, ttl: int = 60, proxied: bool = False
141+
) -> bool:
142+
"""Set an A record (delete existing and create new).
143+
144+
Args:
145+
zone_id: The zone ID
146+
name: The record name
147+
ip_address: The IP address
148+
ttl: Time to live
149+
proxied: Whether to proxy through provider (if supported)
150+
151+
Returns:
152+
True if successful, False otherwise
153+
"""
154+
existing_records = self.get_dns_records(zone_id, name, RecordType.A)
155+
for record in existing_records:
156+
# Check if record already exists with same IP
157+
if record.content == ip_address:
158+
print("A record with the same IP already exists")
159+
return True
160+
if record.id:
161+
self.delete_dns_record(zone_id, record.id)
162+
163+
new_record = DNSRecord(
164+
id=None,
165+
name=name,
166+
type=RecordType.A,
167+
content=ip_address,
168+
ttl=ttl,
169+
proxied=proxied,
170+
)
171+
return self.create_dns_record(zone_id, new_record)
172+
139173
def set_alias_record(
140174
self,
141175
zone_id: str,

custom-domain/dstack-ingress/scripts/dns_providers/linode.py

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ def set_alias_record(
246246
Linode doesn't allow CAA and CNAME records on the same subdomain.
247247
Using A records solves this limitation.
248248
"""
249-
249+
# Resolve domain to IP
250250
domain = content
251251
print(f"Trying to resolve: {domain}")
252252
ip_address = socket.gethostbyname(domain)
@@ -255,34 +255,17 @@ def set_alias_record(
255255
if not ip_address:
256256
raise socket.gaierror("Could not resolve any variant of the domain")
257257

258-
# Check if A record already exists with same IP
259-
existing_a_records = self.get_dns_records(zone_id, name, RecordType.A)
260-
for record in existing_a_records:
261-
if record.content == ip_address:
262-
print("A record with the same IP already exists")
263-
return True
264-
265-
# Delete any existing A or CNAME records for this name
266-
for record_type in [RecordType.A, RecordType.CNAME]:
267-
existing_records = self.get_dns_records(zone_id, name, record_type)
268-
for record in existing_records:
269-
if record.id:
270-
self.delete_dns_record(zone_id, record.id)
271-
272-
# Create A record instead of CNAME
273-
new_record = DNSRecord(
274-
id=None,
275-
name=name,
276-
type=RecordType.A,
277-
content=ip_address,
278-
ttl=ttl,
279-
proxied=False, # Linode doesn't support proxying
280-
)
258+
# Delete any existing CNAME records for this name (clean transition)
259+
existing_cname_records = self.get_dns_records(zone_id, name, RecordType.CNAME)
260+
for record in existing_cname_records:
261+
if record.id:
262+
self.delete_dns_record(zone_id, record.id)
281263

282264
print(
283265
f"Creating A record for {name} pointing to {ip_address} (instead of CNAME to {content})"
284266
)
285-
return self.create_dns_record(zone_id, new_record)
267+
# Use the base class's set_a_record method with idempotency
268+
return self.set_a_record(zone_id, name, ip_address, ttl, proxied=False)
286269

287270
def create_caa_record(self, zone_id: str, caa_record: CAARecord) -> bool:
288271
"""Create a CAA record."""

0 commit comments

Comments
 (0)