k8s.io/kubernetes@v1.29.3/pkg/kubelet/stats/host_stats_provider_fake.go (about) 1 /* 2 Copyright 2017 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 stats 18 19 import ( 20 "fmt" 21 "path/filepath" 22 23 cadvisorapiv2 "github.com/google/cadvisor/info/v2" 24 "k8s.io/apimachinery/pkg/types" 25 statsapi "k8s.io/kubelet/pkg/apis/stats/v1alpha1" 26 kubecontainer "k8s.io/kubernetes/pkg/kubelet/container" 27 kubecontainertest "k8s.io/kubernetes/pkg/kubelet/container/testing" 28 "k8s.io/kubernetes/pkg/kubelet/kuberuntime" 29 "k8s.io/kubernetes/pkg/volume" 30 ) 31 32 type fakeHostStatsProvider struct { 33 fakeStats map[string]*volume.Metrics 34 osInterface kubecontainer.OSInterface 35 } 36 37 // NewFakeHostStatsProvider provides a way to test with fake host statistics 38 func NewFakeHostStatsProvider() HostStatsProvider { 39 return &fakeHostStatsProvider{ 40 osInterface: &kubecontainertest.FakeOS{}, 41 } 42 } 43 44 // NewFakeHostStatsProviderWithData provides a way to test with fake host statistics 45 func NewFakeHostStatsProviderWithData(fakeStats map[string]*volume.Metrics, osInterface kubecontainer.OSInterface) HostStatsProvider { 46 return &fakeHostStatsProvider{ 47 fakeStats: fakeStats, 48 osInterface: osInterface, 49 } 50 } 51 52 func (f *fakeHostStatsProvider) getPodLogStats(podNamespace, podName string, podUID types.UID, rootFsInfo *cadvisorapiv2.FsInfo) (*statsapi.FsStats, error) { 53 path := kuberuntime.BuildPodLogsDirectory(podNamespace, podName, podUID) 54 files, err := f.osInterface.ReadDir(path) 55 if err != nil { 56 return nil, err 57 } 58 var results []volume.MetricsProvider 59 for _, file := range files { 60 if file.IsDir() { 61 continue 62 } 63 // Only include *files* under pod log directory. 64 fpath := filepath.Join(path, file.Name()) 65 results = append(results, NewFakeMetricsDu(fpath, f.fakeStats[fpath])) 66 } 67 return fakeMetricsProvidersToStats(results, rootFsInfo) 68 } 69 70 func (f *fakeHostStatsProvider) getPodContainerLogStats(podNamespace, podName string, podUID types.UID, containerName string, rootFsInfo *cadvisorapiv2.FsInfo) (*statsapi.FsStats, error) { 71 path := kuberuntime.BuildContainerLogsDirectory(podNamespace, podName, podUID, containerName) 72 metricsProvider := NewFakeMetricsDu(path, f.fakeStats[path]) 73 return fakeMetricsProvidersToStats([]volume.MetricsProvider{metricsProvider}, rootFsInfo) 74 } 75 76 func (f *fakeHostStatsProvider) getPodEtcHostsStats(podUID types.UID, rootFsInfo *cadvisorapiv2.FsInfo) (*statsapi.FsStats, error) { 77 return nil, fmt.Errorf("not implemented") 78 } 79 80 func fakeMetricsProvidersToStats(metricsProviders []volume.MetricsProvider, rootFsInfo *cadvisorapiv2.FsInfo) (*statsapi.FsStats, error) { 81 result := rootFsInfoToFsStats(rootFsInfo) 82 for i, metricsProvider := range metricsProviders { 83 hostMetrics, err := metricsProvider.GetMetrics() 84 if err != nil { 85 return nil, fmt.Errorf("failed to get stats for item %d: %v", i, err) 86 } 87 usedBytes := uint64(hostMetrics.Used.Value()) 88 inodesUsed := uint64(hostMetrics.InodesUsed.Value()) 89 result.UsedBytes = addUsage(result.UsedBytes, &usedBytes) 90 result.InodesUsed = addUsage(result.InodesUsed, &inodesUsed) 91 result.Time = maxUpdateTime(&result.Time, &hostMetrics.Time) 92 } 93 return result, nil 94 } 95 96 type fakeMetricsDu struct { 97 fakeStats *volume.Metrics 98 } 99 100 // NewFakeMetricsDu inserts fake statistics when asked for metrics 101 func NewFakeMetricsDu(path string, stats *volume.Metrics) volume.MetricsProvider { 102 return &fakeMetricsDu{fakeStats: stats} 103 } 104 105 func (f *fakeMetricsDu) GetMetrics() (*volume.Metrics, error) { 106 if f.fakeStats == nil { 107 return nil, fmt.Errorf("no stats provided") 108 } 109 return f.fakeStats, nil 110 }