mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-09 03:57:41 +00:00
add resource prefix to multiple items w/ same kind
This patch ensures that a resource prefix is added to multiple items of the same kind, when using `oc get all`. Before, a prefix was added only when a single item was returned on `oc get all`, but ignored if only a single resource kind existed but multiple items for that kind were returned.
This commit is contained in:
parent
7d611fe32b
commit
3484c6f676
@ -153,7 +153,6 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
printAll := false
|
||||
filterFuncs := f.DefaultResourceFilterFunc()
|
||||
filterOpts := f.DefaultResourceFilterOptions(cmd, allNamespaces)
|
||||
|
||||
@ -178,14 +177,6 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [
|
||||
return cmdutil.UsageError(cmd, usageString)
|
||||
}
|
||||
|
||||
// determine if args contains "all"
|
||||
for _, a := range args {
|
||||
if a == "all" {
|
||||
printAll = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// always show resources when getting by name or filename
|
||||
argsHasNames, err := resource.HasNames(args)
|
||||
if err != nil {
|
||||
@ -423,7 +414,7 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [
|
||||
w := kubectl.GetNewTabWriter(out)
|
||||
filteredResourceCount := 0
|
||||
|
||||
if cmdutil.MustPrintWithKinds(objs, infos, sorter, printAll) {
|
||||
if resource.MultipleTypesRequested(args) || cmdutil.MustPrintWithKinds(objs, infos, sorter) {
|
||||
showKind = true
|
||||
}
|
||||
|
||||
|
@ -629,13 +629,9 @@ func MaybeConvertObject(obj runtime.Object, gv schema.GroupVersion, converter ru
|
||||
// with multiple resource kinds, in which case it will
|
||||
// return true, indicating resource kind will be
|
||||
// included as part of printer output
|
||||
func MustPrintWithKinds(objs []runtime.Object, infos []*resource.Info, sorter *kubectl.RuntimeSort, printAll bool) bool {
|
||||
func MustPrintWithKinds(objs []runtime.Object, infos []*resource.Info, sorter *kubectl.RuntimeSort) bool {
|
||||
var lastMap *meta.RESTMapping
|
||||
|
||||
if len(infos) == 1 && printAll {
|
||||
return true
|
||||
}
|
||||
|
||||
for ix := range objs {
|
||||
var mapping *meta.RESTMapping
|
||||
if sorter != nil {
|
||||
|
@ -803,3 +803,36 @@ func HasNames(args []string) (bool, error) {
|
||||
}
|
||||
return hasCombinedTypes || len(args) > 1, nil
|
||||
}
|
||||
|
||||
// MultipleTypesRequested returns true if the provided args contain multiple resource kinds
|
||||
func MultipleTypesRequested(args []string) bool {
|
||||
args = normalizeMultipleResourcesArgs(args)
|
||||
rKinds := sets.NewString()
|
||||
for _, arg := range args {
|
||||
if arg == "all" {
|
||||
return true
|
||||
}
|
||||
rTuple, found, err := splitResourceTypeName(arg)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
// if tuple not found, assume arg is of the form "type1,type2,...".
|
||||
// Since SplitResourceArgument returns a unique list of kinds,
|
||||
// return true here if len(uniqueList) > 1
|
||||
if !found {
|
||||
if strings.Contains(arg, ",") {
|
||||
splitArgs := SplitResourceArgument(arg)
|
||||
if len(splitArgs) > 1 {
|
||||
return true
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
if rKinds.Has(rTuple.Resource) {
|
||||
continue
|
||||
}
|
||||
rKinds.Insert(rTuple.Resource)
|
||||
}
|
||||
return (rKinds.Len() > 1)
|
||||
}
|
||||
|
@ -1235,3 +1235,57 @@ func TestHasNames(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestMultipleTypesRequested(t *testing.T) {
|
||||
tests := []struct {
|
||||
args []string
|
||||
expectedMultipleTypes bool
|
||||
}{
|
||||
{
|
||||
args: []string{""},
|
||||
expectedMultipleTypes: false,
|
||||
},
|
||||
{
|
||||
args: []string{"all"},
|
||||
expectedMultipleTypes: true,
|
||||
},
|
||||
{
|
||||
args: []string{"rc"},
|
||||
expectedMultipleTypes: false,
|
||||
},
|
||||
{
|
||||
args: []string{"rc,pod,svc"},
|
||||
expectedMultipleTypes: true,
|
||||
},
|
||||
{
|
||||
args: []string{"rc/foo"},
|
||||
expectedMultipleTypes: false,
|
||||
},
|
||||
{
|
||||
args: []string{"rc/foo", "rc/bar"},
|
||||
expectedMultipleTypes: false,
|
||||
},
|
||||
{
|
||||
args: []string{"rc", "foo"},
|
||||
expectedMultipleTypes: false,
|
||||
},
|
||||
{
|
||||
args: []string{"rc,pod,svc", "foo"},
|
||||
expectedMultipleTypes: true,
|
||||
},
|
||||
{
|
||||
args: []string{"rc,secrets"},
|
||||
expectedMultipleTypes: true,
|
||||
},
|
||||
{
|
||||
args: []string{"rc/foo", "rc/bar", "svc/svc"},
|
||||
expectedMultipleTypes: true,
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
hasMultipleTypes := MultipleTypesRequested(test.args)
|
||||
if hasMultipleTypes != test.expectedMultipleTypes {
|
||||
t.Errorf("expected HasName to return %v for %s", test.expectedMultipleTypes, test.args)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user