mirror of
https://github.com/containers/podman.git
synced 2025-08-06 11:32:07 +08:00
Fxes /etc/hosts duplicated every time after container restarted in a pod
Signed-off-by: zhangguanzhang <zhangguanzhang@qq.com>
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
package libpod
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
@ -1865,16 +1866,26 @@ func (c *Container) writeStringToStaticDir(filename, contents string) (string, e
|
||||
return destFileName, nil
|
||||
}
|
||||
|
||||
// appendStringToRundir appends the provided string to the runtimedir file
|
||||
func (c *Container) appendStringToRundir(destFile, output string) (string, error) {
|
||||
// appendStringToRunDir appends the provided string to the runtimedir file
|
||||
func (c *Container) appendStringToRunDir(destFile, output string) (string, error) {
|
||||
destFileName := filepath.Join(c.state.RunDir, destFile)
|
||||
|
||||
f, err := os.OpenFile(destFileName, os.O_APPEND|os.O_WRONLY, 0600)
|
||||
f, err := os.OpenFile(destFileName, os.O_APPEND|os.O_RDWR, 0600)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
compareStr := strings.TrimRight(output, "\n")
|
||||
scanner := bufio.NewScanner(f)
|
||||
scanner.Split(bufio.ScanLines)
|
||||
|
||||
for scanner.Scan() {
|
||||
if strings.Compare(scanner.Text(), compareStr) == 0 {
|
||||
return filepath.Join(c.state.RunDir, destFile), nil
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := f.WriteString(output); err != nil {
|
||||
return "", errors.Wrapf(err, "unable to write %s", destFileName)
|
||||
}
|
||||
|
@ -1742,7 +1742,7 @@ func (c *Container) generateHosts(path string) (string, error) {
|
||||
// FIXME. Path should be used by this function,but I am not sure what is correct; remove //lint
|
||||
// once this is fixed
|
||||
func (c *Container) appendHosts(path string, netCtr *Container) (string, error) { //nolint
|
||||
return c.appendStringToRundir("hosts", netCtr.getHosts())
|
||||
return c.appendStringToRunDir("hosts", netCtr.getHosts())
|
||||
}
|
||||
|
||||
// getHosts finds the pertinent information for a container's host file in its config and state
|
||||
|
@ -196,4 +196,33 @@ var _ = Describe("Podman restart", func() {
|
||||
Expect(restartTime.OutputToStringArray()[0]).To(Equal(startTime.OutputToStringArray()[0]))
|
||||
Expect(restartTime.OutputToStringArray()[1]).To(Not(Equal(startTime.OutputToStringArray()[1])))
|
||||
})
|
||||
|
||||
It("Podman restart a container in a pod and hosts shouln't duplicated", func() {
|
||||
// Fixes: https://github.com/containers/podman/issues/8921
|
||||
|
||||
_, ec, _ := podmanTest.CreatePod("foobar99")
|
||||
Expect(ec).To(Equal(0))
|
||||
|
||||
session := podmanTest.RunTopContainerInPod("host-restart-test", "foobar99")
|
||||
session.WaitWithDefaultTimeout()
|
||||
Expect(session.ExitCode()).To(Equal(0))
|
||||
|
||||
testCmd := []string{"exec", "host-restart-test", "sh", "-c", "wc -l < /etc/hosts"}
|
||||
|
||||
// before restart
|
||||
beforeRestart := podmanTest.Podman(testCmd)
|
||||
beforeRestart.WaitWithDefaultTimeout()
|
||||
Expect(beforeRestart.ExitCode()).To(Equal(0))
|
||||
|
||||
session = podmanTest.Podman([]string{"restart", "host-restart-test"})
|
||||
session.WaitWithDefaultTimeout()
|
||||
Expect(session.ExitCode()).To(Equal(0))
|
||||
|
||||
afterRestart := podmanTest.Podman(testCmd)
|
||||
afterRestart.WaitWithDefaultTimeout()
|
||||
Expect(afterRestart.ExitCode()).To(Equal(0))
|
||||
|
||||
// line count should be equal
|
||||
Expect(beforeRestart.OutputToString()).To(Equal(afterRestart.OutputToString()))
|
||||
})
|
||||
})
|
||||
|
Reference in New Issue
Block a user