fake_kuberuntime_manager.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 kuberuntime
  14. import (
  15. "net/http"
  16. "time"
  17. cadvisorapi "github.com/google/cadvisor/info/v1"
  18. metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
  19. "k8s.io/apimachinery/pkg/types"
  20. "k8s.io/client-go/tools/record"
  21. "k8s.io/client-go/util/flowcontrol"
  22. internalapi "k8s.io/cri-api/pkg/apis"
  23. "k8s.io/kubernetes/pkg/credentialprovider"
  24. "k8s.io/kubernetes/pkg/kubelet/cm"
  25. kubecontainer "k8s.io/kubernetes/pkg/kubelet/container"
  26. "k8s.io/kubernetes/pkg/kubelet/images"
  27. "k8s.io/kubernetes/pkg/kubelet/lifecycle"
  28. proberesults "k8s.io/kubernetes/pkg/kubelet/prober/results"
  29. "k8s.io/kubernetes/pkg/kubelet/util/logreduction"
  30. )
  31. const (
  32. fakeSeccompProfileRoot = "/fakeSeccompProfileRoot"
  33. )
  34. type fakeHTTP struct {
  35. url string
  36. err error
  37. }
  38. func (f *fakeHTTP) Get(url string) (*http.Response, error) {
  39. f.url = url
  40. return nil, f.err
  41. }
  42. type fakePodStateProvider struct {
  43. existingPods map[types.UID]struct{}
  44. runningPods map[types.UID]struct{}
  45. }
  46. func newFakePodStateProvider() *fakePodStateProvider {
  47. return &fakePodStateProvider{
  48. existingPods: make(map[types.UID]struct{}),
  49. runningPods: make(map[types.UID]struct{}),
  50. }
  51. }
  52. func (f *fakePodStateProvider) IsPodDeleted(uid types.UID) bool {
  53. _, found := f.existingPods[uid]
  54. return !found
  55. }
  56. func (f *fakePodStateProvider) IsPodTerminated(uid types.UID) bool {
  57. _, found := f.runningPods[uid]
  58. return !found
  59. }
  60. func newFakeKubeRuntimeManager(runtimeService internalapi.RuntimeService, imageService internalapi.ImageManagerService, machineInfo *cadvisorapi.MachineInfo, osInterface kubecontainer.OSInterface, runtimeHelper kubecontainer.RuntimeHelper, keyring credentialprovider.DockerKeyring) (*kubeGenericRuntimeManager, error) {
  61. recorder := &record.FakeRecorder{}
  62. kubeRuntimeManager := &kubeGenericRuntimeManager{
  63. recorder: recorder,
  64. cpuCFSQuota: false,
  65. cpuCFSQuotaPeriod: metav1.Duration{Duration: time.Microsecond * 100},
  66. livenessManager: proberesults.NewManager(),
  67. containerRefManager: kubecontainer.NewRefManager(),
  68. machineInfo: machineInfo,
  69. osInterface: osInterface,
  70. runtimeHelper: runtimeHelper,
  71. runtimeService: runtimeService,
  72. imageService: imageService,
  73. keyring: keyring,
  74. seccompProfileRoot: fakeSeccompProfileRoot,
  75. internalLifecycle: cm.NewFakeInternalContainerLifecycle(),
  76. logReduction: logreduction.NewLogReduction(identicalErrorDelay),
  77. }
  78. typedVersion, err := runtimeService.Version(kubeRuntimeAPIVersion)
  79. if err != nil {
  80. return nil, err
  81. }
  82. kubeRuntimeManager.containerGC = newContainerGC(runtimeService, newFakePodStateProvider(), kubeRuntimeManager)
  83. kubeRuntimeManager.runtimeName = typedVersion.RuntimeName
  84. kubeRuntimeManager.imagePuller = images.NewImageManager(
  85. kubecontainer.FilterEventRecorder(recorder),
  86. kubeRuntimeManager,
  87. flowcontrol.NewBackOff(time.Second, 300*time.Second),
  88. false,
  89. 0, // Disable image pull throttling by setting QPS to 0,
  90. 0,
  91. )
  92. kubeRuntimeManager.runner = lifecycle.NewHandlerRunner(
  93. &fakeHTTP{},
  94. kubeRuntimeManager,
  95. kubeRuntimeManager)
  96. return kubeRuntimeManager, nil
  97. }