dns

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 11 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package dns is the dns behavior: the DNS resolvers of each interface of the host.

Index

Constants

This section is empty.

Variables

View Source
var Behavior = behavior.Register(behavior.Define(
	"dns", "The DNS resolvers of each interface of the host",
	behavior.Title("DNS"),
	behavior.Node,
	behavior.API,
	behavior.Area(area.Networking),
	behavior.CLI,
	behavior.Platform(behavior.PlatformDebian, newDebian),
	behavior.Platform(behavior.PlatformLinux, newReader),
	behavior.Platform(behavior.PlatformDarwin, newReader),
	behavior.Requires(netplan.Behavior),
	behavior.RunsOnPlatform(behavior.PlatformLinux, "ip"),

	behavior.RunsIfPresent("resolvectl"),
	behavior.RunsIfPresent("ethtool"),
	behavior.RunsOnPlatform(behavior.PlatformDarwin, "scutil"),
	behavior.Writes(netplan.Dir+"/osapi-dns-*.yaml"),
	behavior.Read("get", Get).Key("interface_name").
		Describe("Get the DNS resolvers of an interface").
		Explain("Reads the servers and the search domains of the interface: resolvectl "+
			"on Linux, with resolv.conf on a host that has no resolvers per interface, "+
			"such as a container, and scutil on macOS.").
		Example("An interface with servers and a domain", GetInput{InterfaceName: "osapi0"},
			Entry{Servers: []string{"1.1.1.1", "8.8.8.8"}, SearchDomains: []string{"example.com"}},
			append(
				[]behavior.HostOption{
					behavior.OnPlatform(behavior.PlatformDebian, behavior.PlatformLinux),
					behavior.Live("interface_name", behavior.FactInterface),
				},
				linuxHost...)...,
		).
		Example("A container with no resolvers per interface", GetInput{InterfaceName: "osapi0"},
			Entry{Servers: []string{"10.0.0.2"}, SearchDomains: []string{"svc.local"}},
			behavior.OnPlatform(behavior.PlatformDebian, behavior.PlatformLinux),
			behavior.Exists("/etc/resolv.conf", "nameserver 10.0.0.2\nsearch svc.local\n"),
			behavior.Live("interface_name", behavior.FactInterface),
		).
		Example("A Mac", GetInput{InterfaceName: "en0"},
			Entry{Servers: []string{"1.1.1.1"}, SearchDomains: []string{}},
			behavior.OnPlatform(behavior.PlatformDarwin),
			behavior.Prints(
				"scutil --dns",
				"DNS configuration\n\nresolver #1\n  nameserver[0] : 1.1.1.1\n  if_index : 14 (en0)\n",
			),
		).
		Fails("An interface the host does not have", GetInput{InterfaceName: "eth9"}, "not found",
			append([]behavior.HostOption{
				behavior.Prints(
					"scutil --dns",
					"resolver #1\n  nameserver[0] : 1.1.1.1\n  if_index : 14 (en0)\n",
				),
			}, linuxHost...)...).
		Fails("An interface name that is not alphanumeric", GetInput{InterfaceName: "osapi0;reboot"},
			"InterfaceName"),
	behavior.Write("update", Update).Key("interface_name").
		On(behavior.PlatformDebian).
		Describe("Set the DNS resolvers of an interface").
		Explain("Writes a netplan file of the interface's own, "+
			"such as /etc/netplan/osapi-dns-eth0.yaml for eth0, under the netplan definition that "+
			"matches the interface, and applies it. A definition that matches several "+
			"interfaces, such as cloud-init's all for en*, gives each of them the resolvers. An empty list keeps "+
			"the interface's servers or domains, and an interface with no resolvers yet starts from none. override_dhcp drops the servers DHCP "+
			"gives. It runs on the Debian family, and elsewhere, and in a container, "+
			"reports skipped.").
		Example("Set the servers of an interface", UpdateInput{InterfaceName: "osapi0", Servers: []string{"9.9.9.9"}},
			Result{InterfaceName: "osapi0", Changed: true},
			append(
				append([]behavior.HostOption{
					behavior.Prints("netplan status --format json", `{"osapi0":{"type":"ethernet","id":"osapi0"}}`),
					behavior.Wrote(osapi0File, osapi0YAML),
					behavior.Ran("netplan", "generate"),
					behavior.Ran("netplan", "apply"),
				}, linuxHost...),
				behavior.Reads(GetInput{InterfaceName: "osapi0"}, Entry{Servers: []string{"9.9.9.9"}}),
			)...,
		).
		Example("Set the search domains, and keep the servers",
			UpdateInput{
				InterfaceName: "osapi0",
				SearchDomains: []string{"corp.example"},
				OverrideDHCP:  true,
			},
			Result{InterfaceName: "osapi0", Changed: true},
			append(
				append([]behavior.HostOption{
					behavior.Prints(
						"netplan status --format json",
						`{"osapi0":{"type":"ethernet","id":"osapi0"}}`,
					),
					behavior.Wrote(
						osapi0File,
						"network:\n  version: 2\n  ethernets:\n    osapi0:\n      nameservers:\n"+
							"        addresses:\n          - 1.1.1.1\n          - 8.8.8.8\n        search:\n          - corp.example\n"+
							"      dhcp4-overrides:\n        use-dns: false\n      dhcp6-overrides:\n        use-dns: false\n",
					),
					behavior.Ran("netplan", "generate"),
					behavior.Ran("netplan", "apply"),
				}, linuxHost...),
				behavior.Reads(GetInput{InterfaceName: "osapi0"}, Entry{SearchDomains: []string{"corp.example"}}),
			)...,
		).
		Example("An interface with no resolvers yet",
			UpdateInput{InterfaceName: "osapi0", Servers: []string{"9.9.9.9"}},
			Result{InterfaceName: "osapi0", Changed: true},
			behavior.Exists("/sys/class/net/osapi0/ifindex", "2"),
			behavior.Prints("resolvectl dns", "Global:\nLink 3 (eth1): 1.1.1.1\n"),
			behavior.Prints("resolvectl domain", "Global:\n"),
			behavior.Prints("netplan status --format json", `{"osapi0":{"type":"ethernet","id":"osapi0"}}`),
			behavior.Wrote(osapi0File, "network:\n  version: 2\n  ethernets:\n    osapi0:\n      nameservers:\n"+
				"        addresses:\n          - 9.9.9.9\n"),
			behavior.Ran("netplan", "generate"),
			behavior.Ran("netplan", "apply"),
			behavior.Reads(GetInput{InterfaceName: "osapi0"}, Entry{Servers: []string{"9.9.9.9"}}),
		).
		Fails("No servers and no search domains", UpdateInput{InterfaceName: "osapi0"},
			"give servers or search domains").
		Fails("An interface the host does not have, to keep the servers of",
			UpdateInput{
				InterfaceName: "eth9",
				SearchDomains: []string{"corp.example"},
			}, "not found",
			append([]behavior.HostOption{
				behavior.Prints(
					"netplan status --format json",
					`{"osapi0":{"type":"ethernet","id":"osapi0"}}`,
				),
			}, linuxHost...)...).
		Fails("A server that is not an IP address",
			UpdateInput{InterfaceName: "osapi0", Servers: []string{"one.one"}}, "ip").
		Fails("A search domain that is not a host name",
			UpdateInput{
				InterfaceName: "osapi0",
				SearchDomains: []string{"bad domain"},
			}, "hostname").
		Fails("More than 16 servers",
			UpdateInput{
				InterfaceName: "osapi0",
				Servers:       strings.Split(strings.Repeat("1.1.1.1 ", 17), " ")[:17],
			},
			"Servers").
		Fails("More than 16 search domains",
			UpdateInput{
				InterfaceName: "osapi0",
				SearchDomains: strings.Split(strings.Repeat("a.b ", 17), " ")[:17],
			},
			"SearchDomains").
		Fails("An interface name that is not alphanumeric",
			UpdateInput{
				InterfaceName: "osapi0;reboot",
				Servers:       []string{"9.9.9.9"},
			}, "InterfaceName"),
	behavior.Write("delete", Delete).Key("interface_name").
		On(behavior.PlatformDebian).
		Describe("Drop the DNS resolvers osapi set on an interface").
		Explain("Removes the interface's netplan file and applies what is left. An "+
			"interface osapi set nothing on does not change.").
		Example("An interface osapi set", DeleteInput{InterfaceName: "osapi0"},
			Result{InterfaceName: "osapi0", Changed: true},
			append(
				slices.Clone(linuxHost),
				behavior.After(UpdateInput{InterfaceName: "osapi0", Servers: []string{"9.9.9.9"}}),
				behavior.Prints("netplan status --format json", `{"osapi0":{"type":"ethernet","id":"osapi0"}}`),
				behavior.Removed(osapi0File),
				behavior.Ran("netplan", "apply"),
				behavior.ReadsNone(GetInput{InterfaceName: "osapi0"}, Entry{Servers: []string{"9.9.9.9"}}, "servers"),
			)...,
		).
		Example("An interface osapi set nothing on", DeleteInput{InterfaceName: "osapi0"},
			Result{InterfaceName: "osapi0"},
			behavior.ReadsNone(GetInput{InterfaceName: "osapi0"}, Entry{Servers: []string{"9.9.9.9"}}, "servers"),
		).
		Fails("An interface name that is not alphanumeric", DeleteInput{InterfaceName: "osapi0;reboot"},
			"InterfaceName"),
))

