Skip to content

Commit a174d7a

Browse files
committed
Ensure -o yaml populates kind/apiVersion
1 parent 2bd8a7d commit a174d7a

File tree

4 files changed

+20
-0
lines changed

4 files changed

+20
-0
lines changed

hack/make-rules/test-cmd-util.sh

+7
Original file line numberDiff line numberDiff line change
@@ -2386,6 +2386,13 @@ run_secrets_test() {
23862386

23872387
create_and_use_new_namespace
23882388
kube::log::status "Testing secrets"
2389+
2390+
# Ensure dry run succeeds and includes kind, apiVersion and data
2391+
output_message=$(kubectl create secret generic test --from-literal=key1=value1 --dry-run -o yaml)
2392+
kube::test::if_has_string "${output_message}" 'kind: Secret'
2393+
kube::test::if_has_string "${output_message}" 'apiVersion: v1'
2394+
kube::test::if_has_string "${output_message}" 'key1: dmFsdWUx'
2395+
23892396
### Create a new namespace
23902397
# Pre-condition: the test-secrets namespace does not exist
23912398
kube::test::get_object_assert 'namespaces' '{{range.items}}{{ if eq $id_field \"test-secrets\" }}found{{end}}{{end}}:' ':'

pkg/kubectl/cmd/create_clusterrole_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ func TestCreateClusterRole(t *testing.T) {
5050
verbs: "get,watch,list",
5151
resources: "pods,pods",
5252
expectedClusterRole: &rbac.ClusterRole{
53+
TypeMeta: v1.TypeMeta{APIVersion: "rbac.authorization.k8s.io/v1", Kind: "ClusterRole"},
5354
ObjectMeta: v1.ObjectMeta{
5455
Name: clusterRoleName,
5556
},
@@ -67,6 +68,7 @@ func TestCreateClusterRole(t *testing.T) {
6768
verbs: "get,watch,list",
6869
resources: "pods,deployments.extensions",
6970
expectedClusterRole: &rbac.ClusterRole{
71+
TypeMeta: v1.TypeMeta{APIVersion: "rbac.authorization.k8s.io/v1", Kind: "ClusterRole"},
7072
ObjectMeta: v1.ObjectMeta{
7173
Name: clusterRoleName,
7274
},
@@ -90,6 +92,7 @@ func TestCreateClusterRole(t *testing.T) {
9092
verbs: "get",
9193
nonResourceURL: "/logs/,/healthz",
9294
expectedClusterRole: &rbac.ClusterRole{
95+
TypeMeta: v1.TypeMeta{APIVersion: "rbac.authorization.k8s.io/v1", Kind: "ClusterRole"},
9396
ObjectMeta: v1.ObjectMeta{
9497
Name: clusterRoleName,
9598
},
@@ -106,6 +109,7 @@ func TestCreateClusterRole(t *testing.T) {
106109
nonResourceURL: "/logs/,/healthz",
107110
resources: "pods",
108111
expectedClusterRole: &rbac.ClusterRole{
112+
TypeMeta: v1.TypeMeta{APIVersion: "rbac.authorization.k8s.io/v1", Kind: "ClusterRole"},
109113
ObjectMeta: v1.ObjectMeta{
110114
Name: clusterRoleName,
111115
},

pkg/kubectl/cmd/create_role_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ func TestCreateRole(t *testing.T) {
5151
verbs: "get,watch,list",
5252
resources: "pods,pods",
5353
expectedRole: &rbac.Role{
54+
TypeMeta: v1.TypeMeta{APIVersion: "rbac.authorization.k8s.io/v1", Kind: "Role"},
5455
ObjectMeta: v1.ObjectMeta{
5556
Name: roleName,
5657
},
@@ -68,6 +69,7 @@ func TestCreateRole(t *testing.T) {
6869
verbs: "get,watch,list",
6970
resources: "replicasets/scale",
7071
expectedRole: &rbac.Role{
72+
TypeMeta: v1.TypeMeta{APIVersion: "rbac.authorization.k8s.io/v1", Kind: "Role"},
7173
ObjectMeta: v1.ObjectMeta{
7274
Name: roleName,
7375
},
@@ -85,6 +87,7 @@ func TestCreateRole(t *testing.T) {
8587
verbs: "get,watch,list",
8688
resources: "replicasets.extensions/scale",
8789
expectedRole: &rbac.Role{
90+
TypeMeta: v1.TypeMeta{APIVersion: "rbac.authorization.k8s.io/v1", Kind: "Role"},
8891
ObjectMeta: v1.ObjectMeta{
8992
Name: roleName,
9093
},
@@ -102,6 +105,7 @@ func TestCreateRole(t *testing.T) {
102105
verbs: "get,watch,list",
103106
resources: "pods,deployments.extensions",
104107
expectedRole: &rbac.Role{
108+
TypeMeta: v1.TypeMeta{APIVersion: "rbac.authorization.k8s.io/v1", Kind: "Role"},
105109
ObjectMeta: v1.ObjectMeta{
106110
Name: roleName,
107111
},

pkg/printers/versioned.go

+5
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ func (p *VersionedPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
7676
}
7777

7878
if !needsConversion {
79+
// We might be an external type, but have empty kind/apiVersion fields. Ensure they are populated before printing.
80+
if obj.GetObjectKind().GroupVersionKind().Empty() {
81+
obj = obj.DeepCopyObject()
82+
obj.GetObjectKind().SetGroupVersionKind(gvks[0])
83+
}
7984
return p.printer.PrintObj(obj, w)
8085
}
8186

0 commit comments

Comments
 (0)