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
d30da9a8
Commit
d30da9a8
authored
Jan 26, 2015
by
Deyuan Deng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
List objects in deterministic order
parent
60eba74c
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
45 additions
and
8 deletions
+45
-8
etcd_tools.go
pkg/tools/etcd_tools.go
+1
-1
etcd_tools_test.go
pkg/tools/etcd_tools_test.go
+29
-7
fake_etcd_client.go
pkg/tools/fake_etcd_client.go
+15
-0
No files found.
pkg/tools/etcd_tools.go
View file @
d30da9a8
...
@@ -147,7 +147,7 @@ func etcdErrorIndex(err error) (uint64, bool) {
...
@@ -147,7 +147,7 @@ func etcdErrorIndex(err error) (uint64, bool) {
}
}
func
(
h
*
EtcdHelper
)
listEtcdNode
(
key
string
)
([]
*
etcd
.
Node
,
uint64
,
error
)
{
func
(
h
*
EtcdHelper
)
listEtcdNode
(
key
string
)
([]
*
etcd
.
Node
,
uint64
,
error
)
{
result
,
err
:=
h
.
Client
.
Get
(
key
,
fals
e
,
true
)
result
,
err
:=
h
.
Client
.
Get
(
key
,
tru
e
,
true
)
if
err
!=
nil
{
if
err
!=
nil
{
index
,
ok
:=
etcdErrorIndex
(
err
)
index
,
ok
:=
etcdErrorIndex
(
err
)
if
!
ok
{
if
!
ok
{
...
...
pkg/tools/etcd_tools_test.go
View file @
d30da9a8
...
@@ -30,11 +30,6 @@ import (
...
@@ -30,11 +30,6 @@ import (
"github.com/coreos/go-etcd/etcd"
"github.com/coreos/go-etcd/etcd"
)
)
type
fakeClientGetSet
struct
{
get
func
(
key
string
,
sort
,
recursive
bool
)
(
*
etcd
.
Response
,
error
)
set
func
(
key
,
value
string
,
ttl
uint64
)
(
*
etcd
.
Response
,
error
)
}
type
TestResource
struct
{
type
TestResource
struct
{
api
.
TypeMeta
`json:",inline"`
api
.
TypeMeta
`json:",inline"`
api
.
ObjectMeta
`json:"metadata"`
api
.
ObjectMeta
`json:"metadata"`
...
@@ -72,17 +67,24 @@ func TestExtractToList(t *testing.T) {
...
@@ -72,17 +67,24 @@ func TestExtractToList(t *testing.T) {
R
:
&
etcd
.
Response
{
R
:
&
etcd
.
Response
{
EtcdIndex
:
10
,
EtcdIndex
:
10
,
Node
:
&
etcd
.
Node
{
Node
:
&
etcd
.
Node
{
Dir
:
true
,
Nodes
:
[]
*
etcd
.
Node
{
Nodes
:
[]
*
etcd
.
Node
{
{
{
Key
:
"/foo"
,
Value
:
`{"id":"foo","kind":"Pod","apiVersion":"v1beta1"}`
,
Value
:
`{"id":"foo","kind":"Pod","apiVersion":"v1beta1"}`
,
Dir
:
false
,
ModifiedIndex
:
1
,
ModifiedIndex
:
1
,
},
},
{
{
Key
:
"/bar"
,
Value
:
`{"id":"bar","kind":"Pod","apiVersion":"v1beta1"}`
,
Value
:
`{"id":"bar","kind":"Pod","apiVersion":"v1beta1"}`
,
Dir
:
false
,
ModifiedIndex
:
2
,
ModifiedIndex
:
2
,
},
},
{
{
Key
:
"/baz"
,
Value
:
`{"id":"baz","kind":"Pod","apiVersion":"v1beta1"}`
,
Value
:
`{"id":"baz","kind":"Pod","apiVersion":"v1beta1"}`
,
Dir
:
false
,
ModifiedIndex
:
3
,
ModifiedIndex
:
3
,
},
},
},
},
...
@@ -92,9 +94,10 @@ func TestExtractToList(t *testing.T) {
...
@@ -92,9 +94,10 @@ func TestExtractToList(t *testing.T) {
expect
:=
api
.
PodList
{
expect
:=
api
.
PodList
{
ListMeta
:
api
.
ListMeta
{
ResourceVersion
:
"10"
},
ListMeta
:
api
.
ListMeta
{
ResourceVersion
:
"10"
},
Items
:
[]
api
.
Pod
{
Items
:
[]
api
.
Pod
{
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"foo"
,
ResourceVersion
:
"1"
}},
// We expect items to be sorted by its name.
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"bar"
,
ResourceVersion
:
"2"
}},
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"bar"
,
ResourceVersion
:
"2"
}},
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"baz"
,
ResourceVersion
:
"3"
}},
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"baz"
,
ResourceVersion
:
"3"
}},
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"foo"
,
ResourceVersion
:
"1"
}},
},
},
}
}
...
@@ -116,22 +119,34 @@ func TestExtractToListAcrossDirectories(t *testing.T) {
...
@@ -116,22 +119,34 @@ func TestExtractToListAcrossDirectories(t *testing.T) {
R
:
&
etcd
.
Response
{
R
:
&
etcd
.
Response
{
EtcdIndex
:
10
,
EtcdIndex
:
10
,
Node
:
&
etcd
.
Node
{
Node
:
&
etcd
.
Node
{
Dir
:
true
,
Nodes
:
[]
*
etcd
.
Node
{
Nodes
:
[]
*
etcd
.
Node
{
{
{
Key
:
"/directory1"
,
Value
:
`{"name": "directory1"}`
,
Value
:
`{"name": "directory1"}`
,
Dir
:
true
,
Dir
:
true
,
Nodes
:
[]
*
etcd
.
Node
{
Nodes
:
[]
*
etcd
.
Node
{
{
{
Key
:
"/foo"
,
Value
:
`{"id":"foo","kind":"Pod","apiVersion":"v1beta1"}`
,
Value
:
`{"id":"foo","kind":"Pod","apiVersion":"v1beta1"}`
,
Dir
:
false
,
ModifiedIndex
:
1
,
},
{
Key
:
"/baz"
,
Value
:
`{"id":"baz","kind":"Pod","apiVersion":"v1beta1"}`
,
Dir
:
false
,
ModifiedIndex
:
1
,
ModifiedIndex
:
1
,
},
},
},
},
},
},
{
{
Key
:
"/directory2"
,
Value
:
`{"name": "directory2"}`
,
Value
:
`{"name": "directory2"}`
,
Dir
:
true
,
Dir
:
true
,
Nodes
:
[]
*
etcd
.
Node
{
Nodes
:
[]
*
etcd
.
Node
{
{
{
Key
:
"/bar"
,
Value
:
`{"id":"bar","kind":"Pod","apiVersion":"v1beta1"}`
,
Value
:
`{"id":"bar","kind":"Pod","apiVersion":"v1beta1"}`
,
ModifiedIndex
:
2
,
ModifiedIndex
:
2
,
},
},
...
@@ -144,6 +159,8 @@ func TestExtractToListAcrossDirectories(t *testing.T) {
...
@@ -144,6 +159,8 @@ func TestExtractToListAcrossDirectories(t *testing.T) {
expect
:=
api
.
PodList
{
expect
:=
api
.
PodList
{
ListMeta
:
api
.
ListMeta
{
ResourceVersion
:
"10"
},
ListMeta
:
api
.
ListMeta
{
ResourceVersion
:
"10"
},
Items
:
[]
api
.
Pod
{
Items
:
[]
api
.
Pod
{
// We expect list to be sorted by directory (e.g. namespace) first, then by name.
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"baz"
,
ResourceVersion
:
"1"
}},
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"foo"
,
ResourceVersion
:
"1"
}},
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"foo"
,
ResourceVersion
:
"1"
}},
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"bar"
,
ResourceVersion
:
"2"
}},
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"bar"
,
ResourceVersion
:
"2"
}},
},
},
...
@@ -166,20 +183,25 @@ func TestExtractToListExcludesDirectories(t *testing.T) {
...
@@ -166,20 +183,25 @@ func TestExtractToListExcludesDirectories(t *testing.T) {
R
:
&
etcd
.
Response
{
R
:
&
etcd
.
Response
{
EtcdIndex
:
10
,
EtcdIndex
:
10
,
Node
:
&
etcd
.
Node
{
Node
:
&
etcd
.
Node
{
Dir
:
true
,
Nodes
:
[]
*
etcd
.
Node
{
Nodes
:
[]
*
etcd
.
Node
{
{
{
Key
:
"/foo"
,
Value
:
`{"id":"foo","kind":"Pod","apiVersion":"v1beta1"}`
,
Value
:
`{"id":"foo","kind":"Pod","apiVersion":"v1beta1"}`
,
ModifiedIndex
:
1
,
ModifiedIndex
:
1
,
},
},
{
{
Key
:
"/bar"
,
Value
:
`{"id":"bar","kind":"Pod","apiVersion":"v1beta1"}`
,
Value
:
`{"id":"bar","kind":"Pod","apiVersion":"v1beta1"}`
,
ModifiedIndex
:
2
,
ModifiedIndex
:
2
,
},
},
{
{
Key
:
"/baz"
,
Value
:
`{"id":"baz","kind":"Pod","apiVersion":"v1beta1"}`
,
Value
:
`{"id":"baz","kind":"Pod","apiVersion":"v1beta1"}`
,
ModifiedIndex
:
3
,
ModifiedIndex
:
3
,
},
},
{
{
Key
:
"/directory"
,
Value
:
`{"name": "directory"}`
,
Value
:
`{"name": "directory"}`
,
Dir
:
true
,
Dir
:
true
,
},
},
...
@@ -190,9 +212,9 @@ func TestExtractToListExcludesDirectories(t *testing.T) {
...
@@ -190,9 +212,9 @@ func TestExtractToListExcludesDirectories(t *testing.T) {
expect
:=
api
.
PodList
{
expect
:=
api
.
PodList
{
ListMeta
:
api
.
ListMeta
{
ResourceVersion
:
"10"
},
ListMeta
:
api
.
ListMeta
{
ResourceVersion
:
"10"
},
Items
:
[]
api
.
Pod
{
Items
:
[]
api
.
Pod
{
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"foo"
,
ResourceVersion
:
"1"
}},
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"bar"
,
ResourceVersion
:
"2"
}},
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"bar"
,
ResourceVersion
:
"2"
}},
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"baz"
,
ResourceVersion
:
"3"
}},
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"baz"
,
ResourceVersion
:
"3"
}},
{
ObjectMeta
:
api
.
ObjectMeta
{
Name
:
"foo"
,
ResourceVersion
:
"1"
}},
},
},
}
}
...
...
pkg/tools/fake_etcd_client.go
View file @
d30da9a8
...
@@ -19,6 +19,7 @@ package tools
...
@@ -19,6 +19,7 @@ package tools
import
(
import
(
"errors"
"errors"
"fmt"
"fmt"
"sort"
"sync"
"sync"
"github.com/coreos/go-etcd/etcd"
"github.com/coreos/go-etcd/etcd"
...
@@ -132,9 +133,23 @@ func (f *FakeEtcdClient) Get(key string, sort, recursive bool) (*etcd.Response,
...
@@ -132,9 +133,23 @@ func (f *FakeEtcdClient) Get(key string, sort, recursive bool) (*etcd.Response,
return
&
etcd
.
Response
{},
EtcdErrorNotFound
return
&
etcd
.
Response
{},
EtcdErrorNotFound
}
}
f
.
t
.
Logf
(
"returning %v: %#v %#v"
,
key
,
result
.
R
,
result
.
E
)
f
.
t
.
Logf
(
"returning %v: %#v %#v"
,
key
,
result
.
R
,
result
.
E
)
// Sort response, note this will alter resutl.R.
if
result
.
R
.
Node
!=
nil
&&
result
.
R
.
Node
.
Nodes
!=
nil
&&
sort
{
f
.
sortResponse
(
result
.
R
.
Node
.
Nodes
)
}
return
result
.
R
,
result
.
E
return
result
.
R
,
result
.
E
}
}
func
(
f
*
FakeEtcdClient
)
sortResponse
(
nodes
etcd
.
Nodes
)
{
for
i
:=
range
nodes
{
if
nodes
[
i
]
.
Dir
{
f
.
sortResponse
(
nodes
[
i
]
.
Nodes
)
}
}
sort
.
Sort
(
nodes
)
}
func
(
f
*
FakeEtcdClient
)
nodeExists
(
key
string
)
bool
{
func
(
f
*
FakeEtcdClient
)
nodeExists
(
key
string
)
bool
{
result
,
ok
:=
f
.
Data
[
key
]
result
,
ok
:=
f
.
Data
[
key
]
return
ok
&&
result
.
R
!=
nil
&&
result
.
R
.
Node
!=
nil
&&
result
.
E
==
nil
return
ok
&&
result
.
R
!=
nil
&&
result
.
R
.
Node
!=
nil
&&
result
.
E
==
nil
...
...
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