kubernetes/kops · error

memfs context not initialized

Error message

memfs context not initialized

What it means

The memfs VFS backend requires an initialized MemFSContext, which holds the in-memory file tree and its cluster-readable flag. VFSContext only initializes memfsContext in unit tests (via ResetMemfsContext), so a production/default VFSContext asked to resolve a memfs:// path has no backing store and throws this error.

Source

Thrown at util/pkg/vfs/context.go:481

	}

	bucket := strings.TrimSuffix(u.Host, "/")
	if bucket == "" {
		return nil, fmt.Errorf("invalid kubernetes vfs path: %q", p)
	}

	k8sPath := newKubernetesPath(c.k8sContext, bucket, u.Path)
	return k8sPath, nil
}

func (c *VFSContext) buildMemFSPath(p string) (*MemFSPath, error) {
	if !strings.HasPrefix(p, "memfs://") {
		return nil, fmt.Errorf("memfs path not recognized: %q", p)
	}
	location := strings.TrimPrefix(p, "memfs://")
	if c.memfsContext == nil {
		// We only initialize this in unit tests etc
		return nil, fmt.Errorf("memfs context not initialized")
	}
	fspath := NewMemFSPath(c.memfsContext, location)
	return fspath, nil
}

func (c *VFSContext) ResetMemfsContext(clusterReadable bool) {
	c.memfsContext = NewMemFSContext()
	if clusterReadable {
		c.memfsContext.MarkClusterReadable()
	}
}

func (c *VFSContext) buildGCSPath(p string) (*GSPath, error) {
	u, err := url.Parse(p)
	if err != nil {
		return nil, fmt.Errorf("invalid google cloud storage path: %q", p)
	}

View on GitHub (pinned to 4c8573c808)

Solutions

  1. In test setup, call vfs.Context.ResetMemfsContext(true) (or false) before building memfs:// paths
  2. Use a real backend (s3://, gs://, file://) for non-test workloads — memfs is test-only
  3. If constructing VFSContext manually, initialize the memfsContext field via ResetMemfsContext rather than leaving it nil

Example fix

// before
p, err := vfs.Context.BuildVfsPath("memfs://test") // panics error: memfs context not initialized
// after
vfs.Context.ResetMemfsContext(true)
p, err := vfs.Context.BuildVfsPath("memfs://test")
Defensive patterns

Strategy: validation

Validate before calling

if strings.HasPrefix(p, "memfs://") && !memfsInitialized {
	return fmt.Errorf("call vfs.Context.ResetMemfsContext before using memfs:// paths")
}

Type guard

func isUninitializedMemfsErr(err error) bool {
	return err != nil && strings.Contains(err.Error(), "memfs context not initialized")
}

Try / catch

p, err := vfs.Context.BuildVfsPath("memfs://test")
if err != nil {
	if isUninitializedMemfsErr(err) {
		vfs.Context.ResetMemfsContext(true)
		p, err = vfs.Context.BuildVfsPath("memfs://test")
	}
	if err != nil {
		return err
	}
}

Prevention

When it happens

Trigger: BuildVfsPath (or ReadLocation treating a memfs:// path) is called on a VFSContext whose memfsContext field is nil — i.e. the context was never initialized with ResetMemfsContext — while the path correctly starts with memfs://.

Common situations: Calling BuildVfsPath("memfs://...") in non-test code or in a test that constructed vfs.Context{} directly without calling ResetMemfsContext first; running a code path (e.g. a tool or command) against memfs:// state store outside of unit tests where memfs is unsupported.

Related errors


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