Skip to content

Commit 10f939a

Browse files
Andy Goldsteinsttts
Andy Goldstein
authored andcommitted
REVIEW: adapt: pkg/cmd/util/clientcmd
Make sure we convert to v1 for List in PrintResourceInfos.
1 parent ec75747 commit 10f939a

File tree

3 files changed

+38
-85
lines changed

3 files changed

+38
-85
lines changed

pkg/cmd/util/clientcmd/factory.go

+19-14
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func NewFactory(optionalClientConfig kclientcmd.ClientConfig) *Factory {
6767
// PrintResourceInfos receives a list of resource infos and prints versioned objects if a generic output format was specified
6868
// otherwise, it iterates through info objects, printing each resource with a unique printer for its mapping
6969
func (f *Factory) PrintResourceInfos(cmd *cobra.Command, infos []*resource.Info, out io.Writer) error {
70-
printer, generic, err := kcmdutil.PrinterForCommand(cmd)
70+
printer, generic, err := f.PrinterForCommand(cmd)
7171
if err != nil {
7272
return nil
7373
}
@@ -81,24 +81,29 @@ func (f *Factory) PrintResourceInfos(cmd *cobra.Command, infos []*resource.Info,
8181
if err := printer.PrintObj(info.Object, out); err != nil {
8282
return nil
8383
}
84+
return nil
8485
}
85-
return nil
8686
}
8787

88-
clientConfig, err := f.ClientConfig()
89-
if err != nil {
90-
return err
91-
}
92-
outputVersion, err := kcmdutil.OutputVersion(cmd, clientConfig.GroupVersion)
93-
if err != nil {
94-
return err
88+
printAsList := len(infos) != 1
89+
90+
// If we're not printing multiple objects, we'll pass an empty GroupVersion to AsVersionedObject
91+
// because we shouldn't need to do any conversion.
92+
var gv schema.GroupVersion
93+
94+
if printAsList {
95+
// If we're printing a list of objects, we do need to convert at least the outer List container
96+
// from internal to versioned, so we need to set gv appropriately.
97+
// TODO is there a better way to do this?
98+
gv = schema.GroupVersion{Group: "", Version: "v1"}
9599
}
96-
object, err := resource.AsVersionedObject(infos, len(infos) != 1, outputVersion, api.Codecs.LegacyCodec(outputVersion))
100+
101+
object, err := resource.AsVersionedObject(infos, printAsList, gv, api.Codecs.LegacyCodec())
97102
if err != nil {
98103
return err
99104
}
100-
return printer.PrintObj(object, out)
101105

106+
return printer.PrintObj(object, out)
102107
}
103108

104109
// FlagBinder represents an interface that allows to bind extra flags into commands.
@@ -382,12 +387,12 @@ func FindAllCanonicalResources(d discovery.DiscoveryInterface, m meta.RESTMapper
382387
if err != nil {
383388
return nil, err
384389
}
385-
for apiVersion, v := range all {
386-
gv, err := schema.ParseGroupVersion(apiVersion)
390+
for _, serverResource := range all {
391+
gv, err := schema.ParseGroupVersion(serverResource.GroupVersion)
387392
if err != nil {
388393
continue
389394
}
390-
for _, r := range v.APIResources {
395+
for _, r := range serverResource.APIResources {
391396
// ignore subresources
392397
if strings.Contains(r.Name, "/") {
393398
continue

pkg/cmd/util/clientcmd/factory_client_access.go

+19-28
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
kapi "k8s.io/kubernetes/pkg/api"
3030
kclientset "k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
3131
"k8s.io/kubernetes/pkg/kubectl"
32-
"k8s.io/kubernetes/pkg/kubectl/cmd/set"
3332
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
3433
"k8s.io/kubernetes/pkg/kubectl/resource"
3534
kprinters "k8s.io/kubernetes/pkg/printers"
@@ -161,6 +160,10 @@ func (f *ring0Factory) ClientConfig() (*restclient.Config, error) {
161160
return f.kubeClientAccessFactory.ClientConfig()
162161
}
163162

163+
func (f *ring0Factory) BareClientConfig() (*restclient.Config, error) {
164+
return f.clientConfig.ClientConfig()
165+
}
166+
164167
func (f *ring0Factory) ClientConfigForVersion(requiredVersion *schema.GroupVersion) (*restclient.Config, error) {
165168
return f.kubeClientAccessFactory.ClientConfigForVersion(nil)
166169
}
@@ -234,8 +237,8 @@ func (f *ring0Factory) FlagSet() *pflag.FlagSet {
234237
return f.kubeClientAccessFactory.FlagSet()
235238
}
236239

237-
func (f *ring0Factory) Command() string {
238-
return f.kubeClientAccessFactory.Command()
240+
func (f *ring0Factory) Command(cmd *cobra.Command, showSecrets bool) string {
241+
return f.kubeClientAccessFactory.Command(cmd, showSecrets)
239242
}
240243

241244
func (f *ring0Factory) BindFlags(flags *pflag.FlagSet) {
@@ -267,25 +270,19 @@ func (f *ring0Factory) Printer(mapping *meta.RESTMapping, options kprinters.Prin
267270
options.Kind = alias
268271
}
269272
}
270-
return describe.NewHumanReadablePrinter(options), nil
273+
return describe.NewHumanReadablePrinter(f.JSONEncoder(), f.Decoder(true), options), nil
271274
}
272275

273-
func (f *ring0Factory) Pauser(info *resource.Info) (bool, error) {
276+
func (f *ring0Factory) Pauser(info *resource.Info) ([]byte, error) {
277+
// TODO(rebase): compare with origin master, the logic changed completely
274278
switch t := info.Object.(type) {
275279
case *deployapi.DeploymentConfig:
276-
patches := set.CalculatePatches([]*resource.Info{info}, f.JSONEncoder(), func(*resource.Info) (bool, error) {
277-
if t.Spec.Paused {
278-
return false, nil
279-
}
280-
t.Spec.Paused = true
281-
return true, nil
282-
})
283-
if len(patches) == 0 {
284-
return true, nil
280+
if t.Spec.Paused {
281+
return nil, errors.New("is already paused")
285282
}
286-
_, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, kapi.StrategicMergePatchType, patches[0].Patch)
283+
t.Spec.Paused = true
287284
// TODO: Pause the deployer containers.
288-
return false, err
285+
return runtime.Encode(f.JSONEncoder(), info.Object)
289286
default:
290287
return f.kubeClientAccessFactory.Pauser(info)
291288
}
@@ -331,22 +328,16 @@ func (f *ring0Factory) ResolveImage(image string) (string, error) {
331328
return imageutil.ResolveImagePullSpec(oc, oc, options.Source, image, namespace)
332329
}
333330

334-
func (f *ring0Factory) Resumer(info *resource.Info) (bool, error) {
331+
func (f *ring0Factory) Resumer(info *resource.Info) ([]byte, error) {
332+
// TODO(rebase): compare with origin master, the logic changed completely
335333
switch t := info.Object.(type) {
336334
case *deployapi.DeploymentConfig:
337-
patches := set.CalculatePatches([]*resource.Info{info}, f.JSONEncoder(), func(*resource.Info) (bool, error) {
338-
if !t.Spec.Paused {
339-
return false, nil
340-
}
341-
t.Spec.Paused = false
342-
return true, nil
343-
})
344-
if len(patches) == 0 {
345-
return true, nil
335+
if !t.Spec.Paused {
336+
return nil, errors.New("is not paused")
346337
}
347-
_, err := resource.NewHelper(info.Client, info.Mapping).Patch(info.Namespace, info.Name, kapi.StrategicMergePatchType, patches[0].Patch)
338+
t.Spec.Paused = false
348339
// TODO: Resume the deployer containers.
349-
return false, err
340+
return runtime.Encode(f.JSONEncoder(), info.Object)
350341
default:
351342
return f.kubeClientAccessFactory.Resumer(info)
352343
}

pkg/cmd/util/clientcmd/factory_object_mapping.go

-43
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99

1010
"github.com/blang/semver"
1111
"github.com/emicklei/go-restful/swagger"
12-
"github.com/spf13/cobra"
1312

1413
"k8s.io/apimachinery/pkg/api/meta"
1514
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -21,7 +20,6 @@ import (
2120
kapi "k8s.io/kubernetes/pkg/api"
2221
"k8s.io/kubernetes/pkg/api/v1"
2322
"k8s.io/kubernetes/pkg/api/validation"
24-
"k8s.io/kubernetes/pkg/client/typed/dynamic"
2523
"k8s.io/kubernetes/pkg/controller"
2624
"k8s.io/kubernetes/pkg/kubectl"
2725
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
@@ -295,47 +293,6 @@ func (f *ring1Factory) AttachablePodForObject(object runtime.Object) (*kapi.Pod,
295293
}
296294
}
297295

298-
// PrinterForMapping returns a printer suitable for displaying the provided resource type.
299-
// Requires that printer flags have been added to cmd (see AddPrinterFlags).
300-
func (f *ring1Factory) PrinterForMapping(cmd *cobra.Command, mapping *meta.RESTMapping, withNamespace bool) (kubectl.ResourcePrinter, error) {
301-
/*
302-
// TODO FIX ME. COPIED FROM KUBE AS PART OF THE COPY/PASTE FOR
303-
// PrinterForMapping
304-
if latest.OriginKind(mapping.GroupVersionKind) {
305-
printer, ok, err := kcmdutil.PrinterForCommand(cmd)
306-
if err != nil {
307-
return nil, err
308-
}
309-
if ok && mapping != nil {
310-
printer = kubectl.NewVersionedPrinter(printer, mapping.ObjectConvertor, mapping.GroupVersionKind.GroupVersion())
311-
} else {
312-
// Some callers do not have "label-columns" so we can't use the GetFlagStringSlice() helper
313-
columnLabel, err := cmd.Flags().GetStringSlice("label-columns")
314-
if err != nil {
315-
columnLabel = []string{}
316-
}
317-
printer, err = f.Printer(mapping, kubectl.PrintOptions{
318-
NoHeaders: kcmdutil.GetFlagBool(cmd, "no-headers"),
319-
WithNamespace: withNamespace,
320-
Wide: kcmdutil.GetWideFlag(cmd),
321-
ShowAll: kcmdutil.GetFlagBool(cmd, "show-all"),
322-
ShowLabels: kcmdutil.GetFlagBool(cmd, "show-labels"),
323-
AbsoluteTimestamps: isWatch(cmd),
324-
ColumnLabels: columnLabel,
325-
})
326-
if err != nil {
327-
return nil, err
328-
}
329-
printer = maybeWrapSortingPrinter(cmd, printer)
330-
}
331-
332-
return printer, nil
333-
}
334-
*/
335-
336-
return f.kubeObjectMappingFactory.PrinterForMapping(cmd, mapping, withNamespace)
337-
}
338-
339296
func (f *ring1Factory) Validator(validate bool, cacheDir string) (validation.Schema, error) {
340297
return f.kubeObjectMappingFactory.Validator(validate, cacheDir)
341298
}

0 commit comments

Comments
 (0)