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
89266588
Unverified
Commit
89266588
authored
Jan 22, 2017
by
Jordan Liggitt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
label: use unstructured objects
parent
9b60d7fd
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
24 additions
and
20 deletions
+24
-20
label.go
pkg/kubectl/cmd/label.go
+15
-11
label_test.go
pkg/kubectl/cmd/label_test.go
+9
-9
No files found.
pkg/kubectl/cmd/label.go
View file @
89266588
...
@@ -17,21 +17,24 @@ limitations under the License.
...
@@ -17,21 +17,24 @@ limitations under the License.
package
cmd
package
cmd
import
(
import
(
"encoding/json"
"fmt"
"fmt"
"io"
"io"
"reflect"
"reflect"
"strings"
"strings"
jsonpatch
"github.com/evanphx/json-patch"
"github.com/golang/glog"
"github.com/golang/glog"
"github.com/spf13/cobra"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/api/meta"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
metav1
"k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/types"
utilerrors
"k8s.io/apimachinery/pkg/util/errors"
utilerrors
"k8s.io/apimachinery/pkg/util/errors"
"k8s.io/apimachinery/pkg/util/
strategicpatch
"
"k8s.io/apimachinery/pkg/util/
json
"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/validation"
"k8s.io/kubernetes/pkg/kubectl"
"k8s.io/kubernetes/pkg/kubectl"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
cmdutil
"k8s.io/kubernetes/pkg/kubectl/cmd/util"
cmdutil
"k8s.io/kubernetes/pkg/kubectl/cmd/util"
...
@@ -175,8 +178,12 @@ func (o *LabelOptions) RunLabel(f cmdutil.Factory, cmd *cobra.Command) error {
...
@@ -175,8 +178,12 @@ func (o *LabelOptions) RunLabel(f cmdutil.Factory, cmd *cobra.Command) error {
}
}
changeCause
:=
f
.
Command
()
changeCause
:=
f
.
Command
()
mapper
,
typer
:=
f
.
Object
()
b
:=
resource
.
NewBuilder
(
mapper
,
typer
,
resource
.
ClientMapperFunc
(
f
.
ClientForMapping
),
f
.
Decoder
(
true
))
.
mapper
,
typer
,
err
:=
f
.
UnstructuredObject
()
if
err
!=
nil
{
return
err
}
b
:=
resource
.
NewBuilder
(
mapper
,
typer
,
resource
.
ClientMapperFunc
(
f
.
UnstructuredClientForMapping
),
unstructured
.
UnstructuredJSONScheme
)
.
ContinueOnError
()
.
ContinueOnError
()
.
NamespaceParam
(
cmdNamespace
)
.
DefaultNamespace
()
.
NamespaceParam
(
cmdNamespace
)
.
DefaultNamespace
()
.
FilenameParam
(
enforceNamespace
,
&
o
.
FilenameOptions
)
.
FilenameParam
(
enforceNamespace
,
&
o
.
FilenameOptions
)
.
...
@@ -213,10 +220,7 @@ func (o *LabelOptions) RunLabel(f cmdutil.Factory, cmd *cobra.Command) error {
...
@@ -213,10 +220,7 @@ func (o *LabelOptions) RunLabel(f cmdutil.Factory, cmd *cobra.Command) error {
}
}
outputObj
=
info
.
Object
outputObj
=
info
.
Object
}
else
{
}
else
{
obj
,
err
:=
cmdutil
.
MaybeConvertObject
(
info
.
Object
,
info
.
Mapping
.
GroupVersionKind
.
GroupVersion
(),
info
.
Mapping
)
obj
:=
info
.
Object
if
err
!=
nil
{
return
err
}
name
,
namespace
:=
info
.
Name
,
info
.
Namespace
name
,
namespace
:=
info
.
Name
,
info
.
Namespace
oldData
,
err
:=
json
.
Marshal
(
obj
)
oldData
,
err
:=
json
.
Marshal
(
obj
)
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -247,21 +251,21 @@ func (o *LabelOptions) RunLabel(f cmdutil.Factory, cmd *cobra.Command) error {
...
@@ -247,21 +251,21 @@ func (o *LabelOptions) RunLabel(f cmdutil.Factory, cmd *cobra.Command) error {
if
!
reflect
.
DeepEqual
(
oldData
,
newData
)
{
if
!
reflect
.
DeepEqual
(
oldData
,
newData
)
{
dataChangeMsg
=
"labeled"
dataChangeMsg
=
"labeled"
}
}
patchBytes
,
err
:=
strategicpatch
.
CreateTwoWayMergePatch
(
oldData
,
newData
,
obj
)
patchBytes
,
err
:=
jsonpatch
.
CreateMergePatch
(
oldData
,
newData
)
createdPatch
:=
err
==
nil
createdPatch
:=
err
==
nil
if
err
!=
nil
{
if
err
!=
nil
{
glog
.
V
(
2
)
.
Infof
(
"couldn't compute patch: %v"
,
err
)
glog
.
V
(
2
)
.
Infof
(
"couldn't compute patch: %v"
,
err
)
}
}
mapping
:=
info
.
ResourceMapping
()
mapping
:=
info
.
ResourceMapping
()
client
,
err
:=
f
.
ClientForMapping
(
mapping
)
client
,
err
:=
f
.
Unstructured
ClientForMapping
(
mapping
)
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
helper
:=
resource
.
NewHelper
(
client
,
mapping
)
helper
:=
resource
.
NewHelper
(
client
,
mapping
)
if
createdPatch
{
if
createdPatch
{
outputObj
,
err
=
helper
.
Patch
(
namespace
,
name
,
types
.
Strategic
MergePatchType
,
patchBytes
)
outputObj
,
err
=
helper
.
Patch
(
namespace
,
name
,
types
.
MergePatchType
,
patchBytes
)
}
else
{
}
else
{
outputObj
,
err
=
helper
.
Replace
(
namespace
,
name
,
false
,
obj
)
outputObj
,
err
=
helper
.
Replace
(
namespace
,
name
,
false
,
obj
)
}
}
...
...
pkg/kubectl/cmd/label_test.go
View file @
89266588
...
@@ -347,10 +347,10 @@ func TestLabelErrors(t *testing.T) {
...
@@ -347,10 +347,10 @@ func TestLabelErrors(t *testing.T) {
func
TestLabelForResourceFromFile
(
t
*
testing
.
T
)
{
func
TestLabelForResourceFromFile
(
t
*
testing
.
T
)
{
pods
,
_
,
_
:=
testData
()
pods
,
_
,
_
:=
testData
()
f
,
tf
,
codec
,
ns
:=
cmdtesting
.
NewAPIFactory
()
f
,
tf
,
codec
,
_
:=
cmdtesting
.
NewAPIFactory
()
tf
.
Client
=
&
fake
.
RESTClient
{
tf
.
Unstructured
Client
=
&
fake
.
RESTClient
{
APIRegistry
:
api
.
Registry
,
APIRegistry
:
api
.
Registry
,
NegotiatedSerializer
:
ns
,
NegotiatedSerializer
:
unstructuredSerializer
,
Client
:
fake
.
CreateHTTPClient
(
func
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
Client
:
fake
.
CreateHTTPClient
(
func
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
switch
req
.
Method
{
switch
req
.
Method
{
case
"GET"
:
case
"GET"
:
...
@@ -398,10 +398,10 @@ func TestLabelForResourceFromFile(t *testing.T) {
...
@@ -398,10 +398,10 @@ func TestLabelForResourceFromFile(t *testing.T) {
}
}
func
TestLabelLocal
(
t
*
testing
.
T
)
{
func
TestLabelLocal
(
t
*
testing
.
T
)
{
f
,
tf
,
_
,
ns
:=
cmdtesting
.
NewAPIFactory
()
f
,
tf
,
_
,
_
:=
cmdtesting
.
NewAPIFactory
()
tf
.
Client
=
&
fake
.
RESTClient
{
tf
.
Unstructured
Client
=
&
fake
.
RESTClient
{
APIRegistry
:
api
.
Registry
,
APIRegistry
:
api
.
Registry
,
NegotiatedSerializer
:
ns
,
NegotiatedSerializer
:
unstructuredSerializer
,
Client
:
fake
.
CreateHTTPClient
(
func
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
Client
:
fake
.
CreateHTTPClient
(
func
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
t
.
Fatalf
(
"unexpected request: %s %#v
\n
%#v"
,
req
.
Method
,
req
.
URL
,
req
)
t
.
Fatalf
(
"unexpected request: %s %#v
\n
%#v"
,
req
.
Method
,
req
.
URL
,
req
)
return
nil
,
nil
return
nil
,
nil
...
@@ -432,10 +432,10 @@ func TestLabelLocal(t *testing.T) {
...
@@ -432,10 +432,10 @@ func TestLabelLocal(t *testing.T) {
func
TestLabelMultipleObjects
(
t
*
testing
.
T
)
{
func
TestLabelMultipleObjects
(
t
*
testing
.
T
)
{
pods
,
_
,
_
:=
testData
()
pods
,
_
,
_
:=
testData
()
f
,
tf
,
codec
,
ns
:=
cmdtesting
.
NewAPIFactory
()
f
,
tf
,
codec
,
_
:=
cmdtesting
.
NewAPIFactory
()
tf
.
Client
=
&
fake
.
RESTClient
{
tf
.
Unstructured
Client
=
&
fake
.
RESTClient
{
APIRegistry
:
api
.
Registry
,
APIRegistry
:
api
.
Registry
,
NegotiatedSerializer
:
ns
,
NegotiatedSerializer
:
unstructuredSerializer
,
Client
:
fake
.
CreateHTTPClient
(
func
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
Client
:
fake
.
CreateHTTPClient
(
func
(
req
*
http
.
Request
)
(
*
http
.
Response
,
error
)
{
switch
req
.
Method
{
switch
req
.
Method
{
case
"GET"
:
case
"GET"
:
...
...
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