Skip to content

Commit 5b5474a

Browse files
committed
Issue with gitignore
1 parent 33fd544 commit 5b5474a

6 files changed

Lines changed: 700 additions & 2 deletions

File tree

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
*.so
99
*.dylib
1010

11-
# Project binary
12-
sentinel
11+
# Project binary (only in root directory)
12+
/sentinel
1313

1414
# Test binary, built with `go test -c`
1515
*.test

cmd/sentinel/root.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package sentinel
2+
3+
import (
4+
"fmt"
5+
"os"
6+
7+
"github.com/spf13/cobra"
8+
)
9+
10+
var rootCmd = &cobra.Command{
11+
Use: "sentinel",
12+
Short: "Sentinel is a Kubernetes controller that tracks container images across workloads",
13+
Long: `Sentinel is a Kubernetes controller that tracks container images across your cluster and exposes them as Prometheus metrics`,
14+
Run: func(cmd *cobra.Command, args []string) {
15+
16+
},
17+
}
18+
19+
func Execute() {
20+
if err := rootCmd.Execute(); err != nil {
21+
fmt.Fprintf(os.Stderr, "Whoops. There was an error while executing your CLI '%s'", err)
22+
os.Exit(1)
23+
}
24+
}

cmd/sentinel/start.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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

Comments
 (0)