Skip to content

Commit cb75873

Browse files
author
Kubernetes Submit Queue
authored
Merge pull request kubernetes#40265 from feiskyer/cri-verify
Automatic merge from submit-queue CRI: verify responses from remote runtime Closes kubernetes#40264.
2 parents 0567280 + e2fa0ea commit cb75873

File tree

3 files changed

+126
-1
lines changed

3 files changed

+126
-1
lines changed

pkg/kubelet/remote/remote_image.go

+17
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ limitations under the License.
1717
package remote
1818

1919
import (
20+
"errors"
21+
"fmt"
2022
"time"
2123

2224
"github.com/golang/glog"
2325
"google.golang.org/grpc"
26+
2427
internalapi "k8s.io/kubernetes/pkg/kubelet/api"
2528
runtimeapi "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
2629
)
@@ -75,6 +78,14 @@ func (r *RemoteImageService) ImageStatus(image *runtimeapi.ImageSpec) (*runtimea
7578
return nil, err
7679
}
7780

81+
if resp.Image != nil {
82+
if resp.Image.Id == "" || resp.Image.Size_ == 0 {
83+
errorMessage := fmt.Sprintf("Id or size of image %q is not set", image.Image)
84+
glog.Errorf("ImageStatus failed: %s", errorMessage)
85+
return nil, errors.New(errorMessage)
86+
}
87+
}
88+
7889
return resp.Image, nil
7990
}
8091

@@ -92,6 +103,12 @@ func (r *RemoteImageService) PullImage(image *runtimeapi.ImageSpec, auth *runtim
92103
return "", err
93104
}
94105

106+
if resp.ImageRef == "" {
107+
errorMessage := fmt.Sprintf("imageRef of image %q is not set", image.Image)
108+
glog.Errorf("PullImage failed: %s", errorMessage)
109+
return "", errors.New(errorMessage)
110+
}
111+
95112
return resp.ImageRef, nil
96113
}
97114

pkg/kubelet/remote/remote_runtime.go

+54-1
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ limitations under the License.
1717
package remote
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"strings"
2223
"time"
2324

2425
"github.com/golang/glog"
2526
"google.golang.org/grpc"
27+
2628
internalapi "k8s.io/kubernetes/pkg/kubelet/api"
2729
runtimeapi "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
2830
utilexec "k8s.io/kubernetes/pkg/util/exec"
@@ -62,6 +64,10 @@ func (r *RemoteRuntimeService) Version(apiVersion string) (*runtimeapi.VersionRe
6264
return nil, err
6365
}
6466

67+
if typedVersion.Version == "" || typedVersion.RuntimeName == "" || typedVersion.RuntimeApiVersion == "" || typedVersion.RuntimeVersion == "" {
68+
return nil, fmt.Errorf("not all fields are set in VersionResponse (%q)", *typedVersion)
69+
}
70+
6571
return typedVersion, err
6672
}
6773

@@ -79,6 +85,12 @@ func (r *RemoteRuntimeService) RunPodSandbox(config *runtimeapi.PodSandboxConfig
7985
return "", err
8086
}
8187

88+
if resp.PodSandboxId == "" {
89+
errorMessage := fmt.Sprintf("PodSandboxId is not set for sandbox %q", config.GetMetadata())
90+
glog.Errorf("RunPodSandbox failed: %s", errorMessage)
91+
return "", errors.New(errorMessage)
92+
}
93+
8294
return resp.PodSandboxId, nil
8395
}
8496

@@ -125,10 +137,15 @@ func (r *RemoteRuntimeService) PodSandboxStatus(podSandBoxID string) (*runtimeap
125137
PodSandboxId: podSandBoxID,
126138
})
127139
if err != nil {
128-
glog.Errorf("PodSandboxStatus %q from runtime service failed: %v", podSandBoxID, err)
129140
return nil, err
130141
}
131142

143+
if resp.Status != nil {
144+
if err := verifySandboxStatus(resp.Status); err != nil {
145+
return nil, err
146+
}
147+
}
148+
132149
return resp.Status, nil
133150
}
134151

@@ -163,6 +180,12 @@ func (r *RemoteRuntimeService) CreateContainer(podSandBoxID string, config *runt
163180
return "", err
164181
}
165182

183+
if resp.ContainerId == "" {
184+
errorMessage := fmt.Sprintf("ContainerId is not set for container %q", config.GetMetadata())
185+
glog.Errorf("CreateContainer failed: %s", errorMessage)
186+
return "", errors.New(errorMessage)
187+
}
188+
166189
return resp.ContainerId, nil
167190
}
168191

@@ -245,6 +268,13 @@ func (r *RemoteRuntimeService) ContainerStatus(containerID string) (*runtimeapi.
245268
return nil, err
246269
}
247270

