forked from kubernetes/kubernetes
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cri-api: Introduce errors package for the CRI
We start by adding a helper function for IsNotFound errors. The expectation is that CRI implementations return the grcp not found status code for situations where they can't find a container or a pod. This is the lowest hanging fruit to start improving the kubelet to detect such conditions and react better. Signed-off-by: Mrunal Patel <mpatel@redhat.com>
- Loading branch information
Showing
5 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |