kubernetes/kops · error

error writing file %q: %v

Error message

error writing file %q: %v

What it means

The pseudo-atomic rewrite of /etc/hosts (write, pause, re-read loop) ultimately failed; because /etc/hosts is bind-mounted, a rename-based atomic write is impossible and the in-place write itself errored (ENOSPC, EIO, or EACCES).

Source

Thrown at pkg/dns/hosts/hosts.go:195

	// Sort into a consistent order to minimize updates
	sort.Strings(block)

	out = append(out, GUARD_BEGIN)
	out = append(out, block...)
	out = append(out, GUARD_END)
	out = append(out, "")

	updated := []byte(strings.Join(out, "\n"))

	if bytes.Equal(updated, data) {
		klog.V(2).Infof("skipping update of unchanged /etc/hosts")
		return nil
	}

	// Note that because we are bind mounting /etc/hosts, we can't do a normal atomic file write
	// (where we write a temp file and rename it)
	if err := pseudoAtomicWrite(p, updated, stat.Mode()); err != nil {
		return fmt.Errorf("error writing file %q: %v", p, err)
	}

	return nil
}

// Because we are bind-mounting /etc/hosts, we can't do a normal
// atomic file write (where we write a temp file and rename it);
// instead we write the file, pause, re-read and see if anyone else
// wrote in the meantime; if so we rewrite again.  By pausing for a
// random amount of time, eventually we'll win the write race and
// exit.  This doesn't guarantee fairness, but it should mean that the
// end-result is not malformed (i.e. partial writes).
func pseudoAtomicWrite(p string, b []byte, mode os.FileMode) error {
	attempt := 0
	for {
		attempt++
		if attempt > 10 {
			return fmt.Errorf("failed to consistently write file %q - too many retries", p)

View on GitHub (pinned to 4c8573c808)

Solutions

  1. Check disk space and write permissions on /etc/hosts
  2. Verify the bind mount is writable (not mounted read-only)
  3. Retry after resolving the underlying write error
Defensive patterns

Strategy: retry

When it happens

Trigger: Thrown at pkg/dns/hosts/hosts.go:195 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of kubernetes/kops@4c8573c808 (2026-09-05). Data as JSON: /api/errors/6c47a3e0437ee613. Report an issue: GitHub.