{"record":{"id":"5cda8300edb82e45","repo":"kubernetes/kops","slug":"error-writing-temp-file-v","errorCode":null,"errorMessage":"error writing temp file: %v","messagePattern":"error writing temp file: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"channels/pkg/channels/kubectlapplier.go","lineNumber":49,"sourceCode":"\n// Apply calls kubectl apply to apply the manifest.\n// We will likely in future change this to create things directly (or more likely embed this logic into kubectl itself)\nfunc (*KubectlApplier) Apply(ctx context.Context, data []byte) error {\n\t// We copy the manifest to a temp file because it is likely e.g. an s3 URL, which kubectl can't read\n\ttmpDir, err := os.MkdirTemp(\"\", \"channel\")\n\tif err != nil {\n\t\treturn fmt.Errorf(\"error creating temp dir: %v\", err)\n\t}\n\n\tdefer func() {\n\t\tif err := os.RemoveAll(tmpDir); err != nil {\n\t\t\tklog.Warningf(\"error deleting temp dir %q: %v\", tmpDir, err)\n\t\t}\n\t}()\n\n\tlocalManifestFile := path.Join(tmpDir, \"manifest.yaml\")\n\tif err := os.WriteFile(localManifestFile, data, 0o600); err != nil {\n\t\treturn fmt.Errorf(\"error writing temp file: %v\", err)\n\t}\n\t// First do an apply. This may fail when removing things from lists/arrays and required fields are not removed.\n\t{\n\t\t_, err := execKubectl(ctx, \"apply\", \"-f\", localManifestFile, \"--server-side\", \"--force-conflicts\", \"--field-manager=kops\")\n\t\tif err != nil {\n\t\t\tklog.Errorf(\"failed to apply the manifest: %v\", err)\n\t\t}\n\n\t}\n\n\t// Replace will force ownership on all fields to kops. But on some k8s versions, this will fail on e.g trying to set clusterIP to \"\".\n\t{\n\t\t_, err := execKubectl(ctx, \"replace\", \"-f\", localManifestFile, \"--field-manager=kops\")\n\t\tif err != nil {\n\t\t\tklog.Errorf(\"failed to replace manifest: %v\", err)\n\t\t}\n\t}\n","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/channels/pkg/channels/kubectlapplier.go#L31-L67","documentation":"After creating the temp dir, KubectlApplier.Apply writes the manifest bytes to <tmpdir>/manifest.yaml with os.WriteFile so kubectl can read it. If the write fails, this error is returned. Like the temp-dir error, it points to a local filesystem/environment problem rather than a cluster problem.","triggerScenarios":"os.WriteFile failing because the disk filled up between MkdirTemp and WriteFile, the temp dir was concurrently removed, permissions changed, or the manifest data write exceeded available space/inodes.","commonSituations":"Disk filling up mid-operation; aggressive tmp cleaners (systemd-tmpfiles) racing the write; read-only remount of the temp filesystem; security tooling blocking file creation.","solutions":["Check disk space on the temp filesystem (df -h) and free space if full.","Re-run the command — transient disk/cleaner races usually resolve on retry.","Inspect the wrapped %v error (ENOSPC, EACCES, ENOENT) to target the fix.","Disable/rule out tmp-cleaners or security agents interfering with the temp dir.","Point TMPDIR at a different, stable writable filesystem."],"exampleFix":"// before\nexport TMPDIR=/var/tmp-full-disk\nkops ... # error writing temp file: no space left on device\n// after\nexport TMPDIR=/mnt/big-disk-tmp\nkops ...","handlingStrategy":"retry","validationCode":"tmp := os.TempDir()\nif st, err := os.Stat(tmp); err != nil || !st.IsDir() {\n    return fmt.Errorf(\"temp dir %q unusable: %v\", tmp, err)\n}\n// optional free-space check\n// syscall.Statfs(tmp, &buf); if buf.Bavail*uint64(buf.Bsize) < minFree { ... }","typeGuard":"func isTempWriteFailure(err error) bool {\n    return strings.Contains(err.Error(), \"error writing temp file\")\n}","tryCatchPattern":"if err := applier.Apply(ctx, data); err != nil {\n    if isTempWriteFailure(err) {\n        // check df/free space, then retry after cleanup\n        return retryApplyAfterDiskCheck(ctx, applier, data)\n    }\n    return err\n}","preventionTips":["Monitor free disk space on the temp filesystem.","Exclude temp dirs from aggressive cleaners during runs.","Verify the wrapped errno (ENOSPC → free space; EACCES → permissions).","Retry once after transient filesystem errors before failing hard."],"tags":["filesystem","temp-file","disk-full","os"],"backgroundTag":"temp-file-write-failed","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}