Skip to content

Commit

Permalink
Merge pull request kelseyhightower#333 from jnummelin/feature/ip_dns_…
Browse files Browse the repository at this point in the history
…resolving

IP dns resolving function for templates
  • Loading branch information
kelseyhightower committed Jan 26, 2016
2 parents ccf2486 + a03a3f5 commit 071e6ab
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
bin
vendor
pkg
9 changes: 9 additions & 0 deletions Dockerfile.build.alpine
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
FROM gliderlabs/alpine:3.2
MAINTAINER Jussi Nummelin <[email protected]>
RUN apk-install bash go bzr git mercurial subversion openssh-client ca-certificates

RUN mkdir -p /go/src /go/bin && chmod -R 777 /go
ENV GOPATH /go
ENV PATH /go/bin:$PATH
WORKDIR /app
RUN go get github.com/constabulary/gb/...
10 changes: 10 additions & 0 deletions docs/installation.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ $ ./build
$ sudo ./install
```

#### Building from Source for Alpine Linux

Since many people are using Alpine Linux as their base images for Docker there's support to build Alpine package also. Naturally by using Docker itself. :)

```
$ docker build -t confd_builder -f Dockerfile.build.alpine .
$ docker run -ti -v $(pwd):/app confd_builder ./build
```
The above docker commands will produce binary in the local bin directory.

### Next Steps

Get up and running with the [Quick Start Guide](quick-start-guide.md).
11 changes: 10 additions & 1 deletion docs/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,16 @@ Alias for the strings.Join function.
services: {{join $services ","}}
```

### lookupIP

Wrapper for net.LookupIP function. The wrapper also sorts (alphabeticaly) the IP addresses. This is crucial since in dynamic environments DNS servers typically shuffle the addresses linked to domain name. And that would cause unnecessary config reloads.

```
{{range lookupIP "some.host.local"}}
server {{.}};
{{end}}
```

## Example Usage

```Bash
Expand Down Expand Up @@ -380,4 +390,3 @@ server {
```

Go's [`text/template`](http://golang.org/pkg/text/template/) package is very powerful. For more details on it's capabilities see its [documentation.](http://golang.org/pkg/text/template/)

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"path"
"strings"
"time"
"net"
"sort"
)

func newFuncMap() map[string]interface{} {
Expand All @@ -22,6 +24,7 @@ func newFuncMap() map[string]interface{} {
m["toLower"] = strings.ToLower
m["contains"] = strings.Contains
m["replace"] = strings.Replace
m["lookupIP"] = LookupIP
return m
}

Expand All @@ -42,3 +45,18 @@ func UnmarshalJsonArray(data string) ([]interface{}, error) {
err := json.Unmarshal([]byte(data), &ret)
return ret, err
}

func LookupIP(data string) ([]string) {
ips, err := net.LookupIP(data)
if(err != nil) {
return nil
}
// "Cast" IPs into strings and sort the array
ipStrings := make([]string, len(ips))

for i, ip := range ips {
ipStrings[i] = ip.String()
}
sort.Strings(ipStrings)
return ipStrings
}
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,34 @@ dir: {{.}}
dir: /test/data
`,
updateStore: func(tr *TemplateResource) {
tr.store.Set("/test/data", "parent")
tr.store.Set("/test/data/def", "child")
},
},
templateTest{
desc: "ip lookup test",
toml: `
[template]
src = "test.conf.tmpl"
dest = "./tmp/test.conf"
keys = [
"/test/data",
"/test/data/abc",
]
`,
tmpl: `
{{range lookupIP "localhost"}}
ip: {{.}}
{{end}}
`,
expected: `
ip: 127.0.0.1
ip: ::1
`,
updateStore: func(tr *TemplateResource) {
tr.store.Set("/test/data", "parent")
Expand Down

0 comments on commit 071e6ab

Please sign in to comment.