Skip to content

Commit b9405f7

Browse files
authored
Merge pull request kubernetes#91273 from mrunalp/cri_errors
cri-api: Introduce errors package for the CRI
2 parents a79c711 + ba90b40 commit b9405f7

File tree

5 files changed

+144
-0
lines changed

5 files changed

+144
-0
lines changed

staging/src/k8s.io/cri-api/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ filegroup(
1010
srcs = [
1111
":package-srcs",
1212
"//staging/src/k8s.io/cri-api/pkg/apis:all-srcs",
13+
"//staging/src/k8s.io/cri-api/pkg/errors:all-srcs",
1314
],
1415
tags = ["automanaged"],
1516
visibility = ["//visibility:public"],
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
2+
3+
go_library(
4+
name = "go_default_library",
5+
srcs = [
6+
"doc.go",
7+
"errors.go",
8+
],
9+
importmap = "k8s.io/kubernetes/vendor/k8s.io/cri-api/pkg/errors",
10+
importpath = "k8s.io/cri-api/pkg/errors",
11+
visibility = ["//visibility:public"],
12+
deps = [
13+
"//vendor/google.golang.org/grpc/codes:go_default_library",
14+
"//vendor/google.golang.org/grpc/status:go_default_library",
15+
],
16+
)
17+
18+
go_test(
19+
name = "go_default_test",
20+
srcs = ["errors_test.go"],
21+
embed = [":go_default_library"],
22+
deps = [
23+
"//vendor/google.golang.org/grpc/codes:go_default_library",
24+
"//vendor/google.golang.org/grpc/status:go_default_library",
25+
],
26+
)
27+
28+
filegroup(
29+
name = "package-srcs",
30+
srcs = glob(["**"]),
31+
tags = ["automanaged"],
32+
visibility = ["//visibility:private"],
33+
)
34+
35+
filegroup(
36+
name = "all-srcs",
37+
srcs = [":package-srcs"],
38+
tags = ["automanaged"],
39+
visibility = ["//visibility:public"],
40+
)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
Copyright 2020 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 errors provides helper functions for use by the kubelet
18+
// to deal with CRI errors.
19+
package errors // import "k8s.io/cri-api/pkg/errors"
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
Copyright 2020 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 errors
18+
19+
import (
20+
"google.golang.org/grpc/codes"
21+
"google.golang.org/grpc/status"
22+
)
23+
24+
// IsNotFound returns a boolean indicating whether the error
25+
// is grpc not found error.
26+
// See https://github.com/grpc/grpc/blob/master/doc/statuscodes.md
27+
// for a list of grpc status codes.
28+
func IsNotFound(err error) bool {
29+
s, ok := status.FromError(err)
30+
if !ok {
31+
return ok
32+
}
33+
if s.Code() == codes.NotFound {
34+
return true
35+
}
36+
37+
return false
38+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Copyright 2020 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 errors
18+
19+
import (
20+
"fmt"
21+
"testing"
22+
23+
"google.golang.org/grpc/codes"
24+
"google.golang.org/grpc/status"
25+
)
26+
27+
func TestErrorIsNotFound(t *testing.T) {
28+
enf := status.Errorf(codes.NotFound, "container not found")
29+
if !IsNotFound(enf) {
30+
t.Errorf("%v expected to pass not found check", enf)
31+
}
32+
}
33+
34+
func TestSimpleErrorDoesNotTriggerNotFound(t *testing.T) {
35+
err := fmt.Errorf("Some random error")
36+
if IsNotFound(err) {
37+
t.Errorf("%v unexpectedly passed not found check", err)
38+
}
39+
}
40+
41+
func TestOtherGrpcErrorDoesNotTriggerNotFound(t *testing.T) {
42+
gerr := status.Errorf(codes.DeadlineExceeded, "timed out")
43+
if IsNotFound(gerr) {
44+
t.Errorf("%v unexpectedly passed not found check", gerr)
45+
}
46+
}

0 commit comments

Comments
 (0)