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
449aade4
Commit
449aade4
authored
Dec 29, 2016
by
Wojciech Tyczynski
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implement runtime.Object <-> map[string]interface{} conversion
parent
b31cf72f
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
245 additions
and
24 deletions
+245
-24
BUILD
pkg/api/BUILD
+1
-0
unstructured_test.go
pkg/api/unstructured_test.go
+18
-24
converter.go
....io/apimachinery/pkg/conversion/unstructured/converter.go
+0
-0
converter_test.go
...pimachinery/pkg/conversion/unstructured/converter_test.go
+181
-0
doc.go
...rc/k8s.io/apimachinery/pkg/conversion/unstructured/doc.go
+19
-0
BUILD
vendor/BUILD
+26
-0
No files found.
pkg/api/BUILD
View file @
449aade4
...
...
@@ -93,6 +93,7 @@ go_test(
"//vendor:k8s.io/apimachinery/pkg/api/testing",
"//vendor:k8s.io/apimachinery/pkg/apis/meta/v1",
"//vendor:k8s.io/apimachinery/pkg/conversion",
"//vendor:k8s.io/apimachinery/pkg/conversion/unstructured",
"//vendor:k8s.io/apimachinery/pkg/runtime",
"//vendor:k8s.io/apimachinery/pkg/runtime/schema",
"//vendor:k8s.io/apimachinery/pkg/runtime/serializer/protobuf",
...
...
pkg/api/unstructured_test.go
View file @
449aade4
...
...
@@ -28,6 +28,8 @@ import (
kapitesting
"k8s.io/kubernetes/pkg/api/testing"
"k8s.io/kubernetes/pkg/api/v1"
"k8s.io/apimachinery/pkg/conversion/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/diff"
"k8s.io/apimachinery/pkg/util/json"
...
...
@@ -95,27 +97,23 @@ func doRoundTrip(t *testing.T, group testapi.TestGroup, kind string) {
return
}
// TODO; Enable the following part of test once to/from unstructured
// format conversions are implemented.
/*
newUnstr := make(map[string]interface{})
err = unstructured.NewConverter().ToUnstructured(item, &newUnstr)
if err != nil {
t.Errorf("ToUnstructured failed: %v", err)
return
}
newUnstr
:=
make
(
map
[
string
]
interface
{})
err
=
unstructured
.
NewConverter
()
.
ToUnstructured
(
item
,
&
newUnstr
)
if
err
!=
nil
{
t
.
Errorf
(
"ToUnstructured failed: %v"
,
err
)
return
}
newObj := reflect.New(reflect.TypeOf(item).Elem()).Interface().(runtime.Object)
err = unstructured.NewConverter().FromUnstructured(newUnstr, newObj)
if err != nil {
t.Errorf("FromUnstructured failed: %v", err)
return
}
newObj
:=
reflect
.
New
(
reflect
.
TypeOf
(
item
)
.
Elem
())
.
Interface
()
.
(
runtime
.
Object
)
err
=
unstructured
.
NewConverter
()
.
FromUnstructured
(
newUnstr
,
newObj
)
if
err
!=
nil
{
t
.
Errorf
(
"FromUnstructured failed: %v"
,
err
)
return
}
if !apiequality.Semantic.DeepEqual(item, newObj) {
t.Errorf("Object changed, diff: %v", diff.ObjectReflectDiff(item, newObj))
}
*/
if
!
apiequality
.
Semantic
.
DeepEqual
(
item
,
newObj
)
{
t
.
Errorf
(
"Object changed, diff: %v"
,
diff
.
ObjectReflectDiff
(
item
,
newObj
))
}
}
func
TestRoundTrip
(
t
*
testing
.
T
)
{
...
...
@@ -135,11 +133,8 @@ func TestRoundTrip(t *testing.T) {
}
}
// TODO; Enable the following benchmark once to/from unstructured
// format conversions are implemented.
/*
func
BenchmarkToFromUnstructured
(
b
*
testing
.
B
)
{
items := benchmarkItems()
items
:=
benchmarkItems
(
b
)
size
:=
len
(
items
)
b
.
ResetTimer
()
for
i
:=
0
;
i
<
b
.
N
;
i
++
{
...
...
@@ -154,7 +149,6 @@ func BenchmarkToFromUnstructured(b *testing.B) {
}
b
.
StopTimer
()
}
*/
func
BenchmarkToFromUnstructuredViaJSON
(
b
*
testing
.
B
)
{
items
:=
benchmarkItems
(
b
)
...
...
staging/src/k8s.io/apimachinery/pkg/conversion/unstructured/converter.go
0 → 100644
View file @
449aade4
This diff is collapsed.
Click to expand it.
staging/src/k8s.io/apimachinery/pkg/conversion/unstructured/converter_test.go
0 → 100644
View file @
449aade4
/*
Copyright 2015 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package
unstructured
import
(
"reflect"
"testing"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/diff"
"k8s.io/apimachinery/pkg/util/json"
)
// Definte a number of test types.
type
A
struct
{
A
int
`json:"aa,omitempty"`
B
string
`json:"ab,omitempty"`
C
bool
`json:"ac,omitempty"`
}
type
B
struct
{
A
A
`json:"ba"`
B
string
`json:"bb"`
C
map
[
string
]
string
`json:"bc"`
D
[]
string
`json:"bd"`
}
type
C
struct
{
A
[]
A
`json:"ca"`
B
B
`json:",inline"`
C
string
`json:"cc"`
D
*
int64
`json:"cd"`
E
map
[
string
]
int
`json:"ce"`
F
[]
bool
`json:"cf"`
G
[]
int
`json"cg"`
H
float32
`json:ch"`
I
[]
interface
{}
`json:"ci"`
}
// C needs to implement runtime.Object to make it usable for tests.
func
(
c
*
C
)
GetObjectKind
()
schema
.
ObjectKind
{
return
schema
.
EmptyObjectKind
}
func
doRoundTrip
(
t
*
testing
.
T
,
item
runtime
.
Object
)
{
data
,
err
:=
json
.
Marshal
(
item
)
if
err
!=
nil
{
t
.
Errorf
(
"Error when marshaling object: %v"
,
err
)
return
}
unstr
:=
make
(
map
[
string
]
interface
{})
err
=
json
.
Unmarshal
(
data
,
&
unstr
)
if
err
!=
nil
{
t
.
Errorf
(
"Error when unmarshaling to unstructured: %v"
,
err
)
return
}
data
,
err
=
json
.
Marshal
(
unstr
)
if
err
!=
nil
{
t
.
Errorf
(
"Error when marshaling unstructured: %v"
,
err
)
return
}
unmarshalledObj
:=
reflect
.
New
(
reflect
.
TypeOf
(
item
)
.
Elem
())
.
Interface
()
err
=
json
.
Unmarshal
(
data
,
&
unmarshalledObj
)
if
err
!=
nil
{
t
.
Errorf
(
"Error when unmarshaling to object: %v"
,
err
)
return
}
if
!
reflect
.
DeepEqual
(
item
,
unmarshalledObj
)
{
t
.
Errorf
(
"Object changed during JSON operations, diff: %v"
,
diff
.
ObjectReflectDiff
(
item
,
unmarshalledObj
))
return
}
newUnstr
:=
make
(
map
[
string
]
interface
{})
err
=
NewConverter
()
.
ToUnstructured
(
item
,
&
newUnstr
)
if
err
!=
nil
{
t
.
Errorf
(
"ToUnstructured failed: %v"
,
err
)
return
}
newObj
:=
reflect
.
New
(
reflect
.
TypeOf
(
item
)
.
Elem
())
.
Interface
()
.
(
runtime
.
Object
)
err
=
NewConverter
()
.
FromUnstructured
(
newUnstr
,
newObj
)
if
err
!=
nil
{
t
.
Errorf
(
"FromUnstructured failed: %v"
,
err
)
return
}
if
!
reflect
.
DeepEqual
(
item
,
newObj
)
{
t
.
Errorf
(
"Object changed, diff: %v"
,
diff
.
ObjectReflectDiff
(
item
,
newObj
))
}
}
func
TestRoundTrip
(
t
*
testing
.
T
)
{
intVal
:=
int64
(
42
)
testCases
:=
[]
struct
{
obj
runtime
.
Object
}{
{
// This (among others) tests nil map, slice and pointer.
obj
:
&
C
{
C
:
"ccc"
,
},
},
{
// This (among others) tests empty map and slice.
obj
:
&
C
{
A
:
[]
A
{},
C
:
"ccc"
,
E
:
map
[
string
]
int
{},
I
:
[]
interface
{}{},
},
},
{
obj
:
&
C
{
A
:
[]
A
{
{
A
:
1
,
B
:
"11"
,
C
:
true
,
},
{
A
:
2
,
B
:
"22"
,
C
:
false
,
},
},
B
:
B
{
A
:
A
{
A
:
3
,
B
:
"33"
,
},
B
:
"bbb"
,
C
:
map
[
string
]
string
{
"k1"
:
"v1"
,
"k2"
:
"v2"
,
},
D
:
[]
string
{
"s1"
,
"s2"
},
},
C
:
"ccc"
,
D
:
&
intVal
,
E
:
map
[
string
]
int
{
"k1"
:
1
,
"k2"
:
2
,
},
F
:
[]
bool
{
true
,
false
,
false
},
G
:
[]
int
{
1
,
2
,
5
},
H
:
3.3
,
I
:
[]
interface
{}{
nil
,
nil
,
nil
},
},
},
{
// Test slice of interface{} with empty slices.
obj
:
&
C
{
I
:
[]
interface
{}{[]
interface
{}{},
[]
interface
{}{}},
},
},
}
for
i
:=
range
testCases
{
doRoundTrip
(
t
,
testCases
[
i
]
.
obj
)
if
t
.
Failed
()
{
break
}
}
}
staging/src/k8s.io/apimachinery/pkg/conversion/unstructured/doc.go
0 → 100644
View file @
449aade4
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Package unstructured provides conversion from runtime objects
// to map[string]interface{} representation.
package
unstructured
// import "k8s.io/apimachinery/pkg/conversion/unstructured"
vendor/BUILD
View file @
449aade4
...
...
@@ -15414,3 +15414,29 @@ go_library(
"//vendor:k8s.io/sample-apiserver/pkg/apis/wardle",
],
)
go_test(
name = "k8s.io/apimachinery/pkg/conversion/unstructured_test",
srcs = ["k8s.io/apimachinery/pkg/conversion/unstructured/converter_test.go"],
library = ":k8s.io/apimachinery/pkg/conversion/unstructured",
tags = ["automanaged"],
deps = [
"//vendor:k8s.io/apimachinery/pkg/runtime",
"//vendor:k8s.io/apimachinery/pkg/runtime/schema",
"//vendor:k8s.io/apimachinery/pkg/util/diff",
"//vendor:k8s.io/apimachinery/pkg/util/json",
],
)
go_library(
name = "k8s.io/apimachinery/pkg/conversion/unstructured",
srcs = [
"k8s.io/apimachinery/pkg/conversion/unstructured/converter.go",
"k8s.io/apimachinery/pkg/conversion/unstructured/doc.go",
],
tags = ["automanaged"],
deps = [
"//vendor:k8s.io/apimachinery/pkg/runtime",
"//vendor:k8s.io/apimachinery/pkg/util/json",
],
)
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