Skip to content

Commit d3643ed

Browse files
committed
refactor: move flag names to constants
Signed-off-by: Calum Murray <[email protected]>
1 parent 6ba63d7 commit d3643ed

File tree

1 file changed

+66
-44
lines changed
  • pkg/kubernetes-mcp-server/cmd

1 file changed

+66
-44
lines changed

pkg/kubernetes-mcp-server/cmd/root.go

Lines changed: 66 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,28 @@ kubernetes-mcp-server --port 8080 --disable-multi-cluster
5353
`))
5454
)
5555

56+
const (
57+
flagVersion = "version"
58+
flagLogLevel = "log-level"
59+
flagConfig = "config"
60+
flagSSEPort = "sse-port"
61+
flagHttpPort = "http-port"
62+
flagPort = "port"
63+
flagSSEBaseUrl = "sse-base-url"
64+
flagKubeconfig = "kubeconfig"
65+
flagToolsets = "toolsets"
66+
flagListOutput = "list-output"
67+
flagReadOnly = "read-only"
68+
flagDisableDestructive = "disable-destructive"
69+
flagRequireOAuth = "require-oauth"
70+
flagOAuthAudience = "oauth-audience"
71+
flagValidateToken = "validate-token"
72+
flagAuthorizationURL = "authorization-url"
73+
flagServerUrl = "server-url"
74+
flagCertificateAuthority = "certificate-authority"
75+
flagDisableMultiCluster = "disable-multi-cluster"
76+
)
77+
5678
type MCPServerOptions struct {
5779
Version bool
5880
LogLevel int
@@ -108,33 +130,33 @@ func NewMCPServer(streams genericiooptions.IOStreams) *cobra.Command {
108130
},
109131
}
110132

111-
cmd.Flags().BoolVar(&o.Version, "version", o.Version, "Print version information and quit")
112-
cmd.Flags().IntVar(&o.LogLevel, "log-level", o.LogLevel, "Set the log level (from 0 to 9)")
113-
cmd.Flags().StringVar(&o.ConfigPath, "config", o.ConfigPath, "Path of the config file.")
114-
cmd.Flags().IntVar(&o.SSEPort, "sse-port", o.SSEPort, "Start a SSE server on the specified port")
115-
cmd.Flag("sse-port").Deprecated = "Use --port instead"
116-
cmd.Flags().IntVar(&o.HttpPort, "http-port", o.HttpPort, "Start a streamable HTTP server on the specified port")
117-
cmd.Flag("http-port").Deprecated = "Use --port instead"
118-
cmd.Flags().StringVar(&o.Port, "port", o.Port, "Start a streamable HTTP and SSE HTTP server on the specified port (e.g. 8080)")
119-
cmd.Flags().StringVar(&o.SSEBaseUrl, "sse-base-url", o.SSEBaseUrl, "SSE public base URL to use when sending the endpoint message (e.g. https://example.com)")
120-
cmd.Flags().StringVar(&o.Kubeconfig, "kubeconfig", o.Kubeconfig, "Path to the kubeconfig file to use for authentication")
121-
cmd.Flags().StringSliceVar(&o.Toolsets, "toolsets", o.Toolsets, "Comma-separated list of MCP toolsets to use (available toolsets: "+strings.Join(toolsets.ToolsetNames(), ", ")+"). Defaults to "+strings.Join(o.StaticConfig.Toolsets, ", ")+".")
122-
cmd.Flags().StringVar(&o.ListOutput, "list-output", o.ListOutput, "Output format for resource list operations (one of: "+strings.Join(output.Names, ", ")+"). Defaults to "+o.StaticConfig.ListOutput+".")
123-
cmd.Flags().BoolVar(&o.ReadOnly, "read-only", o.ReadOnly, "If true, only tools annotated with readOnlyHint=true are exposed")
124-
cmd.Flags().BoolVar(&o.DisableDestructive, "disable-destructive", o.DisableDestructive, "If true, tools annotated with destructiveHint=true are disabled")
125-
cmd.Flags().BoolVar(&o.RequireOAuth, "require-oauth", o.RequireOAuth, "If true, requires OAuth authorization as defined in the Model Context Protocol (MCP) specification. This flag is ignored if transport type is stdio")
126-
_ = cmd.Flags().MarkHidden("require-oauth")
127-
cmd.Flags().StringVar(&o.OAuthAudience, "oauth-audience", o.OAuthAudience, "OAuth audience for token claims validation. Optional. If not set, the audience is not validated. Only valid if require-oauth is enabled.")
128-
_ = cmd.Flags().MarkHidden("oauth-audience")
129-
cmd.Flags().BoolVar(&o.ValidateToken, "validate-token", o.ValidateToken, "If true, validates the token against the Kubernetes API Server using TokenReview. Optional. If not set, the token is not validated. Only valid if require-oauth is enabled.")
130-
_ = cmd.Flags().MarkHidden("validate-token")
131-
cmd.Flags().StringVar(&o.AuthorizationURL, "authorization-url", o.AuthorizationURL, "OAuth authorization server URL for protected resource endpoint. If not provided, the Kubernetes API server host will be used. Only valid if require-oauth is enabled.")
132-
_ = cmd.Flags().MarkHidden("authorization-url")
133-
cmd.Flags().StringVar(&o.ServerURL, "server-url", o.ServerURL, "Server URL of this application. Optional. If set, this url will be served in protected resource metadata endpoint and tokens will be validated with this audience. If not set, expected audience is kubernetes-mcp-server. Only valid if require-oauth is enabled.")
134-
_ = cmd.Flags().MarkHidden("server-url")
135-
cmd.Flags().StringVar(&o.CertificateAuthority, "certificate-authority", o.CertificateAuthority, "Certificate authority path to verify certificates. Optional. Only valid if require-oauth is enabled.")
136-
_ = cmd.Flags().MarkHidden("certificate-authority")
137-
cmd.Flags().BoolVar(&o.DisableMultiCluster, "disable-multi-cluster", o.DisableMultiCluster, "Disable multi cluster tools. Optional. If true, all tools will be run against the default cluster/context.")
133+
cmd.Flags().BoolVar(&o.Version, flagVersion, o.Version, "Print version information and quit")
134+
cmd.Flags().IntVar(&o.LogLevel, flagLogLevel, o.LogLevel, "Set the log level (from 0 to 9)")
135+
cmd.Flags().StringVar(&o.ConfigPath, flagConfig, o.ConfigPath, "Path of the config file.")
136+
cmd.Flags().IntVar(&o.SSEPort, flagSSEPort, o.SSEPort, "Start a SSE server on the specified port")
137+
cmd.Flag(flagSSEPort).Deprecated = "Use --port instead"
138+
cmd.Flags().IntVar(&o.HttpPort, flagHttpPort, o.HttpPort, "Start a streamable HTTP server on the specified port")
139+
cmd.Flag(flagHttpPort).Deprecated = "Use --port instead"
140+
cmd.Flags().StringVar(&o.Port, flagPort, o.Port, "Start a streamable HTTP and SSE HTTP server on the specified port (e.g. 8080)")
141+
cmd.Flags().StringVar(&o.SSEBaseUrl, flagSSEBaseUrl, o.SSEBaseUrl, "SSE public base URL to use when sending the endpoint message (e.g. https://example.com)")
142+
cmd.Flags().StringVar(&o.Kubeconfig, flagKubeconfig, o.Kubeconfig, "Path to the kubeconfig file to use for authentication")
143+
cmd.Flags().StringSliceVar(&o.Toolsets, flagToolsets, o.Toolsets, "Comma-separated list of MCP toolsets to use (available toolsets: "+strings.Join(toolsets.ToolsetNames(), ", ")+"). Defaults to "+strings.Join(o.StaticConfig.Toolsets, ", ")+".")
144+
cmd.Flags().StringVar(&o.ListOutput, flagListOutput, o.ListOutput, "Output format for resource list operations (one of: "+strings.Join(output.Names, ", ")+"). Defaults to "+o.StaticConfig.ListOutput+".")
145+
cmd.Flags().BoolVar(&o.ReadOnly, flagReadOnly, o.ReadOnly, "If true, only tools annotated with readOnlyHint=true are exposed")
146+
cmd.Flags().BoolVar(&o.DisableDestructive, flagDisableDestructive, o.DisableDestructive, "If true, tools annotated with destructiveHint=true are disabled")
147+
cmd.Flags().BoolVar(&o.RequireOAuth, flagRequireOAuth, o.RequireOAuth, "If true, requires OAuth authorization as defined in the Model Context Protocol (MCP) specification. This flag is ignored if transport type is stdio")
148+
_ = cmd.Flags().MarkHidden(flagRequireOAuth)
149+
cmd.Flags().StringVar(&o.OAuthAudience, flagOAuthAudience, o.OAuthAudience, "OAuth audience for token claims validation. Optional. If not set, the audience is not validated. Only valid if require-oauth is enabled.")
150+
_ = cmd.Flags().MarkHidden(flagOAuthAudience)
151+
cmd.Flags().BoolVar(&o.ValidateToken, flagValidateToken, o.ValidateToken, "If true, validates the token against the Kubernetes API Server using TokenReview. Optional. If not set, the token is not validated. Only valid if require-oauth is enabled.")
152+
_ = cmd.Flags().MarkHidden(flagValidateToken)
153+
cmd.Flags().StringVar(&o.AuthorizationURL, flagAuthorizationURL, o.AuthorizationURL, "OAuth authorization server URL for protected resource endpoint. If not provided, the Kubernetes API server host will be used. Only valid if require-oauth is enabled.")
154+
_ = cmd.Flags().MarkHidden(flagAuthorizationURL)
155+
cmd.Flags().StringVar(&o.ServerURL, flagServerUrl, o.ServerURL, "Server URL of this application. Optional. If set, this url will be served in protected resource metadata endpoint and tokens will be validated with this audience. If not set, expected audience is kubernetes-mcp-server. Only valid if require-oauth is enabled.")
156+
_ = cmd.Flags().MarkHidden(flagServerUrl)
157+
cmd.Flags().StringVar(&o.CertificateAuthority, flagCertificateAuthority, o.CertificateAuthority, "Certificate authority path to verify certificates. Optional. Only valid if require-oauth is enabled.")
158+
_ = cmd.Flags().MarkHidden(flagCertificateAuthority)
159+
cmd.Flags().BoolVar(&o.DisableMultiCluster, flagDisableMultiCluster, o.DisableMultiCluster, "Disable multi cluster tools. Optional. If true, all tools will be run against the default cluster/context.")
138160

139161
return cmd
140162
}
@@ -161,53 +183,53 @@ func (m *MCPServerOptions) Complete(cmd *cobra.Command) error {
161183
}
162184

163185
func (m *MCPServerOptions) loadFlags(cmd *cobra.Command) {
164-
if cmd.Flag("log-level").Changed {
186+
if cmd.Flag(flagLogLevel).Changed {
165187
m.StaticConfig.LogLevel = m.LogLevel
166188
}
167-
if cmd.Flag("port").Changed {
189+
if cmd.Flag(flagPort).Changed {
168190
m.StaticConfig.Port = m.Port
169-
} else if cmd.Flag("sse-port").Changed {
191+
} else if cmd.Flag(flagSSEPort).Changed {
170192
m.StaticConfig.Port = strconv.Itoa(m.SSEPort)
171-
} else if cmd.Flag("http-port").Changed {
193+
} else if cmd.Flag(flagHttpPort).Changed {
172194
m.StaticConfig.Port = strconv.Itoa(m.HttpPort)
173195
}
174-
if cmd.Flag("sse-base-url").Changed {
196+
if cmd.Flag(flagSSEBaseUrl).Changed {
175197
m.StaticConfig.SSEBaseURL = m.SSEBaseUrl
176198
}
177-
if cmd.Flag("kubeconfig").Changed {
199+
if cmd.Flag(flagKubeconfig).Changed {
178200
m.StaticConfig.KubeConfig = m.Kubeconfig
179201
}
180-
if cmd.Flag("list-output").Changed {
202+
if cmd.Flag(flagListOutput).Changed {
181203
m.StaticConfig.ListOutput = m.ListOutput
182204
}
183-
if cmd.Flag("read-only").Changed {
205+
if cmd.Flag(flagReadOnly).Changed {
184206
m.StaticConfig.ReadOnly = m.ReadOnly
185207
}
186-
if cmd.Flag("disable-destructive").Changed {
208+
if cmd.Flag(flagDisableDestructive).Changed {
187209
m.StaticConfig.DisableDestructive = m.DisableDestructive
188210
}
189-
if cmd.Flag("toolsets").Changed {
211+
if cmd.Flag(flagToolsets).Changed {
190212
m.StaticConfig.Toolsets = m.Toolsets
191213
}
192-
if cmd.Flag("require-oauth").Changed {
214+
if cmd.Flag(flagRequireOAuth).Changed {
193215
m.StaticConfig.RequireOAuth = m.RequireOAuth
194216
}
195-
if cmd.Flag("oauth-audience").Changed {
217+
if cmd.Flag(flagOAuthAudience).Changed {
196218
m.StaticConfig.OAuthAudience = m.OAuthAudience
197219
}
198-
if cmd.Flag("validate-token").Changed {
220+
if cmd.Flag(flagValidateToken).Changed {
199221
m.StaticConfig.ValidateToken = m.ValidateToken
200222
}
201-
if cmd.Flag("authorization-url").Changed {
223+
if cmd.Flag(flagAuthorizationURL).Changed {
202224
m.StaticConfig.AuthorizationURL = m.AuthorizationURL
203225
}
204-
if cmd.Flag("server-url").Changed {
226+
if cmd.Flag(flagServerUrl).Changed {
205227
m.StaticConfig.ServerURL = m.ServerURL
206228
}
207-
if cmd.Flag("certificate-authority").Changed {
229+
if cmd.Flag(flagCertificateAuthority).Changed {
208230
m.StaticConfig.CertificateAuthority = m.CertificateAuthority
209231
}
210-
if cmd.Flag("disable-multi-cluster").Changed && m.DisableMultiCluster {
232+
if cmd.Flag(flagDisableMultiCluster).Changed && m.DisableMultiCluster {
211233
m.StaticConfig.ClusterProviderStrategy = config.ClusterProviderDisabled
212234
}
213235
}

0 commit comments

Comments
 (0)