-
Notifications
You must be signed in to change notification settings - Fork 35
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 #71 from Roblox/extra_hosts
Add support for extra_hosts.
- Loading branch information
Showing
8 changed files
with
221 additions
and
0 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
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,112 @@ | ||
/* | ||
Copyright 2020 Roblox Corporation | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package etchosts | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
"io/ioutil" | ||
"os" | ||
"strings" | ||
|
||
"github.com/docker/docker/opts" | ||
) | ||
|
||
// Code referenced from https://github.com/moby/libnetwork/blob/master/etchosts/etchosts.go | ||
|
||
// Record Structure for a single host record | ||
type Record struct { | ||
Hosts string | ||
IP string | ||
} | ||
|
||
// WriteTo writes record to file and returns bytes written or error | ||
func (r Record) WriteTo(w io.Writer) (int64, error) { | ||
n, err := fmt.Fprintf(w, "%s\t%s\n", r.IP, r.Hosts) | ||
return int64(n), err | ||
} | ||
|
||
var ( | ||
// Default hosts config records slice | ||
defaultContent = []Record{ | ||
{Hosts: "localhost", IP: "127.0.0.1"}, | ||
{Hosts: "localhost ip6-localhost ip6-loopback", IP: "::1"}, | ||
{Hosts: "ip6-localnet", IP: "fe00::0"}, | ||
{Hosts: "ip6-mcastprefix", IP: "ff00::0"}, | ||
{Hosts: "ip6-allnodes", IP: "ff02::1"}, | ||
{Hosts: "ip6-allrouters", IP: "ff02::2"}, | ||
} | ||
) | ||
|
||
// BuildEtcHosts builds NOMAD_TASK_DIR/etc_hosts with defaults. | ||
func BuildEtcHosts(hostsFile string) error { | ||
content := bytes.NewBuffer(nil) | ||
|
||
// Write defaultContent slice to buffer | ||
for _, r := range defaultContent { | ||
if _, err := r.WriteTo(content); err != nil { | ||
return err | ||
} | ||
} | ||
return ioutil.WriteFile(hostsFile, content.Bytes(), 0644) | ||
} | ||
|
||
// CopyEtcHosts copies /etc/hosts to NOMAD_TASK_DIR/etc_hosts | ||
func CopyEtcHosts(hostsFile string) error { | ||
srcFile, err := os.Open("/etc/hosts") | ||
if err != nil { | ||
return err | ||
} | ||
defer srcFile.Close() | ||
|
||
destFile, err := os.Create(hostsFile) | ||
if err != nil { | ||
return err | ||
} | ||
defer destFile.Close() | ||
|
||
_, err = io.Copy(destFile, srcFile) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// AddExtraHosts add hosts, given as name:IP to container /etc/hosts. | ||
func AddExtraHosts(hostsFile string, extraHosts []string) error { | ||
fd, err := os.OpenFile(hostsFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
defer fd.Close() | ||
|
||
for _, extraHost := range extraHosts { | ||
// allow IPv6 addresses in extra hosts; only split on first ":" | ||
if _, err := opts.ValidateExtraHost(extraHost); err != nil { | ||
return err | ||
} | ||
|
||
hostnameIP := strings.SplitN(extraHost, ":", 2) | ||
msg := fmt.Sprintf("%s\t%s\n", hostnameIP[1], hostnameIP[0]) | ||
if _, err := fd.WriteString(msg); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
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 @@ | ||
job "extra_hosts" { | ||
datacenters = ["dc1"] | ||
|
||
group "extra_hosts-group" { | ||
task "extra_hosts-task" { | ||
driver = "containerd-driver" | ||
config { | ||
image = "docker.io/library/ubuntu:16.04" | ||
extra_hosts = ["postgres:127.0.1.1", "redis:127.0.1.2"] | ||
host_network = true | ||
command = "sleep" | ||
args = ["600s"] | ||
} | ||
|
||
resources { | ||
cpu = 500 | ||
memory = 256 | ||
} | ||
} | ||
} | ||
} |
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,49 @@ | ||
#!/bin/bash | ||
|
||
source $SRCDIR/utils.sh | ||
|
||
job_name=extra_hosts | ||
|
||
test_extra_hosts_nomad_job() { | ||
pushd ~/go/src/github.com/Roblox/nomad-driver-containerd/example | ||
|
||
echo "INFO: Starting nomad $job_name job using nomad-driver-containerd." | ||
nomad job run $job_name.nomad | ||
|
||
# Even though $(nomad job status) reports job status as "running" | ||
# The actual container process might not be running yet. | ||
# We need to wait for actual container to start running before trying exec. | ||
echo "INFO: Wait for ${job_name} container to get into RUNNING state, before trying exec." | ||
is_container_active ${job_name} true | ||
|
||
echo "INFO: Checking status of $job_name job." | ||
job_status=$(nomad job status -short $job_name|grep Status|awk '{split($0,a,"="); print a[2]}'|tr -d ' ') | ||
if [ "$job_status" != "running" ];then | ||
echo "ERROR: Error in getting ${job_name} job status." | ||
return 1 | ||
fi | ||
|
||
echo "INFO: Checking extra hosts info in /etc/hosts." | ||
output=$(nomad alloc exec -job ${job_name} cat /etc/hosts) | ||
for host in "127.0.1.1 postgres" "127.0.1.2 redis" ; do | ||
echo -e "$output" |grep "$host" &>/dev/null | ||
if [ $? -ne 0 ];then | ||
echo "ERROR: extra host $host not found." | ||
return 1 | ||
fi | ||
done | ||
|
||
echo "INFO: Stopping nomad ${job_name} job." | ||
nomad job stop ${job_name} | ||
job_status=$(nomad job status -short ${job_name}|grep Status|awk '{split($0,a,"="); print a[2]}'|tr -d ' ') | ||
if [ $job_status != "dead(stopped)" ];then | ||
echo "ERROR: Error in stopping ${job_name} job." | ||
exit 1 | ||
fi | ||
|
||
echo "INFO: purge nomad ${job_name} job." | ||
nomad job stop -purge ${job_name} | ||
popd | ||
} | ||
|
||
test_extra_hosts_nomad_job |