Behavior is the DNS resolvers of each interface of the host. gohai reads them on every platform; a netplan file of their own sets them on the Debian family.

Functions

This section is empty.

Types

type DeleteInput

type DeleteInput struct {
	InterfaceName string `` /* 133-byte string literal not displayed */
}

DeleteInput names the interface whose resolvers osapi set.

type Entry

type Entry struct {
	Servers       []string `json:"servers"        doc:"The DNS servers"`
	SearchDomains []string `json:"search_domains" doc:"The search domains"`
}

Entry is the resolvers of an interface.

func Get

func Get(
	ctx context.Context,
	p Provider,
	in GetInput,
) (Entry, error)

Get is the resolvers of an interface.

type GetInput

type GetInput struct {
	InterfaceName string `` /* 133-byte string literal not displayed */
}

GetInput names the interface whose resolvers to read.

type Provider

type Provider interface {
	// Resolvers is the interface's servers and search domains.
	Resolvers(ctx context.Context, name string) (Entry, error)
	// Set writes the interface's resolvers, and reports whether it changed.
	Set(ctx context.Context, in UpdateInput) (bool, error)
	// Remove drops the resolvers osapi set on the interface.
	Remove(ctx context.Context, name string) (bool, error)
}

Provider reads and sets the DNS resolvers of an interface.

type Result

type Result struct {
	InterfaceName string `json:"interface_name" doc:"The interface"`
	Changed       bool   `json:"changed"        doc:"Whether the host changed"`
}

Result is an interface after an update or a removal.

func Delete

func Delete(
	ctx context.Context,
	p Provider,
	in DeleteInput,
) (Result, error)

Delete drops the resolvers osapi set on an interface.

func Update

func Update(
	ctx context.Context,
	p Provider,
	in UpdateInput,
) (Result, error)

Update sets the resolvers of an interface.

type UpdateInput

type UpdateInput struct {
	InterfaceName string   `` /* 150-byte string literal not displayed */
	Servers       []string `` /* 141-byte string literal not displayed */
	SearchDomains []string `` /* 130-byte string literal not displayed */
	OverrideDHCP  bool     `` /* 128-byte string literal not displayed */
}

UpdateInput is the resolvers to set on an interface.

Directories

Path Synopsis
Package mocks holds the generated double of the dns provider, for the methods' tests.
Package mocks holds the generated double of the dns provider, for the methods' tests.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL