|
| 1 | +/* |
| 2 | +Copyright 2024 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 e2enode |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "fmt" |
| 22 | + "time" |
| 23 | + |
| 24 | + "github.com/onsi/ginkgo/v2" |
| 25 | + "github.com/onsi/gomega" |
| 26 | + "github.com/onsi/gomega/gstruct" |
| 27 | + "github.com/onsi/gomega/types" |
| 28 | + |
| 29 | + "k8s.io/kubernetes/test/e2e/framework" |
| 30 | + e2emetrics "k8s.io/kubernetes/test/e2e/framework/metrics" |
| 31 | + e2evolume "k8s.io/kubernetes/test/e2e/framework/volume" |
| 32 | + admissionapi "k8s.io/pod-security-admission/api" |
| 33 | +) |
| 34 | + |
| 35 | +var _ = SIGDescribe("ContainerMetrics", "[LinuxOnly]", framework.WithNodeConformance(), func() { |
| 36 | + f := framework.NewDefaultFramework("container-metrics") |
| 37 | + f.NamespacePodSecurityEnforceLevel = admissionapi.LevelPrivileged |
| 38 | + ginkgo.Context("when querying /metrics/cadvisor", func() { |
| 39 | + ginkgo.BeforeEach(func(ctx context.Context) { |
| 40 | + createMetricsPods(ctx, f) |
| 41 | + }) |
| 42 | + ginkgo.AfterEach(func(ctx context.Context) { |
| 43 | + removeMetricsPods(ctx, f) |
| 44 | + }) |
| 45 | + ginkgo.It("should report container metrics", func(ctx context.Context) { |
| 46 | + keys := gstruct.Keys{} |
| 47 | + ctrMatches := map[string]types.GomegaMatcher{ |
| 48 | + "container_blkio_device_usage_total": boundedSample(0, 10000000), |
| 49 | + "container_cpu_load_average_10s": boundedSample(0, 100), |
| 50 | + "container_cpu_system_seconds_total": boundedSample(0, 100), |
| 51 | + "container_cpu_usage_seconds_total": boundedSample(0, 100), |
| 52 | + "container_cpu_user_seconds_total": boundedSample(0, 100), |
| 53 | + "container_file_descriptors": boundedSample(0, 100), |
| 54 | + "container_fs_reads_bytes_total": boundedSample(0, 10000000), |
| 55 | + "container_fs_reads_total": boundedSample(0, 100), |
| 56 | + "container_fs_usage_bytes": boundedSample(0, 1000000), |
| 57 | + "container_fs_writes_bytes_total": boundedSample(0, 1000000), |
| 58 | + "container_fs_writes_total": boundedSample(0, 100), |
| 59 | + "container_last_seen": boundedSample(time.Now().Add(-maxStatsAge).Unix(), time.Now().Add(2*time.Minute).Unix()), |
| 60 | + "container_memory_cache": boundedSample(1*e2evolume.Kb, 10*e2evolume.Mb), |
| 61 | + "container_memory_failcnt": preciseSample(0), |
| 62 | + "container_memory_failures_total": boundedSample(0, 1000000), |
| 63 | + "container_memory_mapped_file": boundedSample(0, 10000000), |
| 64 | + "container_memory_max_usage_bytes": boundedSample(0, 80*e2evolume.Mb), |
| 65 | + "container_memory_rss": boundedSample(10*e2evolume.Kb, 80*e2evolume.Mb), |
| 66 | + "container_memory_swap": preciseSample(0), |
| 67 | + "container_memory_usage_bytes": boundedSample(10*e2evolume.Kb, 80*e2evolume.Mb), |
| 68 | + "container_memory_working_set_bytes": boundedSample(10*e2evolume.Kb, 80*e2evolume.Mb), |
| 69 | + "container_oom_events_total": preciseSample(0), |
| 70 | + "container_processes": boundedSample(0, 10), |
| 71 | + "container_sockets": boundedSample(0, 10), |
| 72 | + "container_spec_cpu_period": preciseSample(100000), |
| 73 | + "container_spec_cpu_shares": preciseSample(2), |
| 74 | + "container_spec_memory_limit_bytes": preciseSample(79998976), |
| 75 | + "container_spec_memory_reservation_limit_bytes": preciseSample(0), |
| 76 | + "container_spec_memory_swap_limit_bytes": preciseSample(0), |
| 77 | + "container_start_time_seconds": boundedSample(time.Now().Add(-maxStatsAge).Unix(), time.Now().Add(2*time.Minute).Unix()), |
| 78 | + "container_tasks_state": preciseSample(0), |
| 79 | + "container_threads": boundedSample(0, 10), |
| 80 | + "container_threads_max": boundedSample(0, 100000), |
| 81 | + "container_ulimits_soft": boundedSample(0, 10000000), |
| 82 | + } |
| 83 | + appendMatchesForContainer(f.Namespace.Name, pod0, pod1, "busybox-container", ctrMatches, keys, gstruct.AllowDuplicates|gstruct.IgnoreExtras) |
| 84 | + |
| 85 | + ctrOptionalMatches := map[string]types.GomegaMatcher{ |
| 86 | + "container_fs_io_current": boundedSample(0, 100), |
| 87 | + "container_fs_io_time_seconds_total": boundedSample(0, 100), |
| 88 | + "container_fs_io_time_weighted_seconds_total": boundedSample(0, 100), |
| 89 | + "container_fs_inodes_free": boundedSample(0, 10*e2evolume.Kb), |
| 90 | + "container_fs_inodes_total": boundedSample(0, 100), |
| 91 | + "container_fs_limit_bytes": boundedSample(100*e2evolume.Mb, 10*e2evolume.Tb), |
| 92 | + "container_fs_usage_bytes": boundedSample(0, 1000000), |
| 93 | + "container_fs_read_seconds_total": preciseSample(0), |
| 94 | + "container_fs_reads_merged_total": preciseSample(0), |
| 95 | + "container_fs_sector_reads_total": preciseSample(0), |
| 96 | + "container_fs_sector_writes_total": preciseSample(0), |
| 97 | + "container_fs_write_seconds_total": preciseSample(0), |
| 98 | + "container_fs_writes_merged_total": preciseSample(0), |
| 99 | + } |
| 100 | + // Missing from containerd, so set gstruct.IgnoreMissing |
| 101 | + // See https://github.com/google/cadvisor/issues/2785 |
| 102 | + appendMatchesForContainer(f.Namespace.Name, pod0, pod1, "busybox-container", ctrOptionalMatches, keys, gstruct.AllowDuplicates|gstruct.IgnoreMissing|gstruct.IgnoreExtras) |
| 103 | + |
| 104 | + podMatches := map[string]types.GomegaMatcher{ |
| 105 | + "container_network_receive_bytes_total": boundedSample(10, 10*e2evolume.Mb), |
| 106 | + "container_network_receive_errors_total": boundedSample(0, 1000), |
| 107 | + "container_network_receive_packets_dropped_total": boundedSample(0, 1000), |
| 108 | + "container_network_receive_packets_total": boundedSample(0, 1000), |
| 109 | + "container_network_transmit_bytes_total": boundedSample(10, 10*e2evolume.Mb), |
| 110 | + "container_network_transmit_errors_total": boundedSample(0, 1000), |
| 111 | + "container_network_transmit_packets_dropped_total": boundedSample(0, 1000), |
| 112 | + "container_network_transmit_packets_total": boundedSample(0, 1000), |
| 113 | + } |
| 114 | + // TODO: determine why these are missing from containerd but not CRI-O |
| 115 | + appendMatchesForContainer(f.Namespace.Name, pod0, pod1, "POD", podMatches, keys, gstruct.AllowDuplicates|gstruct.IgnoreMissing|gstruct.IgnoreExtras) |
| 116 | + |
| 117 | + matchResourceMetrics := gstruct.MatchKeys(gstruct.IgnoreExtras, keys) |
| 118 | + ginkgo.By("Giving pods a minute to start up and produce metrics") |
| 119 | + gomega.Eventually(ctx, getContainerMetrics, 1*time.Minute, 15*time.Second).Should(matchResourceMetrics) |
| 120 | + ginkgo.By("Ensuring the metrics match the expectations a few more times") |
| 121 | + gomega.Consistently(ctx, getContainerMetrics, 1*time.Minute, 15*time.Second).Should(matchResourceMetrics) |
| 122 | + }) |
| 123 | + }) |
| 124 | +}) |
| 125 | + |
| 126 | +func getContainerMetrics(ctx context.Context) (e2emetrics.KubeletMetrics, error) { |
| 127 | + ginkgo.By("getting container metrics from cadvisor") |
| 128 | + return e2emetrics.GrabKubeletMetricsWithoutProxy(ctx, framework.TestContext.NodeName+":10255", "/metrics/cadvisor") |
| 129 | +} |
| 130 | + |
| 131 | +func preciseSample(value interface{}) types.GomegaMatcher { |
| 132 | + return gstruct.PointTo(gstruct.MatchAllFields(gstruct.Fields{ |
| 133 | + "Metric": gstruct.Ignore(), |
| 134 | + "Value": gomega.BeEquivalentTo(value), |
| 135 | + "Timestamp": gstruct.Ignore(), |
| 136 | + "Histogram": gstruct.Ignore(), |
| 137 | + })) |
| 138 | +} |
| 139 | + |
| 140 | +func appendMatchesForContainer(ns, pod1, pod2, ctr string, matches map[string]types.GomegaMatcher, keys gstruct.Keys, options gstruct.Options) { |
| 141 | + for k, v := range matches { |
| 142 | + keys[k] = gstruct.MatchElements(containerID, options, gstruct.Elements{ |
| 143 | + fmt.Sprintf("%s::%s::%s", ns, pod1, ctr): v, |
| 144 | + fmt.Sprintf("%s::%s::%s", ns, pod2, ctr): v, |
| 145 | + }) |
| 146 | + } |
| 147 | +} |
0 commit comments