k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/pkg/kubelet/kuberuntime/fake_kuberuntime_manager.go (about) 1 /* 2 Copyright 2016 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 kuberuntime 18 19 import ( 20 "context" 21 "net/http" 22 "time" 23 24 cadvisorapi "github.com/google/cadvisor/info/v1" 25 "go.opentelemetry.io/otel/trace" 26 v1 "k8s.io/api/core/v1" 27 "k8s.io/apimachinery/pkg/api/resource" 28 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 29 "k8s.io/apimachinery/pkg/types" 30 "k8s.io/client-go/tools/record" 31 "k8s.io/client-go/util/flowcontrol" 32 "k8s.io/component-base/logs/logreduction" 33 internalapi "k8s.io/cri-api/pkg/apis" 34 "k8s.io/kubernetes/pkg/credentialprovider" 35 "k8s.io/kubernetes/pkg/kubelet/cm" 36 kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" 37 "k8s.io/kubernetes/pkg/kubelet/images" 38 "k8s.io/kubernetes/pkg/kubelet/lifecycle" 39 "k8s.io/kubernetes/pkg/kubelet/logs" 40 proberesults "k8s.io/kubernetes/pkg/kubelet/prober/results" 41 utilpointer "k8s.io/utils/pointer" 42 ) 43 44 const ( 45 fakeSeccompProfileRoot = "/fakeSeccompProfileRoot" 46 47 fakeNodeAllocatableMemory = "32Gi" 48 fakeNodeAllocatableCPU = "16" 49 50 fakePodLogsDirectory = "/var/log/pods" 51 ) 52 53 type fakeHTTP struct { 54 req *http.Request 55 err error 56 } 57 58 func (f *fakeHTTP) Do(req *http.Request) (*http.Response, error) { 59 f.req = req 60 return nil, f.err 61 } 62 63 type fakePodStateProvider struct { 64 terminated map[types.UID]struct{} 65 removed map[types.UID]struct{} 66 } 67 68 func newFakePodStateProvider() *fakePodStateProvider { 69 return &fakePodStateProvider{ 70 terminated: make(map[types.UID]struct{}), 71 removed: make(map[types.UID]struct{}), 72 } 73 } 74 75 func (f *fakePodStateProvider) IsPodTerminationRequested(uid types.UID) bool { 76 _, found := f.removed[uid] 77 return found 78 } 79 80 func (f *fakePodStateProvider) ShouldPodRuntimeBeRemoved(uid types.UID) bool { 81 _, found := f.terminated[uid] 82 return found 83 } 84 85 func (f *fakePodStateProvider) ShouldPodContentBeRemoved(uid types.UID) bool { 86 _, found := f.removed[uid] 87 return found 88 } 89 90 type fakePodPullingTimeRecorder struct{} 91 92 func (f *fakePodPullingTimeRecorder) RecordImageStartedPulling(podUID types.UID) {} 93 94 func (f *fakePodPullingTimeRecorder) RecordImageFinishedPulling(podUID types.UID) {} 95 96 func newFakeKubeRuntimeManager(runtimeService internalapi.RuntimeService, imageService internalapi.ImageManagerService, machineInfo *cadvisorapi.MachineInfo, osInterface kubecontainer.OSInterface, runtimeHelper kubecontainer.RuntimeHelper, keyring credentialprovider.DockerKeyring, tracer trace.Tracer) (*kubeGenericRuntimeManager, error) { 97 ctx := context.Background() 98 recorder := &record.FakeRecorder{} 99 logManager, err := logs.NewContainerLogManager(runtimeService, osInterface, "1", 2, 10, metav1.Duration{Duration: 10 * time.Second}) 100 if err != nil { 101 return nil, err 102 } 103 kubeRuntimeManager := &kubeGenericRuntimeManager{ 104 recorder: recorder, 105 cpuCFSQuota: false, 106 cpuCFSQuotaPeriod: metav1.Duration{Duration: time.Millisecond * 100}, 107 livenessManager: proberesults.NewManager(), 108 startupManager: proberesults.NewManager(), 109 machineInfo: machineInfo, 110 osInterface: osInterface, 111 runtimeHelper: runtimeHelper, 112 runtimeService: runtimeService, 113 imageService: imageService, 114 keyring: keyring, 115 seccompProfileRoot: fakeSeccompProfileRoot, 116 internalLifecycle: cm.NewFakeInternalContainerLifecycle(), 117 logReduction: logreduction.NewLogReduction(identicalErrorDelay), 118 logManager: logManager, 119 memoryThrottlingFactor: 0.9, 120 podLogsDirectory: fakePodLogsDirectory, 121 } 122 123 typedVersion, err := runtimeService.Version(ctx, kubeRuntimeAPIVersion) 124 if err != nil { 125 return nil, err 126 } 127 128 podStateProvider := newFakePodStateProvider() 129 kubeRuntimeManager.containerGC = newContainerGC(runtimeService, podStateProvider, kubeRuntimeManager, tracer) 130 kubeRuntimeManager.podStateProvider = podStateProvider 131 kubeRuntimeManager.runtimeName = typedVersion.RuntimeName 132 kubeRuntimeManager.imagePuller = images.NewImageManager( 133 kubecontainer.FilterEventRecorder(recorder), 134 kubeRuntimeManager, 135 flowcontrol.NewBackOff(time.Second, 300*time.Second), 136 false, 137 utilpointer.Int32Ptr(0), // No limit on max parallel image pulls, 138 0, // Disable image pull throttling by setting QPS to 0, 139 0, 140 &fakePodPullingTimeRecorder{}, 141 ) 142 kubeRuntimeManager.runner = lifecycle.NewHandlerRunner( 143 &fakeHTTP{}, 144 kubeRuntimeManager, 145 kubeRuntimeManager, 146 recorder) 147 148 kubeRuntimeManager.getNodeAllocatable = func() v1.ResourceList { 149 return v1.ResourceList{ 150 v1.ResourceMemory: resource.MustParse(fakeNodeAllocatableMemory), 151 v1.ResourceCPU: resource.MustParse(fakeNodeAllocatableCPU), 152 } 153 } 154 155 return kubeRuntimeManager, nil 156 }