diff --git a/staging/src/k8s.io/cri-api/BUILD b/staging/src/k8s.io/cri-api/BUILD index 20c07ce7d11..e51fc78c08e 100644 --- a/staging/src/k8s.io/cri-api/BUILD +++ b/staging/src/k8s.io/cri-api/BUILD @@ -10,6 +10,7 @@ filegroup( srcs = [ ":package-srcs", "//staging/src/k8s.io/cri-api/pkg/apis:all-srcs", + "//staging/src/k8s.io/cri-api/pkg/errors:all-srcs", ], tags = ["automanaged"], visibility = ["//visibility:public"], diff --git a/staging/src/k8s.io/cri-api/pkg/errors/BUILD b/staging/src/k8s.io/cri-api/pkg/errors/BUILD new file mode 100644 index 00000000000..2b8f51b78cf --- /dev/null +++ b/staging/src/k8s.io/cri-api/pkg/errors/BUILD @@ -0,0 +1,40 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") + +go_library( + name = "go_default_library", + srcs = [ + "doc.go", + "errors.go", + ], + importmap = "k8s.io/kubernetes/vendor/k8s.io/cri-api/pkg/errors", + importpath = "k8s.io/cri-api/pkg/errors", + visibility = ["//visibility:public"], + deps = [ + "//vendor/google.golang.org/grpc/codes:go_default_library", + "//vendor/google.golang.org/grpc/status:go_default_library", + ], +) + +go_test( + name = "go_default_test", + srcs = ["errors_test.go"], + embed = [":go_default_library"], + deps = [ + "//vendor/google.golang.org/grpc/codes:go_default_library", + "//vendor/google.golang.org/grpc/status:go_default_library", + ], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [":package-srcs"], + tags = ["automanaged"], + visibility = ["//visibility:public"], +) diff --git a/staging/src/k8s.io/cri-api/pkg/errors/doc.go b/staging/src/k8s.io/cri-api/pkg/errors/doc.go new file mode 100644 index 00000000000..f3413ee9804 --- /dev/null +++ b/staging/src/k8s.io/cri-api/pkg/errors/doc.go @@ -0,0 +1,19 @@ +/* +Copyright 2020 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Package errors provides helper functions for use by the kubelet +// to deal with CRI errors. +package errors // import "k8s.io/cri-api/pkg/errors" diff --git a/staging/src/k8s.io/cri-api/pkg/errors/errors.go b/staging/src/k8s.io/cri-api/pkg/errors/errors.go new file mode 100644 index 00000000000..41d7b92466d --- /dev/null +++ b/staging/src/k8s.io/cri-api/pkg/errors/errors.go @@ -0,0 +1,38 @@ +/* +Copyright 2020 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package errors + +import ( + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +// IsNotFound returns a boolean indicating whether the error +// is grpc not found error. +// See https://github.com/grpc/grpc/blob/master/doc/statuscodes.md +// for a list of grpc status codes. +func IsNotFound(err error) bool { + s, ok := status.FromError(err) + if !ok { + return ok + } + if s.Code() == codes.NotFound { + return true + } + + return false +} diff --git a/staging/src/k8s.io/cri-api/pkg/errors/errors_test.go b/staging/src/k8s.io/cri-api/pkg/errors/errors_test.go new file mode 100644 index 00000000000..e512254b62a --- /dev/null +++ b/staging/src/k8s.io/cri-api/pkg/errors/errors_test.go @@ -0,0 +1,46 @@ +/* +Copyright 2020 The Kubernetes Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package errors + +import ( + "fmt" + "testing" + + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" +) + +func TestErrorIsNotFound(t *testing.T) { + enf := status.Errorf(codes.NotFound, "container not found") + if !IsNotFound(enf) { + t.Errorf("%v expected to pass not found check", enf) + } +} + +func TestSimpleErrorDoesNotTriggerNotFound(t *testing.T) { + err := fmt.Errorf("Some random error") + if IsNotFound(err) { + t.Errorf("%v unexpectedly passed not found check", err) + } +} + +func TestOtherGrpcErrorDoesNotTriggerNotFound(t *testing.T) { + gerr := status.Errorf(codes.DeadlineExceeded, "timed out") + if IsNotFound(gerr) { + t.Errorf("%v unexpectedly passed not found check", gerr) + } +}