@@ -28,6 +28,7 @@ import (
2828
2929 "github.com/kagent-dev/kagent/go/internal/version"
3030
31+ "k8s.io/apimachinery/pkg/api/resource"
3132 "k8s.io/apimachinery/pkg/types"
3233
3334 "github.com/kagent-dev/kagent/go/controller/translator"
@@ -85,51 +86,71 @@ func init() {
8586 ctrl .SetLogger (zap .New (zap .UseDevMode (true )))
8687}
8788
89+ type Config struct {
90+ Metrics struct {
91+ Addr string
92+ CertPath string
93+ CertName string
94+ CertKey string
95+ }
96+ Webhook struct {
97+ CertPath string
98+ CertName string
99+ CertKey string
100+ }
101+ Streaming struct {
102+ MaxBufSize resource.QuantityValue `default:"1Mi"`
103+ InitialBufSize resource.QuantityValue `default:"4Ki"`
104+ }
105+ LeaderElection bool
106+ ProbeAddr string
107+ SecureMetrics bool
108+ EnableHTTP2 bool
109+ DefaultModelConfig types.NamespacedName
110+ HttpServerAddr string
111+ WatchNamespaces string
112+ A2ABaseUrl string
113+ Database struct {
114+ Type string
115+ Path string
116+ Url string
117+ }
118+ }
119+
88120// nolint:gocyclo
89121func main () {
90- var metricsAddr string
91- var metricsCertPath , metricsCertName , metricsCertKey string
92- var webhookCertPath , webhookCertName , webhookCertKey string
93- var enableLeaderElection bool
94- var probeAddr string
95- var secureMetrics bool
96- var enableHTTP2 bool
97- var defaultModelConfig types.NamespacedName
122+ cfg := Config {}
98123 var tlsOpts []func (* tls.Config )
99- var httpServerAddr string
100- var watchNamespaces string
101- var a2aBaseUrl string
102- var databasePath string
103- var databaseType string
104- var databaseURL string
105-
106- flag .StringVar (& metricsAddr , "metrics-bind-address" , "0" , "The address the metrics endpoint binds to. " +
124+ flag .StringVar (& cfg .Metrics .Addr , "metrics-bind-address" , "0" , "The address the metrics endpoint binds to. " +
107125 "Use :8443 for HTTPS or :8080 for HTTP, or leave as 0 to disable the metrics service." )
108- flag .StringVar (& probeAddr , "health-probe-bind-address" , ":8082" , "The address the probe endpoint binds to." )
109- flag .BoolVar (& enableLeaderElection , "leader-elect" , false ,
126+ flag .StringVar (& cfg . ProbeAddr , "health-probe-bind-address" , ":8082" , "The address the probe endpoint binds to." )
127+ flag .BoolVar (& cfg . LeaderElection , "leader-elect" , false ,
110128 "Enable leader election for controller manager. " +
111129 "Enabling this will ensure there is only one active controller manager." )
112- flag .BoolVar (& secureMetrics , "metrics-secure" , true ,
130+ flag .BoolVar (& cfg . SecureMetrics , "metrics-secure" , true ,
113131 "If set, the metrics endpoint is served securely via HTTPS. Use --metrics-secure=false to use HTTP instead." )
114- flag .StringVar (& webhookCertPath , "webhook-cert-path" , "" , "The directory that contains the webhook certificate." )
115- flag .StringVar (& webhookCertName , "webhook-cert-name" , "tls.crt" , "The name of the webhook certificate file." )
116- flag .StringVar (& webhookCertKey , "webhook-cert-key" , "tls.key" , "The name of the webhook key file." )
117- flag .StringVar (& metricsCertPath , "metrics-cert-path" , "" ,
132+ flag .StringVar (& cfg . Webhook . CertPath , "webhook-cert-path" , "" , "The directory that contains the webhook certificate." )
133+ flag .StringVar (& cfg . Webhook . CertName , "webhook-cert-name" , "tls.crt" , "The name of the webhook certificate file." )
134+ flag .StringVar (& cfg . Webhook . CertKey , "webhook-cert-key" , "tls.key" , "The name of the webhook key file." )
135+ flag .StringVar (& cfg . Metrics . CertPath , "metrics-cert-path" , "" ,
118136 "The directory that contains the metrics server certificate." )
119- flag .StringVar (& metricsCertName , "metrics-cert-name" , "tls.crt" , "The name of the metrics server certificate file." )
120- flag .StringVar (& metricsCertKey , "metrics-cert-key" , "tls.key" , "The name of the metrics server key file." )
121- flag .BoolVar (& enableHTTP2 , "enable-http2" , false ,
137+ flag .StringVar (& cfg . Metrics . CertName , "metrics-cert-name" , "tls.crt" , "The name of the metrics server certificate file." )
138+ flag .StringVar (& cfg . Metrics . CertKey , "metrics-cert-key" , "tls.key" , "The name of the metrics server key file." )
139+ flag .BoolVar (& cfg . EnableHTTP2 , "enable-http2" , false ,
122140 "If set, HTTP/2 will be enabled for the metrics and webhook servers" )
123141
124- flag .StringVar (& defaultModelConfig .Name , "default-model-config-name" , "default-model-config" , "The name of the default model config." )
125- flag .StringVar (& defaultModelConfig .Namespace , "default-model-config-namespace" , kagentNamespace , "The namespace of the default model config." )
126- flag .StringVar (& httpServerAddr , "http-server-address" , ":8083" , "The address the HTTP server binds to." )
127- flag .StringVar (& a2aBaseUrl , "a2a-base-url" , "http://127.0.0.1:8083" , "The base URL of the A2A Server endpoint, as advertised to clients." )
128- flag .StringVar (& databaseType , "database-type" , "sqlite" , "The type of the database to use. Supported values: sqlite, postgres." )
129- flag .StringVar (& databasePath , "sqlite-database-path" , "./kagent.db" , "The path to the SQLite database file." )
130- flag .StringVar (& databaseURL , "postgres-database-url" , "postgres://postgres:kagent@db.kagent.svc.cluster.local:5432/crud" , "The URL of the PostgreSQL database." )
142+ flag .StringVar (& cfg . DefaultModelConfig .Name , "default-model-config-name" , "default-model-config" , "The name of the default model config." )
143+ flag .StringVar (& cfg . DefaultModelConfig .Namespace , "default-model-config-namespace" , kagentNamespace , "The namespace of the default model config." )
144+ flag .StringVar (& cfg . HttpServerAddr , "http-server-address" , ":8083" , "The address the HTTP server binds to." )
145+ flag .StringVar (& cfg . A2ABaseUrl , "a2a-base-url" , "http://127.0.0.1:8083" , "The base URL of the A2A Server endpoint, as advertised to clients." )
146+ flag .StringVar (& cfg . Database . Type , "database-type" , "sqlite" , "The type of the database to use. Supported values: sqlite, postgres." )
147+ flag .StringVar (& cfg . Database . Path , "sqlite-database-path" , "./kagent.db" , "The path to the SQLite database file." )
148+ flag .StringVar (& cfg . Database . Url , "postgres-database-url" , "postgres://postgres:kagent@db.kagent.svc.cluster.local:5432/crud" , "The URL of the PostgreSQL database." )
131149
132- flag .StringVar (& watchNamespaces , "watch-namespaces" , "" , "The namespaces to watch for ." )
150+ flag .StringVar (& cfg .WatchNamespaces , "watch-namespaces" , "" , "The namespaces to watch for ." )
151+
152+ flag .Var (& cfg .Streaming .MaxBufSize , "streaming-max-buf-size" , "The maximum size of the streaming buffer." )
153+ flag .Var (& cfg .Streaming .InitialBufSize , "streaming-initial-buf-size" , "The initial size of the streaming buffer." )
133154
134155 opts := zap.Options {
135156 Development : true ,
@@ -139,6 +160,9 @@ func main() {
139160
140161 logger := zap .New (zap .UseFlagOptions (& opts ))
141162
163+ logger .Info ("Starting KAgent Controller" , "version" , Version , "git_commit" , GitCommit , "build_date" , BuildDate )
164+ logger .Info ("Config" , "config" , cfg )
165+
142166 ctrl .SetLogger (logger )
143167
144168 goruntime .SetMaxProcs (logger )
@@ -156,7 +180,7 @@ func main() {
156180 c .NextProtos = []string {"http/1.1" }
157181 }
158182
159- if ! enableHTTP2 {
183+ if ! cfg . EnableHTTP2 {
160184 tlsOpts = append (tlsOpts , disableHTTP2 )
161185 }
162186
@@ -166,14 +190,14 @@ func main() {
166190 // Initial webhook TLS options
167191 webhookTLSOpts := tlsOpts
168192
169- if len (webhookCertPath ) > 0 {
193+ if len (cfg . Webhook . CertPath ) > 0 {
170194 setupLog .Info ("Initializing webhook certificate watcher using provided certificates" ,
171- "webhook-cert-path" , webhookCertPath , "webhook-cert-name" , webhookCertName , "webhook-cert-key" , webhookCertKey )
195+ "webhook-cert-path" , cfg . Webhook . CertPath , "webhook-cert-name" , cfg . Webhook . CertName , "webhook-cert-key" , cfg . Webhook . CertKey )
172196
173197 var err error
174198 webhookCertWatcher , err = certwatcher .New (
175- filepath .Join (webhookCertPath , webhookCertName ),
176- filepath .Join (webhookCertPath , webhookCertKey ),
199+ filepath .Join (cfg . Webhook . CertPath , cfg . Webhook . CertName ),
200+ filepath .Join (cfg . Webhook . CertPath , cfg . Webhook . CertKey ),
177201 )
178202 if err != nil {
179203 setupLog .Error (err , "Failed to initialize webhook certificate watcher" )
@@ -196,12 +220,12 @@ func main() {
196220 // - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.20.0/pkg/metrics/server
197221 // - https://book.kubebuilder.io/reference/metrics.html
198222 metricsServerOptions := metricsserver.Options {
199- BindAddress : metricsAddr ,
200- SecureServing : secureMetrics ,
223+ BindAddress : cfg . Metrics . Addr ,
224+ SecureServing : cfg . SecureMetrics ,
201225 TLSOpts : tlsOpts ,
202226 }
203227
204- if secureMetrics {
228+ if cfg . SecureMetrics {
205229 // FilterProvider is used to protect the metrics endpoint with authn/authz.
206230 // These configurations ensure that only authorized users and service accounts
207231 // can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
@@ -217,14 +241,14 @@ func main() {
217241 // - [METRICS-WITH-CERTS] at config/default/kustomization.yaml to generate and use certificates
218242 // managed by cert-manager for the metrics server.
219243 // - [PROMETHEUS-WITH-CERTS] at config/prometheus/kustomization.yaml for TLS certification.
220- if len (metricsCertPath ) > 0 {
244+ if len (cfg . Metrics . CertPath ) > 0 {
221245 setupLog .Info ("Initializing metrics certificate watcher using provided certificates" ,
222- "metrics-cert-path" , metricsCertPath , "metrics-cert-name" , metricsCertName , "metrics-cert-key" , metricsCertKey )
246+ "metrics-cert-path" , cfg . Metrics . CertPath , "metrics-cert-name" , cfg . Metrics . CertName , "metrics-cert-key" , cfg . Metrics . CertKey )
223247
224248 var err error
225249 metricsCertWatcher , err = certwatcher .New (
226- filepath .Join (metricsCertPath , metricsCertName ),
227- filepath .Join (metricsCertPath , metricsCertKey ),
250+ filepath .Join (cfg . Metrics . CertPath , cfg . Metrics . CertName ),
251+ filepath .Join (cfg . Metrics . CertPath , cfg . Metrics . CertKey ),
228252 )
229253 if err != nil {
230254 setupLog .Error (err , "to initialize metrics certificate watcher" , "error" , err )
@@ -237,14 +261,14 @@ func main() {
237261 }
238262
239263 // filter out invalid namespaces from the watchNamespaces flag (comma separated list)
240- watchNamespacesList := filterValidNamespaces (strings .Split (watchNamespaces , "," ))
264+ watchNamespacesList := filterValidNamespaces (strings .Split (cfg . WatchNamespaces , "," ))
241265
242266 mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
243267 Scheme : scheme ,
244268 Metrics : metricsServerOptions ,
245269 WebhookServer : webhookServer ,
246- HealthProbeBindAddress : probeAddr ,
247- LeaderElection : enableLeaderElection ,
270+ HealthProbeBindAddress : cfg . ProbeAddr ,
271+ LeaderElection : cfg . LeaderElection ,
248272 LeaderElectionID : "0e9f6799.kagent.dev" ,
249273 Cache : cache.Options {
250274 DefaultNamespaces : configureNamespaceWatching (watchNamespacesList ),
@@ -268,12 +292,12 @@ func main() {
268292
269293 // Initialize database
270294 dbManager , err := database .NewManager (& database.Config {
271- DatabaseType : database .DatabaseType (databaseType ),
295+ DatabaseType : database .DatabaseType (cfg . Database . Type ),
272296 SqliteConfig : & database.SqliteConfig {
273- DatabasePath : databasePath ,
297+ DatabasePath : cfg . Database . Path ,
274298 },
275299 PostgresConfig : & database.PostgresConfig {
276- URL : databaseURL ,
300+ URL : cfg . Database . Url ,
277301 },
278302 })
279303 if err != nil {
@@ -293,21 +317,23 @@ func main() {
293317
294318 apiTranslator := translator .NewAdkApiTranslator (
295319 kubeClient ,
296- defaultModelConfig ,
320+ cfg . DefaultModelConfig ,
297321 )
298322
299323 a2aHandler := a2a .NewA2AHttpMux (httpserver .APIPathA2A )
300324
301325 a2aReconciler := a2a_reconciler .NewReconciler (
302326 a2aHandler ,
303- a2aBaseUrl + httpserver .APIPathA2A ,
327+ cfg .A2ABaseUrl + httpserver .APIPathA2A ,
328+ int (cfg .Streaming .MaxBufSize .Value ()),
329+ int (cfg .Streaming .InitialBufSize .Value ()),
304330 )
305331
306332 rcnclr := reconciler .NewKagentReconciler (
307333 apiTranslator ,
308334 kubeClient ,
309335 dbClient ,
310- defaultModelConfig ,
336+ cfg . DefaultModelConfig ,
311337 a2aReconciler ,
312338 )
313339
@@ -383,7 +409,7 @@ func main() {
383409 }
384410
385411 httpServer , err := httpserver .NewHTTPServer (httpserver.ServerConfig {
386- BindAddr : httpServerAddr ,
412+ BindAddr : cfg . HttpServerAddr ,
387413 KubeClient : kubeClient ,
388414 A2AHandler : a2aHandler ,
389415 WatchedNamespaces : watchNamespacesList ,
0 commit comments