-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from phamhongviet/feature/custom-domain-name
Feature/custom domain name
- Loading branch information
Showing
9 changed files
with
279 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"my-custom-dn-1.serf": { | ||
"name": "^web-[0-5][0-9]", | ||
"status": "alive", | ||
"tags": { | ||
"role": "web" | ||
} | ||
}, | ||
"failed.web.serf": { | ||
"name": "^web-.*", | ||
"status": "failed", | ||
"tags": { | ||
"role": "web" | ||
} | ||
}, | ||
"us.dc.serf": { | ||
"tags": { | ||
"dc": "us-.*" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Custom Domain Name | ||
|
||
## Problem | ||
Using serf-dns with domain name parsed into tags has some limitation: | ||
- cannot use together with name and status | ||
- cannot use regex | ||
- limited to 127 labels, or 63 tags | ||
- limited to 253 characters in full domain name | ||
|
||
## Solution | ||
Use custom pre-registered domain name with pre-configured serf filters | ||
|
||
For example: configure domain name `my-custom-dn-1.serf` with name `^web-[0-5][0-9]`, and tags `role=web` | ||
|
||
Use a JSON file to configure custom domain name, like this: | ||
|
||
```json | ||
{ | ||
"my-custom-dn-1.serf": { | ||
"name": "^web-[0-5][0-9]", | ||
"status": "alive", | ||
"tags": { | ||
"role": "web" | ||
} | ||
}, | ||
"failed.web.serf": { | ||
"name": "^web-.*", | ||
"status": "failed", | ||
"tags": { | ||
"role": "web", | ||
"version": "201605[0-9][0-9].*" | ||
} | ||
}, | ||
"us.dc.serf": { | ||
"tags": { | ||
"dc": "us-.*" | ||
} | ||
} | ||
} | ||
``` | ||
|
||
The JSON file above will register 3 domain names: | ||
- my-custom-dn-1.serf: return hosts named range from web-00 to web-59, still alive and tagged role=web | ||
- failed.web.serf: return hosts whose name start with web-, already dead and tagged role=web and version start with 201605 | ||
- us.dc.serf: return hosts in US whose tag dc start with us-. Normally, query to this domain name will return hosts with tag dc=us |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
type serfFilterTable map[string]serfFilter | ||
|
||
func checkCustomDomainNameExistence(domainName string, sftab serfFilterTable) bool { | ||
_, ok := sftab[domainName] | ||
return ok | ||
} | ||
|
||
func loadCustomDomainName(data []byte) serfFilterTable { | ||
var sftab serfFilterTable | ||
|
||
// TODO: handle error here | ||
json.Unmarshal(data, &sftab) | ||
return sftab | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package main | ||
|
||
import ( | ||
"io/ioutil" | ||
"testing" | ||
) | ||
|
||
func TestCheckCustomDomainNameExistence(t *testing.T) { | ||
SFTable := serfFilterTable{ | ||
"dead.digit.serf.": serfFilter{ | ||
Name: "^[0-9].*", | ||
Status: "failed", | ||
}, | ||
"digit.name.serf.": serfFilter{ | ||
Name: "^[0-9].*", | ||
Status: "alive", | ||
}, | ||
"dead.serf.": serfFilter{ | ||
Status: "failed", | ||
}, | ||
} | ||
|
||
for dn := range SFTable { | ||
ok := checkCustomDomainNameExistence(dn, SFTable) | ||
if !ok { | ||
t.Errorf("Failed to find existing custom domain name %s", dn) | ||
} | ||
} | ||
|
||
for _, dn := range []string{"not.exist.serf.", "no.good.nope.", "also.not.exists."} { | ||
ok := checkCustomDomainNameExistence(dn, SFTable) | ||
if ok { | ||
t.Errorf("Failed: Custom domain name %s actually does not exist", dn) | ||
} | ||
} | ||
} | ||
|
||
func TestLoadCustomDomainName(t *testing.T) { | ||
data, err := ioutil.ReadFile("custom-domain-name.json") | ||
if err != nil { | ||
t.Errorf("Error reading data from custom-domain-name.json: %s", err.Error()) | ||
} | ||
|
||
expect := serfFilterTable{ | ||
"my-custom-dn-1.serf": serfFilter{ | ||
Name: "^web-[0-5][0-9]", | ||
Status: "alive", | ||
Tags: map[string]string{ | ||
"role": "web", | ||
}, | ||
}, | ||
"failed.web.serf": serfFilter{ | ||
Name: "^web-.*", | ||
Status: "failed", | ||
Tags: map[string]string{ | ||
"role": "web", | ||
}, | ||
}, | ||
"us.dc.serf": serfFilter{ | ||
Tags: map[string]string{ | ||
"dc": "us-.*", | ||
}, | ||
}, | ||
} | ||
result := loadCustomDomainName(data) | ||
|
||
if len(expect) != len(result) { | ||
t.Errorf("Failed to load custom domain name: result serf filter table is different from the expected one.") | ||
} | ||
|
||
for dn, sf := range expect { | ||
resultSF := result[dn] | ||
if sf.Compare(resultSF) != true { | ||
t.Errorf("Failed to load custom domain name: result serf filter table is different from the expected one.") | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters