Skip to content

Commit 46f46eb

Browse files
committed
Introduce LogStartupConfig(), use in all CLIs in Before() hook
Signed-off-by: Dr. Jan-Philip Gehrcke <[email protected]>
1 parent c614e61 commit 46f46eb

File tree

6 files changed

+70
-57
lines changed

6 files changed

+70
-57
lines changed

cmd/compute-domain-controller/main.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ import (
4040
_ "k8s.io/component-base/metrics/prometheus/workqueue" // register work queues in the default legacy registry
4141

4242
"github.com/NVIDIA/k8s-dra-driver-gpu/internal/info"
43-
"github.com/NVIDIA/k8s-dra-driver-gpu/pkg/flags"
43+
pkgflags "github.com/NVIDIA/k8s-dra-driver-gpu/pkg/flags"
4444
)
4545

4646
const (
@@ -54,9 +54,7 @@ const (
5454
)
5555

5656
type Flags struct {
57-
kubeClientConfig flags.KubeClientConfig
58-
loggingConfig *flags.LoggingConfig
59-
featureGateConfig *flags.FeatureGateConfig
57+
kubeClientConfig pkgflags.KubeClientConfig
6058

6159
podName string
6260
namespace string
@@ -74,7 +72,7 @@ type Flags struct {
7472
type Config struct {
7573
driverName string
7674
flags *Flags
77-
clientsets flags.ClientSets
75+
clientsets pkgflags.ClientSets
7876
mux *http.ServeMux
7977
}
8078

@@ -86,10 +84,8 @@ func main() {
8684
}
8785

8886
func newApp() *cli.App {
89-
flags := &Flags{
90-
loggingConfig: flags.NewLoggingConfig(),
91-
featureGateConfig: flags.NewFeatureGateConfig(),
92-
}
87+
loggingConfig := pkgflags.NewLoggingConfig()
88+
flags := &Flags{}
9389
cliFlags := []cli.Flag{
9490
&cli.StringFlag{
9591
Name: "pod-name",
@@ -157,8 +153,8 @@ func newApp() *cli.App {
157153
}
158154

159155
cliFlags = append(cliFlags, flags.kubeClientConfig.Flags()...)
160-
cliFlags = append(cliFlags, flags.featureGateConfig.Flags()...)
161-
cliFlags = append(cliFlags, flags.loggingConfig.Flags()...)
156+
cliFlags = append(cliFlags, pkgflags.NewFeatureGateConfig().Flags()...)
157+
cliFlags = append(cliFlags, loggingConfig.Flags()...)
162158

163159
app := &cli.App{
164160
Name: "compute-domain-controller",
@@ -170,7 +166,10 @@ func newApp() *cli.App {
170166
if c.Args().Len() > 0 {
171167
return fmt.Errorf("arguments not supported: %v", c.Args().Slice())
172168
}
173-
return flags.loggingConfig.Apply()
169+
// `loggingConfig` is now rendered based on user input such as CLI args
170+
err := loggingConfig.Apply()
171+
pkgflags.LogStartupConfig(flags, loggingConfig)
172+
return err
174173
},
175174
Action: func(c *cli.Context) error {
176175
mux := http.NewServeMux()

cmd/compute-domain-daemon/main.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434

3535
nvapi "github.com/NVIDIA/k8s-dra-driver-gpu/api/nvidia.com/resource/v1beta1"
3636
"github.com/NVIDIA/k8s-dra-driver-gpu/pkg/featuregates"
37-
"github.com/NVIDIA/k8s-dra-driver-gpu/pkg/flags"
37+
pkgflags "github.com/NVIDIA/k8s-dra-driver-gpu/pkg/flags"
3838
)
3939

4040
const (
@@ -55,8 +55,6 @@ type Flags struct {
5555
podName string
5656
podNamespace string
5757
maxNodesPerIMEXDomain int
58-
loggingConfig *flags.LoggingConfig
59-
featureGateConfig *flags.FeatureGateConfig
6058
}
6159

6260
type IMEXConfigTemplateData struct {
@@ -71,11 +69,9 @@ func main() {
7169
}
7270

7371
func newApp() *cli.App {
74-
flags := Flags{
75-
loggingConfig: flags.NewLoggingConfig(),
76-
featureGateConfig: flags.NewFeatureGateConfig(),
77-
}
72+
loggingConfig := pkgflags.NewLoggingConfig()
7873

74+
flags := Flags{}
7975
// Create a wrapper that will be used to gracefully shut down all subcommands
8076
wrapper := func(ctx context.Context, f func(ctx context.Context, cancel context.CancelFunc, flags *Flags) error) error {
8177
// Create a cancelable context from the one passed in
@@ -151,16 +147,19 @@ func newApp() *cli.App {
151147
Destination: &flags.maxNodesPerIMEXDomain,
152148
},
153149
}
154-
cliFlags = append(cliFlags, flags.featureGateConfig.Flags()...)
155-
cliFlags = append(cliFlags, flags.loggingConfig.Flags()...)
150+
cliFlags = append(cliFlags, pkgflags.NewFeatureGateConfig().Flags()...)
151+
cliFlags = append(cliFlags, loggingConfig.Flags()...)
156152

157153
// Create the app
158154
app := &cli.App{
159155
Name: "compute-domain-daemon",
160156
Usage: "compute-domain-daemon manages the IMEX daemon for NVIDIA compute domains.",
161157
Flags: cliFlags,
162158
Before: func(c *cli.Context) error {
163-
return flags.loggingConfig.Apply()
159+
// `loggingConfig` is now rendered based on user input such as CLI args
160+
err := loggingConfig.Apply()
161+
pkgflags.LogStartupConfig(flags, loggingConfig)
162+
return err
164163
},
165164
Commands: []*cli.Command{
166165
{
@@ -197,7 +196,6 @@ func run(ctx context.Context, cancel context.CancelFunc, flags *Flags) error {
197196
podNamespace: flags.podNamespace,
198197
maxNodesPerIMEXDomain: flags.maxNodesPerIMEXDomain,
199198
}
200-
klog.Infof("config: %v", config)
201199

202200
// Support heterogeneous ComputeDomains. That means that a CD may contain
203201
// nodes that do not take part in Multi-Node NVLink communication. On such

cmd/compute-domain-kubelet-plugin/main.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import (
3333
"k8s.io/component-base/logs"
3434

3535
"github.com/NVIDIA/k8s-dra-driver-gpu/internal/info"
36-
"github.com/NVIDIA/k8s-dra-driver-gpu/pkg/flags"
36+
pkgflags "github.com/NVIDIA/k8s-dra-driver-gpu/pkg/flags"
3737
)
3838

3939
const (
@@ -43,9 +43,7 @@ const (
4343
)
4444

4545
type Flags struct {
46-
kubeClientConfig flags.KubeClientConfig
47-
loggingConfig *flags.LoggingConfig
48-
featureGateConfig *flags.FeatureGateConfig
46+
kubeClientConfig pkgflags.KubeClientConfig
4947

5048
nodeName string
5149
namespace string
@@ -60,7 +58,7 @@ type Flags struct {
6058

6159
type Config struct {
6260
flags *Flags
63-
clientsets flags.ClientSets
61+
clientsets pkgflags.ClientSets
6462
}
6563

6664
func (c Config) DriverPluginPath() string {
@@ -75,10 +73,8 @@ func main() {
7573
}
7674

7775
func newApp() *cli.App {
78-
flags := &Flags{
79-
loggingConfig: flags.NewLoggingConfig(),
80-
featureGateConfig: flags.NewFeatureGateConfig(),
81-
}
76+
loggingConfig := pkgflags.NewLoggingConfig()
77+
flags := &Flags{}
8278
cliFlags := []cli.Flag{
8379
&cli.StringFlag{
8480
Name: "node-name",
@@ -145,8 +141,8 @@ func newApp() *cli.App {
145141
},
146142
}
147143
cliFlags = append(cliFlags, flags.kubeClientConfig.Flags()...)
148-
cliFlags = append(cliFlags, flags.featureGateConfig.Flags()...)
149-
cliFlags = append(cliFlags, flags.loggingConfig.Flags()...)
144+
cliFlags = append(cliFlags, pkgflags.NewFeatureGateConfig().Flags()...)
145+
cliFlags = append(cliFlags, loggingConfig.Flags()...)
150146

151147
app := &cli.App{
152148
Name: "compute-domain-kubelet-plugin",
@@ -158,11 +154,12 @@ func newApp() *cli.App {
158154
if c.Args().Len() > 0 {
159155
return fmt.Errorf("arguments not supported: %v", c.Args().Slice())
160156
}
161-
return flags.loggingConfig.Apply()
157+
// `loggingConfig` is now rendered based on user input such as CLI args
158+
err := loggingConfig.Apply()
159+
pkgflags.LogStartupConfig(flags, loggingConfig)
160+
return err
162161
},
163162
Action: func(c *cli.Context) error {
164-
ctx := c.Context
165-
166163
clientSets, err := flags.kubeClientConfig.NewClientSets()
167164
if err != nil {
168165
return fmt.Errorf("create client: %w", err)
@@ -173,7 +170,7 @@ func newApp() *cli.App {
173170
clientsets: clientSets,
174171
}
175172

176-
return RunPlugin(ctx, config)
173+
return RunPlugin(c.Context, config)
177174
},
178175
After: func(c *cli.Context) error {
179176
// Runs after `Action` (regardless of success/error). In urfave cli

cmd/gpu-kubelet-plugin/main.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import (
3232
"k8s.io/klog/v2"
3333

3434
"github.com/NVIDIA/k8s-dra-driver-gpu/internal/info"
35-
"github.com/NVIDIA/k8s-dra-driver-gpu/pkg/flags"
35+
pkgflags "github.com/NVIDIA/k8s-dra-driver-gpu/pkg/flags"
3636
)
3737

3838
const (
@@ -41,9 +41,7 @@ const (
4141
)
4242

4343
type Flags struct {
44-
kubeClientConfig flags.KubeClientConfig
45-
loggingConfig *flags.LoggingConfig
46-
featureGateConfig *flags.FeatureGateConfig
44+
kubeClientConfig pkgflags.KubeClientConfig
4745

4846
nodeName string
4947
namespace string
@@ -59,7 +57,7 @@ type Flags struct {
5957

6058
type Config struct {
6159
flags *Flags
62-
clientsets flags.ClientSets
60+
clientsets pkgflags.ClientSets
6361
}
6462

6563
func (c Config) DriverPluginPath() string {
@@ -74,10 +72,8 @@ func main() {
7472
}
7573

7674
func newApp() *cli.App {
77-
flags := &Flags{
78-
loggingConfig: flags.NewLoggingConfig(),
79-
featureGateConfig: flags.NewFeatureGateConfig(),
80-
}
75+
loggingConfig := pkgflags.NewLoggingConfig()
76+
flags := &Flags{}
8177
cliFlags := []cli.Flag{
8278
&cli.StringFlag{
8379
Name: "node-name",
@@ -151,8 +147,8 @@ func newApp() *cli.App {
151147
},
152148
}
153149
cliFlags = append(cliFlags, flags.kubeClientConfig.Flags()...)
154-
cliFlags = append(cliFlags, flags.featureGateConfig.Flags()...)
155-
cliFlags = append(cliFlags, flags.loggingConfig.Flags()...)
150+
cliFlags = append(cliFlags, pkgflags.NewFeatureGateConfig().Flags()...)
151+
cliFlags = append(cliFlags, loggingConfig.Flags()...)
156152

157153
app := &cli.App{
158154
Name: "gpu-kubelet-plugin",
@@ -164,11 +160,12 @@ func newApp() *cli.App {
164160
if c.Args().Len() > 0 {
165161
return fmt.Errorf("arguments not supported: %v", c.Args().Slice())
166162
}
167-
return flags.loggingConfig.Apply()
163+
// `loggingConfig` is now rendered based on user input such as CLI args
164+
err := loggingConfig.Apply()
165+
pkgflags.LogStartupConfig(flags, loggingConfig)
166+
return err
168167
},
169168
Action: func(c *cli.Context) error {
170-
ctx := c.Context
171-
172169
clientSets, err := flags.kubeClientConfig.NewClientSets()
173170
if err != nil {
174171
return fmt.Errorf("create client: %w", err)
@@ -179,7 +176,7 @@ func newApp() *cli.App {
179176
clientsets: clientSets,
180177
}
181178

182-
return RunPlugin(ctx, config)
179+
return RunPlugin(c.Context, config)
183180
},
184181
After: func(c *cli.Context) error {
185182
// Runs after `Action` (regardless of success/error). In urfave cli

pkg/flags/logging.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,29 @@ import (
2929
)
3030

3131
type LoggingConfig struct {
32-
config *logsapi.LoggingConfiguration
32+
Config *logsapi.LoggingConfiguration
3333
}
3434

3535
// NewLoggingConfig creates a new logging configuration.
3636
func NewLoggingConfig() *LoggingConfig {
3737
return &LoggingConfig{
38-
config: logsapi.NewLoggingConfiguration(),
38+
Config: logsapi.NewLoggingConfiguration(),
3939
}
4040
}
4141

4242
// Apply should be called in a cli.App.Before directly after parsing command
4343
// line flags and before running any code which emits log entries.
4444
// It uses the global feature gate singleton.
4545
func (l *LoggingConfig) Apply() error {
46-
return logsapi.ValidateAndApply(l.config, featuregates.FeatureGates)
46+
return logsapi.ValidateAndApply(l.Config, featuregates.FeatureGates)
4747
}
4848

4949
// Flags returns the flags for logging configuration (NOT including feature gates).
5050
func (l *LoggingConfig) Flags() []cli.Flag {
5151
var fs pflag.FlagSet
5252

5353
// This also registers klog configuration flags (such as -v).
54-
logsapi.AddFlags(l.config, &fs)
54+
logsapi.AddFlags(l.Config, &fs)
5555

5656
// Note: We do NOT add the feature-gates flag here anymore.
5757
// That's handled by FeatureGateConfig to maintain proper separation of concerns.

pkg/flags/utils.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ import (
2121

2222
"github.com/spf13/pflag"
2323
"github.com/urfave/cli/v2"
24+
"k8s.io/apimachinery/pkg/util/dump"
25+
"k8s.io/klog/v2"
26+
27+
"github.com/NVIDIA/k8s-dra-driver-gpu/pkg/featuregates"
2428
)
2529

2630
func pflagToCLI(flag *pflag.Flag, category string) cli.Flag {
@@ -33,3 +37,21 @@ func pflagToCLI(flag *pflag.Flag, category string) cli.Flag {
3337
EnvVars: []string{strings.ToUpper(strings.ReplaceAll(flag.Name, "-", "_"))},
3438
}
3539
}
40+
41+
func LogStartupConfig(parsedFlags any, loggingConfig *LoggingConfig) {
42+
// Always log component startup config (level 0).
43+
klog.Infof("Feature gates: %#v\nVerbosity: %d\nFlags: %s",
44+
// Flat boolean map -- no pretty-printing needed.
45+
featuregates.ToMap(),
46+
loggingConfig.Config.Verbosity,
47+
// Based on go-spew's Sdump(), with indentation. Type information is
48+
// always displayed (cannot be disabled).
49+
dump.Pretty(parsedFlags),
50+
)
51+
52+
// This is a complex object, comprised of largely static default klog
53+
// component configuration. Various parts can be overridden via environment
54+
// variables or CLI flags: it makes sense to log the interpolated config,
55+
// but only on a high verbosity level.
56+
klog.V(6).Infof("Logging config: %s", dump.Pretty(loggingConfig))
57+
}

0 commit comments

Comments
 (0)