271+
if resp.Status != nil {
272+
if err := verifyContainerStatus(resp.Status); err != nil {
273+
glog.Errorf("ContainerStatus of %q failed: %v", containerID, err)
274+
return nil, err
275+
}
276+
}
277+
248278
return resp.Status, nil
249279
}
250280

@@ -288,6 +318,12 @@ func (r *RemoteRuntimeService) Exec(req *runtimeapi.ExecRequest) (*runtimeapi.Ex
288318
return nil, err
289319
}
290320

321+
if resp.Url == "" {
322+
errorMessage := "URL is not set"
323+
glog.Errorf("Exec failed: %s", errorMessage)
324+
return nil, errors.New(errorMessage)
325+
}
326+
291327
return resp, nil
292328
}
293329

@@ -302,6 +338,11 @@ func (r *RemoteRuntimeService) Attach(req *runtimeapi.AttachRequest) (*runtimeap
302338
return nil, err
303339
}
304340

341+
if resp.Url == "" {
342+
errorMessage := "URL is not set"
343+
glog.Errorf("Exec failed: %s", errorMessage)
344+
return nil, errors.New(errorMessage)
345+
}
305346
return resp, nil
306347
}
307348

@@ -316,6 +357,12 @@ func (r *RemoteRuntimeService) PortForward(req *runtimeapi.PortForwardRequest) (
316357
return nil, err
317358
}
318359

360+
if resp.Url == "" {
361+
errorMessage := "URL is not set"
362+
glog.Errorf("Exec failed: %s", errorMessage)
363+
return nil, errors.New(errorMessage)
364+
}
365+
319366
return resp, nil
320367
}
321368

@@ -351,5 +398,11 @@ func (r *RemoteRuntimeService) Status() (*runtimeapi.RuntimeStatus, error) {
351398
return nil, err
352399
}
353400

401+
if resp.Status == nil || len(resp.Status.Conditions) < 2 {
402+
errorMessage := "RuntimeReady or NetworkReady condition are not set"
403+
glog.Errorf("Status failed: %s", errorMessage)
404+
return nil, errors.New(errorMessage)
405+
}
406+
354407
return resp.Status, nil
355408
}

pkg/kubelet/remote/utils.go

+55
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ limitations under the License.
1717
package remote
1818

1919
import (
20+
"fmt"
2021
"net"
2122
"time"
2223

2324
"golang.org/x/net/context"
25+
26+
runtimeapi "k8s.io/kubernetes/pkg/kubelet/api/v1alpha1/runtime"
2427
)
2528

2629
// dial creates a net.Conn by unix socket addr.
@@ -32,3 +35,55 @@ func dial(addr string, timeout time.Duration) (net.Conn, error) {
3235
func getContextWithTimeout(timeout time.Duration) (context.Context, context.CancelFunc) {
3336
return context.WithTimeout(context.Background(), timeout)
3437
}
38+
39+
// verifySandboxStatus verified whether all required fields are set in PodSandboxStatus.
40+
func verifySandboxStatus(status *runtimeapi.PodSandboxStatus) error {
41+
if status.Id == "" {
42+
return fmt.Errorf("Id is not set")
43+
}
44+
45+
if status.Metadata == nil {
46+
return fmt.Errorf("Metadata is not set")
47+
}
48+
49+
metadata := status.Metadata
50+
if metadata.Name == "" || metadata.Namespace == "" || metadata.Uid == "" {
51+
return fmt.Errorf("Name, Namespace or Uid is not in metadata %q", metadata)
52+
}
53+
54+
if status.CreatedAt == 0 {
55+
return fmt.Errorf("CreatedAt is not set")
56+
}
57+
58+
return nil
59+
}
60+
61+
// verifyContainerStatus verified whether all required fields are set in ContainerStatus.
62+
func verifyContainerStatus(status *runtimeapi.ContainerStatus) error {
63+
if status.Id == "" {
64+
return fmt.Errorf("Id is not set")
65+
}
66+
67+
if status.Metadata == nil {
68+
return fmt.Errorf("Metadata is not set")
69+
}
70+
71+
metadata := status.Metadata
72+
if metadata.Name == "" {
73+
return fmt.Errorf("Name is not in metadata %q", metadata)
74+
}
75+
76+
if status.CreatedAt == 0 {
77+
return fmt.Errorf("CreatedAt is not set")
78+
}
79+
80+
if status.Image == nil || status.Image.Image == "" {
81+
return fmt.Errorf("Image is not set")
82+
}
83+
84+
if status.ImageRef == "" {
85+
return fmt.Errorf("ImageRef is not set")
86+
}
87+
88+
return nil
89+
}

0 commit comments

Comments
 (0)