@@ -11,36 +11,27 @@ import (
1111 "fmt"
1212 "os"
1313 "os/signal"
14- "sync"
1514 "syscall"
1615
1716 "github.com/siderolabs/gen/ensure"
18- "github.com/siderolabs/go-debug "
17+ "github.com/siderolabs/talos/pkg/machinery/config/merge "
1918 "github.com/spf13/cobra"
2019 "go.uber.org/zap"
2120 "go.uber.org/zap/zapcore"
2221
2322 "github.com/siderolabs/omni/client/pkg/constants"
2423 "github.com/siderolabs/omni/client/pkg/panichandler"
24+ "github.com/siderolabs/omni/cmd/omni/pkg/app"
25+ "github.com/siderolabs/omni/internal/pkg/auth/actor"
2526 "github.com/siderolabs/omni/internal/pkg/config"
2627 "github.com/siderolabs/omni/internal/version"
2728)
2829
29- func runDebugServer (ctx context.Context , logger * zap.Logger , bindEndpoint string ) {
30- debugLogFunc := func (msg string ) {
31- logger .Info (msg )
32- }
33-
34- if err := debug .ListenAndServe (ctx , bindEndpoint , debugLogFunc ); err != nil {
35- logger .Panic ("failed to start debug server" , zap .Error (err ))
36- }
37- }
38-
3930// rootCmd represents the base command when called without any subcommands.
4031var rootCmd = & cobra.Command {
4132 Use : "omni" ,
4233 Short : "Omni Kubernetes management platform service" ,
43- Long : "This executable runs both frontend and backend" ,
34+ Long : "This executable runs both Omni frontend and Omni backend" ,
4435 SilenceUsage : true ,
4536 Version : version .Tag ,
4637 RunE : func (* cobra.Command , []string ) error {
@@ -82,20 +73,14 @@ var rootCmd = &cobra.Command{
8273 cancel ()
8374 }, logger )
8475
85- var configs []* config.Params
86-
87- if rootCmdArgs .configPath != "" {
88- var cfg * config.Params
89-
90- cfg , err = config .LoadFromFile (rootCmdArgs .configPath )
91- if err != nil {
92- return err
93- }
94-
95- configs = append (configs , cfg )
76+ config , err := app .PrepareConfig (logger , cmdConfig )
77+ if err != nil {
78+ return err
9679 }
9780
98- return RunService (ctx , logger , append (configs , cmdConfig )... )
81+ ctx = actor .MarkContextAsInternalActor (ctx )
82+
83+ return app .Run (ctx , config , logger )
9984 },
10085}
10186
@@ -104,13 +89,35 @@ var rootCmdArgs struct {
10489 debug bool
10590}
10691
107- // RootCmd returns the root command.
108- func RootCmd () * cobra.Command { return initOnce () }
92+ // Execute the command.
93+ func Execute () error {
94+ rootCmd .Flags ().StringVar (& rootCmdArgs .configPath , "config-path" , "" , "load the config from the file, flags have bigger priority" )
95+
96+ // Parsing the config path flag early to let it populate the initial configuration
97+ // ignore all errors, they will be handled in the Execute
98+ rootCmd .Flags ().Parse (os .Args [1 :]) //nolint:errcheck
99+
100+ if rootCmdArgs .configPath == "" {
101+ return newCommand ().Execute ()
102+ }
103+
104+ cfg , err := config .LoadFromFile (rootCmdArgs .configPath )
105+ if err != nil {
106+ return err
107+ }
108+
109+ // override the cmdConfig with the config loaded from the file
110+ if err := merge .Merge (cmdConfig , cfg ); err != nil {
111+ return err
112+ }
113+
114+ return newCommand ().Execute ()
115+ }
109116
110- var cmdConfig = config .InitDefault ()
117+ var cmdConfig = config .Default ()
111118
112- var initOnce = sync . OnceValue ( func () * cobra.Command {
113- rootCmd .Flags ().BoolVar (& rootCmdArgs .debug , "debug" , false , "enable debug logs." )
119+ func newCommand () * cobra.Command {
120+ rootCmd .Flags ().BoolVar (& rootCmdArgs .debug , "debug" , constants . IsDebugBuild , "enable debug logs." )
114121
115122 rootCmd .Flags ().StringVar (& cmdConfig .Account .ID , "account-id" , cmdConfig .Account .ID , "instance account ID, should never be changed." )
116123 rootCmd .Flags ().StringVar (& cmdConfig .Account .Name , "name" , cmdConfig .Account .Name , "instance user-facing name." )
@@ -124,10 +131,8 @@ var initOnce = sync.OnceValue(func() *cobra.Command {
124131 defineDebugFlags ()
125132 defineEtcdBackupsFlags ()
126133
127- rootCmd .Flags ().StringVar (& rootCmdArgs .configPath , "config-path" , "" , "load the config from the file, flags have bigger priority" )
128-
129134 return rootCmd
130- })
135+ }
131136
132137func defineServiceFlags () {
133138 // API
@@ -146,13 +151,13 @@ func defineServiceFlags() {
146151 rootCmd .Flags ().StringVar (
147152 & cmdConfig .Services .API .KeyFile ,
148153 "key" ,
149- "" ,
154+ cmdConfig . Services . API . KeyFile ,
150155 "TLS key file" ,
151156 )
152157 rootCmd .Flags ().StringVar (
153158 & cmdConfig .Services .API .CertFile ,
154159 "cert" ,
155- "" ,
160+ cmdConfig . Services . API . CertFile ,
156161 "TLS cert file" ,
157162 )
158163
@@ -194,13 +199,13 @@ func defineServiceFlags() {
194199 rootCmd .Flags ().BoolVar (
195200 & cmdConfig .Services .Siderolink .DisableLastEndpoint ,
196201 "siderolink-disable-last-endpoint" ,
197- false ,
202+ cmdConfig . Services . Siderolink . DisableLastEndpoint ,
198203 "do not populate last known peer endpoint for the WireGuard peers" ,
199204 )
200205 rootCmd .Flags ().BoolVar (
201206 & cmdConfig .Services .Siderolink .UseGRPCTunnel ,
202207 "siderolink-use-grpc-tunnel" ,
203- false ,
208+ cmdConfig . Services . Siderolink . UseGRPCTunnel ,
204209 "use gRPC tunnel to wrap WireGuard traffic instead of UDP. When enabled, " +
205210 "the SideroLink connections from Talos machines will be configured to use the tunnel mode, regardless of their individual configuration. " ,
206211 )
@@ -216,9 +221,10 @@ func defineServiceFlags() {
216221 cmdConfig .Services .Siderolink .LogServerPort ,
217222 "port for TCP log server" ,
218223 )
219- rootCmd .Flags ().Var (
224+ rootCmd .Flags ().StringVar (
220225 & cmdConfig .Services .Siderolink .JoinTokensMode ,
221226 "join-tokens-mode" ,
227+ cmdConfig .Services .Siderolink .JoinTokensMode ,
222228 "configures Talos machine join flow to use secure node tokens" ,
223229 )
224230
@@ -331,10 +337,14 @@ func defineServiceFlags() {
331337
332338 // DevServerProxy
333339 rootCmd .Flags ().StringVar (
334- & cmdConfig .Services .DevServerProxy .ProxyTo , "frontend-dst" , "" ,
340+ & cmdConfig .Services .DevServerProxy .ProxyTo ,
341+ "frontend-dst" ,
342+ cmdConfig .Services .DevServerProxy .ProxyTo ,
335343 "destination address non API requests from proxy server." )
336344 rootCmd .Flags ().StringVar (
337- & cmdConfig .Services .DevServerProxy .BindEndpoint , "frontend-bind" , "" ,
345+ & cmdConfig .Services .DevServerProxy .BindEndpoint ,
346+ "frontend-bind" ,
347+ cmdConfig .Services .DevServerProxy .BindEndpoint ,
338348 "proxy server which will redirect all non API requests to the definied frontend server." )
339349}
340350
@@ -540,8 +550,6 @@ func defineStorageFlags() {
540550 "file containing private key to use for decrypting master key slot." ,
541551 )
542552
543- ensure .NoError (rootCmd .MarkFlagRequired ("private-key-source" ))
544-
545553 rootCmd .Flags ().StringSliceVar (
546554 & cmdConfig .Storage .Default .Etcd .PublicKeyFiles ,
547555 "public-key-files" ,
@@ -585,7 +593,7 @@ func defineRegistriesFlags() {
585593 rootCmd .Flags ().StringSliceVar (
586594 & cmdConfig .Registries .Mirrors ,
587595 "registry-mirror" ,
588- [] string {} ,
596+ cmdConfig . Registries . Mirrors ,
589597 "list of registry mirrors to use in format: <registry host>=<mirror URL>" ,
590598 )
591599}
0 commit comments