Fix: prevent nil pointer dereference in legacy kube.List
Merged
CP
20d ago+156+156+012345678901234567890123456789-13-13-01234567890123456789
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
ListVarsaccepting bothresourceandvalue - Add nil checks on
workloadandfilter - Only apply
client.InNamespacewhenfilter.Namespace != ""so all-namespace listing works - Add
omitemptytoListReturns.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 reviewableto ensure this PR is ready for review. - Added
backport release-x.ylabels 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 bothresourceandvaluefields for backward compatibility. - Returns explicit errors when resource kind or apiVersion is missing, or when the kube client is not initialized.
- Only applies
client.InNamespacewhen a namespace is set, enabling cluster-wide listing. - Adds
omitemptytoListReturns.Resources.