forked from containerd/containerd
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cmd/ctr/commands/tasks: build metrics with target platform
The windows container is using containerd/[email protected] for metrics while the linux container is using containerd/[email protected]. They are two different packages but the metric fields in proto share the same name. For instance, the `Metrics` type: * in [email protected], it is named by io.containerd.cgroups.v1.Metrics[1]. * in [email protected], it's also named by io.containerd.cgroups.v1.Metrics[2]. It's hard to tell the real type, even if both types are compatible. According to typeurl/[email protected]'s behavior[3], it will check the type registed by the `RegisterType` first. Let's see a case. If the data is marshaled by containerd/[email protected] and the typeurl is `io.containerd.cgroups.v1.Metrics`, ideally the receiver should use the same type to unmarshal it. However, if the receiver has a dependency using the [email protected], the [email protected] package will register the type with `io.containerd.cgroups.v1.Metrics` by `gogo.RegisterType`. And the receiver will [email protected]'s type to unmarshal the data. It's compatible but it is unexpected because the receiver will use the `[email protected]` to do the type assertion, like what nerdctl pr[4] does. They will fail to do assertion. Currently, the windows container metrics is using containerd/[email protected], which impacts nerdctl to do release. So I would like to file this pr to build metrics with target platform. REF: [1]: https://github.com/containerd/cgroups/blob/v1.1.0/stats/v1/metrics.pb.go#L705 [2]: https://github.com/containerd/cgroups/blob/v3.0.1/cgroup1/stats/metrics.pb.go#L1705 [3]: https://github.com/containerd/typeurl/blob/v2.1.0/types.go#L238-L264 [4]: containerd/nerdctl#1952 Signed-off-by: Wei Fu <[email protected]>
- Loading branch information
Showing
4 changed files
with
266 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
Copyright The containerd Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package tasks | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"text/tabwriter" | ||
|
||
v1 "github.com/containerd/cgroups/v3/cgroup1/stats" | ||
v2 "github.com/containerd/cgroups/v3/cgroup2/stats" | ||
"github.com/containerd/containerd/api/types" | ||
"github.com/containerd/typeurl/v2" | ||
|
||
"github.com/urfave/cli" | ||
) | ||
|
||
func printTaskMetrics(gocontext *cli.Context, metric *types.Metric) error { | ||
anydata, err := typeurl.UnmarshalAny(metric.Data) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var ( | ||
data *v1.Metrics | ||
data2 *v2.Metrics | ||
) | ||
|
||
switch v := anydata.(type) { | ||
case *v1.Metrics: | ||
data = v | ||
case *v2.Metrics: | ||
data2 = v | ||
default: | ||
return errors.New("cannot convert metric data to cgroups.Metrics") | ||
} | ||
|
||
switch gocontext.String(formatFlag) { | ||
case formatTable: | ||
w := tabwriter.NewWriter(os.Stdout, 1, 8, 4, ' ', 0) | ||
fmt.Fprintf(w, "ID\tTIMESTAMP\t\n") | ||
fmt.Fprintf(w, "%s\t%s\t\n\n", metric.ID, metric.Timestamp) | ||
if data != nil { | ||
printCgroupMetricsTable(w, data) | ||
} else { | ||
printCgroup2MetricsTable(w, data2) | ||
} | ||
return w.Flush() | ||
case formatJSON: | ||
marshaledJSON, err := json.MarshalIndent(anydata, "", " ") | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(string(marshaledJSON)) | ||
return nil | ||
default: | ||
return errors.New("format must be table or json") | ||
} | ||
} | ||
|
||
func printCgroup2MetricsTable(w *tabwriter.Writer, data *v2.Metrics) { | ||
fmt.Fprintf(w, "METRIC\tVALUE\t\n") | ||
if data.Pids != nil { | ||
fmt.Fprintf(w, "pids.current\t%v\t\n", data.Pids.Current) | ||
fmt.Fprintf(w, "pids.limit\t%v\t\n", data.Pids.Limit) | ||
} | ||
if data.CPU != nil { | ||
fmt.Fprintf(w, "cpu.usage_usec\t%v\t\n", data.CPU.UsageUsec) | ||
fmt.Fprintf(w, "cpu.user_usec\t%v\t\n", data.CPU.UserUsec) | ||
fmt.Fprintf(w, "cpu.system_usec\t%v\t\n", data.CPU.SystemUsec) | ||
fmt.Fprintf(w, "cpu.nr_periods\t%v\t\n", data.CPU.NrPeriods) | ||
fmt.Fprintf(w, "cpu.nr_throttled\t%v\t\n", data.CPU.NrThrottled) | ||
fmt.Fprintf(w, "cpu.throttled_usec\t%v\t\n", data.CPU.ThrottledUsec) | ||
} | ||
if data.Memory != nil { | ||
fmt.Fprintf(w, "memory.usage\t%v\t\n", data.Memory.Usage) | ||
fmt.Fprintf(w, "memory.usage_limit\t%v\t\n", data.Memory.UsageLimit) | ||
fmt.Fprintf(w, "memory.swap_usage\t%v\t\n", data.Memory.SwapUsage) | ||
fmt.Fprintf(w, "memory.swap_limit\t%v\t\n", data.Memory.SwapLimit) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
//go:build !linux && !windows | ||
|
||
/* | ||
Copyright The containerd Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package tasks | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/containerd/containerd/api/types" | ||
|
||
"github.com/urfave/cli" | ||
) | ||
|
||
func printTaskMetrics(*cli.Context, *types.Metric) error { | ||
return fmt.Errorf("unsupported platforms") | ||
} |
Oops, something went wrong.