Skip to content

Commit

Permalink
Merge pull request #225 from ehashman/safer-nils
Browse files Browse the repository at this point in the history
KObj should be able to handle nil objects
  • Loading branch information
k8s-ci-robot committed Mar 12, 2021
2 parents c1fc6e6 + 80a291b commit 0f69eb5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions klog.go
Expand Up @@ -1577,6 +1577,10 @@ type KMetadata interface {

// KObj returns ObjectRef from ObjectMeta
func KObj(obj KMetadata) ObjectRef {
if obj == nil {
return ObjectRef{}
}

return ObjectRef{
Name: obj.GetName(),
Namespace: obj.GetNamespace(),
Expand Down
5 changes: 5 additions & 0 deletions klog_test.go
Expand Up @@ -763,6 +763,11 @@ func TestKObj(t *testing.T) {
obj KMetadata
want ObjectRef
}{
{
name: "nil",
obj: nil,
want: ObjectRef{},
},
{
name: "with ns",
obj: mockKmeta{"test-name", "test-ns"},
Expand Down

0 comments on commit 0f69eb5

Please sign in to comment.