Skip to content

Commit 7b7a796

Browse files
authored
Merge pull request kubernetes#125314 from enj/enj/i/proto_for_core
Use protobuf for core clients
2 parents fc9330e + e4d10cf commit 7b7a796

File tree

179 files changed

+872
-254
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

179 files changed

+872
-254
lines changed

cmd/kubelet/app/server_bootstrap_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,14 @@ func Test_buildClientCertificateManager(t *testing.T) {
7373
defer s.Close()
7474

7575
config1 := &restclient.Config{
76-
UserAgent: "FirstClient",
77-
Host: s.URL,
76+
UserAgent: "FirstClient",
77+
Host: s.URL,
78+
ContentConfig: restclient.ContentConfig{ContentType: runtime.ContentTypeJSON},
7879
}
7980
config2 := &restclient.Config{
80-
UserAgent: "SecondClient",
81-
Host: s.URL,
81+
UserAgent: "SecondClient",
82+
Host: s.URL,
83+
ContentConfig: restclient.ContentConfig{ContentType: runtime.ContentTypeJSON},
8284
}
8385

8486
nodeName := types.NodeName("test")

hack/update-codegen.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ function codegen::clients() {
645645
--input-base="k8s.io/api" \
646646
--plural-exceptions "${PLURAL_EXCEPTIONS}" \
647647
--apply-configuration-package "${APPLYCONFIG_PKG}" \
648+
--prefers-protobuf \
648649
$(printf -- " --input %s" "${gv_dirs[@]}") \
649650
"$@"
650651

pkg/client/tests/clientset_test.go

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
Copyright 2024 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 tests
18+
19+
import (
20+
"context"
21+
"io"
22+
"net/http"
23+
"net/http/httptest"
24+
"sync/atomic"
25+
"testing"
26+
27+
"github.com/google/go-cmp/cmp"
28+
29+
autoscalingv1 "k8s.io/api/autoscaling/v1"
30+
corev1 "k8s.io/api/core/v1"
31+
crv1 "k8s.io/apiextensions-apiserver/examples/client-go/pkg/apis/cr/v1"
32+
crv1client "k8s.io/apiextensions-apiserver/examples/client-go/pkg/client/clientset/versioned"
33+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
34+
"k8s.io/client-go/kubernetes"
35+
"k8s.io/client-go/rest"
36+
)
37+
38+
func TestClientContentType(t *testing.T) {
39+
createPodFunc := func(t *testing.T, config *rest.Config) {
40+
client, err := kubernetes.NewForConfig(config)
41+
if err != nil {
42+
t.Fatalf("failed to create REST client: %v", err)
43+
}
44+
45+
_, err = client.CoreV1().Pods("panda").
46+
Create(context.TODO(), &corev1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "snorlax"}}, metav1.CreateOptions{})
47+
if err != nil {
48+
t.Fatal(err)
49+
}
50+
}
51+
52+
updateScaleFunc := func(t *testing.T, config *rest.Config) {
53+
client, err := kubernetes.NewForConfig(config)
54+
if err != nil {
55+
t.Fatalf("failed to create REST client: %v", err)
56+
}
57+
58+
_, err = client.AppsV1().Deployments("panda").
59+
UpdateScale(context.TODO(), "snorlax", &autoscalingv1.Scale{ObjectMeta: metav1.ObjectMeta{Name: "snorlax"}}, metav1.UpdateOptions{})
60+
if err != nil {
61+
t.Fatal(err)
62+
}
63+
}
64+
65+
createExampleViaRESTClientFunc := func(t *testing.T, config *rest.Config) {
66+
kubeClient, err := kubernetes.NewForConfig(config)
67+
if err != nil {
68+
t.Fatalf("failed to create REST client: %v", err)
69+
}
70+
71+
client := crv1client.New(kubeClient.CoreV1().RESTClient())
72+
73+
_, err = client.CrV1().Examples("panda").
74+
Create(context.TODO(), &crv1.Example{ObjectMeta: metav1.ObjectMeta{Name: "snorlax"}}, metav1.CreateOptions{})
75+
if err != nil {
76+
t.Fatal(err)
77+
}
78+
}
79+
80+
tests := []struct {
81+
name string
82+
createFunc func(*testing.T, *rest.Config)
83+
contentType string
84+
expectedPath string
85+
expectContentType string
86+
expectBody string
87+
}{
88+
{
89+
name: "default",
90+
createFunc: createPodFunc,
91+
contentType: "",
92+
expectedPath: "/api/v1/namespaces/panda/pods",
93+
expectContentType: "application/vnd.kubernetes.protobuf",
94+
expectBody: "k8s\x00\n\t\n\x02v1\x12\x03Pod\x12I\n\x17\n\asnorlax\x12\x00\x1a\x00\"\x00*\x002\x008\x00B\x00\x12\x1c\x1a\x002\x00B\x00J\x00R\x00X\x00`\x00h\x00\x82\x01\x00\x8a\x01\x00\x9a\x01\x00\xc2\x01\x00\x1a\x10\n\x00\x1a\x00\"\x00*\x002\x00J\x00Z\x00r\x00\x1a\x00\"\x00",
95+
},
96+
{
97+
name: "json",
98+
createFunc: createPodFunc,
99+
contentType: "application/json",
100+
expectedPath: "/api/v1/namespaces/panda/pods",
101+
expectContentType: "application/json",
102+
expectBody: `{"kind":"Pod","apiVersion":"v1","metadata":{"name":"snorlax","creationTimestamp":null},"spec":{"containers":null},"status":{}}
103+
`,
104+
},
105+
{
106+
name: "default update scale",
107+
createFunc: updateScaleFunc,
108+
contentType: "",
109+
expectedPath: "/apis/apps/v1/namespaces/panda/deployments/snorlax/scale",
110+
expectContentType: "application/vnd.kubernetes.protobuf",
111+
expectBody: "k8s\u0000\n\u0017\n\u000Eautoscaling/v1\u0012\u0005Scale\u0012#\n\u0017\n\asnorlax\u0012\u0000\u001A\u0000\"\u0000*\u00002\u00008\u0000B\u0000\u0012\u0002\b\u0000\u001A\u0004\b\u0000\u0012\u0000\u001A\u0000\"\u0000",
112+
},
113+
{
114+
name: "json update scale",
115+
createFunc: updateScaleFunc,
116+
contentType: "application/json",
117+
expectedPath: "/apis/apps/v1/namespaces/panda/deployments/snorlax/scale",
118+
expectContentType: "application/json",
119+
expectBody: `{"kind":"Scale","apiVersion":"autoscaling/v1","metadata":{"name":"snorlax","creationTimestamp":null},"spec":{},"status":{"replicas":0}}
120+
`,
121+
},
122+
{
123+
name: "default via RESTClient",
124+
createFunc: createExampleViaRESTClientFunc,
125+
contentType: "",
126+
expectedPath: "/api/v1/namespaces/panda/examples",
127+
expectContentType: "application/json",
128+
expectBody: `{"metadata":{"name":"snorlax","creationTimestamp":null},"spec":{"foo":"","bar":false},"status":{}}
129+
`,
130+
},
131+
{
132+
name: "json via RESTClient",
133+
createFunc: createExampleViaRESTClientFunc,
134+
contentType: "application/json",
135+
expectedPath: "/api/v1/namespaces/panda/examples",
136+
expectContentType: "application/json",
137+
expectBody: `{"metadata":{"name":"snorlax","creationTimestamp":null},"spec":{"foo":"","bar":false},"status":{}}
138+
`,
139+
},
140+
}
141+
142+
for _, tc := range tests {
143+
t.Run(tc.name, func(t *testing.T) {
144+
var calls atomic.Uint64
145+
ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
146+
calls.Add(1)
147+
148+
if got, want := r.URL.Path, tc.expectedPath; got != want {
149+
t.Errorf("unexpected path, got=%q, want=%q", got, want)
150+
}
151+
152+
if got, want := r.Header.Get("content-type"), tc.expectContentType; got != want {
153+
t.Errorf("unexpected content-type, got=%q, want=%q", got, want)
154+
}
155+
156+
if r.Body == nil {
157+
t.Fatal("request body is nil")
158+
}
159+
body, err := io.ReadAll(r.Body)
160+
if err != nil {
161+
t.Fatal(err)
162+
}
163+
_ = r.Body.Close()
164+
if diff := cmp.Diff(tc.expectBody, string(body)); len(diff) > 0 {
165+
t.Errorf("body diff (-want, +got):\n%s", diff)
166+
}
167+
168+
w.Header().Set("Content-Type", "application/json")
169+
_, _ = w.Write([]byte("{}"))
170+
}))
171+
ts.Start()
172+
defer ts.Close()
173+
174+
config := &rest.Config{
175+
Host: ts.URL,
176+
ContentConfig: rest.ContentConfig{ContentType: tc.contentType},
177+
}
178+
179+
tc.createFunc(t, config)
180+
181+
if calls.Load() != 1 {
182+
t.Errorf("unexpected handler call count: %d", calls.Load())
183+
}
184+
})
185+
}
186+
}

