mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-08 11:38:15 +00:00
Error instead of crash on GetRef of nil object
This commit is contained in:
parent
34a64a270d
commit
0c58ee98c8
@ -17,18 +17,24 @@ limitations under the License.
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrNilObject = errors.New("Can't reference a nil object")
|
||||||
|
|
||||||
var versionFromSelfLink = regexp.MustCompile("/api/([^/]*)/")
|
var versionFromSelfLink = regexp.MustCompile("/api/([^/]*)/")
|
||||||
|
|
||||||
// GetReference returns an ObjectReference which refers to the given
|
// GetReference returns an ObjectReference which refers to the given
|
||||||
// object, or an error if the object doesn't follow the conventions
|
// object, or an error if the object doesn't follow the conventions
|
||||||
// that would allow this.
|
// that would allow this.
|
||||||
func GetReference(obj runtime.Object) (*ObjectReference, error) {
|
func GetReference(obj runtime.Object) (*ObjectReference, error) {
|
||||||
|
if obj == nil {
|
||||||
|
return nil, ErrNilObject
|
||||||
|
}
|
||||||
jsonBase, err := runtime.FindTypeMeta(obj)
|
jsonBase, err := runtime.FindTypeMeta(obj)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -80,6 +80,11 @@ func TestGetReference(t *testing.T) {
|
|||||||
ref: nil,
|
ref: nil,
|
||||||
shouldErr: true,
|
shouldErr: true,
|
||||||
},
|
},
|
||||||
|
"errorNil": {
|
||||||
|
obj: nil,
|
||||||
|
ref: nil,
|
||||||
|
shouldErr: true,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for name, item := range table {
|
for name, item := range table {
|
||||||
|
Loading…
Reference in New Issue
Block a user