|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed |
| 2 | +// under the Apache License Version 2.0. |
| 3 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 4 | +// Copyright 2016-present Datadog, Inc. |
| 5 | + |
| 6 | +// Package dumpconfig implements 'agent dumpconfig'. |
| 7 | +// |
| 8 | +// It initializes the configuration defaults (without reading any datadog.yaml |
| 9 | +// or system-probe.yaml file) and prints the resulting runtime configuration as |
| 10 | +// JSON. It is used by CI to verify that regenerating the config settings code |
| 11 | +// (`dda inv schema.codegen`) does not change the Agent's runtime defaults. |
| 12 | +package dumpconfig |
| 13 | + |
| 14 | +import ( |
| 15 | + "encoding/json" |
| 16 | + "fmt" |
| 17 | + "time" |
| 18 | + |
| 19 | + "github.com/spf13/cobra" |
| 20 | + |
| 21 | + "github.com/DataDog/datadog-agent/cmd/agent/command" |
| 22 | + "github.com/DataDog/datadog-agent/pkg/config/model" |
| 23 | + pkgconfigsetup "github.com/DataDog/datadog-agent/pkg/config/setup" |
| 24 | +) |
| 25 | + |
| 26 | +// cliParams are the command-line arguments for this subcommand |
| 27 | +type cliParams struct { |
| 28 | + *command.GlobalParams |
| 29 | + Target string |
| 30 | +} |
| 31 | + |
| 32 | +// Commands returns a slice of subcommands for the 'agent' command. |
| 33 | +func Commands(globalParams *command.GlobalParams) []*cobra.Command { |
| 34 | + cliParams := &cliParams{ |
| 35 | + GlobalParams: globalParams, |
| 36 | + } |
| 37 | + |
| 38 | + dumpConfigCommand := &cobra.Command{ |
| 39 | + Use: "dumpconfig", |
| 40 | + Short: "Dump the runtime configuration defaults as JSON", |
| 41 | + Long: ``, |
| 42 | + Hidden: true, |
| 43 | + RunE: func(_ *cobra.Command, _ []string) error { |
| 44 | + return run(cliParams) |
| 45 | + }, |
| 46 | + } |
| 47 | + dumpConfigCommand.Flags().StringVar(&cliParams.Target, "target", "", "config to dump: core or system-probe") |
| 48 | + |
| 49 | + return []*cobra.Command{dumpConfigCommand} |
| 50 | +} |
| 51 | + |
| 52 | +func run(cliParams *cliParams) error { |
| 53 | + // Initialize the global config objects with their defaults only. No |
| 54 | + // datadog.yaml or system-probe.yaml file is loaded, so what we dump is the |
| 55 | + // pure set of runtime defaults. |
| 56 | + pkgconfigsetup.InitConfigObjects() |
| 57 | + |
| 58 | + var cfg model.Config |
| 59 | + switch cliParams.Target { |
| 60 | + case "core": |
| 61 | + cfg = pkgconfigsetup.Datadog() |
| 62 | + case "system-probe": |
| 63 | + cfg = pkgconfigsetup.SystemProbe() |
| 64 | + default: |
| 65 | + return fmt.Errorf("unknown target '%s', valid ones are 'core' or 'system-probe'", cliParams.Target) |
| 66 | + } |
| 67 | + |
| 68 | + // json.Marshal sorts map keys, so the output is deterministic across runs. |
| 69 | + data, err := json.MarshalIndent(durationsToString(cfg.AllSettings()), "", " ") |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + fmt.Println(string(data)) |
| 74 | + return nil |
| 75 | +} |
| 76 | + |
| 77 | +// durationsToString recursively walks a value decoded from the config and |
| 78 | +// replaces every time.Duration with its string representation (e.g. "10s"), |
| 79 | +// so durations are dumped as human-readable strings instead of raw |
| 80 | +// nanosecond integers. |
| 81 | +func durationsToString(v interface{}) interface{} { |
| 82 | + switch val := v.(type) { |
| 83 | + case time.Duration: |
| 84 | + return val.String() |
| 85 | + case map[string]interface{}: |
| 86 | + for k, elem := range val { |
| 87 | + val[k] = durationsToString(elem) |
| 88 | + } |
| 89 | + return val |
| 90 | + case []interface{}: |
| 91 | + for i, elem := range val { |
| 92 | + val[i] = durationsToString(elem) |
| 93 | + } |
| 94 | + return val |
| 95 | + default: |
| 96 | + return v |
| 97 | + } |
| 98 | +} |
0 commit comments