etchosts

package
v0.59.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2024 License: Apache-2.0 Imports: 14 Imported by: 8

Documentation

Index

Constants

View Source
const (
	HostContainersInternal = "host.containers.internal"
	HostGateway            = "host-gateway"
)

Variables

This section is empty.

Functions

func Add

func Add(file string, entries HostEntries) error

Add adds the given entries to the hosts file, entries are only added if they are not already present. Add is not atomic because it will keep the current file inode. This is required to keep bind mounts for containers working.

func AddIfExists

func AddIfExists(file string, existsEntries, newEntries HostEntries) error

AddIfExists will add the given entries only if one of the existsEntries is in the hosts file. This API is required for podman network connect. Since we want to add the same host name for each network ip we want to add duplicates and the normal Add() call prevents us from doing so. However since we also do not want to overwrite potential entries that were added by users manually we first have to check if there are the current expected entries in the file. Note that this will only check for one match not all. It will also only check that the ip and one of the hostnames match like Remove().

func GetBaseHostFile

func GetBaseHostFile(confValue, imageRoot string) (string, error)

GetBaseHostFile return the hosts file which should be used as base. The first param should be the config value config.Containers.BaseHostsFile The second param should be the root path to the mounted image. This is required when the user conf value is set to "image".

func GetHostContainersInternalIP

func GetHostContainersInternalIP(conf *config.Config, netStatus map[string]types.StatusBlock, networkInterface types.ContainerNetwork) string

GetHostContainersInternalIP returns the host.containers.internal ip if netStatus is not nil then networkInterface also must be non nil otherwise this function panics

func GetHostContainersInternalIPExcluding added in v0.58.1

func GetHostContainersInternalIPExcluding(conf *config.Config, netStatus map[string]types.StatusBlock, networkInterface types.ContainerNetwork, exclude []net.IP) string

GetHostContainersInternalIPExcluding returns the host.containers.internal ip Exclude are ips that should not be returned, this is useful to prevent returning the same ip as in the container. if netStatus is not nil then networkInterface also must be non nil otherwise this function panics

func New

func New(params *Params) error

New will create a new hosts file and write this to the target file. This function does not prevent any kind of concurrency problems, it is the callers responsibility to avoid concurrent writes to this file. The extraHosts are written first, then the hosts from the file baseFile and the containerIps. The container ip entry is only added when the name was not already added before.

func Remove

func Remove(file string, entries HostEntries) error

Remove will remove the given entries from the file. An entry will be removed when the ip and at least one name matches. Not all names have to match. If the given entries are not present in the file no error is returned. Remove is not atomic because it will keep the current file inode. This is required to keep bind mounts for containers working.

Types

type HostEntries

type HostEntries []HostEntry

func GetNetworkHostEntries

func GetNetworkHostEntries(netStatus map[string]types.StatusBlock, names ...string) HostEntries

GetNetworkHostEntries returns HostEntries for all ips in the network status with the given hostnames.

type HostEntry

type HostEntry struct {
	IP    string
	Names []string
}

type Params

type Params struct {
	// BaseFile is the file where we read entries from and add entries to
	// the target hosts file. If the name is empty it will not read any entries.
	BaseFile string
	// ExtraHosts is a slice of entries in the "hostname:ip" format.
	// Optional.
	ExtraHosts []string
	// ContainerIPs should contain the main container ipv4 and ipv6 if available
	// with the container name and host name as names set.
	// Optional.
	ContainerIPs HostEntries
	// HostContainersInternalIP is the IP for the host.containers.internal entry.
	// Optional.
	HostContainersInternalIP string
	// TargetFile where the hosts are written to.
	TargetFile string
}

Params for the New() function call

Jump to

Keyboard shortcuts

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