f-86: agent: dummy, DNAT et open par VPC, DHCP 6 et 42, watchdog #86
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
aa1cc58ae3
commit
27dda545cf
16 changed files with 565 additions and 13 deletions
|
|
@ -248,8 +248,8 @@ func TestProbe_DescribesWhatWouldBeSent(t *testing.T) {
|
|||
if lease.Router != "10.0.5.254" {
|
||||
t.Errorf("router = %s, want 10.0.5.254", lease.Router)
|
||||
}
|
||||
if len(lease.DNS) != 2 {
|
||||
t.Errorf("dns = %v, want two servers", lease.DNS)
|
||||
if len(lease.DNS) != 1 || lease.DNS[0] != "10.0.5.1" {
|
||||
t.Errorf("dns = %v, want only the interface ip 10.0.5.1", lease.DNS)
|
||||
}
|
||||
if len(lease.Routes) != 3 {
|
||||
t.Errorf("routes = %v, want metadata, vpc and default", lease.Routes)
|
||||
|
|
|
|||
|
|
@ -69,6 +69,24 @@ func TestGenerateConfig_ContainsDefaultGateway(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestGenerateConfig_DNSAndNTPAreTheInterfaceIP(t *testing.T) {
|
||||
conf := newConf(t, "192.168.1.0/24")
|
||||
path, err := GenerateConfig(conf)
|
||||
if err != nil {
|
||||
t.Fatalf("GenerateConfig : %v", err)
|
||||
}
|
||||
content, _ := os.ReadFile(path)
|
||||
|
||||
for _, want := range []string{"\ndhcp-option=6,192.168.1.254\n", "\ndhcp-option=42,192.168.1.254\n"} {
|
||||
if !strings.Contains(string(content), want) {
|
||||
t.Errorf("%q absente : DNS et NTP passent par la gateway, relayés par two-hostsvc\n%s", strings.TrimSpace(want), content)
|
||||
}
|
||||
}
|
||||
if strings.Contains(string(content), "1.1.1.1") || strings.Contains(string(content), "8.8.8.8") {
|
||||
t.Errorf("résolveurs publics encore annoncés :\n%s", content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerateConfig_NoDefaultGateway(t *testing.T) {
|
||||
conf := newConf(t, "192.168.1.0/29")
|
||||
conf.DefaultGateway = nil
|
||||
|
|
|
|||
|
|
@ -30,7 +30,8 @@ func GenerateConfig(c Config) (string, error) {
|
|||
} else {
|
||||
fmt.Fprintf(&sb, "dhcp-option=3\n")
|
||||
}
|
||||
fmt.Fprintf(&sb, "dhcp-option=6,1.1.1.1,8.8.8.8\n")
|
||||
fmt.Fprintf(&sb, "dhcp-option=6,%s\n", c.InterfaceIP.String())
|
||||
fmt.Fprintf(&sb, "dhcp-option=42,%s\n", c.InterfaceIP.String())
|
||||
fmt.Fprintf(&sb, "dhcp-option=26,%d\n", c.interfaceMTU())
|
||||
fmt.Fprintf(&sb, "dhcp-hostsdir=%s\n", HostsDir(c.ConfDir, c.Name))
|
||||
fmt.Fprintf(&sb, "dhcp-optsdir=%s\n", OptsDir(c.ConfDir, c.Name))
|
||||
|
|
|
|||
|
|
@ -7,13 +7,6 @@ import (
|
|||
|
||||
const LeaseTime = 12 * time.Hour
|
||||
|
||||
func DNSServers() []net.IP {
|
||||
return []net.IP{
|
||||
net.IPv4(1, 1, 1, 1),
|
||||
net.IPv4(8, 8, 8, 8),
|
||||
}
|
||||
}
|
||||
|
||||
type SubnetConfig struct {
|
||||
Network *net.IPNet
|
||||
InterfaceIP net.IP
|
||||
|
|
|
|||
|
|
@ -86,7 +86,8 @@ func BuildReply(c SubnetConfig, h Host, req *dhcpv4.DHCPv4) (*dhcpv4.DHCPv4, err
|
|||
dhcpv4.WithNetmask(c.Network.Mask),
|
||||
dhcpv4.WithLeaseTime(uint32(LeaseTime.Seconds())),
|
||||
dhcpv4.WithOption(dhcpv4.OptServerIdentifier(c.InterfaceIP)),
|
||||
dhcpv4.WithOption(dhcpv4.OptDNS(DNSServers()...)),
|
||||
dhcpv4.WithOption(dhcpv4.OptDNS(c.InterfaceIP)),
|
||||
dhcpv4.WithOption(dhcpv4.OptNTPServers(c.InterfaceIP)),
|
||||
dhcpv4.WithOption(dhcpv4.OptClasslessStaticRoute(routes...)),
|
||||
dhcpv4.WithOption(dhcpv4.OptGeneric(dhcpv4.OptionInterfaceMTU, dhcpv4.Uint16(c.InterfaceMTU()).ToBytes())),
|
||||
}
|
||||
|
|
|
|||
|
|
@ -244,8 +244,11 @@ func TestBuildReply_CarriesAddressMaskLeaseAndServerIdentifier(t *testing.T) {
|
|||
if got := reply.ServerIdentifier(); !got.Equal(c.InterfaceIP) {
|
||||
t.Errorf("server identifier = %s, want %s", got, c.InterfaceIP)
|
||||
}
|
||||
if got := reply.DNS(); len(got) != 2 || !got[0].Equal(net.IPv4(1, 1, 1, 1)) || !got[1].Equal(net.IPv4(8, 8, 8, 8)) {
|
||||
t.Errorf("dns = %v, want 1.1.1.1 and 8.8.8.8", got)
|
||||
if got := reply.DNS(); len(got) != 1 || !got[0].Equal(net.ParseIP("10.0.5.1")) {
|
||||
t.Errorf("dns = %v, want only the interface ip 10.0.5.1, relayed by two-hostsvc", got)
|
||||
}
|
||||
if got := reply.NTPServers(); len(got) != 1 || !got[0].Equal(net.ParseIP("10.0.5.1")) {
|
||||
t.Errorf("ntp = %v, want only the interface ip 10.0.5.1, relayed by two-hostsvc", got)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
60
internal/iptables/hostsvc.go
Normal file
60
internal/iptables/hostsvc.go
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
package iptables
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
)
|
||||
|
||||
const HostSvcChain = "TWO-HOSTSVC"
|
||||
|
||||
var hostSvcExcluded = []string{"br-public", "l3-+"}
|
||||
|
||||
func hostSvcJump() []string {
|
||||
return []string{"PREROUTING", "-m", "addrtype", "--dst-type", "LOCAL", "-j", HostSvcChain}
|
||||
}
|
||||
|
||||
func hostSvcRules(target string) [][]string {
|
||||
var rules [][]string
|
||||
for _, iface := range hostSvcExcluded {
|
||||
rules = append(rules, []string{HostSvcChain, "-i", iface, "-j", "RETURN"})
|
||||
}
|
||||
for _, p := range []struct{ proto, port string }{{"udp", "53"}, {"tcp", "53"}, {"udp", "123"}} {
|
||||
rules = append(rules, []string{HostSvcChain,
|
||||
"-p", p.proto, "-m", p.proto, "--dport", p.port,
|
||||
"-j", "DNAT", "--to-destination", target,
|
||||
})
|
||||
}
|
||||
return rules
|
||||
}
|
||||
|
||||
func nat(args ...string) *exec.Cmd {
|
||||
return exec.Command("iptables", append([]string{"-t", "nat"}, args...)...)
|
||||
}
|
||||
|
||||
func AddHostSvcRedirect(target string) error {
|
||||
if out, err := nat("-N", HostSvcChain).CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("iptables create chain %s: %w: %s", HostSvcChain, err, out)
|
||||
}
|
||||
for _, rule := range append(hostSvcRules(target), hostSvcJump()) {
|
||||
if out, err := nat(append([]string{"-A"}, rule...)...).CombinedOutput(); err != nil {
|
||||
return fmt.Errorf("iptables hostsvc redirect %v: %w: %s", rule, err, out)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func HasHostSvcRedirect(target string) (bool, error) {
|
||||
for _, rule := range append(hostSvcRules(target), hostSvcJump()) {
|
||||
err := nat(append([]string{"-C"}, rule...)...).Run()
|
||||
var exit *exec.ExitError
|
||||
switch {
|
||||
case err == nil:
|
||||
case errors.As(err, &exit) && exit.ExitCode() == 1:
|
||||
return false, nil
|
||||
default:
|
||||
return false, fmt.Errorf("iptables check %v: %w", rule, err)
|
||||
}
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
48
internal/iptables/hostsvc_test.go
Normal file
48
internal/iptables/hostsvc_test.go
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
package iptables
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestHostSvcJump_OnlyLocalDestinations(t *testing.T) {
|
||||
want := []string{"PREROUTING", "-m", "addrtype", "--dst-type", "LOCAL", "-j", "TWO-HOSTSVC"}
|
||||
if got := hostSvcJump(); !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("jump = %v, want %v: traffic to an outside resolver must not be hijacked", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHostSvcRules_ExclusionsComeFirst(t *testing.T) {
|
||||
rules := hostSvcRules("169.254.169.253")
|
||||
if len(rules) != 5 {
|
||||
t.Fatalf("%d rules, want 2 exclusions and 3 redirections: %v", len(rules), rules)
|
||||
}
|
||||
for i, iface := range []string{"br-public", "l3-+"} {
|
||||
want := []string{"TWO-HOSTSVC", "-i", iface, "-j", "RETURN"}
|
||||
if !reflect.DeepEqual(rules[i], want) {
|
||||
t.Errorf("rule %d = %v, want %v: the public segment and the vrf link must never reach the relay", i, rules[i], want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestHostSvcRules_RedirectDNSAndNTPOnly(t *testing.T) {
|
||||
var got []string
|
||||
for _, rule := range hostSvcRules("169.254.169.253") {
|
||||
if rule[len(rule)-3] != "DNAT" {
|
||||
continue
|
||||
}
|
||||
if rule[len(rule)-1] != "169.254.169.253" {
|
||||
t.Errorf("rule %v does not target 169.254.169.253", rule)
|
||||
}
|
||||
got = append(got, strings.Join(rule[1:7], " "))
|
||||
}
|
||||
want := []string{
|
||||
"-p udp -m udp --dport 53",
|
||||
"-p tcp -m tcp --dport 53",
|
||||
"-p udp -m udp --dport 123",
|
||||
}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("redirections = %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
13
internal/netif/dummy.go
Normal file
13
internal/netif/dummy.go
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package netif
|
||||
|
||||
import (
|
||||
"github.com/vishvananda/netlink"
|
||||
)
|
||||
|
||||
func CreateDummy(name string) error {
|
||||
dummy := &netlink.Dummy{LinkAttrs: netlink.LinkAttrs{Name: name}}
|
||||
if err := netlink.LinkAdd(dummy); err != nil {
|
||||
return err
|
||||
}
|
||||
return netlink.LinkSetUp(dummy)
|
||||
}
|
||||
|
|
@ -53,6 +53,9 @@ func CreateVPC(db *badger.DB, name string, cfg *configuration.Config) error {
|
|||
if err := l3vni.Create(db, name, cfg); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := setupHostSvc(name, newSystemHostSvc()); err != nil {
|
||||
return err
|
||||
}
|
||||
return state.Set(db, "vpc/"+name, state.Running)
|
||||
}
|
||||
return nil
|
||||
|
|
|
|||
|
|
@ -17,6 +17,10 @@ func DeleteVPC(db *badger.DB, name string) error {
|
|||
} else if current == state.Deleting {
|
||||
vpcID := strings.SplitN(name, "-", 2)[1]
|
||||
|
||||
if err := teardownHostSvc(name, newSystemHostSvc()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := l3vni.Delete(db, name); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
72
internal/vpc/hostsvc.go
Normal file
72
internal/vpc/hostsvc.go
Normal file
|
|
@ -0,0 +1,72 @@
|
|||
package vpc
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
hostsvcapi "git.g3e.fr/syonad/two/internal/api/hostsvc"
|
||||
hostsvcclient "git.g3e.fr/syonad/two/internal/client/hostsvc"
|
||||
"git.g3e.fr/syonad/two/internal/hostsvc"
|
||||
"git.g3e.fr/syonad/two/internal/iptables"
|
||||
"git.g3e.fr/syonad/two/internal/netif"
|
||||
"git.g3e.fr/syonad/two/internal/netns"
|
||||
)
|
||||
|
||||
const HostSvcLink = "hostsvc"
|
||||
|
||||
type hostSvcHost interface {
|
||||
InNetns(vpc string, fn func() error) error
|
||||
CreateDummy(name string) error
|
||||
AddrAdd(iface string, ip net.IP) error
|
||||
AddRedirect(target string) error
|
||||
Open(vpc string) error
|
||||
Close(vpc string) error
|
||||
}
|
||||
|
||||
type systemHostSvc struct {
|
||||
client *hostsvcclient.Client
|
||||
}
|
||||
|
||||
func newSystemHostSvc() systemHostSvc {
|
||||
return systemHostSvc{client: hostsvcclient.New(hostsvcapi.SocketPath(hostsvcapi.DefaultRunDir))}
|
||||
}
|
||||
|
||||
func (systemHostSvc) InNetns(vpc string, fn func() error) error { return netns.Call(vpc, fn) }
|
||||
func (systemHostSvc) CreateDummy(name string) error { return netif.CreateDummy(name) }
|
||||
func (systemHostSvc) AddrAdd(iface string, ip net.IP) error { return netif.AddrAdd(iface, ip) }
|
||||
func (systemHostSvc) AddRedirect(target string) error { return iptables.AddHostSvcRedirect(target) }
|
||||
func (s systemHostSvc) Open(vpc string) error { return s.client.Open(vpc) }
|
||||
func (s systemHostSvc) Close(vpc string) error { return s.client.Close(vpc) }
|
||||
|
||||
func setupHostSvc(vpc string, h hostSvcHost) error {
|
||||
ip := net.ParseIP(hostsvc.ServiceIP)
|
||||
if err := h.InNetns(vpc, func() error {
|
||||
steps := []struct {
|
||||
name string
|
||||
fn func() error
|
||||
}{
|
||||
{"create " + HostSvcLink, func() error { return h.CreateDummy(HostSvcLink) }},
|
||||
{"address " + hostsvc.ServiceIP + " on " + HostSvcLink, func() error { return h.AddrAdd(HostSvcLink, ip) }},
|
||||
{"redirect dns and ntp to " + hostsvc.ServiceIP, func() error { return h.AddRedirect(hostsvc.ServiceIP) }},
|
||||
}
|
||||
for _, step := range steps {
|
||||
if err := step.fn(); err != nil {
|
||||
return fmt.Errorf("%s: %w", step.name, err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
return fmt.Errorf("hostsvc in %s: %w", vpc, err)
|
||||
}
|
||||
if err := h.Open(vpc); err != nil {
|
||||
return fmt.Errorf("hostsvc open %s: %w", vpc, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func teardownHostSvc(vpc string, h hostSvcHost) error {
|
||||
if err := h.Close(vpc); err != nil {
|
||||
return fmt.Errorf("hostsvc close %s: %w", vpc, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
92
internal/vpc/hostsvc_test.go
Normal file
92
internal/vpc/hostsvc_test.go
Normal file
|
|
@ -0,0 +1,92 @@
|
|||
package vpc
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type fakeHostSvc struct {
|
||||
calls []string
|
||||
inside string
|
||||
failOn string
|
||||
}
|
||||
|
||||
func (f *fakeHostSvc) record(kind, detail string) error {
|
||||
call := kind + " " + detail
|
||||
if f.inside != "" {
|
||||
call = f.inside + ": " + call
|
||||
}
|
||||
f.calls = append(f.calls, call)
|
||||
if kind == f.failOn {
|
||||
return errors.New("boom")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeHostSvc) InNetns(vpc string, fn func() error) error {
|
||||
f.inside = vpc
|
||||
defer func() { f.inside = "" }()
|
||||
return fn()
|
||||
}
|
||||
|
||||
func (f *fakeHostSvc) CreateDummy(name string) error { return f.record("dummy", name) }
|
||||
func (f *fakeHostSvc) AddrAdd(iface string, ip net.IP) error {
|
||||
return f.record("addr", ip.String()+" "+iface)
|
||||
}
|
||||
func (f *fakeHostSvc) AddRedirect(target string) error { return f.record("redirect", target) }
|
||||
func (f *fakeHostSvc) Open(vpc string) error { return f.record("open", vpc) }
|
||||
func (f *fakeHostSvc) Close(vpc string) error { return f.record("close", vpc) }
|
||||
|
||||
func TestSetupHostSvc_OrderAndPlace(t *testing.T) {
|
||||
f := &fakeHostSvc{}
|
||||
if err := setupHostSvc("vpc-00001", f); err != nil {
|
||||
t.Fatalf("setupHostSvc: %v", err)
|
||||
}
|
||||
want := []string{
|
||||
"vpc-00001: dummy hostsvc",
|
||||
"vpc-00001: addr 169.254.169.253 hostsvc",
|
||||
"vpc-00001: redirect 169.254.169.253",
|
||||
"open vpc-00001",
|
||||
}
|
||||
if !reflect.DeepEqual(f.calls, want) {
|
||||
t.Errorf("calls = %v, want %v: the address and the redirection live in the vpc netns, the open is asked from the host once they exist", f.calls, want)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSetupHostSvc_StopsAtTheFirstFailure(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
failOn string
|
||||
want string
|
||||
last string
|
||||
}{
|
||||
{"dummy", "hostsvc in vpc-00001: create hostsvc: boom", "vpc-00001: dummy hostsvc"},
|
||||
{"addr", "hostsvc in vpc-00001: address 169.254.169.253 on hostsvc: boom", "vpc-00001: addr 169.254.169.253 hostsvc"},
|
||||
{"redirect", "hostsvc in vpc-00001: redirect dns and ntp to 169.254.169.253: boom", "vpc-00001: redirect 169.254.169.253"},
|
||||
{"open", "hostsvc open vpc-00001: boom", "open vpc-00001"},
|
||||
} {
|
||||
f := &fakeHostSvc{failOn: c.failOn}
|
||||
err := setupHostSvc("vpc-00001", f)
|
||||
if err == nil || err.Error() != c.want {
|
||||
t.Errorf("fail on %s: error %v, want %q", c.failOn, err, c.want)
|
||||
}
|
||||
if got := f.calls[len(f.calls)-1]; got != c.last {
|
||||
t.Errorf("fail on %s: last call %q, want %q: nothing must run after a failure", c.failOn, got, c.last)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTeardownHostSvc(t *testing.T) {
|
||||
f := &fakeHostSvc{}
|
||||
if err := teardownHostSvc("vpc-00001", f); err != nil {
|
||||
t.Fatalf("teardownHostSvc: %v", err)
|
||||
}
|
||||
if !reflect.DeepEqual(f.calls, []string{"close vpc-00001"}) {
|
||||
t.Errorf("calls = %v: only the close is needed, the dummy and the rules go away with the netns", f.calls)
|
||||
}
|
||||
f = &fakeHostSvc{failOn: "close"}
|
||||
if err := teardownHostSvc("vpc-00001", f); err == nil || err.Error() != "hostsvc close vpc-00001: boom" {
|
||||
t.Errorf("error %v: a failed close must stop the deletion", err)
|
||||
}
|
||||
}
|
||||
110
internal/watchdog/check_hostsvc.go
Normal file
110
internal/watchdog/check_hostsvc.go
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
package watchdog
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
|
||||
hostsvcapi "git.g3e.fr/syonad/two/internal/api/hostsvc"
|
||||
hostsvcclient "git.g3e.fr/syonad/two/internal/client/hostsvc"
|
||||
"git.g3e.fr/syonad/two/internal/hostsvc"
|
||||
"git.g3e.fr/syonad/two/internal/iptables"
|
||||
"git.g3e.fr/syonad/two/internal/netif"
|
||||
"git.g3e.fr/syonad/two/internal/netns"
|
||||
"git.g3e.fr/syonad/two/internal/state"
|
||||
"git.g3e.fr/syonad/two/internal/vpc"
|
||||
"git.g3e.fr/syonad/two/internal/watchdog/notify"
|
||||
"git.g3e.fr/syonad/two/pkg/db/kv"
|
||||
|
||||
"github.com/dgraph-io/badger/v4"
|
||||
)
|
||||
|
||||
const kindHostSvc = "hostsvc"
|
||||
|
||||
type hostSvcProbe interface {
|
||||
Served() ([]string, error)
|
||||
ServiceAddress(vpc string) (bool, error)
|
||||
Redirect(vpc string) (bool, error)
|
||||
}
|
||||
|
||||
type systemHostSvcProbe struct {
|
||||
client *hostsvcclient.Client
|
||||
}
|
||||
|
||||
func newSystemHostSvcProbe() systemHostSvcProbe {
|
||||
return systemHostSvcProbe{client: hostsvcclient.New(hostsvcapi.SocketPath(hostsvcapi.DefaultRunDir))}
|
||||
}
|
||||
|
||||
func (p systemHostSvcProbe) Served() ([]string, error) { return p.client.List() }
|
||||
|
||||
func (systemHostSvcProbe) ServiceAddress(name string) (bool, error) {
|
||||
want := net.ParseIP(hostsvc.ServiceIP)
|
||||
found := false
|
||||
err := netns.Call(name, func() error {
|
||||
ips, err := netif.LinkIPv4(vpc.HostSvcLink)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, ip := range ips {
|
||||
if ip.Equal(want) {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
return found, err
|
||||
}
|
||||
|
||||
func (systemHostSvcProbe) Redirect(name string) (bool, error) {
|
||||
var ok bool
|
||||
err := netns.Call(name, func() error {
|
||||
var err error
|
||||
ok, err = iptables.HasHostSvcRedirect(hostsvc.ServiceIP)
|
||||
return err
|
||||
})
|
||||
return ok, err
|
||||
}
|
||||
|
||||
func CheckHostSvc(db *badger.DB, p hostSvcProbe, n notify.Notifier) error {
|
||||
pairs, err := kv.ListByPrefix(db, prefixVPC)
|
||||
if err != nil {
|
||||
return fmt.Errorf("watchdog: listing vpcs: %w", err)
|
||||
}
|
||||
var running []string
|
||||
for _, name := range resourceNames(pairs, prefixVPC) {
|
||||
if st, err := state.Get(db, prefixVPC+name); err == nil && st == state.Running {
|
||||
running = append(running, name)
|
||||
}
|
||||
}
|
||||
if len(running) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
served, err := p.Served()
|
||||
if err != nil {
|
||||
n.Notify(kindHostSvc, hostsvcapi.Unit, fmt.Sprintf("unreachable, no vpc of this host gets dns or ntp: %v", err))
|
||||
} else {
|
||||
open := map[string]bool{}
|
||||
for _, name := range served {
|
||||
open[name] = true
|
||||
}
|
||||
for _, name := range running {
|
||||
if !open[name] {
|
||||
n.Notify(kindVPC, name, "not served by two-hostsvc: its vms get no dns nor ntp")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for _, name := range running {
|
||||
if ok, err := p.ServiceAddress(name); err != nil {
|
||||
n.Notify(kindVPC, name, fmt.Sprintf("%s on %s unreadable: %v", hostsvc.ServiceIP, vpc.HostSvcLink, err))
|
||||
} else if !ok {
|
||||
n.Notify(kindVPC, name, fmt.Sprintf("%s missing on %s (in netns)", hostsvc.ServiceIP, vpc.HostSvcLink))
|
||||
}
|
||||
if ok, err := p.Redirect(name); err != nil {
|
||||
n.Notify(kindVPC, name, fmt.Sprintf("dns and ntp redirection unreadable: %v", err))
|
||||
} else if !ok {
|
||||
n.Notify(kindVPC, name, fmt.Sprintf("dns and ntp redirection to %s missing or incomplete (chain %s)", hostsvc.ServiceIP, iptables.HostSvcChain))
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
129
internal/watchdog/check_hostsvc_test.go
Normal file
129
internal/watchdog/check_hostsvc_test.go
Normal file
|
|
@ -0,0 +1,129 @@
|
|||
package watchdog
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"testing"
|
||||
|
||||
"git.g3e.fr/syonad/two/internal/state"
|
||||
)
|
||||
|
||||
type fakeHostSvcProbe struct {
|
||||
served []string
|
||||
servedErr error
|
||||
address map[string]bool
|
||||
redirect map[string]bool
|
||||
readErr error
|
||||
asked int
|
||||
}
|
||||
|
||||
func (f *fakeHostSvcProbe) Served() ([]string, error) {
|
||||
f.asked++
|
||||
return f.served, f.servedErr
|
||||
}
|
||||
|
||||
func (f *fakeHostSvcProbe) ServiceAddress(vpc string) (bool, error) {
|
||||
return f.address[vpc], f.readErr
|
||||
}
|
||||
|
||||
func (f *fakeHostSvcProbe) Redirect(vpc string) (bool, error) {
|
||||
return f.redirect[vpc], f.readErr
|
||||
}
|
||||
|
||||
func healthyHostSvc(vpcs ...string) *fakeHostSvcProbe {
|
||||
f := &fakeHostSvcProbe{served: vpcs, address: map[string]bool{}, redirect: map[string]bool{}}
|
||||
for _, v := range vpcs {
|
||||
f.address[v] = true
|
||||
f.redirect[v] = true
|
||||
}
|
||||
return f
|
||||
}
|
||||
|
||||
func TestCheckHostSvc_SilentWhenEverythingIsInPlace(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
seedResource(t, db, prefixVPC, "vp-a", state.Running)
|
||||
r := &recorder{}
|
||||
if err := CheckHostSvc(db, healthyHostSvc("vp-a"), r); err != nil {
|
||||
t.Fatalf("CheckHostSvc: %v", err)
|
||||
}
|
||||
if len(r.calls) != 0 {
|
||||
t.Errorf("notifications = %+v, want none", r.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckHostSvc_NoRunningVPCDoesNotAskTheRelay(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
seedResource(t, db, prefixVPC, "vp-a", state.Creating)
|
||||
p := &fakeHostSvcProbe{servedErr: errors.New("down")}
|
||||
r := &recorder{}
|
||||
if err := CheckHostSvc(db, p, r); err != nil {
|
||||
t.Fatalf("CheckHostSvc: %v", err)
|
||||
}
|
||||
if p.asked != 0 || len(r.calls) != 0 {
|
||||
t.Errorf("asked %d times, notifications %+v: a host without running vpc needs no relay", p.asked, r.calls)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckHostSvc_ReportsAnUnreachableRelay(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
seedResource(t, db, prefixVPC, "vp-a", state.Running)
|
||||
p := healthyHostSvc("vp-a")
|
||||
p.servedErr = errors.New("dial /run/two/hostsvc/hostsvc.sock: connection refused")
|
||||
r := &recorder{}
|
||||
if err := CheckHostSvc(db, p, r); err != nil {
|
||||
t.Fatalf("CheckHostSvc: %v", err)
|
||||
}
|
||||
got := r.forName("two-hostsvc.service")
|
||||
if len(got) != 1 || got[0].kind != "hostsvc" {
|
||||
t.Fatalf("notifications = %+v, want one for two-hostsvc.service", r.calls)
|
||||
}
|
||||
if !r.hasProblemContaining("unreachable, no vpc of this host gets dns or ntp: dial /run/two/hostsvc/hostsvc.sock: connection refused") {
|
||||
t.Errorf("problem = %q", got[0].problem)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckHostSvc_ReportsEachMissingPiece(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
for _, v := range []string{"vp-ok", "vp-unserved", "vp-noaddr", "vp-noredirect"} {
|
||||
seedResource(t, db, prefixVPC, v, state.Running)
|
||||
}
|
||||
p := healthyHostSvc("vp-ok", "vp-unserved", "vp-noaddr", "vp-noredirect")
|
||||
p.served = []string{"vp-ok", "vp-noaddr", "vp-noredirect"}
|
||||
p.address["vp-noaddr"] = false
|
||||
p.redirect["vp-noredirect"] = false
|
||||
r := &recorder{}
|
||||
if err := CheckHostSvc(db, p, r); err != nil {
|
||||
t.Fatalf("CheckHostSvc: %v", err)
|
||||
}
|
||||
for name, want := range map[string]string{
|
||||
"vp-unserved": "not served by two-hostsvc: its vms get no dns nor ntp",
|
||||
"vp-noaddr": "169.254.169.253 missing on hostsvc (in netns)",
|
||||
"vp-noredirect": "dns and ntp redirection to 169.254.169.253 missing or incomplete (chain TWO-HOSTSVC)",
|
||||
} {
|
||||
got := r.forName(name)
|
||||
if len(got) != 1 || got[0].kind != "vpc" || got[0].problem != want {
|
||||
t.Errorf("%s: notifications %+v, want one vpc problem %q", name, got, want)
|
||||
}
|
||||
}
|
||||
if got := r.forName("vp-ok"); len(got) != 0 {
|
||||
t.Errorf("vp-ok: notifications %+v, want none", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckHostSvc_ReportsUnreadableChecks(t *testing.T) {
|
||||
db := newTestDB(t)
|
||||
seedResource(t, db, prefixVPC, "vp-a", state.Running)
|
||||
p := healthyHostSvc("vp-a")
|
||||
p.readErr = errors.New("cannot enter netns")
|
||||
r := &recorder{}
|
||||
if err := CheckHostSvc(db, p, r); err != nil {
|
||||
t.Fatalf("CheckHostSvc: %v", err)
|
||||
}
|
||||
for _, want := range []string{
|
||||
"169.254.169.253 on hostsvc unreadable: cannot enter netns",
|
||||
"dns and ntp redirection unreadable: cannot enter netns",
|
||||
} {
|
||||
if !r.hasProblemContaining(want) {
|
||||
t.Errorf("missing %q in %+v", want, r.calls)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -23,6 +23,7 @@ type Watchdog struct {
|
|||
interval time.Duration
|
||||
dbusDown bool
|
||||
l3 l3Probe
|
||||
hostsvc hostSvcProbe
|
||||
}
|
||||
|
||||
func New(db *badger.DB, cfg *configuration.Config, n notify.Notifier, logger *slog.Logger, interval time.Duration) *Watchdog {
|
||||
|
|
@ -41,6 +42,7 @@ func New(db *badger.DB, cfg *configuration.Config, n notify.Notifier, logger *sl
|
|||
logger: logger,
|
||||
interval: interval,
|
||||
l3: systemProbe{frr: frr.NewClient()},
|
||||
hostsvc: newSystemHostSvcProbe(),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -68,6 +70,9 @@ func (w *Watchdog) tick() {
|
|||
if err := CheckVPCs(w.db, w.notifier); err != nil {
|
||||
w.logger.Error("watchdog: vpc check failed", "err", err)
|
||||
}
|
||||
if err := CheckHostSvc(w.db, w.hostsvc, w.notifier); err != nil {
|
||||
w.logger.Error("watchdog: hostsvc check failed", "err", err)
|
||||
}
|
||||
if err := CheckSubnets(w.db, w.cfg, u, w.notifier); err != nil {
|
||||
w.logger.Error("watchdog: subnet check failed", "err", err)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue