Skip to content

Commit 2b4314d

Browse files
authored
feat(root-zone): handle dns root zone with alias (#17)
1 parent 3b4f42e commit 2b4314d

File tree

4 files changed

+55
-12
lines changed

4 files changed

+55
-12
lines changed

.github/workflows/main.yaml

+2-3
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,8 @@ jobs:
5353
scw_secret_key: ${{ secrets.SECRET_KEY }}
5454
scw_containers_namespace_id: ae28eaf1-3b94-4660-bce0-9b0e0a5d1062
5555
scw_registry: rg.fr-par.scw.cloud/aphilibeaux/scaleway-form:latest
56-
scw_container_port: "80"
5756
scw_dns: containers.philibeaux.fr
58-
scw_dns_prefix: testing-github
57+
root_zone: true
5958

6059
- name: check output
6160
shell: bash
@@ -104,7 +103,7 @@ jobs:
104103
scw_containers_namespace_id: ae28eaf1-3b94-4660-bce0-9b0e0a5d1062
105104
scw_registry: rg.fr-par.scw.cloud/aphilibeaux/scaleway-form:latest
106105
scw_dns: containers.philibeaux.fr
107-
scw_dns_prefix: testing-github
106+
root_zone: true
108107

109108
- name: check output
110109
shell: bash

action.yaml

+6-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,12 @@ inputs:
2929
description: "DNS name where your container will be available"
3030
required: false
3131
scw_dns_prefix:
32-
description: "DNS name where your container will be available"
32+
description: "This will override prefix of your dns. if it's not set thane name of the containers ( max 34 char actually) will be set"
33+
required: false
34+
root_zone:
35+
description: "This will add ALIAS on your root zone. Actually CNAME is used for all `¨scw_dns_prefix.scw_dns` of your dns"
3336
required: false
37+
default: "false"
3438

3539
# scw_debug:
3640
# description: "debug log api call"
@@ -60,6 +64,7 @@ runs:
6064
- ${{ inputs.scw_memory_limit }}
6165
- ${{ inputs.scw_dns }}
6266
- ${{ inputs.scw_dns_prefix }}
67+
- ${{ inputs.root_zone }}
6368
# - ${{ inputs.scw_debug }}
6469
branding:
6570
icon: "cloud-lightning"

dns.go

+46-8
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ const (
1616
)
1717

1818
const (
19-
TYPE = "CNAME"
19+
CNAME = "CNAME"
20+
ALIAS = "ALIAS"
2021
)
2122

2223
var (
@@ -73,22 +74,39 @@ func DeleteDNSRecord(
7374
) (*domain.UpdateDNSZoneRecordsResponse, error) {
7475
fmt.Println("Update Zone DNS - Delete")
7576

77+
// ENV
78+
Prefix := os.Getenv(EnvDNSPrefix)
79+
RootZone := os.Getenv(EnvRootZone)
80+
7681
api := domain.NewAPI(client)
7782

7883
Data := Container.DomainName + "."
7984

80-
Prefix := os.Getenv(EnvDNSPrefix)
81-
8285
var Name string = Container.Name
86+
var Type domain.RecordType = CNAME
87+
88+
// Handle Prefix DNS
8389

8490
if Prefix != "" {
8591
Name = Prefix
92+
93+
fmt.Println("Update With Prefix Zone DNS - Delete", Prefix)
94+
}
95+
96+
// Handle Root Zone Alias
97+
// Some DNS doesn't handle correctly CNAME on Root Zone.
98+
// We should use an Alias
99+
100+
if RootZone == "true" {
101+
Name = ""
102+
Type = ALIAS
103+
fmt.Println("Update Root Zone DNS - Delete")
86104
}
87105

88106
IDFields := &domain.RecordIdentifier{
89107
Name: Name,
90108
Data: &Data,
91-
Type: TYPE,
109+
Type: Type,
92110
TTL: &TTL,
93111
}
94112

@@ -120,22 +138,42 @@ func AddDNSRecord(
120138
DNSZone string,
121139
) (string, error) {
122140

141+
// ENV
142+
Prefix := os.Getenv(EnvDNSPrefix)
143+
RootZone := os.Getenv(EnvRootZone)
144+
123145
fmt.Println("Update Zone DNS - Add")
124146

125147
api := domain.NewAPI(client)
126148

127-
Prefix := os.Getenv(EnvDNSPrefix)
128-
129149
var Name string = Container.Name
150+
var Type domain.RecordType = CNAME
151+
152+
// Handle Prefix DNS
130153

131154
if Prefix != "" {
132155
Name = Prefix
156+
157+
fmt.Println("Update With Prefix Zone DNS - Add", Prefix)
158+
}
159+
160+
var Hostname string = Name + "." + DNSZone
161+
162+
// Handle Root Zone Alias
163+
// Some DNS doesn't handle correctly CNAME on Root Zone.
164+
// We should use an Alias
165+
166+
if RootZone == "true" {
167+
Name = ""
168+
Type = ALIAS
169+
Hostname = DNSZone
170+
fmt.Println("Update Root Zone DNS - Add")
133171
}
134172

135173
Records := []*domain.Record{
136174
{
137175
Name: Name,
138-
Type: TYPE,
176+
Type: Type,
139177
TTL: TTL,
140178
Data: Container.DomainName + ".",
141179
},
@@ -159,7 +197,7 @@ func AddDNSRecord(
159197
return "", err
160198
}
161199

162-
Hostname := Name + "." + DNSZone
200+
fmt.Println("Hostname", Hostname)
163201

164202
return Hostname, nil
165203
}

main.go

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ const (
2020
EnvProjectID = "INPUT_SCW_PROJECT_ID"
2121
EnvSecretKey = "INPUT_SCW_SECRET_KEY"
2222
EnvMemoryLimit = "INPUT_SCW_MEMORY_LIMIT"
23+
EnvRootZone = "INPUT_ROOT_ZONE"
2324
)
2425

2526
var (

0 commit comments

Comments
 (0)