runsc: Do not embed metric-server subcommand in fastbuild mode.

When compiling with `-c fastbuild`, the `runsc metric-server` subcommand will
be stubbed out, which makes compilation faster.

The `metricclient` library (used in tests that execute the metric server) is
updated to call the embedded metric server binary directly, rather than
relying on it being embedded in `runsc`.

PiperOrigin-RevId: 580000119
This commit is contained in:
Etienne Perot
2023-11-06 17:01:45 -08:00
committed by gVisor bot
parent 51074f1996
commit 9f6b200214
6 changed files with 83 additions and 7 deletions
+32 -2
View File
@@ -5,6 +5,31 @@ package(
licenses = ["notice"],
)
config_setting(
name = "metric_server_elided",
values = {
"compilation_mode": "fastbuild",
},
)
# @unused
glaze_ignore = [
"metric_server_elided.go",
"metric_server_embedded.go",
]
# Use either metric_server_elided.go or metric_server_embedded.go
# as metric_server.go.
genrule(
name = "metricserver_cmd",
srcs = select({
":metric_server_elided": ["metric_server_elided.go"],
"//conditions:default": ["metric_server_embedded.go"],
}),
outs = ["metric_server.go"],
cmd = "cat < $(SRCS) > $(OUTS)",
)
go_library(
name = "cmd",
srcs = [
@@ -50,6 +75,7 @@ go_library(
"wait.go",
"write_control.go",
],
force_add_state_pkg = True,
visibility = ["//runsc:__subpackages__"],
deps = [
"//pkg/abi/linux",
@@ -70,7 +96,6 @@ go_library(
"//pkg/unet",
"//pkg/urpc",
"//runsc/boot",
"//runsc/cmd/metricserver",
"//runsc/cmd/metricserver/metricservercmd",
"//runsc/cmd/util",
"//runsc/config",
@@ -88,7 +113,12 @@ go_library(
"@com_github_syndtr_gocapability//capability:go_default_library",
"@org_golang_google_protobuf//encoding/prototext:go_default_library",
"@org_golang_x_sys//unix:go_default_library",
],
] + select({
":metric_server_elided": [],
"//conditions:default": [
"//runsc/cmd/metricserver",
],
}),
)
go_test(
+35
View File
@@ -0,0 +1,35 @@
// Copyright 2022 The gVisor 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 cmd
import (
"context"
"github.com/google/subcommands"
"gvisor.dev/gvisor/runsc/cmd/metricserver/metricservercmd"
"gvisor.dev/gvisor/runsc/cmd/util"
"gvisor.dev/gvisor/runsc/flag"
)
// MetricServer implements subcommands.Command for the "metric-server" command.
type MetricServer struct {
metricservercmd.Cmd
}
// Execute implements subcommands.Command.Execute.
func (m *MetricServer) Execute(ctx context.Context, f *flag.FlagSet, args ...any) subcommands.ExitStatus {
util.Fatalf("this build does not support the metric-server subcommand")
return subcommands.ExitFailure
}
+4 -1
View File
@@ -11,6 +11,9 @@ go_library(
srcs = [
"metricclient.go",
],
data = [
"//runsc/cmd/metricserver:metricserver_bin",
],
visibility = [
"//runsc:__subpackages__",
],
@@ -18,8 +21,8 @@ go_library(
"//pkg/cleanup",
"//pkg/prometheus",
"//pkg/sync",
"//pkg/test/testutil",
"//runsc/config",
"//runsc/specutils",
"@com_github_cenkalti_backoff//:go_default_library",
"@com_github_prometheus_common//expfmt",
"@org_golang_x_sys//unix:go_default_library",
+6 -2
View File
@@ -37,8 +37,8 @@ import (
"gvisor.dev/gvisor/pkg/cleanup"
"gvisor.dev/gvisor/pkg/prometheus"
"gvisor.dev/gvisor/pkg/sync"
"gvisor.dev/gvisor/pkg/test/testutil"
"gvisor.dev/gvisor/runsc/config"
"gvisor.dev/gvisor/runsc/specutils"
)
// MetricClient implements an HTTP client that can spawn and connect to a running runsc metrics
@@ -179,6 +179,10 @@ func (c *MetricClient) HealthCheck(ctx context.Context) error {
// A running server must be stopped before a new one can be successfully started.
// baseConf is used for passing other flags to the server, e.g. debug log directory.
func (c *MetricClient) SpawnServer(ctx context.Context, baseConf *config.Config, extraArgs ...string) error {
metricServerBinPath, err := testutil.FindFile("runsc/cmd/metricserver/metricserver_bin")
if err != nil {
return fmt.Errorf("cannot find metricserver_bin: %w", err)
}
c.mu.Lock()
defer c.mu.Unlock()
if c.server != nil {
@@ -198,7 +202,7 @@ func (c *MetricClient) SpawnServer(ctx context.Context, baseConf *config.Config,
overriddenConf := *baseConf
overriddenConf.MetricServer = c.addr
overriddenConf.RootDir = c.rootDir
c.server = exec.Command(specutils.ExePath, overriddenConf.ToFlags()...)
c.server = exec.Command(metricServerBinPath, overriddenConf.ToFlags()...)
cu := cleanup.Make(func() {
c.server = nil
})
+6 -2
View File
@@ -138,7 +138,7 @@ def go_imports(name, src, out):
cmd = ("$(location @org_golang_x_tools//cmd/goimports:goimports) $(SRCS) > $@"),
)
def go_library(name, srcs, deps = [], imports = [], stateify = True, marshal = False, marshal_debug = False, nogo = True, **kwargs):
def go_library(name, srcs, deps = [], imports = [], stateify = True, force_add_state_pkg = False, marshal = False, marshal_debug = False, nogo = True, **kwargs):
"""Wraps the standard go_library and does stateification and marshalling.
The recommended way is to use this rule with mostly identical configuration as the native
@@ -160,6 +160,10 @@ def go_library(name, srcs, deps = [], imports = [], stateify = True, marshal = F
deps: the library dependencies.
imports: imports required for stateify.
stateify: whether statify is enabled (default: true).
force_add_state_pkg: whether to skip checking whether the state package
is included in `deps`, and to just instead include it outright.
This allows `go_library` to be used in conjunction with `select`
statements in `deps`.
marshal: whether marshal is enabled (default: false).
marshal_debug: whether the gomarshal tools emits debugging output (default: false).
nogo: enable nogo analysis.
@@ -192,7 +196,7 @@ def go_library(name, srcs, deps = [], imports = [], stateify = True, marshal = F
for suffix in state_sets.keys()
]
if "//pkg/state" not in all_deps:
if force_add_state_pkg or "//pkg/state" not in all_deps:
all_deps = all_deps + ["//pkg/state"]
if marshal: