Skip to content

Commit fcf9b1f

Browse files
author
Kubernetes Submit Queue
authored
Merge pull request kubernetes#54893 from mengqiy/fix_convert
Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. Switch kubectl convert back to use legacyscheme and add tests Revert the change of `convert.go` in kubernetes#54533. Add tests for kubectl convert. Fixes kubernetes#54873 ```release-note NONE ``` cc: @smarterclayton
2 parents 444a161 + b518a80 commit fcf9b1f

File tree

7 files changed

+183
-1
lines changed

7 files changed

+183
-1
lines changed

pkg/kubectl/cmd/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ go_test(
157157
"attach_test.go",
158158
"clusterinfo_dump_test.go",
159159
"cmd_test.go",
160+
"convert_test.go",
160161
"cp_test.go",
161162
"create_clusterrole_test.go",
162163
"create_clusterrolebinding_test.go",

pkg/kubectl/cmd/convert.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ import (
2424
"k8s.io/apimachinery/pkg/runtime"
2525
"k8s.io/apimachinery/pkg/runtime/schema"
2626
"k8s.io/kubernetes/pkg/api"
27+
scheme "k8s.io/kubernetes/pkg/api/legacyscheme"
2728
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
2829
cmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
2930
"k8s.io/kubernetes/pkg/kubectl/resource"
30-
"k8s.io/kubernetes/pkg/kubectl/scheme"
3131
"k8s.io/kubernetes/pkg/kubectl/util/i18n"
3232
"k8s.io/kubernetes/pkg/printers"
3333

pkg/kubectl/cmd/convert_test.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package cmd
18+
19+
import (
20+
"bytes"
21+
"net/http"
22+
"testing"
23+
24+
"k8s.io/client-go/rest/fake"
25+
cmdtesting "k8s.io/kubernetes/pkg/kubectl/cmd/testing"
26+
"k8s.io/kubernetes/pkg/printers"
27+
)
28+
29+
type testcase struct {
30+
name string
31+
file string
32+
outputVersion string
33+
fields []checkField
34+
}
35+
36+
type checkField struct {
37+
template string
38+
expected string
39+
}
40+
41+
func TestConvertObject(t *testing.T) {
42+
testcases := []testcase{
43+
{
44+
name: "apps deployment to extensions deployment",
45+
file: "../../../test/fixtures/pkg/kubectl/cmd/convert/appsdeployment.yaml",
46+
outputVersion: "extensions/v1beta1",
47+
fields: []checkField{
48+
{
49+
template: "{{.apiVersion}}",
50+
expected: "extensions/v1beta1",
51+
},
52+
},
53+
},
54+
{
55+
name: "extensions deployment to apps deployment",
56+
file: "../../../test/fixtures/pkg/kubectl/cmd/convert/extensionsdeployment.yaml",
57+
outputVersion: "apps/v1beta2",
58+
fields: []checkField{
59+
{
60+
template: "{{.apiVersion}}",
61+
expected: "apps/v1beta2",
62+
},
63+
},
64+
},
65+
{
66+
name: "v1 HPA to v2beta1 HPA",
67+
file: "../../../test/fixtures/pkg/kubectl/cmd/convert/v1HPA.yaml",
68+
outputVersion: "autoscaling/v2beta1",
69+
fields: []checkField{
70+
{
71+
template: "{{.apiVersion}}",
72+
expected: "autoscaling/v2beta1",
73+
},
74+
{
75+
template: "{{(index .spec.metrics 0).resource.name}}",
76+
expected: "cpu",
77+
},
78+
{
79+
template: "{{(index .spec.metrics 0).resource.targetAverageUtilization}}",
80+
expected: "50",
81+
},
82+
},
83+
},
84+
{
85+
name: "v2beta1 HPA to v1 HPA",
86+
file: "../../../test/fixtures/pkg/kubectl/cmd/convert/v2beta1HPA.yaml",
87+
outputVersion: "autoscaling/v1",
88+
fields: []checkField{
89+
{
90+
template: "{{.apiVersion}}",
91+
expected: "autoscaling/v1",
92+
},
93+
{
94+
template: "{{.spec.targetCPUUtilizationPercentage}}",
95+
expected: "50",
96+
},
97+
},
98+
},
99+
}
100+
101+
f, tf, _, _ := cmdtesting.NewAPIFactory()
102+
tf.UnstructuredClient = &fake.RESTClient{
103+
Client: fake.CreateHTTPClient(func(req *http.Request) (*http.Response, error) {
104+
t.Fatalf("unexpected request: %#v\n%#v", req.URL, req)
105+
return nil, nil
106+
}),
107+
}
108+
tf.Namespace = "test"
109+
buf := bytes.NewBuffer([]byte{})
110+
111+
for _, tc := range testcases {
112+
cmd := NewCmdConvert(f, buf)
113+
cmd.Flags().Set("filename", tc.file)
114+
cmd.Flags().Set("output-version", tc.outputVersion)
115+
cmd.Flags().Set("local", "true")
116+
cmd.Flags().Set("output", "go-template")
117+
118+
for _, field := range tc.fields {
119+
buf.Reset()
120+
tf.Printer, _ = printers.NewTemplatePrinter([]byte(field.template))
121+
cmd.Run(cmd, []string{})
122+
if buf.String() != field.expected {
123+
t.Errorf("unexpected output when converting %s to %q, expected: %q, but got %q", tc.file, tc.outputVersion, field.expected, buf.String())
124+
}
125+
}
126+
}
127+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: apps/v1beta2
2+
kind: Deployment
3+
metadata:
4+
name: nginx-deployment
5+
spec:
6+
template:
7+
metadata:
8+
labels:
9+
name: nginx
10+
spec:
11+
containers:
12+
- name: nginx
13+
image: nginx
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
apiVersion: extensions/v1beta1
2+
kind: Deployment
3+
metadata:
4+
name: nginx-deployment
5+
spec:
6+
template:
7+
metadata:
8+
labels:
9+
name: nginx
10+
spec:
11+
containers:
12+
- name: nginx
13+
image: nginx
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: autoscaling/v1
2+
kind: HorizontalPodAutoscaler
3+
metadata:
4+
name: php-apache
5+
spec:
6+
scaleTargetRef:
7+
apiVersion: apps/v1beta1
8+
kind: Deployment
9+
name: php-apache
10+
minReplicas: 1
11+
maxReplicas: 10
12+
targetCPUUtilizationPercentage: 50
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
apiVersion: autoscaling/v2beta1
2+
kind: HorizontalPodAutoscaler
3+
metadata:
4+
name: php-apache
5+
spec:
6+
scaleTargetRef:
7+
apiVersion: apps/v1beta1
8+
kind: Deployment
9+
name: php-apache
10+
minReplicas: 1
11+
maxReplicas: 10
12+
metrics:
13+
- type: Resource
14+
resource:
15+
name: cpu
16+
targetAverageUtilization: 50

0 commit comments

Comments
 (0)