VMware vSphere Integrated Containers provider (#206)

* Add Virtual Kubelet provider for VIC

Initial virtual kubelet provider for VMware VIC.  This provider currently
handles creating and starting of a pod VM via the VIC portlayer and persona
server.  Image store handling via the VIC persona server.  This provider
currently requires the feature/wolfpack branch of VIC.

* Added pod stop and delete.  Also added node capacity.

Added the ability to stop and delete pod VMs via VIC.  Also retrieve
node capacity information from the VCH.

* Cleanup and readme file

Some file clean up and added a Readme.md markdown file for the VIC
provider.

* Cleaned up errors, added function comments, moved operation code

1. Cleaned up error handling.  Set standard for creating errors.
2. Added method prototype comments for all interface functions.
3. Moved PodCreator, PodStarter, PodStopper, and PodDeleter to a new folder.

* Add mocking code and unit tests for podcache, podcreator, and podstarter

Used the unit test framework used in VIC to handle assertions in the provider's
unit test.  Mocking code generated using OSS project mockery, which is compatible
with the testify assertion framework.

* Vendored packages for the VIC provider

Requires feature/wolfpack branch of VIC and a few specific commit sha of
projects used within VIC.

* Implementation of POD Stopper and Deleter unit tests (#4)

* Updated files for initial PR
This commit is contained in:
Loc Nguyen
2018-06-04 15:41:32 -07:00
committed by Ria Bhatia
parent 98a111e8b7
commit 513cebe7b7
6296 changed files with 1123685 additions and 8 deletions

114
vendor/github.com/vmware/vic/lib/etcconf/etcconf.go generated vendored Normal file
View File

@@ -0,0 +1,114 @@
// Copyright 2016 VMware, Inc. All Rights Reserved.
//
// 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 etcconf
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"path"
"strings"
log "github.com/Sirupsen/logrus"
)
type EntryConsumer interface {
ConsumeEntry(string) error
}
type EntryWalker interface {
HasNext() bool
Next() string
}
type Conf interface {
Copy(Conf) error
Load() error
Save() error
Path() string
}
func load(filePath string, con EntryConsumer) error {
f, err := os.Open(filePath)
if err != nil {
if os.IsNotExist(err) {
log.Infof("not loading file %s as it does not exist", filePath)
return nil
}
return err
}
// #nosec: Errors unhandled
defer f.Close()
s := bufio.NewScanner(f)
for s.Scan() {
t := strings.TrimSpace(s.Text())
if t == "" || strings.HasPrefix(t, "#") {
continue
}
if err = con.ConsumeEntry(t); err != nil {
return err
}
}
return nil
}
func save(filePath string, walker EntryWalker) error {
f, err := ioutil.TempFile(path.Dir(filePath), path.Base(filePath))
if err != nil {
return err
}
defer os.Remove(f.Name())
w := bufio.NewWriter(f)
for walker.HasNext() {
l := fmt.Sprintf("%s\n", walker.Next())
log.Debugf("writing %q to %s", l, filePath)
var n int
for n < len(l) {
b, err := w.WriteString(l[n:])
if err != nil {
// #nosec: Errors unhandled
_ = f.Close()
return err
}
n += b
}
}
if err = w.Flush(); err != nil {
// #nosec: Errors unhandled
_ = f.Close()
log.Errorf("Unable to flush file content to %s: %s", f.Name(), err)
return err
}
if err = f.Close(); err != nil {
log.Errorf("Error closing file %s: %s", f.Name(), err)
return err
}
if err = os.Rename(f.Name(), filePath); err != nil {
log.Errorf("Unable to rename tempory file into final location %s->%s: %s", f.Name(), filePath, err)
return err
}
return nil
}

319
vendor/github.com/vmware/vic/lib/etcconf/hosts.go generated vendored Normal file
View File

@@ -0,0 +1,319 @@
// Copyright 2016-2017 VMware, Inc. All Rights Reserved.
//
// 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 etcconf
import (
"bytes"
"fmt"
"net"
"os"
"sort"
"strings"
"sync"
log "github.com/Sirupsen/logrus"
)
type Hosts interface {
Conf
SetHost(hostname string, ip net.IP)
RemoveHost(hostname string)
RemoveAll()
HostIP(hostname string) []net.IP
}
type hostEntry struct {
IP net.IP
Hostnames []string
newAddr bool
}
func (e *hostEntry) String() string {
return fmt.Sprintf("%s %s", e.IP, strings.Join(e.Hostnames, " "))
}
func (e *hostEntry) addNames(names ...string) string {
e.Hostnames = append(e.Hostnames, names...)
sort.Strings(e.Hostnames)
return e.IP.String() + " " + strings.Join(e.Hostnames, " ")
}
func (e *hostEntry) setAddress(ip net.IP) {
if e.IP != nil {
hostnames := strings.Join(e.Hostnames, " ")
log.Infof("Changing IP address: %s -> %s", e.IP.String(), ip.String())
log.Infof("IP change impacts the following hostnames: %s", hostnames)
if e.newAddr {
log.Warn("Address has changed more than once since last load, implying a configuration race")
}
e.newAddr = true
}
e.IP = ip
}
type hosts struct {
sync.Mutex
EntryConsumer
hostsIPv4 map[string]*hostEntry
hostsIPv6 map[string]*hostEntry
entries map[string]*hostEntry
dirty bool
path string
}
type hostsWalker struct {
entries []*hostEntry
i int
}
func (w *hostsWalker) HasNext() bool {
return w.i < len(w.entries)
}
func (w *hostsWalker) Next() string {
s := w.entries[w.i].String()
w.i++
return s
}
func NewHosts(path string) Hosts {
return newHosts(path)
}
func newHosts(path string) *hosts {
if path == "" {
path = HostsPath
}
return &hosts{
path: path,
hostsIPv4: make(map[string]*hostEntry),
hostsIPv6: make(map[string]*hostEntry),
entries: make(map[string]*hostEntry),
}
}
func (h *hosts) ConsumeEntry(t string) error {
h.Lock()
defer h.Unlock()
fs := strings.Fields(t)
if len(fs) < 2 {
log.Warnf("ignoring incomplete line %q", t)
return nil
}
ip := net.ParseIP(fs[0])
if ip == nil {
log.Warnf("ignoring line %q due to invalid ip address", t)
return nil
}
for _, hs := range fs[1:] {
h.setHost(hs, ip)
}
return nil
}
// Needs to ensure that host entries don't occur twice, and that we have stable reconsiliation if they do
func (h *hosts) Load() error {
h.Lock()
defer h.Unlock()
newHosts := newHosts(h.path)
if err := load(h.path, newHosts); err != nil {
return err
}
h.hostsIPv4 = newHosts.hostsIPv4
h.hostsIPv6 = newHosts.hostsIPv6
h.entries = newHosts.entries
h.dirty = false
return nil
}
// Seed pulls replaces the current state with that from the provided hosts
// It _does not_ perform a deep copy so performs a Save immediately.
func (h *hosts) Copy(conf Conf) error {
// straight up panic if not the appropriate type
existing := conf.(*hosts)
existing.Lock()
defer existing.Unlock()
h.hostsIPv4 = existing.hostsIPv4
h.hostsIPv6 = existing.hostsIPv6
h.entries = existing.entries
h.dirty = true
return h.Save()
}
// ensure hostname is associated with localhost
func (h *hosts) Save() error {
h.Lock()
defer h.Unlock()
if !h.dirty {
log.Debugf("skipping writing file since there are no new entries")
return nil
}
var entries []*hostEntry
for _, v := range h.entries {
entries = append(entries, v)
}
if err := save(h.path, &hostsWalker{entries: entries}); err != nil {
return err
}
// make sure the file is readable
// #nosec: Expect file permissions to be 0600 or less
if err := os.Chmod(h.path, 0644); err != nil {
return err
}
h.dirty = false
return nil
}
func (h *hosts) SetHost(hostname string, ip net.IP) {
h.Lock()
defer h.Unlock()
h.setHost(hostname, ip)
}
func (h *hosts) setHost(hostname string, ip net.IP) {
h.dirty = true
if ip == nil {
return
}
// what type of address is it?
hostmap := h.hostsIPv6
ipv4 := ip.To4()
if ipv4 != nil {
// this drops any leading garbage in the array so that byte comparisons work as
// expected
ip = ipv4
hostmap = h.hostsIPv4
}
hentry := hostmap[hostname]
ientry := h.entries[ip.String()]
if hentry != nil {
// existing entry with no changes
if bytes.Equal(hentry.IP, ip) {
h.dirty = false
return
}
// existing hostname with a new address - change address for all assocated hostnames
hentry.setAddress(ip)
return
}
// completely new entry for this ip address type
if ientry == nil {
entry := &hostEntry{
IP: ip,
Hostnames: []string{hostname},
}
h.entries[ip.String()] = entry
hostmap[hostname] = entry
return
}
// add the hostname indexed IP record
ientry.Hostnames = append(ientry.Hostnames, hostname)
hostmap[hostname] = ientry
return
}
func (h *hosts) RemoveHost(hostname string) {
h.Lock()
defer h.Unlock()
for _, hostmap := range []map[string]*hostEntry{h.hostsIPv4, h.hostsIPv6} {
entry := hostmap[hostname]
if entry == nil {
continue
}
h.dirty = true
delete(hostmap, hostname)
if len(entry.Hostnames) < 2 {
log.Infof("Removing hostname and address: %s (%s)", hostname, entry.IP.String())
delete(h.entries, entry.IP.String())
continue
}
var remaining []string
for i := range entry.Hostnames {
if entry.Hostnames[i] == hostname {
remaining = entry.Hostnames[:i]
remaining = append(remaining, entry.Hostnames[i+1:]...)
}
}
entry.Hostnames = remaining
}
}
func (h *hosts) RemoveAll() {
h.Lock()
defer h.Unlock()
h.dirty = len(h.hostsIPv4) > 0 || len(h.hostsIPv6) > 0
h.hostsIPv4 = make(map[string]*hostEntry)
h.hostsIPv6 = make(map[string]*hostEntry)
h.entries = make(map[string]*hostEntry)
}
func (h *hosts) HostIP(hostname string) []net.IP {
h.Lock()
defer h.Unlock()
var ips []net.IP
if ipv4, ok := h.hostsIPv4[hostname]; ok {
ips = append(ips, ipv4.IP)
}
if ipv6, ok := h.hostsIPv6[hostname]; ok {
ips = append(ips, ipv6.IP)
}
return ips
}
func (h *hosts) Path() string {
return h.path
}

View File

@@ -0,0 +1,17 @@
// Copyright 2016 VMware, Inc. All Rights Reserved.
//
// 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 etcconf
const HostsPath = "/etc/hosts"

View File

@@ -0,0 +1,17 @@
// Copyright 2016 VMware, Inc. All Rights Reserved.
//
// 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 etcconf
const HostsPath = "/etc/hosts"

222
vendor/github.com/vmware/vic/lib/etcconf/hosts_test.go generated vendored Normal file
View File

@@ -0,0 +1,222 @@
// Copyright 2016 VMware, Inc. All Rights Reserved.
//
// 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 etcconf
import (
"io/ioutil"
"net"
"os"
"testing"
log "github.com/Sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type entry struct {
addr net.IP
hostnames []string
}
var (
localv4 = &entry{
addr: net.ParseIP("127.0.0.1"),
hostnames: []string{"localhost", "localhost.localdomain", "localhost4"},
}
localv6 = &entry{
addr: net.ParseIP("::1"),
hostnames: []string{"localhost", "localhost.localdomain", "localhost6"},
}
independent = &entry{
addr: net.ParseIP("8.8.8.8"),
hostnames: []string{"dns.google.com"},
}
name = &entry{
addr: net.ParseIP("192.168.0.2"),
hostnames: []string{"system.host.name"},
}
namev6 = &entry{
addr: net.ParseIP("::2"),
hostnames: []string{"system.host.name"},
}
alias = &entry{
addr: net.ParseIP("192.168.0.2"),
hostnames: []string{"other.host.name"},
}
)
// initializes a hosts file from entry, adds name and alias, and saves the file
func initializeHostsFile(t *testing.T) *hosts {
log.SetLevel(log.DebugLevel)
f, err := ioutil.TempFile("", "hosts-test")
require.NoError(t, err, "Unable to create tmpfile")
// defer os.Remove(f.Name())
hosts := newHosts(f.Name())
hosts.Load()
require.Equal(t, 0, len(hosts.entries), "New hosts file contains entries")
hosts.SetHost(name.hostnames[0], name.addr)
hosts.SetHost(alias.hostnames[0], alias.addr)
require.Equal(t, 1, len(hosts.entries), "Hosts was expected to have only one entry after non-local entry and alias added")
err = hosts.Save()
require.NoError(t, err, "Failed to save hosts file")
return hosts
}
// TestAddressTypeIsolation checks that hostnames can be assigned to multiple
// addresses so long as those addresses are of different types. Types are limited
// to IPv4 and IPv6 at this time.
func TestAddressTypeIsolation(t *testing.T) {
log.SetLevel(log.DebugLevel)
f, err := ioutil.TempFile("", "hosts-test")
require.NoError(t, err, "Unable to create tmpfile")
defer os.Remove(f.Name())
hosts := newHosts(f.Name())
hosts.Load()
require.Equal(t, 0, len(hosts.entries), "New hosts file contains entries")
hosts.SetHost(localv4.hostnames[0], localv4.addr)
hosts.SetHost(localv4.hostnames[1], localv4.addr)
hosts.SetHost(localv4.hostnames[2], localv4.addr)
require.Equal(t, 1, len(hosts.entries), "Hosts was expected to have only one entry after IPv4 local aliases added")
hosts.SetHost(localv6.hostnames[0], localv6.addr)
hosts.SetHost(localv6.hostnames[1], localv6.addr)
hosts.SetHost(localv6.hostnames[2], localv6.addr)
require.Equal(t, 2, len(hosts.entries), "Hosts was expected to have two entries after IPv6 local aliases added")
localhostIPs := hosts.HostIP("localhost")
require.Equal(t, 2, len(localhostIPs), "Expected two results for localhost lookup")
ipv4 := localhostIPs[0].To4() != nil
require.Equal(t, !ipv4, localhostIPs[1].To4() != nil, "Expected one address for localhost to be IPv6")
require.True(t, localhostIPs[0].IsLoopback(), "Expected localhost IP to be loopback: %s", localhostIPs[0].String())
require.True(t, localhostIPs[1].IsLoopback(), "Expected localhost IP to be loopback: %s", localhostIPs[1].String())
err = hosts.Save()
require.NoError(t, err, "Failed to save hosts file")
// confirm preservation across load/save - overwrite previous hosts to avoid cross referencing accidents
hosts = newHosts(f.Name())
hosts.Load()
localhostIPs = hosts.HostIP("localhost")
require.Equal(t, 2, len(localhostIPs), "Expected two results for localhost lookup")
ipv4 = localhostIPs[0].To4() != nil
require.Equal(t, !ipv4, localhostIPs[1].To4() != nil, "Expected one address for localhost to be IPv6")
require.True(t, localhostIPs[0].IsLoopback(), "Expected localhost IP to be loopback: %s", localhostIPs[0].String())
require.True(t, localhostIPs[1].IsLoopback(), "Expected localhost IP to be loopback: %s", localhostIPs[1].String())
}
// TestAddressReassignment confirms that if the IP address is updated for a given
// hostname then it's updated for all of the current aliases (i.e. first field in hosts
// entry is the only thing that changes)
func TestAddressReassignment(t *testing.T) {
log.SetLevel(log.DebugLevel)
hosts := initializeHostsFile(t)
f := hosts.Path()
// confirm preservation across load/save - overwrite previous hosts to avoid cross referencing accidents
hosts = newHosts(f)
hosts.Load()
// changes the IP address for the entire entry
hosts.SetHost(name.hostnames[0], net.ParseIP("192.168.0.3"))
ips := hosts.HostIP(name.hostnames[0])
require.Equal(t, 1, len(ips), "Expected only one results for name lookup")
require.Equal(t, "192.168.0.3", ips[0].String(), "Expected update of address to take effect")
// and check the alias is updated as well
ips = hosts.HostIP(alias.hostnames[0])
require.Equal(t, 1, len(ips), "Expected only one results for name lookup")
require.Equal(t, "192.168.0.3", ips[0].String(), "Expected update of address to take effect")
}
// TestNameReassignment confirms that if a name is Removed then set again with a different
// address it does not cause updates to aliases prior to the Removal.
func TestNameReassignment(t *testing.T) {
log.SetLevel(log.DebugLevel)
hosts := initializeHostsFile(t)
f := hosts.Path()
// confirm preservation across load/save - overwrite previous hosts to avoid cross referencing accidents
hosts = newHosts(f)
hosts.Load()
// This is the primary different from TestAddressReassignment test
hosts.RemoveHost(name.hostnames[0])
// changes the IP address for the entire entry
hosts.SetHost(name.hostnames[0], net.ParseIP("192.168.0.3"))
ips := hosts.HostIP(name.hostnames[0])
require.Equal(t, 1, len(ips), "Expected only one results for name lookup")
require.Equal(t, "192.168.0.3", ips[0].String(), "Expected update of address to take effect")
// and check the alias is NOT updated as well
ips = hosts.HostIP(alias.hostnames[0])
require.Equal(t, 1, len(ips), "Expected only one results for name lookup")
require.Equal(t, "192.168.0.2", ips[0].String(), "Expected alias entry not to have been updated to take effect")
}
// TestNoChange confirms that performing an operation that should not cause a change causes
// no update
func TestNoChange(t *testing.T) {
hosts := initializeHostsFile(t)
f := hosts.Path()
finfo, err := os.Stat(f)
require.NoError(t, err, "Unable to stat hosts file")
modTime := finfo.ModTime()
// perform the no-op update
ip := hosts.HostIP(name.hostnames[0])
hosts.SetHost(name.hostnames[0], ip[0])
// save the file - this should be a a no-op
err = hosts.Save()
require.NoError(t, err, "Unable to save hosts file")
finfo, err = os.Stat(f)
require.NoError(t, err, "Unable to stat hosts file for verification")
assert.Equal(t, modTime, finfo.ModTime(), "Expected modification time to be unchanged")
}
// TestLocalToPublic checks the behaviour of reassignment for a hostname that is currently
// assigned to a local address with the new address being public.
// This is a distinct case as we should NOT be moving the standard localhost aliases.
func TestLocalToPublic(t *testing.T) {
t.Skipf("Unimplemented - current implementation behaviour does not reflect this requirement")
}
// TestPublicToLocal checks the behaviour of reassignment for a hostname that is currently
// assigned to a public address with the new address being private.
// This is a distinct case as we should NOT be moving any aliases
func TestPublicToLocal(t *testing.T) {
t.Skipf("Unimplemented - current implementation behaviour does not reflect this requirement")
}

View File

@@ -0,0 +1,17 @@
// Copyright 2016 VMware, Inc. All Rights Reserved.
//
// 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 etcconf
const HostsPath = "c:\\Windows\\System32\\Drivers\\etc\\hosts"

281
vendor/github.com/vmware/vic/lib/etcconf/resolvconf.go generated vendored Normal file
View File

@@ -0,0 +1,281 @@
// Copyright 2016-2017 VMware, Inc. All Rights Reserved.
//
// 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 etcconf
import (
"fmt"
"net"
"os"
"strconv"
"strings"
"sync"
"time"
log "github.com/Sirupsen/logrus"
)
const (
ResolvConfPath = "/etc/resolv.conf"
DefaultAttempts uint = 5
DefaultTimeout = 15 * time.Second
)
type ResolvConf interface {
Conf
AddNameservers(...net.IP)
RemoveNameservers(...net.IP)
Nameservers() []net.IP
Attempts() uint
Timeout() time.Duration
SetAttempts(uint)
SetTimeout(time.Duration)
}
type resolvConf struct {
sync.Mutex
EntryConsumer
dirty bool
path string
nameservers []net.IP
timeout time.Duration
attempts uint
}
type resolvConfWalker struct {
lines []string
i int
}
func (w *resolvConfWalker) HasNext() bool {
return w.i < len(w.lines)
}
func (w *resolvConfWalker) Next() string {
s := w.lines[w.i]
w.i++
return s
}
func NewResolvConf(path string) ResolvConf {
if path == "" {
path = ResolvConfPath
}
return &resolvConf{
path: path,
timeout: DefaultTimeout,
attempts: DefaultAttempts,
}
}
func (r *resolvConf) ConsumeEntry(t string) error {
r.Lock()
defer r.Unlock()
fs := strings.Fields(t)
if len(fs) < 2 {
log.Warnf("skipping invalid line %q", t)
return nil
}
switch fs[0] {
case "nameserver":
ip := net.ParseIP(fs[1])
if ip == nil {
log.Warnf("skipping invalid line %q: invalid ip address", t)
return nil
}
r.addNameservers(ip)
case "options":
parts := strings.Split(fs[1], ":")
if len(parts) > 2 {
log.Warnf("skipping invalid line %q", t)
return nil
}
var v uint
switch parts[0] {
case "timeout":
fallthrough
case "attempts":
if len(parts) < 2 {
log.Warnf("skipping invalid line %q", t)
return nil
}
o, err := strconv.ParseUint(parts[1], 10, strconv.IntSize)
if err != nil {
log.Warnf("skipping invalid line %q: %s", t, err)
return nil
}
v = uint(o)
}
switch parts[0] {
case "timeout":
r.timeout = time.Duration(v) * time.Second
case "attempts":
r.attempts = v
}
}
return nil
}
func (r *resolvConf) Copy(conf Conf) error {
existing := conf.(*resolvConf)
existing.Lock()
defer existing.Unlock()
r.nameservers = existing.nameservers
r.dirty = true
return r.Save()
}
func (r *resolvConf) Load() error {
r.Lock()
defer r.Unlock()
rc := &resolvConf{}
if err := load(r.path, rc); err != nil {
return err
}
r.nameservers = rc.nameservers
return nil
}
func (r *resolvConf) Save() error {
r.Lock()
defer r.Unlock()
log.Debugf("%+v", r)
if !r.dirty {
return nil
}
walker := &resolvConfWalker{lines: r.lines()}
log.Debugf("%+v", walker)
if err := save(r.path, walker); err != nil {
return err
}
// make sure the file is readable
// #nosec: Expect file permissions to be 0600 or less
if err := os.Chmod(r.path, 0644); err != nil {
return err
}
r.dirty = false
return nil
}
func (r *resolvConf) AddNameservers(nss ...net.IP) {
r.Lock()
defer r.Unlock()
r.addNameservers(nss...)
}
func (r *resolvConf) addNameservers(nss ...net.IP) {
for _, n := range nss {
if n == nil {
continue
}
found := false
for _, rn := range r.nameservers {
if rn.Equal(n) {
found = true
break
}
}
if !found {
r.nameservers = append(r.nameservers, n)
r.dirty = true
}
}
}
func (r *resolvConf) RemoveNameservers(nss ...net.IP) {
r.Lock()
defer r.Unlock()
for _, n := range nss {
if n == nil {
continue
}
for i, rn := range r.nameservers {
if n.Equal(rn) {
r.nameservers = append(r.nameservers[:i], r.nameservers[i+1:]...)
r.dirty = true
break
}
}
}
}
func (r *resolvConf) Nameservers() []net.IP {
r.Lock()
defer r.Unlock()
return r.nameservers
}
func (r *resolvConf) Timeout() time.Duration {
return r.timeout
}
func (r *resolvConf) Attempts() uint {
return r.attempts
}
func (r *resolvConf) SetTimeout(t time.Duration) {
r.timeout = t
r.dirty = true
}
func (r *resolvConf) SetAttempts(attempts uint) {
if attempts > 0 {
r.attempts = attempts
r.dirty = true
}
}
func (r *resolvConf) Path() string {
return r.path
}
func (r *resolvConf) lines() []string {
var l []string
for _, n := range r.nameservers {
l = append(l, fmt.Sprintf("nameserver %s", n))
}
l = append(l, []string{
fmt.Sprintf("options timeout:%d", r.timeout/time.Second),
fmt.Sprintf("options attempts:%d", r.attempts),
}...)
return l
}

View File

@@ -0,0 +1,56 @@
// Copyright 2016 VMware, Inc. All Rights Reserved.
//
// 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 etcconf
import (
"net"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestConsumeEntry(t *testing.T) {
r := NewResolvConf("")
assert.Equal(t, r.Timeout(), DefaultTimeout)
c := r.(EntryConsumer)
var tests = []struct {
in string
nameservers []net.IP
timeout time.Duration
attempts uint
}{
{"options", nil, DefaultTimeout, DefaultAttempts},
{"nameserver", nil, DefaultTimeout, DefaultAttempts},
{"options timeout", nil, DefaultTimeout, DefaultAttempts},
{"options attempts", nil, DefaultTimeout, DefaultAttempts},
{"options timeout:", nil, DefaultTimeout, DefaultAttempts},
{"options attempts:", nil, DefaultTimeout, DefaultAttempts},
{"options foo:1", nil, DefaultTimeout, DefaultAttempts},
{"options timeout:10", nil, 10 * time.Second, DefaultAttempts},
{"options attempts:5", nil, 10 * time.Second, 5},
{"nameserver 10.10.10", nil, 10 * time.Second, 5},
{"nameserver 10.10.10.10", []net.IP{net.ParseIP("10.10.10.10")}, 10 * time.Second, 5},
}
for _, te := range tests {
c.ConsumeEntry(te.in)
assert.EqualValues(t, te.nameservers, r.Nameservers())
assert.Equal(t, te.timeout, r.Timeout())
assert.Equal(t, te.attempts, r.Attempts())
}
}