|
| 1 | +package sentinel |
| 2 | + |
| 3 | +import ( |
| 4 | + "log/slog" |
| 5 | + |
| 6 | + "github.com/MatteoMori/sentinel/pkg/sentinel" |
| 7 | + sentinelShared "github.com/MatteoMori/sentinel/pkg/shared" |
| 8 | + "github.com/spf13/cobra" |
| 9 | + "github.com/spf13/viper" |
| 10 | +) |
| 11 | + |
| 12 | +var config sentinelShared.Config |
| 13 | + |
| 14 | +var startSentinel = &cobra.Command{ |
| 15 | + Use: "start", |
| 16 | + Aliases: []string{"start"}, |
| 17 | + Short: "Start Sentinel controller", |
| 18 | + Args: cobra.ExactArgs(0), // 0 arguments |
| 19 | + Run: func(cmd *cobra.Command, args []string) { |
| 20 | + sentinel.Start(config) |
| 21 | + }, |
| 22 | +} |
| 23 | + |
| 24 | +func init() { |
| 25 | + cobra.OnInitialize(initConfig) |
| 26 | + rootCmd.PersistentFlags().IntP("verbosity", "v", 0, "verbosity level (0-2)") |
| 27 | + |
| 28 | + // Viper bindings to Flags |
| 29 | + // This allows Viper to read the flags set by Cobra and use them in the configuration |
| 30 | + viper.BindPFlag("namespaceSelector", rootCmd.PersistentFlags().Lookup("namespaceSelector")) |
| 31 | + viper.BindPFlag("metricsPort", rootCmd.PersistentFlags().Lookup("metricsPort")) |
| 32 | + viper.BindPFlag("verbosity", rootCmd.PersistentFlags().Lookup("verbosity")) |
| 33 | + |
| 34 | + // Viper defaults |
| 35 | + viper.SetDefault("namespaceSelector", map[string]string{"sentinel.io/controlled": "enabled"}) |
| 36 | + viper.SetDefault("metricsPort", "9090") // Default port for Prometheus metrics endpoint |
| 37 | + viper.SetDefault("verbosity", 0) |
| 38 | + viper.SetDefault("extraLabels", []sentinelShared.ExtraLabel{}) // Empty by default |
| 39 | + |
| 40 | + // Start the sentinel command |
| 41 | + rootCmd.AddCommand(startSentinel) |
| 42 | +} |
| 43 | + |
| 44 | +/* |
| 45 | + Sentinel Config |
| 46 | +
|
| 47 | +Here is where we initialize the Sentinel configuration. |
| 48 | +This configuration is used to set up the controller, metrics, and other parameters. |
| 49 | +- Ideally, all Sentinel parameters should have an equivalent here so that humans can override as they want. |
| 50 | +*/ |
| 51 | +func initConfig() { |
| 52 | + viper.SetConfigName("sentinel") // Name of config file without extension |
| 53 | + viper.SetConfigType("yaml") |
| 54 | + viper.AddConfigPath("/etc/sentinel") // Config file expected location |
| 55 | + |
| 56 | + /* |
| 57 | + Viper will read environment variables and use them as configuration values if they match your config keys. |
| 58 | + EXAMPLE: |
| 59 | + export METRICSPORT=12345 --> Viper will use the value from the environment variable instead of the default or config file. |
| 60 | + */ |
| 61 | + viper.AutomaticEnv() |
| 62 | + |
| 63 | + if err := viper.ReadInConfig(); err == nil { |
| 64 | + slog.Info("Config file loaded", "file", viper.ConfigFileUsed()) |
| 65 | + } else { |
| 66 | + slog.Warn("No config file found, using environment variables and defaults", "err", err) |
| 67 | + } |
| 68 | + |
| 69 | + // Load into config struct |
| 70 | + if err := viper.Unmarshal(&config); err != nil { |
| 71 | + slog.Error("Unable to decode config into struct", "err", err) |
| 72 | + } |
| 73 | + |
| 74 | + // Apply fallback defaults if any field is missing |
| 75 | + //shared.ApplyDefaultConfig(&config) |
| 76 | +} |
0 commit comments