mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 04:33:26 +00:00
Ensure non-nil items in lists
This commit is contained in:
parent
1e0b9c6e20
commit
8c1e8355f8
@ -236,12 +236,6 @@ func PatchResource(r rest.Patcher, scope *RequestScope, admit admission.Interfac
|
|||||||
}
|
}
|
||||||
trace.Step("Object stored in database")
|
trace.Step("Object stored in database")
|
||||||
|
|
||||||
if err := setObjectSelfLink(ctx, result, req, scope.Namer); err != nil {
|
|
||||||
scope.err(err, w, req)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
trace.Step("Self-link added")
|
|
||||||
|
|
||||||
status := http.StatusOK
|
status := http.StatusOK
|
||||||
if wasCreated {
|
if wasCreated {
|
||||||
status = http.StatusCreated
|
status = http.StatusCreated
|
||||||
|
@ -59,7 +59,7 @@ func doTransformObject(ctx context.Context, obj runtime.Object, opts interface{}
|
|||||||
if _, ok := obj.(*metav1.Status); ok {
|
if _, ok := obj.(*metav1.Status); ok {
|
||||||
return obj, nil
|
return obj, nil
|
||||||
}
|
}
|
||||||
if err := setObjectSelfLink(ctx, obj, req, scope.Namer); err != nil {
|
if err := ensureNonNilItems(obj); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -356,49 +356,14 @@ func dedupOwnerReferencesAndAddWarning(obj runtime.Object, requestContext contex
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// setObjectSelfLink sets the self link of an object as needed.
|
// ensureNonNilItems ensures that for empty lists we don't return <nil> items.
|
||||||
// TODO: remove the need for the namer LinkSetters by requiring objects implement either Object or List
|
func ensureNonNilItems(obj runtime.Object) error {
|
||||||
// interfaces
|
if meta.IsListType(obj) && meta.LenList(obj) == 0 {
|
||||||
func setObjectSelfLink(ctx context.Context, obj runtime.Object, req *http.Request, namer ScopeNamer) error {
|
|
||||||
if utilfeature.DefaultFeatureGate.Enabled(features.RemoveSelfLink) {
|
|
||||||
// Ensure that for empty lists we don't return <nil> items.
|
|
||||||
if meta.IsListType(obj) && meta.LenList(obj) == 0 {
|
|
||||||
if err := meta.SetList(obj, []runtime.Object{}); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// We only generate list links on objects that implement ListInterface - historically we duck typed this
|
|
||||||
// check via reflection, but as we move away from reflection we require that you not only carry Items but
|
|
||||||
// ListMeta into order to be identified as a list.
|
|
||||||
if !meta.IsListType(obj) {
|
|
||||||
_, ok := request.RequestInfoFrom(ctx)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("missing requestInfo")
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
_, ok := request.RequestInfoFrom(ctx)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("missing requestInfo")
|
|
||||||
}
|
|
||||||
|
|
||||||
count := 0
|
|
||||||
err := meta.EachListItem(obj, func(obj runtime.Object) error {
|
|
||||||
count++
|
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
if count == 0 {
|
|
||||||
if err := meta.SetList(obj, []runtime.Object{}); err != nil {
|
if err := meta.SetList(obj, []runtime.Object{}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
return err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func summarizeData(data []byte, maxLength int) string {
|
func summarizeData(data []byte, maxLength int) string {
|
||||||
|
Loading…
Reference in New Issue
Block a user