Skip to content

Feat: Add --readonly and register tools in two separate locations #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions internal/config/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package config

import (
"flag"
"slices"
"strings"
)

Expand All @@ -10,6 +11,10 @@ type Options struct {
// AllowedContexts is a list of k8s contexts that users are allowed to access
// If empty, all contexts are allowed
AllowedContexts []string

// Readonly determine if tools that 'write' to the cluster are
// registered and advertised to clients.
Readonly bool
}

// GlobalOptions contains the parsed command line options
Expand All @@ -19,6 +24,7 @@ var GlobalOptions = &Options{}
func ParseFlags() bool {
var allowedContextsStr string
flag.StringVar(&allowedContextsStr, "allowed-contexts", "", "Comma-separated list of allowed k8s contexts. If empty, all contexts are allowed")
flag.BoolVar(&GlobalOptions.Readonly, "readonly", false, "Disables any tool which can write changes to the cluster. If not specified, all tools are allowed")

// Add other flags here

Expand Down Expand Up @@ -52,11 +58,5 @@ func IsContextAllowed(contextName string) bool {
return true
}

for _, allowed := range GlobalOptions.AllowedContexts {
if allowed == contextName {
return true
}
}

return false
return slices.Contains(GlobalOptions.AllowedContexts, contextName)
}
13 changes: 11 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ func printHelp() {
println(" version: Print the version, commit and date of the server")
println(" --allowed-contexts=<ctx1,ctx2,...>: Comma-separated list of allowed k8s contexts")
println(" If not specified, all contexts are allowed")
println(" --readonly: Disables any tool which can write changes to the cluster")
println(" If not specified, all tools are available")
}

func getApp() *app.Builder {
return app.
app := app.
NewBuilder().
WithFxOptions(
fx.Provide(func() clientcmd.ClientConfig {
Expand Down Expand Up @@ -127,7 +129,6 @@ func getApp() *app.Builder {
WithTool(tools.NewGetResourceTool).
WithTool(tools.NewListNodesTool).
WithTool(tools.NewListEventsTool).
WithTool(tools.NewPodExecCommandTool).
WithPrompt(prompts.NewListPodsPrompt).
WithPrompt(prompts.NewListNamespacesPrompt).
WithResourceProvider(resources.NewContextsResourceProvider).
Expand All @@ -150,4 +151,12 @@ func getApp() *app.Builder {
},
)),
)

// Skip registering remaining tools if --readonly detected
if config.GlobalOptions.Readonly {
println("Mode=Readonly, Skipping remaining tools")
return app
}

return app.WithTool(tools.NewPodExecCommandTool)
}