pkg/controller/controller_utils_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,7 @@ func TestCreatePodsWithGenerateName(t *testing.T) {
375375
}
376376
testServer := httptest.NewServer(&fakeHandler)
377377
defer testServer.Close()
378-
clientset := clientset.NewForConfigOrDie(&restclient.Config{Host: testServer.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
378+
clientset := clientset.NewForConfigOrDie(&restclient.Config{Host: testServer.URL, ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}, ContentType: runtime.ContentTypeJSON}})
379379

380380
podControl := RealPodControl{
381381
KubeClient: clientset,

pkg/controller/endpoint/endpoints_controller_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"time"
2828

2929
"github.com/google/go-cmp/cmp"
30+
3031
v1 "k8s.io/api/core/v1"
3132
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3233
"k8s.io/apimachinery/pkg/runtime"
@@ -225,7 +226,7 @@ type endpointController struct {
225226
}
226227

227228
func newController(ctx context.Context, url string, batchPeriod time.Duration) *endpointController {
228-
client := clientset.NewForConfigOrDie(&restclient.Config{Host: url, ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}}})
229+
client := clientset.NewForConfigOrDie(&restclient.Config{Host: url, ContentConfig: restclient.ContentConfig{GroupVersion: &schema.GroupVersion{Group: "", Version: "v1"}, ContentType: runtime.ContentTypeJSON}})
229230
informerFactory := informers.NewSharedInformerFactory(client, controllerpkg.NoResyncPeriodFunc())
230231
endpoints := NewEndpointController(ctx, informerFactory.Core().V1().Pods(), informerFactory.Core().V1().Services(),
231232
informerFactory.Core().V1().Endpoints(), client, batchPeriod)

staging/src/k8s.io/apiextensions-apiserver/examples/client-go/pkg/client/clientset/versioned/typed/cr/v1/example.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/apiextensions-apiserver/hack/update-codegen.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,5 @@ kube::codegen::gen_client \
5252
--output-pkg "${THIS_PKG}/pkg/client" \
5353
--versioned-name "clientset" \
5454
--boilerplate "${SCRIPT_ROOT}/hack/boilerplate.go.txt" \
55+
--prefers-protobuf \
5556
"${SCRIPT_ROOT}/pkg/apis"

staging/src/k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1/customresourcedefinition.go

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

staging/src/k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1/customresourcedefinition.go

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)