parse DNSOptions

Signed-off-by: Haiyan Meng <hmeng@redhat.com>
This commit is contained in:
Haiyan Meng 2016-07-25 10:24:22 -04:00 committed by Mrunal Patel
parent f2a2b06e61
commit b98900eb55
3 changed files with 87 additions and 3 deletions

7
server/const.go Normal file
View file

@ -0,0 +1,7 @@
package server
const (
// According to http://man7.org/linux/man-pages/man5/resolv.conf.5.html:
// "The search list is currently limited to six domains with a total of 256 characters."
maxDNSSearches = 6
)

View file

@ -73,9 +73,19 @@ func (s *Server) CreatePodSandbox(ctx context.Context, req *pb.CreatePodSandboxR
logDir = fmt.Sprintf("/var/log/ocid/pods/%s", name)
}
// TODO: construct /etc/resolv.conf based on dnsOpts.
dnsOpts := req.GetConfig().GetDnsOptions()
fmt.Println(dnsOpts)
dnsServers := req.GetConfig().GetDnsOptions().GetServers()
dnsSearches := req.GetConfig().GetDnsOptions().GetSearches()
resolvPath := fmt.Sprintf("%s/resolv.conf", podSandboxDir)
if err := parseDNSOptions(dnsServers, dnsSearches, resolvPath); err != nil {
if err1 := removeFile(resolvPath); err1 != nil {
return nil, fmt.Errorf("%v; failed to remove %s: %v", err, resolvPath, err1)
}
return nil, err
}
if err := g.AddBindMount(fmt.Sprintf("%s:/etc/resolv.conf", resolvPath)); err != nil {
return nil, err
}
// TODO: the unit of cpu here is cores. How to map it into specs.Spec.Linux.Resouces.CPU?
cpu := req.GetConfig().GetResources().GetCpu()

View file

@ -3,8 +3,11 @@ package server
import (
"errors"
"fmt"
"io"
"os"
"path/filepath"
"runtime"
"strings"
)
func getGPRCVersion() (string, error) {
@ -24,3 +27,67 @@ func getGPRCVersion() (string, error) {
}
return out, nil
}
func copyFile(src, dest string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dest)
if err != nil {
return err
}
defer out.Close()
if _, err := io.Copy(out, in); err != nil {
return err
}
return nil
}
func removeFile(path string) error {
if _, err := os.Stat(path); err == nil {
if err := os.Remove(path); err != nil {
return err
}
}
return nil
}
func parseDNSOptions(servers, searches []string, path string) error {
nServers := len(servers)
nSearches := len(searches)
if nServers == 0 && nSearches == 0 {
return copyFile("/etc/resolv.conf", path)
}
if nSearches > maxDNSSearches {
return fmt.Errorf("DNSOption.Searches has more than 6 domains")
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
if nSearches > 0 {
data := fmt.Sprintf("search %s\n", strings.Join(searches, " "))
_, err = f.Write([]byte(data))
if err != nil {
return err
}
}
if nServers > 0 {
data := fmt.Sprintf("nameserver %s\n", strings.Join(servers, "\nnameserver "))
_, err = f.Write([]byte(data))
if err != nil {
return err
}
}
return nil
}