From 8e4ea2dd15726c4a8c3d673d7c1515a19b1934d4 Mon Sep 17 00:00:00 2001 From: Pengfei Ni Date: Fri, 20 Jan 2017 09:54:49 +0800 Subject: [PATCH] kubelet/server: update cri to protobuf v3 --- pkg/kubelet/server/streaming/server.go | 31 ++++----- pkg/kubelet/server/streaming/server_test.go | 77 ++++++++++----------- 2 files changed, 53 insertions(+), 55 deletions(-) diff --git a/pkg/kubelet/server/streaming/server.go b/pkg/kubelet/server/streaming/server.go index 95e01c492f2..b17ee1f035f 100644 --- a/pkg/kubelet/server/streaming/server.go +++ b/pkg/kubelet/server/streaming/server.go @@ -146,7 +146,7 @@ type server struct { } func (s *server) GetExec(req *runtimeapi.ExecRequest) (*runtimeapi.ExecResponse, error) { - if req.GetContainerId() == "" { + if req.ContainerId == "" { return nil, grpc.Errorf(codes.InvalidArgument, "missing required container_id") } token, err := s.cache.Insert(req) @@ -159,7 +159,7 @@ func (s *server) GetExec(req *runtimeapi.ExecRequest) (*runtimeapi.ExecResponse, } func (s *server) GetAttach(req *runtimeapi.AttachRequest) (*runtimeapi.AttachResponse, error) { - if req.GetContainerId() == "" { + if req.ContainerId == "" { return nil, grpc.Errorf(codes.InvalidArgument, "missing required container_id") } token, err := s.cache.Insert(req) @@ -172,7 +172,7 @@ func (s *server) GetAttach(req *runtimeapi.AttachRequest) (*runtimeapi.AttachRes } func (s *server) GetPortForward(req *runtimeapi.PortForwardRequest) (*runtimeapi.PortForwardResponse, error) { - if req.GetPodSandboxId() == "" { + if req.PodSandboxId == "" { return nil, grpc.Errorf(codes.InvalidArgument, "missing required pod_sandbox_id") } token, err := s.cache.Insert(req) @@ -211,11 +211,10 @@ func (s *server) ServeHTTP(w http.ResponseWriter, r *http.Request) { s.handler.ServeHTTP(w, r) } -func (s *server) buildURL(method, token string) *string { - loc := s.config.BaseURL.ResolveReference(&url.URL{ +func (s *server) buildURL(method, token string) string { + return s.config.BaseURL.ResolveReference(&url.URL{ Path: path.Join(method, token), }).String() - return &loc } func (s *server) serveExec(req *restful.Request, resp *restful.Response) { @@ -232,10 +231,10 @@ func (s *server) serveExec(req *restful.Request, resp *restful.Response) { } streamOpts := &remotecommand.Options{ - Stdin: exec.GetStdin(), + Stdin: exec.Stdin, Stdout: true, - Stderr: !exec.GetTty(), - TTY: exec.GetTty(), + Stderr: !exec.Tty, + TTY: exec.Tty, } remotecommand.ServeExec( @@ -244,8 +243,8 @@ func (s *server) serveExec(req *restful.Request, resp *restful.Response) { s.runtime, "", // unused: podName "", // unusued: podUID - exec.GetContainerId(), - exec.GetCmd(), + exec.ContainerId, + exec.Cmd, streamOpts, s.config.StreamIdleTimeout, s.config.StreamCreationTimeout, @@ -266,10 +265,10 @@ func (s *server) serveAttach(req *restful.Request, resp *restful.Response) { } streamOpts := &remotecommand.Options{ - Stdin: attach.GetStdin(), + Stdin: attach.Stdin, Stdout: true, - Stderr: !attach.GetTty(), - TTY: attach.GetTty(), + Stderr: !attach.Tty, + TTY: attach.Tty, } remotecommand.ServeAttach( resp.ResponseWriter, @@ -277,7 +276,7 @@ func (s *server) serveAttach(req *restful.Request, resp *restful.Response) { s.runtime, "", // unused: podName "", // unusued: podUID - attach.GetContainerId(), + attach.ContainerId, streamOpts, s.config.StreamIdleTimeout, s.config.StreamCreationTimeout, @@ -301,7 +300,7 @@ func (s *server) servePortForward(req *restful.Request, resp *restful.Response) resp.ResponseWriter, req.Request, s.runtime, - pf.GetPodSandboxId(), + pf.PodSandboxId, "", // unused: podUID s.config.StreamIdleTimeout, s.config.StreamCreationTimeout) diff --git a/pkg/kubelet/server/streaming/server_test.go b/pkg/kubelet/server/streaming/server_test.go index e6170a9222c..10068e6eb05 100644 --- a/pkg/kubelet/server/streaming/server_test.go +++ b/pkg/kubelet/server/streaming/server_test.go @@ -82,25 +82,25 @@ func TestGetExec(t *testing.T) { assertRequestToken := func(test testcase, cache *requestCache, token string) { req, ok := cache.Consume(token) require.True(t, ok, "token %s not found! testcase=%+v", token, test) - assert.Equal(t, testContainerID, req.(*runtimeapi.ExecRequest).GetContainerId(), "testcase=%+v", test) - assert.Equal(t, test.cmd, req.(*runtimeapi.ExecRequest).GetCmd(), "testcase=%+v", test) - assert.Equal(t, test.tty, req.(*runtimeapi.ExecRequest).GetTty(), "testcase=%+v", test) - assert.Equal(t, test.stdin, req.(*runtimeapi.ExecRequest).GetStdin(), "testcase=%+v", test) + assert.Equal(t, testContainerID, req.(*runtimeapi.ExecRequest).ContainerId, "testcase=%+v", test) + assert.Equal(t, test.cmd, req.(*runtimeapi.ExecRequest).Cmd, "testcase=%+v", test) + assert.Equal(t, test.tty, req.(*runtimeapi.ExecRequest).Tty, "testcase=%+v", test) + assert.Equal(t, test.stdin, req.(*runtimeapi.ExecRequest).Stdin, "testcase=%+v", test) } containerID := testContainerID for _, test := range testcases { request := &runtimeapi.ExecRequest{ - ContainerId: &containerID, + ContainerId: containerID, Cmd: test.cmd, - Tty: &test.tty, - Stdin: &test.stdin, + Tty: test.tty, + Stdin: test.stdin, } { // Non-TLS resp, err := serv.GetExec(request) assert.NoError(t, err, "testcase=%+v", test) expectedURL := "http://" + testAddr + "/exec/" - assert.Contains(t, resp.GetUrl(), expectedURL, "testcase=%+v", test) - token := strings.TrimPrefix(resp.GetUrl(), expectedURL) + assert.Contains(t, resp.Url, expectedURL, "testcase=%+v", test) + token := strings.TrimPrefix(resp.Url, expectedURL) assertRequestToken(test, serv.(*server).cache, token) } @@ -108,8 +108,8 @@ func TestGetExec(t *testing.T) { resp, err := tlsServer.GetExec(request) assert.NoError(t, err, "testcase=%+v", test) expectedURL := "https://" + testAddr + "/exec/" - assert.Contains(t, resp.GetUrl(), expectedURL, "testcase=%+v", test) - token := strings.TrimPrefix(resp.GetUrl(), expectedURL) + assert.Contains(t, resp.Url, expectedURL, "testcase=%+v", test) + token := strings.TrimPrefix(resp.Url, expectedURL) assertRequestToken(test, tlsServer.(*server).cache, token) } @@ -117,8 +117,8 @@ func TestGetExec(t *testing.T) { resp, err := prefixServer.GetExec(request) assert.NoError(t, err, "testcase=%+v", test) expectedURL := "http://" + testAddr + "/" + pathPrefix + "/exec/" - assert.Contains(t, resp.GetUrl(), expectedURL, "testcase=%+v", test) - token := strings.TrimPrefix(resp.GetUrl(), expectedURL) + assert.Contains(t, resp.Url, expectedURL, "testcase=%+v", test) + token := strings.TrimPrefix(resp.Url, expectedURL) assertRequestToken(test, prefixServer.(*server).cache, token) } } @@ -149,23 +149,23 @@ func TestGetAttach(t *testing.T) { assertRequestToken := func(test testcase, cache *requestCache, token string) { req, ok := cache.Consume(token) require.True(t, ok, "token %s not found! testcase=%+v", token, test) - assert.Equal(t, testContainerID, req.(*runtimeapi.AttachRequest).GetContainerId(), "testcase=%+v", test) - assert.Equal(t, test.tty, req.(*runtimeapi.AttachRequest).GetTty(), "testcase=%+v", test) - assert.Equal(t, test.stdin, req.(*runtimeapi.AttachRequest).GetStdin(), "testcase=%+v", test) + assert.Equal(t, testContainerID, req.(*runtimeapi.AttachRequest).ContainerId, "testcase=%+v", test) + assert.Equal(t, test.tty, req.(*runtimeapi.AttachRequest).Tty, "testcase=%+v", test) + assert.Equal(t, test.stdin, req.(*runtimeapi.AttachRequest).Stdin, "testcase=%+v", test) } containerID := testContainerID for _, test := range testcases { request := &runtimeapi.AttachRequest{ - ContainerId: &containerID, - Stdin: &test.stdin, - Tty: &test.tty, + ContainerId: containerID, + Stdin: test.stdin, + Tty: test.tty, } { // Non-TLS resp, err := serv.GetAttach(request) assert.NoError(t, err, "testcase=%+v", test) expectedURL := "http://" + testAddr + "/attach/" - assert.Contains(t, resp.GetUrl(), expectedURL, "testcase=%+v", test) - token := strings.TrimPrefix(resp.GetUrl(), expectedURL) + assert.Contains(t, resp.Url, expectedURL, "testcase=%+v", test) + token := strings.TrimPrefix(resp.Url, expectedURL) assertRequestToken(test, serv.(*server).cache, token) } @@ -173,8 +173,8 @@ func TestGetAttach(t *testing.T) { resp, err := tlsServer.GetAttach(request) assert.NoError(t, err, "testcase=%+v", test) expectedURL := "https://" + testAddr + "/attach/" - assert.Contains(t, resp.GetUrl(), expectedURL, "testcase=%+v", test) - token := strings.TrimPrefix(resp.GetUrl(), expectedURL) + assert.Contains(t, resp.Url, expectedURL, "testcase=%+v", test) + token := strings.TrimPrefix(resp.Url, expectedURL) assertRequestToken(test, tlsServer.(*server).cache, token) } } @@ -183,7 +183,7 @@ func TestGetAttach(t *testing.T) { func TestGetPortForward(t *testing.T) { podSandboxID := testPodSandboxID request := &runtimeapi.PortForwardRequest{ - PodSandboxId: &podSandboxID, + PodSandboxId: podSandboxID, Port: []int32{1, 2, 3, 4}, } @@ -195,11 +195,11 @@ func TestGetPortForward(t *testing.T) { resp, err := serv.GetPortForward(request) assert.NoError(t, err) expectedURL := "http://" + testAddr + "/portforward/" - assert.True(t, strings.HasPrefix(resp.GetUrl(), expectedURL)) - token := strings.TrimPrefix(resp.GetUrl(), expectedURL) + assert.True(t, strings.HasPrefix(resp.Url, expectedURL)) + token := strings.TrimPrefix(resp.Url, expectedURL) req, ok := serv.(*server).cache.Consume(token) require.True(t, ok, "token %s not found!", token) - assert.Equal(t, testPodSandboxID, req.(*runtimeapi.PortForwardRequest).GetPodSandboxId()) + assert.Equal(t, testPodSandboxID, req.(*runtimeapi.PortForwardRequest).PodSandboxId) } { // TLS @@ -211,11 +211,11 @@ func TestGetPortForward(t *testing.T) { resp, err := tlsServer.GetPortForward(request) assert.NoError(t, err) expectedURL := "https://" + testAddr + "/portforward/" - assert.True(t, strings.HasPrefix(resp.GetUrl(), expectedURL)) - token := strings.TrimPrefix(resp.GetUrl(), expectedURL) + assert.True(t, strings.HasPrefix(resp.Url, expectedURL)) + token := strings.TrimPrefix(resp.Url, expectedURL) req, ok := tlsServer.(*server).cache.Consume(token) require.True(t, ok, "token %s not found!", token) - assert.Equal(t, testPodSandboxID, req.(*runtimeapi.PortForwardRequest).GetPodSandboxId()) + assert.Equal(t, testPodSandboxID, req.(*runtimeapi.PortForwardRequest).PodSandboxId) } } @@ -231,12 +231,11 @@ func TestServePortForward(t *testing.T) { s, testServer := startTestServer(t) defer testServer.Close() - podSandboxID := testPodSandboxID resp, err := s.GetPortForward(&runtimeapi.PortForwardRequest{ - PodSandboxId: &podSandboxID, + PodSandboxId: testPodSandboxID, }) require.NoError(t, err) - reqURL, err := url.Parse(resp.GetUrl()) + reqURL, err := url.Parse(resp.Url) require.NoError(t, err) exec, err := remotecommand.NewExecutor(&restclient.Config{}, "POST", reqURL) @@ -273,20 +272,20 @@ func runRemoteCommandTest(t *testing.T, commandType string) { switch commandType { case "exec": resp, err := s.GetExec(&runtimeapi.ExecRequest{ - ContainerId: &containerID, + ContainerId: containerID, Cmd: []string{"echo"}, - Stdin: &stdin, + Stdin: stdin, }) require.NoError(t, err) - reqURL, err = url.Parse(resp.GetUrl()) + reqURL, err = url.Parse(resp.Url) require.NoError(t, err) case "attach": resp, err := s.GetAttach(&runtimeapi.AttachRequest{ - ContainerId: &containerID, - Stdin: &stdin, + ContainerId: containerID, + Stdin: stdin, }) require.NoError(t, err) - reqURL, err = url.Parse(resp.GetUrl()) + reqURL, err = url.Parse(resp.Url) require.NoError(t, err) }