Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
K
k3s
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Jacklull
k3s
Commits
3445bd58
Commit
3445bd58
authored
Apr 20, 2015
by
derekwaynecarr
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixup event object reference generation to allow downstream objects to have an event
parent
74d6f30b
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
64 additions
and
17 deletions
+64
-17
ref.go
pkg/api/ref.go
+34
-16
ref_test.go
pkg/api/ref_test.go
+28
-1
ref_test.go
pkg/kubelet/container/ref_test.go
+2
-0
No files found.
pkg/api/ref.go
View file @
3445bd58
...
...
@@ -19,7 +19,8 @@ package api
import
(
"errors"
"fmt"
"regexp"
"net/url"
"strings"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/meta"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
...
...
@@ -31,8 +32,6 @@ var (
ErrNoSelfLink
=
errors
.
New
(
"selfLink was empty, can't make reference"
)
)
var
versionFromSelfLink
=
regexp
.
MustCompile
(
"/api/([^/]*)/"
)
// ForTesting_ReferencesAllowBlankSelfLinks can be set to true in tests to avoid
// "ErrNoSelfLink" errors.
var
ForTesting_ReferencesAllowBlankSelfLinks
=
false
...
...
@@ -40,6 +39,7 @@ var ForTesting_ReferencesAllowBlankSelfLinks = false
// GetReference returns an ObjectReference which refers to the given
// object, or an error if the object doesn't follow the conventions
// that would allow this.
// TODO: should take a meta.Interface see https://github.com/GoogleCloudPlatform/kubernetes/issues/7127
func
GetReference
(
obj
runtime
.
Object
)
(
*
ObjectReference
,
error
)
{
if
obj
==
nil
{
return
nil
,
ErrNilObject
...
...
@@ -52,23 +52,41 @@ func GetReference(obj runtime.Object) (*ObjectReference, error) {
if
err
!=
nil
{
return
nil
,
err
}
_
,
kind
,
err
:=
Scheme
.
ObjectVersionAndKind
(
obj
)
if
err
!=
nil
{
return
nil
,
err
// if the object referenced is actually persisted, we can just get kind from meta
// if we are building an object reference to something not yet persisted, we should fallback to scheme
kind
:=
meta
.
Kind
()
if
kind
==
""
{
_
,
kind
,
err
=
Scheme
.
ObjectVersionAndKind
(
obj
)
if
err
!=
nil
{
return
nil
,
err
}
}
version
:=
""
parsedSelfLink
:=
versionFromSelfLink
.
FindStringSubmatch
(
meta
.
SelfLink
())
if
len
(
parsedSelfLink
)
<
2
{
if
ForTesting_ReferencesAllowBlankSelfLinks
{
version
=
"testing"
}
else
if
meta
.
SelfLink
()
==
""
{
return
nil
,
ErrNoSelfLink
// if the object referenced is actually persisted, we can also get version from meta
version
:=
meta
.
APIVersion
()
if
version
==
""
{
selfLink
:=
meta
.
SelfLink
()
if
selfLink
==
""
{
if
ForTesting_ReferencesAllowBlankSelfLinks
{
version
=
"testing"
}
else
{
return
nil
,
ErrNoSelfLink
}
}
else
{
return
nil
,
fmt
.
Errorf
(
"unexpected self link format: '%v'; got version '%v'"
,
meta
.
SelfLink
(),
version
)
selfLinkUrl
,
err
:=
url
.
Parse
(
selfLink
)
if
err
!=
nil
{
return
nil
,
err
}
// example paths: /<prefix>/<version>/*
parts
:=
strings
.
Split
(
selfLinkUrl
.
Path
,
"/"
)
if
len
(
parts
)
<
3
{
return
nil
,
fmt
.
Errorf
(
"unexpected self link format: '%v'; got version '%v'"
,
selfLink
,
version
)
}
version
=
parts
[
2
]
}
}
else
{
version
=
parsedSelfLink
[
1
]
}
return
&
ObjectReference
{
Kind
:
kind
,
APIVersion
:
version
,
...
...
pkg/api/ref_test.go
View file @
3445bd58
...
...
@@ -27,6 +27,13 @@ type FakeAPIObject struct{}
func
(
*
FakeAPIObject
)
IsAnAPIObject
()
{}
type
ExtensionAPIObject
struct
{
TypeMeta
ObjectMeta
}
func
(
*
ExtensionAPIObject
)
IsAnAPIObject
()
{}
func
TestGetReference
(
t
*
testing
.
T
)
{
table
:=
map
[
string
]
struct
{
obj
runtime
.
Object
...
...
@@ -66,6 +73,26 @@ func TestGetReference(t *testing.T) {
ResourceVersion
:
"42"
,
},
},
"extensionAPIObject"
:
{
obj
:
&
ExtensionAPIObject
{
TypeMeta
:
TypeMeta
{
Kind
:
"ExtensionAPIObject"
,
},
ObjectMeta
:
ObjectMeta
{
Name
:
"foo"
,
UID
:
"bar"
,
ResourceVersion
:
"42"
,
SelfLink
:
"/custom_prefix/version1/extensions/foo"
,
},
},
ref
:
&
ObjectReference
{
Kind
:
"ExtensionAPIObject"
,
APIVersion
:
"version1"
,
Name
:
"foo"
,
UID
:
"bar"
,
ResourceVersion
:
"42"
,
},
},
"badSelfLink"
:
{
obj
:
&
ServiceList
{
ListMeta
:
ListMeta
{
...
...
@@ -90,7 +117,7 @@ func TestGetReference(t *testing.T) {
for
name
,
item
:=
range
table
{
ref
,
err
:=
GetPartialReference
(
item
.
obj
,
item
.
fieldPath
)
if
e
,
a
:=
item
.
shouldErr
,
(
err
!=
nil
);
e
!=
a
{
t
.
Errorf
(
"%v: expected %v, got %v
"
,
name
,
e
,
a
)
t
.
Errorf
(
"%v: expected %v, got %v
, err %v"
,
name
,
e
,
a
,
err
)
continue
}
if
e
,
a
:=
item
.
ref
,
ref
;
!
reflect
.
DeepEqual
(
e
,
a
)
{
...
...
pkg/kubelet/container/ref_test.go
View file @
3445bd58
...
...
@@ -86,6 +86,8 @@ func TestGenerateContainerRef(t *testing.T) {
noSelfLinkPod
=
okPod
defaultedSelfLinkPod
=
okPod
)
noSelfLinkPod
.
Kind
=
""
noSelfLinkPod
.
APIVersion
=
""
noSelfLinkPod
.
ObjectMeta
.
SelfLink
=
""
defaultedSelfLinkPod
.
ObjectMeta
.
SelfLink
=
"/api/v1beta1/pods/ok"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment