Skip to content

Commit

Permalink
Ensure -o yaml populates kind/apiVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
liggitt committed Mar 28, 2018
1 parent 2bd8a7d commit a174d7a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions hack/make-rules/test-cmd-util.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2386,6 +2386,13 @@ run_secrets_test() {

create_and_use_new_namespace
kube::log::status "Testing secrets"

# Ensure dry run succeeds and includes kind, apiVersion and data
output_message=$(kubectl create secret generic test --from-literal=key1=value1 --dry-run -o yaml)
kube::test::if_has_string "${output_message}" 'kind: Secret'
kube::test::if_has_string "${output_message}" 'apiVersion: v1'
kube::test::if_has_string "${output_message}" 'key1: dmFsdWUx'

### Create a new namespace
# Pre-condition: the test-secrets namespace does not exist
kube::test::get_object_assert 'namespaces' '{{range.items}}{{ if eq $id_field \"test-secrets\" }}found{{end}}{{end}}:' ':'
Expand Down
4 changes: 4 additions & 0 deletions pkg/kubectl/cmd/create_clusterrole_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func TestCreateClusterRole(t *testing.T) {
verbs: "get,watch,list",
resources: "pods,pods",
expectedClusterRole: &rbac.ClusterRole{
TypeMeta: v1.TypeMeta{APIVersion: "rbac.authorization.k8s.io/v1", Kind: "ClusterRole"},
ObjectMeta: v1.ObjectMeta{
Name: clusterRoleName,
},
Expand All @@ -67,6 +68,7 @@ func TestCreateClusterRole(t *testing.T) {
verbs: "get,watch,list",
resources: "pods,deployments.extensions",
expectedClusterRole: &rbac.ClusterRole{
TypeMeta: v1.TypeMeta{APIVersion: "rbac.authorization.k8s.io/v1", Kind: "ClusterRole"},
ObjectMeta: v1.ObjectMeta{
Name: clusterRoleName,
},
Expand All @@ -90,6 +92,7 @@ func TestCreateClusterRole(t *testing.T) {
verbs: "get",
nonResourceURL: "/logs/,/healthz",
expectedClusterRole: &rbac.ClusterRole{
TypeMeta: v1.TypeMeta{APIVersion: "rbac.authorization.k8s.io/v1", Kind: "ClusterRole"},
ObjectMeta: v1.ObjectMeta{
Name: clusterRoleName,
},
Expand All @@ -106,6 +109,7 @@ func TestCreateClusterRole(t *testing.T) {
nonResourceURL: "/logs/,/healthz",
resources: "pods",
expectedClusterRole: &rbac.ClusterRole{
TypeMeta: v1.TypeMeta{APIVersion: "rbac.authorization.k8s.io/v1", Kind: "ClusterRole"},
ObjectMeta: v1.ObjectMeta{
Name: clusterRoleName,
},
Expand Down
4 changes: 4 additions & 0 deletions pkg/kubectl/cmd/create_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ func TestCreateRole(t *testing.T) {
verbs: "get,watch,list",
resources: "pods,pods",
expectedRole: &rbac.Role{
TypeMeta: v1.TypeMeta{APIVersion: "rbac.authorization.k8s.io/v1", Kind: "Role"},
ObjectMeta: v1.ObjectMeta{
Name: roleName,
},
Expand All @@ -68,6 +69,7 @@ func TestCreateRole(t *testing.T) {
verbs: "get,watch,list",
resources: "replicasets/scale",
expectedRole: &rbac.Role{
TypeMeta: v1.TypeMeta{APIVersion: "rbac.authorization.k8s.io/v1", Kind: "Role"},
ObjectMeta: v1.ObjectMeta{
Name: roleName,
},
Expand All @@ -85,6 +87,7 @@ func TestCreateRole(t *testing.T) {
verbs: "get,watch,list",
resources: "replicasets.extensions/scale",
expectedRole: &rbac.Role{
TypeMeta: v1.TypeMeta{APIVersion: "rbac.authorization.k8s.io/v1", Kind: "Role"},
ObjectMeta: v1.ObjectMeta{
Name: roleName,
},
Expand All @@ -102,6 +105,7 @@ func TestCreateRole(t *testing.T) {
verbs: "get,watch,list",
resources: "pods,deployments.extensions",
expectedRole: &rbac.Role{
TypeMeta: v1.TypeMeta{APIVersion: "rbac.authorization.k8s.io/v1", Kind: "Role"},
ObjectMeta: v1.ObjectMeta{
Name: roleName,
},
Expand Down
5 changes: 5 additions & 0 deletions pkg/printers/versioned.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ func (p *VersionedPrinter) PrintObj(obj runtime.Object, w io.Writer) error {
}

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

Expand Down

0 comments on commit a174d7a

Please sign in to comment.