Skip to content

Commit

Permalink
enable host name and default resolv.conf and hosts file in none network
Browse files Browse the repository at this point in the history
Signed-off-by: Shubharanshu Mahapatra <[email protected]>
  • Loading branch information
Shubhranshu153 committed Oct 26, 2024
1 parent b17673d commit 29760dc
Showing 1 changed file with 60 additions and 2 deletions.
62 changes: 60 additions & 2 deletions pkg/containerutil/container_network_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,11 @@ type noneNetworkManager struct {
client *containerd.Client
}

func WriteDefaultResolvConf(filePath string) error {
content := []byte("# resolv.conf for none network mode\n nameserver 127.0.0.1\n")
return os.WriteFile(filePath, content, 0644)
}

// NetworkOptions Returns a copy of the internal types.NetworkOptions.
func (m *noneNetworkManager) NetworkOptions() types.NetworkOptions {
return m.netOpts
Expand Down Expand Up @@ -179,9 +184,62 @@ func (m *noneNetworkManager) InternalNetworkingOptionLabels(_ context.Context) (

// ContainerNetworkingOpts Returns a slice of `oci.SpecOpts` and `containerd.NewContainerOpts` which represent
// the network specs which need to be applied to the container with the given ID.
func (m *noneNetworkManager) ContainerNetworkingOpts(_ context.Context, _ string) ([]oci.SpecOpts, []containerd.NewContainerOpts, error) {
func (m *noneNetworkManager) ContainerNetworkingOpts(_ context.Context, containerID string) ([]oci.SpecOpts, []containerd.NewContainerOpts, error) {
// No options to return if no network settings are provided.
return []oci.SpecOpts{}, []containerd.NewContainerOpts{}, nil
dataStore, err := clientutil.DataStore(m.globalOptions.DataRoot, m.globalOptions.Address)
if err != nil {
return nil, nil, err
}

stateDir, err := ContainerStateDirPath(m.globalOptions.Namespace, dataStore, containerID)
if err != nil {
return nil, nil, err
}

resolvConfPath := filepath.Join(stateDir, "resolv.conf")
if err := WriteDefaultResolvConf(resolvConfPath); err != nil {
return nil, nil, err
}

hs, err := hostsstore.New(dataStore, m.globalOptions.Namespace)
if err != nil {
return nil, nil, err
}

content := []byte(`127.0.0.1 localhost
::1 localhost
`)
etcHostsPath, err := hs.AllocHostsFile(containerID, content)
if err != nil {
return nil, nil, err
}

specs := []oci.SpecOpts{
withDedupMounts("/etc/hosts", withCustomHosts(etcHostsPath)),
withDedupMounts("/etc/resolv.conf", withCustomResolvConf(resolvConfPath)),
}

// `/etc/hostname` does not exist on FreeBSD
if runtime.GOOS == "linux" {
// If no hostname is set, default to first 12 characters of the container ID.
hostname := m.netOpts.Hostname
if hostname == "" {
hostname = containerID
if len(hostname) > 12 {
hostname = hostname[0:12]
}
}
m.netOpts.Hostname = hostname

hostnameOpts, err := writeEtcHostnameForContainer(m.globalOptions, m.netOpts.Hostname, containerID)
if err != nil {
return nil, nil, err
}
if hostnameOpts != nil {
specs = append(specs, hostnameOpts...)
}
}
return specs, []containerd.NewContainerOpts{}, nil
}

// types.NetworkOptionsManager implementation for container networking settings.
Expand Down

0 comments on commit 29760dc

Please sign in to comment.