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
1b181224
Commit
1b181224
authored
May 03, 2018
by
Cao Shufeng
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix status subresource
This change make the codes consistent with the document. Fixes:
https://github.com/kubernetes/kubernetes/issues/63359
parent
444881d0
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
163 additions
and
19 deletions
+163
-19
BUILD
...apiextensions-apiserver/pkg/registry/customresource/BUILD
+10
-0
status_strategy.go
...-apiserver/pkg/registry/customresource/status_strategy.go
+15
-19
status_strategy_test.go
...erver/pkg/registry/customresource/status_strategy_test.go
+138
-0
No files found.
staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresource/BUILD
View file @
1b181224
...
...
@@ -85,3 +85,13 @@ go_test(
"//vendor/k8s.io/client-go/discovery:go_default_library",
],
)
go_test(
name = "go_default_test",
srcs = ["status_strategy_test.go"],
embed = [":go_default_library"],
deps = [
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1/unstructured:go_default_library",
"//vendor/k8s.io/apiserver/pkg/endpoints/request:go_default_library",
],
)
staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresource/status_strategy.go
View file @
1b181224
...
...
@@ -32,28 +32,24 @@ func NewStatusStrategy(strategy customResourceStrategy) statusStrategy {
}
func
(
a
statusStrategy
)
PrepareForUpdate
(
ctx
genericapirequest
.
Context
,
obj
,
old
runtime
.
Object
)
{
// update is only allowed to set status
newCustomResourceObject
:=
obj
.
(
*
unstructured
.
Unstructured
)
oldCustomResourceObject
:=
old
.
(
*
unstructured
.
Unstructured
)
newCustomResource
:=
newCustomResourceObject
.
UnstructuredContent
()
oldCustomResource
:=
oldCustomResourceObject
.
UnstructuredContent
()
// update is not allowed to set spec and metadata
_
,
ok1
:=
newCustomResource
[
"spec"
]
_
,
ok2
:=
oldCustomResource
[
"spec"
]
switch
{
case
ok2
:
newCustomResource
[
"spec"
]
=
oldCustomResource
[
"spec"
]
case
ok1
:
delete
(
newCustomResource
,
"spec"
)
}
status
,
ok
:=
newCustomResource
[
"status"
]
newCustomResourceObject
.
SetAnnotations
(
oldCustomResourceObject
.
GetAnnotations
())
newCustomResourceObject
.
SetFinalizers
(
oldCustomResourceObject
.
GetFinalizers
())
newCustomResourceObject
.
SetGeneration
(
oldCustomResourceObject
.
GetGeneration
())
newCustomResourceObject
.
SetLabels
(
oldCustomResourceObject
.
GetLabels
())
newCustomResourceObject
.
SetOwnerReferences
(
oldCustomResourceObject
.
GetOwnerReferences
())
newCustomResourceObject
.
SetSelfLink
(
oldCustomResourceObject
.
GetSelfLink
())
// copy old object into new object
oldCustomResourceObject
:=
old
.
(
*
unstructured
.
Unstructured
)
// overridding the resourceVersion in metadata is safe here, we have already checked that
// new object and old object have the same resourceVersion.
*
newCustomResourceObject
=
*
oldCustomResourceObject
.
DeepCopy
()
// set status
newCustomResource
=
newCustomResourceObject
.
UnstructuredContent
()
if
ok
{
newCustomResource
[
"status"
]
=
status
}
else
{
delete
(
newCustomResource
,
"status"
)
}
}
// ValidateUpdate is the default update validation for an end user updating status.
...
...
staging/src/k8s.io/apiextensions-apiserver/pkg/registry/customresource/status_strategy_test.go
0 → 100644
View file @
1b181224
/*
Copyright 2018 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
customresource
import
(
"reflect"
"testing"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
genericapirequest
"k8s.io/apiserver/pkg/endpoints/request"
)
func
TestPrepareForUpdate
(
t
*
testing
.
T
)
{
strategy
:=
statusStrategy
{}
tcs
:=
[]
struct
{
old
*
unstructured
.
Unstructured
obj
*
unstructured
.
Unstructured
expected
*
unstructured
.
Unstructured
}{
{
// changes to spec are ignored
old
:
&
unstructured
.
Unstructured
{
Object
:
map
[
string
]
interface
{}{
"spec"
:
"old"
,
},
},
obj
:
&
unstructured
.
Unstructured
{
Object
:
map
[
string
]
interface
{}{
"spec"
:
"new"
,
},
},
expected
:
&
unstructured
.
Unstructured
{
Object
:
map
[
string
]
interface
{}{
"spec"
:
"old"
,
},
},
},
{
// changes to other places are also ignored
old
:
&
unstructured
.
Unstructured
{
Object
:
map
[
string
]
interface
{}{
"spec"
:
"old"
,
},
},
obj
:
&
unstructured
.
Unstructured
{
Object
:
map
[
string
]
interface
{}{
"new"
:
"new"
,
},
},
expected
:
&
unstructured
.
Unstructured
{
Object
:
map
[
string
]
interface
{}{
"spec"
:
"old"
,
},
},
},
{
// delete status
old
:
&
unstructured
.
Unstructured
{
Object
:
map
[
string
]
interface
{}{
"spec"
:
"old"
,
"status"
:
"old"
,
},
},
obj
:
&
unstructured
.
Unstructured
{
Object
:
map
[
string
]
interface
{}{
"spec"
:
"old"
,
},
},
expected
:
&
unstructured
.
Unstructured
{
Object
:
map
[
string
]
interface
{}{
"spec"
:
"old"
,
},
},
},
{
// update status
old
:
&
unstructured
.
Unstructured
{
Object
:
map
[
string
]
interface
{}{
"spec"
:
"old"
,
"status"
:
"old"
,
},
},
obj
:
&
unstructured
.
Unstructured
{
Object
:
map
[
string
]
interface
{}{
"status"
:
"new"
,
},
},
expected
:
&
unstructured
.
Unstructured
{
Object
:
map
[
string
]
interface
{}{
"spec"
:
"old"
,
"status"
:
"new"
,
},
},
},
{
// update status and other parts
old
:
&
unstructured
.
Unstructured
{
Object
:
map
[
string
]
interface
{}{
"spec"
:
"old"
,
"status"
:
"old"
,
},
},
obj
:
&
unstructured
.
Unstructured
{
Object
:
map
[
string
]
interface
{}{
"spec"
:
"new"
,
"new"
:
"new"
,
"status"
:
"new"
,
},
},
expected
:
&
unstructured
.
Unstructured
{
Object
:
map
[
string
]
interface
{}{
"spec"
:
"old"
,
"status"
:
"new"
,
},
},
},
}
for
index
,
tc
:=
range
tcs
{
strategy
.
PrepareForUpdate
(
genericapirequest
.
NewContext
(),
tc
.
obj
,
tc
.
old
)
if
!
reflect
.
DeepEqual
(
tc
.
obj
,
tc
.
expected
)
{
t
.
Errorf
(
"test %d failed: expected: %v, got %v"
,
index
,
tc
.
expected
,
tc
.
obj
)
}
}
}
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