Skip to content

Commit

Permalink
switch to versioned objects only for set
Browse files Browse the repository at this point in the history
  • Loading branch information
deads2k committed May 14, 2018
1 parent d5a930b commit 3af9e75
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 163 deletions.
4 changes: 2 additions & 2 deletions pkg/kubectl/cmd/set/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ go_library(
"//vendor/github.com/golang/glog:go_default_library",
"//vendor/github.com/spf13/cobra:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/api/rbac/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/errors:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/api/meta:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
Expand Down Expand Up @@ -63,9 +64,7 @@ go_test(
],
embed = [":go_default_library"],
deps = [
"//pkg/api/legacyscheme:go_default_library",
"//pkg/api/testapi:go_default_library",
"//pkg/apis/rbac:go_default_library",
"//pkg/kubectl/cmd/testing:go_default_library",
"//pkg/kubectl/cmd/util:go_default_library",
"//pkg/kubectl/genericclioptions:go_default_library",
Expand All @@ -80,6 +79,7 @@ go_test(
"//vendor/k8s.io/api/batch/v1:go_default_library",
"//vendor/k8s.io/api/core/v1:go_default_library",
"//vendor/k8s.io/api/extensions/v1beta1:go_default_library",
"//vendor/k8s.io/api/rbac/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
"//vendor/k8s.io/apimachinery/pkg/runtime/schema:go_default_library",
Expand Down
7 changes: 3 additions & 4 deletions pkg/kubectl/cmd/set/set_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,26 +414,25 @@ func (o *EnvOptions) RunEnv() error {
}

if o.Local || o.dryRun {
if err := o.PrintObj(patch.Info.Object, o.Out); err != nil {
if err := o.PrintObj(info.Object, o.Out); err != nil {
return err
}
continue
}

obj, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, types.StrategicMergePatchType, patch.Patch)
actual, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, types.StrategicMergePatchType, patch.Patch)
if err != nil {
allErrs = append(allErrs, fmt.Errorf("failed to patch env update to pod template: %v\n", err))
continue
}
info.Refresh(obj, true)

// make sure arguments to set or replace environment variables are set
// before returning a successful message
if len(env) == 0 && len(o.envArgs) == 0 {
return fmt.Errorf("at least one environment variable must be provided")
}

if err := o.PrintObj(info.Object, o.Out); err != nil {
if err := o.PrintObj(actual, o.Out); err != nil {
return err
}
}
Expand Down
27 changes: 11 additions & 16 deletions pkg/kubectl/cmd/set/set_env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import (
"k8s.io/apimachinery/pkg/runtime/serializer"
restclient "k8s.io/client-go/rest"
"k8s.io/client-go/rest/fake"
"k8s.io/kubernetes/pkg/api/legacyscheme"
"k8s.io/kubernetes/pkg/api/testapi"
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
Expand All @@ -50,10 +49,9 @@ func TestSetEnvLocal(t *testing.T) {
tf := cmdtesting.NewTestFactory()
defer tf.Cleanup()

ns := serializer.DirectCodecFactory{CodecFactory: scheme.Codecs}
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: ""},
NegotiatedSerializer: ns,
NegotiatedSerializer: serializer.DirectCodecFactory{CodecFactory: scheme.Codecs},
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
t.Fatalf("unexpected request: %s %#v\n%#v", req.Method, req.URL, req)
return nil, nil
Expand All @@ -66,8 +64,8 @@ func TestSetEnvLocal(t *testing.T) {
streams, _, buf, bufErr := genericclioptions.NewTestIOStreams()
opts := NewEnvOptions(streams)
opts.PrintFlags = &printers.PrintFlags{
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(legacyscheme.Scheme),
NamePrintFlags: printers.NewNamePrintFlags("", legacyscheme.Scheme),
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme.Scheme),
NamePrintFlags: printers.NewNamePrintFlags("", scheme.Scheme),
OutputFormat: &outputFormat,
}
opts.FilenameOptions = resource.FilenameOptions{
Expand All @@ -93,10 +91,9 @@ func TestSetMultiResourcesEnvLocal(t *testing.T) {
tf := cmdtesting.NewTestFactory()
defer tf.Cleanup()

ns := serializer.DirectCodecFactory{CodecFactory: scheme.Codecs}
tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: ""},
NegotiatedSerializer: ns,
NegotiatedSerializer: serializer.DirectCodecFactory{CodecFactory: scheme.Codecs},
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
t.Fatalf("unexpected request: %s %#v\n%#v", req.Method, req.URL, req)
return nil, nil
Expand All @@ -109,8 +106,8 @@ func TestSetMultiResourcesEnvLocal(t *testing.T) {
streams, _, buf, bufErr := genericclioptions.NewTestIOStreams()
opts := NewEnvOptions(streams)
opts.PrintFlags = &printers.PrintFlags{
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(legacyscheme.Scheme),
NamePrintFlags: printers.NewNamePrintFlags("", legacyscheme.Scheme),
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme.Scheme),
NamePrintFlags: printers.NewNamePrintFlags("", scheme.Scheme),
OutputFormat: &outputFormat,
}
opts.FilenameOptions = resource.FilenameOptions{
Expand Down Expand Up @@ -463,17 +460,15 @@ func TestSetEnvRemote(t *testing.T) {
tf.ClientConfigVal = &restclient.Config{ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Version: ""}}}
defer tf.Cleanup()

codec := scheme.Codecs.CodecForVersions(scheme.Codecs.LegacyCodec(groupVersion), scheme.Codecs.UniversalDecoder(groupVersion), groupVersion, groupVersion)
ns := serializer.DirectCodecFactory{CodecFactory: scheme.Codecs}
tf.Namespace = "test"
tf.Client = &fake.RESTClient{
GroupVersion: groupVersion,
NegotiatedSerializer: ns,
NegotiatedSerializer: serializer.DirectCodecFactory{CodecFactory: scheme.Codecs},
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
resourcePath := testapi.Default.ResourcePath(input.args[0]+"s", tf.Namespace, input.args[1])
switch p, m := req.URL.Path, req.Method; {
case p == resourcePath && m == http.MethodGet:
return &http.Response{StatusCode: http.StatusOK, Header: defaultHeader(), Body: objBody(codec, input.object)}, nil
return &http.Response{StatusCode: http.StatusOK, Header: defaultHeader(), Body: objBody(input.object)}, nil
case p == resourcePath && m == http.MethodPatch:
stream, err := req.GetBody()
if err != nil {
Expand All @@ -484,7 +479,7 @@ func TestSetEnvRemote(t *testing.T) {
return nil, err
}
assert.Contains(t, string(bytes), `"value":`+`"`+"prod"+`"`, fmt.Sprintf("env not updated for %#v", input.object))
return &http.Response{StatusCode: http.StatusOK, Header: defaultHeader(), Body: objBody(codec, input.object)}, nil
return &http.Response{StatusCode: http.StatusOK, Header: defaultHeader(), Body: objBody(input.object)}, nil
default:
t.Errorf("%s: unexpected request: %s %#v\n%#v", "image", req.Method, req.URL, req)
return nil, fmt.Errorf("unexpected request")
Expand All @@ -497,8 +492,8 @@ func TestSetEnvRemote(t *testing.T) {
streams := genericclioptions.NewTestIOStreamsDiscard()
opts := NewEnvOptions(streams)
opts.PrintFlags = &printers.PrintFlags{
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(legacyscheme.Scheme),
NamePrintFlags: printers.NewNamePrintFlags("", legacyscheme.Scheme),
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme.Scheme),
NamePrintFlags: printers.NewNamePrintFlags("", scheme.Scheme),
OutputFormat: &outputFormat,
}
opts.Local = false
Expand Down
7 changes: 3 additions & 4 deletions pkg/kubectl/cmd/set/set_image.go
Original file line number Diff line number Diff line change
Expand Up @@ -275,21 +275,20 @@ func (o *SetImageOptions) Run() error {
}

if o.Local || o.DryRun {
if err := o.PrintObj(patch.Info.Object, o.Out); err != nil {
if err := o.PrintObj(info.Object, o.Out); err != nil {
return err
}
continue
}

// patch the change
obj, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, types.StrategicMergePatchType, patch.Patch)
actual, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, types.StrategicMergePatchType, patch.Patch)
if err != nil {
allErrs = append(allErrs, fmt.Errorf("failed to patch image update to pod template: %v\n", err))
continue
}
info.Refresh(obj, true)

if err := o.PrintObj(info.Object, o.Out); err != nil {
if err := o.PrintObj(actual, o.Out); err != nil {
return err
}
}
Expand Down
33 changes: 13 additions & 20 deletions pkg/kubectl/cmd/set/set_image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"k8s.io/kubernetes/pkg/api/legacyscheme"

appsv1 "k8s.io/api/apps/v1"
appsv1beta1 "k8s.io/api/apps/v1beta1"
Expand All @@ -51,11 +50,9 @@ func TestImageLocal(t *testing.T) {
tf := cmdtesting.NewTestFactory()
defer tf.Cleanup()

ns := serializer.DirectCodecFactory{CodecFactory: scheme.Codecs}

tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: ""},
NegotiatedSerializer: ns,
NegotiatedSerializer: serializer.DirectCodecFactory{CodecFactory: scheme.Codecs},
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
t.Fatalf("unexpected request: %s %#v\n%#v", req.Method, req.URL, req)
return nil, nil
Expand All @@ -74,8 +71,8 @@ func TestImageLocal(t *testing.T) {

opts := SetImageOptions{
PrintFlags: &printers.PrintFlags{
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(legacyscheme.Scheme),
NamePrintFlags: printers.NewNamePrintFlags("", legacyscheme.Scheme),
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme.Scheme),
NamePrintFlags: printers.NewNamePrintFlags("", scheme.Scheme),

OutputFormat: &outputFormat,
},
Expand All @@ -101,8 +98,8 @@ func TestImageLocal(t *testing.T) {

func TestSetImageValidation(t *testing.T) {
printFlags := &printers.PrintFlags{
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(legacyscheme.Scheme),
NamePrintFlags: printers.NewNamePrintFlags("", legacyscheme.Scheme),
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme.Scheme),
NamePrintFlags: printers.NewNamePrintFlags("", scheme.Scheme),
}

testCases := []struct {
Expand Down Expand Up @@ -174,11 +171,9 @@ func TestSetMultiResourcesImageLocal(t *testing.T) {
tf := cmdtesting.NewTestFactory()
defer tf.Cleanup()

ns := serializer.DirectCodecFactory{CodecFactory: scheme.Codecs}

tf.Client = &fake.RESTClient{
GroupVersion: schema.GroupVersion{Version: ""},
NegotiatedSerializer: ns,
NegotiatedSerializer: serializer.DirectCodecFactory{CodecFactory: scheme.Codecs},
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
t.Fatalf("unexpected request: %s %#v\n%#v", req.Method, req.URL, req)
return nil, nil
Expand All @@ -197,8 +192,8 @@ func TestSetMultiResourcesImageLocal(t *testing.T) {

opts := SetImageOptions{
PrintFlags: &printers.PrintFlags{
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(legacyscheme.Scheme),
NamePrintFlags: printers.NewNamePrintFlags("", legacyscheme.Scheme),
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme.Scheme),
NamePrintFlags: printers.NewNamePrintFlags("", scheme.Scheme),

OutputFormat: &outputFormat,
},
Expand Down Expand Up @@ -554,17 +549,15 @@ func TestSetImageRemote(t *testing.T) {
tf := cmdtesting.NewTestFactory()
defer tf.Cleanup()

codec := scheme.Codecs.CodecForVersions(scheme.Codecs.LegacyCodec(groupVersion), scheme.Codecs.UniversalDecoder(groupVersion), groupVersion, groupVersion)
ns := serializer.DirectCodecFactory{CodecFactory: scheme.Codecs}
tf.Namespace = "test"
tf.Client = &fake.RESTClient{
GroupVersion: groupVersion,
NegotiatedSerializer: ns,
NegotiatedSerializer: serializer.DirectCodecFactory{CodecFactory: scheme.Codecs},
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
resourcePath := testapi.Default.ResourcePath(input.args[0]+"s", tf.Namespace, input.args[1])
switch p, m := req.URL.Path, req.Method; {
case p == resourcePath && m == http.MethodGet:
return &http.Response{StatusCode: http.StatusOK, Header: defaultHeader(), Body: objBody(codec, input.object)}, nil
return &http.Response{StatusCode: http.StatusOK, Header: defaultHeader(), Body: objBody(input.object)}, nil
case p == resourcePath && m == http.MethodPatch:
stream, err := req.GetBody()
if err != nil {
Expand All @@ -575,7 +568,7 @@ func TestSetImageRemote(t *testing.T) {
return nil, err
}
assert.Contains(t, string(bytes), `"image":`+`"`+"thingy"+`"`, fmt.Sprintf("image not updated for %#v", input.object))
return &http.Response{StatusCode: http.StatusOK, Header: defaultHeader(), Body: objBody(codec, input.object)}, nil
return &http.Response{StatusCode: http.StatusOK, Header: defaultHeader(), Body: objBody(input.object)}, nil
default:
t.Errorf("%s: unexpected request: %s %#v\n%#v", "image", req.Method, req.URL, req)
return nil, fmt.Errorf("unexpected request")
Expand All @@ -591,8 +584,8 @@ func TestSetImageRemote(t *testing.T) {
cmd.Flags().Set("output", outputFormat)
opts := SetImageOptions{
PrintFlags: &printers.PrintFlags{
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(legacyscheme.Scheme),
NamePrintFlags: printers.NewNamePrintFlags("", legacyscheme.Scheme),
JSONYamlPrintFlags: printers.NewJSONYamlPrintFlags(scheme.Scheme),
NamePrintFlags: printers.NewNamePrintFlags("", scheme.Scheme),

OutputFormat: &outputFormat,
},
Expand Down
15 changes: 7 additions & 8 deletions pkg/kubectl/cmd/set/set_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions"
"k8s.io/kubernetes/pkg/kubectl/genericclioptions/resource"
"k8s.io/kubernetes/pkg/kubectl/scheme"
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
)

Expand Down Expand Up @@ -173,7 +174,7 @@ func (o *SetResourcesOptions) Complete(f cmdutil.Factory, cmd *cobra.Command, ar

includeUninitialized := cmdutil.ShouldIncludeUninitialized(cmd, false)
builder := f.NewBuilder().
WithScheme(legacyscheme.Scheme).
WithScheme(scheme.Scheme, scheme.Scheme.PrioritizedVersionsAllGroups()...).
LocalParam(o.Local).
ContinueOnError().
NamespaceParam(cmdNamespace).DefaultNamespace().
Expand Down Expand Up @@ -222,9 +223,8 @@ func (o *SetResourcesOptions) Validate() error {

func (o *SetResourcesOptions) Run() error {
allErrs := []error{}
patches := CalculatePatches(o.Infos, cmdutil.InternalVersionJSONEncoder(), func(info *resource.Info) ([]byte, error) {
patches := CalculatePatches(o.Infos, scheme.DefaultJSONEncoder(), func(info *resource.Info) ([]byte, error) {
transformed := false
info.Object = cmdutil.AsDefaultVersionedOrOriginal(info.Object, info.Mapping)
_, err := o.UpdatePodSpecForObject(info.Object, func(spec *v1.PodSpec) error {
containers, _ := selectContainers(spec.Containers, o.ContainerSelector)
if len(containers) != 0 {
Expand Down Expand Up @@ -260,7 +260,7 @@ func (o *SetResourcesOptions) Run() error {
glog.V(4).Infof("error recording current command: %v", err)
}

return runtime.Encode(cmdutil.InternalVersionJSONEncoder(), info.Object)
return runtime.Encode(scheme.DefaultJSONEncoder(), info.Object)
})

for _, patch := range patches {
Expand All @@ -277,20 +277,19 @@ func (o *SetResourcesOptions) Run() error {
}

if o.Local || o.DryRun {
if err := o.PrintObj(cmdutil.AsDefaultVersionedOrOriginal(patch.Info.Object, patch.Info.Mapping), o.Out); err != nil {
if err := o.PrintObj(info.Object, o.Out); err != nil {
return err
}
continue
}

obj, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, types.StrategicMergePatchType, patch.Patch)
actual, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, types.StrategicMergePatchType, patch.Patch)
if err != nil {
allErrs = append(allErrs, fmt.Errorf("failed to patch limit update to pod template %v\n", err))
continue
}
info.Refresh(obj, true)

if err := o.PrintObj(cmdutil.AsDefaultVersionedOrOriginal(info.Object, info.Mapping), o.Out); err != nil {
if err := o.PrintObj(actual, o.Out); err != nil {
return err
}
}
Expand Down
Loading

0 comments on commit 3af9e75

Please sign in to comment.