|
17 | 17 | package sbserver |
18 | 18 |
|
19 | 19 | import ( |
| 20 | + "errors" |
20 | 21 | "fmt" |
| 22 | + "reflect" |
21 | 23 | "time" |
22 | 24 |
|
| 25 | + wstats "github.com/Microsoft/hcsshim/cmd/containerd-shim-runhcs-v1/stats" |
| 26 | + v1 "github.com/containerd/cgroups/v3/cgroup1/stats" |
| 27 | + v2 "github.com/containerd/cgroups/v3/cgroup2/stats" |
23 | 28 | "github.com/containerd/containerd/api/types" |
24 | | - v1 "github.com/containerd/containerd/metrics/types/v1" |
25 | | - v2 "github.com/containerd/containerd/metrics/types/v2" |
26 | 29 | "github.com/containerd/containerd/protobuf" |
27 | 30 | "github.com/containerd/typeurl/v2" |
28 | 31 | runtime "k8s.io/cri-api/pkg/apis/runtime/v1" |
@@ -59,18 +62,29 @@ func (c *criService) containerMetrics( |
59 | 62 | } |
60 | 63 |
|
61 | 64 | if stats != nil { |
62 | | - s, err := typeurl.UnmarshalAny(stats.Data) |
63 | | - if err != nil { |
| 65 | + var data interface{} |
| 66 | + switch { |
| 67 | + case typeurl.Is(stats.Data, (*v1.Metrics)(nil)): |
| 68 | + data = &v1.Metrics{} |
| 69 | + case typeurl.Is(stats.Data, (*v2.Metrics)(nil)): |
| 70 | + data = &v2.Metrics{} |
| 71 | + case typeurl.Is(stats.Data, (*wstats.Statistics)(nil)): |
| 72 | + data = &wstats.Statistics{} |
| 73 | + default: |
| 74 | + return nil, errors.New("cannot convert metric data to cgroups.Metrics or windows.Statistics") |
| 75 | + } |
| 76 | + |
| 77 | + if err := typeurl.UnmarshalTo(stats.Data, data); err != nil { |
64 | 78 | return nil, fmt.Errorf("failed to extract container metrics: %w", err) |
65 | 79 | } |
66 | 80 |
|
67 | | - cpuStats, err := c.cpuContainerStats(meta.ID, false /* isSandbox */, s, protobuf.FromTimestamp(stats.Timestamp)) |
| 81 | + cpuStats, err := c.cpuContainerStats(meta.ID, false /* isSandbox */, data, protobuf.FromTimestamp(stats.Timestamp)) |
68 | 82 | if err != nil { |
69 | 83 | return nil, fmt.Errorf("failed to obtain cpu stats: %w", err) |
70 | 84 | } |
71 | 85 | cs.Cpu = cpuStats |
72 | 86 |
|
73 | | - memoryStats, err := c.memoryContainerStats(meta.ID, s, protobuf.FromTimestamp(stats.Timestamp)) |
| 87 | + memoryStats, err := c.memoryContainerStats(meta.ID, data, protobuf.FromTimestamp(stats.Timestamp)) |
74 | 88 | if err != nil { |
75 | 89 | return nil, fmt.Errorf("failed to obtain memory stats: %w", err) |
76 | 90 | } |
@@ -151,7 +165,7 @@ func (c *criService) cpuContainerStats(ID string, isSandbox bool, stats interfac |
151 | 165 | }, nil |
152 | 166 | } |
153 | 167 | default: |
154 | | - return nil, fmt.Errorf("unexpected metrics type: %v", metrics) |
| 168 | + return nil, fmt.Errorf("unexpected metrics type: %T from %s", metrics, reflect.TypeOf(metrics).Elem().PkgPath()) |
155 | 169 | } |
156 | 170 | return nil, nil |
157 | 171 | } |
@@ -193,7 +207,7 @@ func (c *criService) memoryContainerStats(ID string, stats interface{}, timestam |
193 | 207 | }, nil |
194 | 208 | } |
195 | 209 | default: |
196 | | - return nil, fmt.Errorf("unexpected metrics type: %v", metrics) |
| 210 | + return nil, fmt.Errorf("unexpected metrics type: %T from %s", metrics, reflect.TypeOf(metrics).Elem().PkgPath()) |
197 | 211 | } |
198 | 212 | return nil, nil |
199 | 213 | } |
0 commit comments