mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-13 05:46:16 +00:00
ensure unstructured resources in kubectl get
Ensure we are dealing with unstructured objects before attempting to compose into an unstructured list.
This commit is contained in:
parent
55cb667b9e
commit
cf8c142933
@ -17,6 +17,7 @@ limitations under the License.
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"strings"
|
"strings"
|
||||||
@ -26,11 +27,13 @@ import (
|
|||||||
|
|
||||||
kapierrors "k8s.io/apimachinery/pkg/api/errors"
|
kapierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
"k8s.io/apimachinery/pkg/api/meta"
|
"k8s.io/apimachinery/pkg/api/meta"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
||||||
"k8s.io/apimachinery/pkg/util/sets"
|
"k8s.io/apimachinery/pkg/util/sets"
|
||||||
"k8s.io/apimachinery/pkg/watch"
|
"k8s.io/apimachinery/pkg/watch"
|
||||||
|
"k8s.io/kubernetes/pkg/api"
|
||||||
"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"
|
||||||
@ -344,17 +347,32 @@ func RunGet(f cmdutil.Factory, out, errOut io.Writer, cmd *cobra.Command, args [
|
|||||||
var obj runtime.Object
|
var obj runtime.Object
|
||||||
if !singleItemImplied || len(infos) > 1 {
|
if !singleItemImplied || len(infos) > 1 {
|
||||||
// we have more than one item, so coerce all items into a list
|
// we have more than one item, so coerce all items into a list
|
||||||
list := &unstructured.UnstructuredList{
|
// we have more than one item, so coerce all items into a list.
|
||||||
Object: map[string]interface{}{
|
// we don't want an *unstructured.Unstructured list yet, as we
|
||||||
"kind": "List",
|
// may be dealing with non-unstructured objects. Compose all items
|
||||||
"apiVersion": "v1",
|
// into an api.List, and then decode using an unstructured scheme.
|
||||||
"metadata": map[string]interface{}{},
|
list := api.List{
|
||||||
|
TypeMeta: metav1.TypeMeta{
|
||||||
|
Kind: "List",
|
||||||
|
APIVersion: "v1",
|
||||||
},
|
},
|
||||||
|
ListMeta: metav1.ListMeta{},
|
||||||
}
|
}
|
||||||
for _, info := range infos {
|
for _, info := range infos {
|
||||||
list.Items = append(list.Items, *info.Object.(*unstructured.Unstructured))
|
list.Items = append(list.Items, info.Object)
|
||||||
}
|
}
|
||||||
obj = list
|
|
||||||
|
listData, err := json.Marshal(list)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
converted, err := runtime.Decode(unstructured.UnstructuredJSONScheme, listData)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
obj = converted
|
||||||
} else {
|
} else {
|
||||||
obj = infos[0].Object
|
obj = infos[0].Object
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user