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
224a8d63
Commit
224a8d63
authored
Jan 13, 2015
by
Daniel Smith
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3384 from derekwaynecarr/list_watch_by_namespace
Add ability to listwatch a resource in an optional namespace
parents
bb140d63
7c630fd7
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
63 additions
and
6 deletions
+63
-6
listwatch.go
pkg/client/cache/listwatch.go
+3
-0
listwatch_test.go
pkg/client/cache/listwatch_test.go
+60
-6
No files found.
pkg/client/cache/listwatch.go
View file @
224a8d63
...
@@ -29,12 +29,14 @@ type ListWatch struct {
...
@@ -29,12 +29,14 @@ type ListWatch struct {
Client
*
client
.
Client
Client
*
client
.
Client
FieldSelector
labels
.
Selector
FieldSelector
labels
.
Selector
Resource
string
Resource
string
Namespace
string
}
}
// ListWatch knows how to list and watch a set of apiserver resources.
// ListWatch knows how to list and watch a set of apiserver resources.
func
(
lw
*
ListWatch
)
List
()
(
runtime
.
Object
,
error
)
{
func
(
lw
*
ListWatch
)
List
()
(
runtime
.
Object
,
error
)
{
return
lw
.
Client
.
return
lw
.
Client
.
Get
()
.
Get
()
.
Namespace
(
lw
.
Namespace
)
.
Resource
(
lw
.
Resource
)
.
Resource
(
lw
.
Resource
)
.
SelectorParam
(
"fields"
,
lw
.
FieldSelector
)
.
SelectorParam
(
"fields"
,
lw
.
FieldSelector
)
.
Do
()
.
Do
()
.
...
@@ -45,6 +47,7 @@ func (lw *ListWatch) Watch(resourceVersion string) (watch.Interface, error) {
...
@@ -45,6 +47,7 @@ func (lw *ListWatch) Watch(resourceVersion string) (watch.Interface, error) {
return
lw
.
Client
.
return
lw
.
Client
.
Get
()
.
Get
()
.
Prefix
(
"watch"
)
.
Prefix
(
"watch"
)
.
Namespace
(
lw
.
Namespace
)
.
Resource
(
lw
.
Resource
)
.
Resource
(
lw
.
Resource
)
.
SelectorParam
(
"fields"
,
lw
.
FieldSelector
)
.
SelectorParam
(
"fields"
,
lw
.
FieldSelector
)
.
Param
(
"resourceVersion"
,
resourceVersion
)
.
Param
(
"resourceVersion"
,
resourceVersion
)
.
...
...
pkg/client/cache/listwatch_test.go
View file @
224a8d63
...
@@ -18,8 +18,11 @@ package cache
...
@@ -18,8 +18,11 @@ package cache
import
(
import
(
"net/http/httptest"
"net/http/httptest"
"net/url"
"path"
"testing"
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api/testapi"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
"github.com/GoogleCloudPlatform/kubernetes/pkg/labels"
...
@@ -34,6 +37,39 @@ func parseSelectorOrDie(s string) labels.Selector {
...
@@ -34,6 +37,39 @@ func parseSelectorOrDie(s string) labels.Selector {
return
selector
return
selector
}
}
// buildResourcePath is a convenience function for knowing if a namespace should be in a path param or not
func
buildResourcePath
(
prefix
,
namespace
,
resource
string
)
string
{
base
:=
path
.
Join
(
"/api"
,
testapi
.
Version
(),
prefix
)
if
len
(
namespace
)
>
0
{
if
!
(
testapi
.
Version
()
==
"v1beta1"
||
testapi
.
Version
()
==
"v1beta2"
)
{
base
=
path
.
Join
(
base
,
"ns"
,
namespace
)
}
}
return
path
.
Join
(
base
,
resource
)
}
// buildQueryValues is a convenience function for knowing if a namespace should be in a query param or not
func
buildQueryValues
(
namespace
string
,
query
url
.
Values
)
url
.
Values
{
v
:=
url
.
Values
{}
if
query
!=
nil
{
for
key
,
values
:=
range
query
{
for
_
,
value
:=
range
values
{
v
.
Add
(
key
,
value
)
}
}
}
if
len
(
namespace
)
>
0
{
if
testapi
.
Version
()
==
"v1beta1"
||
testapi
.
Version
()
==
"v1beta2"
{
v
.
Set
(
"namespace"
,
namespace
)
}
}
return
v
}
func
buildLocation
(
resourcePath
string
,
query
url
.
Values
)
string
{
return
resourcePath
+
"?"
+
query
.
Encode
()
}
func
TestListWatchesCanList
(
t
*
testing
.
T
)
{
func
TestListWatchesCanList
(
t
*
testing
.
T
)
{
table
:=
[]
struct
{
table
:=
[]
struct
{
location
string
location
string
...
@@ -41,7 +77,7 @@ func TestListWatchesCanList(t *testing.T) {
...
@@ -41,7 +77,7 @@ func TestListWatchesCanList(t *testing.T) {
}{
}{
// Minion
// Minion
{
{
location
:
"/api/"
+
testapi
.
Version
()
+
"/minions"
,
location
:
buildLocation
(
buildResourcePath
(
""
,
api
.
NamespaceAll
,
"minions"
),
buildQueryValues
(
api
.
NamespaceAll
,
nil
))
,
lw
:
ListWatch
{
lw
:
ListWatch
{
FieldSelector
:
parseSelectorOrDie
(
""
),
FieldSelector
:
parseSelectorOrDie
(
""
),
Resource
:
"minions"
,
Resource
:
"minions"
,
...
@@ -49,14 +85,22 @@ func TestListWatchesCanList(t *testing.T) {
...
@@ -49,14 +85,22 @@ func TestListWatchesCanList(t *testing.T) {
},
},
// pod with "assigned" field selector.
// pod with "assigned" field selector.
{
{
location
:
"/api/"
+
testapi
.
Version
()
+
"/pods?fields=DesiredState.Host%3D"
,
location
:
buildLocation
(
buildResourcePath
(
""
,
api
.
NamespaceAll
,
"pods"
),
buildQueryValues
(
api
.
NamespaceAll
,
url
.
Values
{
"fields"
:
[]
string
{
"DesiredState.Host="
}})),
lw
:
ListWatch
{
FieldSelector
:
labels
.
Set
{
"DesiredState.Host"
:
""
}
.
AsSelector
(),
Resource
:
"pods"
,
},
},
// pod in namespace "foo"
{
location
:
buildLocation
(
buildResourcePath
(
""
,
"foo"
,
"pods"
),
buildQueryValues
(
"foo"
,
url
.
Values
{
"fields"
:
[]
string
{
"DesiredState.Host="
}})),
lw
:
ListWatch
{
lw
:
ListWatch
{
FieldSelector
:
labels
.
Set
{
"DesiredState.Host"
:
""
}
.
AsSelector
(),
FieldSelector
:
labels
.
Set
{
"DesiredState.Host"
:
""
}
.
AsSelector
(),
Resource
:
"pods"
,
Resource
:
"pods"
,
Namespace
:
"foo"
,
},
},
},
},
}
}
for
_
,
item
:=
range
table
{
for
_
,
item
:=
range
table
{
handler
:=
util
.
FakeHandler
{
handler
:=
util
.
FakeHandler
{
StatusCode
:
500
,
StatusCode
:
500
,
...
@@ -80,7 +124,7 @@ func TestListWatchesCanWatch(t *testing.T) {
...
@@ -80,7 +124,7 @@ func TestListWatchesCanWatch(t *testing.T) {
}{
}{
// Minion
// Minion
{
{
location
:
"/api/"
+
testapi
.
Version
()
+
"/watch/minions?resourceVersion="
,
location
:
buildLocation
(
buildResourcePath
(
"watch"
,
api
.
NamespaceAll
,
"minions"
),
buildQueryValues
(
api
.
NamespaceAll
,
url
.
Values
{
"resourceVersion"
:
[]
string
{
""
}}))
,
rv
:
""
,
rv
:
""
,
lw
:
ListWatch
{
lw
:
ListWatch
{
FieldSelector
:
parseSelectorOrDie
(
""
),
FieldSelector
:
parseSelectorOrDie
(
""
),
...
@@ -88,7 +132,7 @@ func TestListWatchesCanWatch(t *testing.T) {
...
@@ -88,7 +132,7 @@ func TestListWatchesCanWatch(t *testing.T) {
},
},
},
},
{
{
location
:
"/api/"
+
testapi
.
Version
()
+
"/watch/minions?resourceVersion=42"
,
location
:
buildLocation
(
buildResourcePath
(
"watch"
,
api
.
NamespaceAll
,
"minions"
),
buildQueryValues
(
api
.
NamespaceAll
,
url
.
Values
{
"resourceVersion"
:
[]
string
{
"42"
}}))
,
rv
:
"42"
,
rv
:
"42"
,
lw
:
ListWatch
{
lw
:
ListWatch
{
FieldSelector
:
parseSelectorOrDie
(
""
),
FieldSelector
:
parseSelectorOrDie
(
""
),
...
@@ -97,11 +141,21 @@ func TestListWatchesCanWatch(t *testing.T) {
...
@@ -97,11 +141,21 @@ func TestListWatchesCanWatch(t *testing.T) {
},
},
// pod with "assigned" field selector.
// pod with "assigned" field selector.
{
{
location
:
"/api/"
+
testapi
.
Version
()
+
"/watch/pods?fields=DesiredState.Host%3D&resourceVersion=0"
,
location
:
buildLocation
(
buildResourcePath
(
"watch"
,
api
.
NamespaceAll
,
"pods"
),
buildQueryValues
(
api
.
NamespaceAll
,
url
.
Values
{
"fields"
:
[]
string
{
"DesiredState.Host="
},
"resourceVersion"
:
[]
string
{
"0"
}})),
rv
:
"0"
,
lw
:
ListWatch
{
FieldSelector
:
labels
.
Set
{
"DesiredState.Host"
:
""
}
.
AsSelector
(),
Resource
:
"pods"
,
},
},
// pod with namespace foo and assigned field selector
{
location
:
buildLocation
(
buildResourcePath
(
"watch"
,
"foo"
,
"pods"
),
buildQueryValues
(
"foo"
,
url
.
Values
{
"fields"
:
[]
string
{
"DesiredState.Host="
},
"resourceVersion"
:
[]
string
{
"0"
}})),
rv
:
"0"
,
rv
:
"0"
,
lw
:
ListWatch
{
lw
:
ListWatch
{
FieldSelector
:
labels
.
Set
{
"DesiredState.Host"
:
""
}
.
AsSelector
(),
FieldSelector
:
labels
.
Set
{
"DesiredState.Host"
:
""
}
.
AsSelector
(),
Resource
:
"pods"
,
Resource
:
"pods"
,
Namespace
:
"foo"
,
},
},
},
},
}
}
...
...
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