utils.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package remote
  14. import (
  15. "context"
  16. "fmt"
  17. "time"
  18. runtimeapi "k8s.io/cri-api/pkg/apis/runtime/v1alpha2"
  19. )
  20. // maxMsgSize use 16MB as the default message size limit.
  21. // grpc library default is 4MB
  22. const maxMsgSize = 1024 * 1024 * 16
  23. // getContextWithTimeout returns a context with timeout.
  24. func getContextWithTimeout(timeout time.Duration) (context.Context, context.CancelFunc) {
  25. return context.WithTimeout(context.Background(), timeout)
  26. }
  27. // getContextWithCancel returns a context with cancel.
  28. func getContextWithCancel() (context.Context, context.CancelFunc) {
  29. return context.WithCancel(context.Background())
  30. }
  31. // verifySandboxStatus verified whether all required fields are set in PodSandboxStatus.
  32. func verifySandboxStatus(status *runtimeapi.PodSandboxStatus) error {
  33. if status.Id == "" {
  34. return fmt.Errorf("Id is not set")
  35. }
  36. if status.Metadata == nil {
  37. return fmt.Errorf("Metadata is not set")
  38. }
  39. metadata := status.Metadata
  40. if metadata.Name == "" || metadata.Namespace == "" || metadata.Uid == "" {
  41. return fmt.Errorf("Name, Namespace or Uid is not in metadata %q", metadata)
  42. }
  43. if status.CreatedAt == 0 {
  44. return fmt.Errorf("CreatedAt is not set")
  45. }
  46. return nil
  47. }
  48. // verifyContainerStatus verified whether all required fields are set in ContainerStatus.
  49. func verifyContainerStatus(status *runtimeapi.ContainerStatus) error {
  50. if status.Id == "" {
  51. return fmt.Errorf("Id is not set")
  52. }
  53. if status.Metadata == nil {
  54. return fmt.Errorf("Metadata is not set")
  55. }
  56. metadata := status.Metadata
  57. if metadata.Name == "" {
  58. return fmt.Errorf("Name is not in metadata %q", metadata)
  59. }
  60. if status.CreatedAt == 0 {
  61. return fmt.Errorf("CreatedAt is not set")
  62. }
  63. if status.Image == nil || status.Image.Image == "" {
  64. return fmt.Errorf("Image is not set")
  65. }
  66. if status.ImageRef == "" {
  67. return fmt.Errorf("ImageRef is not set")
  68. }
  69. return nil
  70. }