Fix: prevent nil pointer dereference in legacy kube.List

Merged
CP
20d ago
+156-13

Description of your changes

Fixes kubevela/kubevela#7280
op.#List panics with a nil pointer dereference when evaluated from CUE because ResourceVars tagged Resource as json:"value" instead of json:"resource". Also missing nil guards on filter prevented listing across all namespaces when filter: {} was passed

  • Add ListVars accepting both resource and value
  • Add nil checks on workload and filter
  • Only apply client.InNamespace when filter.Namespace != "" so all-namespace listing works
  • Add omitempty to ListReturns.Resources

I have:

  • Read and followed KubeVela's contribution process.
  • Related Docs updated properly. In a new feature or configuration option, an update to the documentation is necessary.
  • Run make reviewable to ensure this PR is ready for review.
  • Added backport release-x.y labels to auto-backport this PR if necessary.

How has this code been tested

Added unit tests in kube_test.go
Ran go test ./pkg/providers/legacy/kube/... and golangci-lint


Summary by cubic

Fixes the nil pointer dereference that panicked op.#List when called from CUE with the resource field, and makes empty filter: {} list across all namespaces instead of crashing.

  • Introduces ListVars, which accepts both resource and value fields for backward compatibility.
  • Returns explicit errors when resource kind or apiVersion is missing, or when the kube client is not initialized.
  • Only applies client.InNamespace when a namespace is set, enabling cluster-wide listing.
  • Adds omitempty to ListReturns.Resources.