k8sgpt auth command under the hood — part 3
In the past two days, we have explored k8sgpt and learned how to install it. Now that k8sgpt is installed on your system let’s experiment with its various command-line options.
k8sgpt is the main entry point for the AI-powered Kubernetes debugging tool. This command allows you to interact with Kubernetes clusters and perform various tasks. Let’s break down each command step by step to understand its purpose and how to use it effectively.
- analyze: Identifies and analyzes problems within your Kubernetes cluster using AI-powered insights.
- auth: Authenticates with your chosen backend (e.g., a cloud service or AI provider).
- cache: This works with cached results of previous analyses, which can improve performance by avoiding reanalysis of unchanged data.
- completion: Generates autocompletion scripts for your shell (e.g., Bash, Zsh, Fish).
- custom-analyzer: Manages custom analyzers, allowing you to extend or modify the default analysis behavior.
- filters: Manages filters for analyzing specific Kubernetes resources or types.
- generate: Generates a key for authentication with your backend. Opens a browser to complete the process.
- help: Displays detailed information about any command.
- integration: Integrates external tools with K8sGPT, enabling more comprehensive analysis.
- serve: Runs k8sgpt as a server, enabling it to accept requests and provide debugging as a service.
- version: Prints the version number of k8sgpt and build details.
Let’s start with the auth option in the k8sgpt command, which helps authenticate with a chosen backend, with OpenAI as the default.
k8sgpt auth
Provide the necessary credentials to authenticate with your chosen backend.
Usage:
k8sgpt auth [flags]
k8sgpt auth [command]
Available Commands:
add Add new provider
default Set your default AI backend provider
list List configured providers
remove Remove provider(s)
update Update a backend provider
Flags:
-h, --help help for auth
Global Flags:
--config string Default config file (/Users/plakhera/Library/Application Support/k8sgpt/k8sgpt.yaml)
--kubeconfig string Path to a kubeconfig. Only required if out-of-cluster.
--kubecontext string Kubernetes context to use. Only required if out-of-cluster.
- Next, use the list sub-option to display all the configured providers.
k8sgpt auth list
Default:
> openai
Active:
Unused:
> openai
> localai
> ollama
> azureopenai
> cohere
> amazonbedrock
> amazonsagemaker
> google
> noopai
> huggingface
> googlevertexai
> oci
> ibmwatsonxai
The k8sgpt auth list command output provides information about the AI providers configured and their statuses within your K8sGPT setup. Let’s break it down:
- Default:This indicates the AI provider is currently set as the default for generating AI-based explanations. In this case, the default is openai.
- Active: This shows that the AI provider is actively in use. If no provider is active, this field remains empty, as you see in the output.
- Unused: This lists all configured AI providers that are available but not currently in use.
If you’re wondering how k8sgpt determines that OpenAI will be used as the default backend, let’s take a look at the code.
- Default Backend Configuration:
In default.go, the default backend is specified as "openai"
if no backend is explicitly set.
if providerName == "" {
if configAI.DefaultProvider != "" {
color.Yellow("Your default provider is %s", configAI.DefaultProvider)
} else {
color.Yellow("Your default provider is openai")
}
os.Exit(0)
}
2. Command Initialization:
Now the auth command is initialized in auth.go https://github.com/k8sgpt-ai/k8sgpt/blob/8cd3b2985e4cd61711497fb0436e72b6b8aa3162/cmd/auth/auth.go#L41
// authCmd represents the auth command
var AuthCmd = &cobra.Command{
Use: "auth",
Short: "Authenticate with your chosen backend",
Long: `Provide the necessary credentials to authenticate with your chosen backend.`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
_ = cmd.Help()
return
}
},
}
with subcommands like list
, add
, remove
, default
, and update https://github.com/k8sgpt-ai/k8sgpt/blob/8cd3b2985e4cd61711497fb0436e72b6b8aa3162/cmd/auth/auth.go#L53
func init() {
// add subcommand to list backends
AuthCmd.AddCommand(listCmd)
// add subcommand to create new backend provider
AuthCmd.AddCommand(addCmd)
// add subcommand to remove new backend provider
AuthCmd.AddCommand(removeCmd)
// add subcommand to set default backend provider
AuthCmd.AddCommand(defaultCmd)
// add subcommand to update backend provider
AuthCmd.AddCommand(updateCmd)
}
The default
subcommand specifically checks or sets the default backend.
3. Default Provider in list
Command:
Now when you run k8sgpt auth list command, in the list.go, OpenAI is explicitly treated as the default backend if no provider is set in the configuration https://github.com/k8sgpt-ai/k8sgpt/blob/8cd3b2985e4cd61711497fb0436e72b6b8aa3162/cmd/auth/default.go#L39
// Print the default if it is set
fmt.Print(color.YellowString("Default: \n"))
if configAI.DefaultProvider != "" {
fmt.Printf("> %s\n", color.BlueString(configAI.DefaultProvider))
} else {
fmt.Printf("> %s\n", color.BlueString("openai"))
}
4. Default Setting in default.go
Now going back to default.go, it sets the default backend, validating it against existing configured providers. If no backend is configured, OpenAI remains the fallback. https://github.com/k8sgpt-ai/k8sgpt/blob/8cd3b2985e4cd61711497fb0436e72b6b8aa3162/cmd/auth/default.go#L61C3-L62C42
// Set the default provider
configAI.DefaultProvider = providerName
5. Environment Variable for Backend Initialization:
In add.go
, if the backend
is not provided explicitly during setup, the default backend (openai
) is automatically used https://github.com/k8sgpt-ai/k8sgpt/blob/8cd3b2985e4cd61711497fb0436e72b6b8aa3162/cmd/auth/add.go#L66C1-L75C4
// check if backend is not empty and a valid value
if backend == "" {
color.Yellow(fmt.Sprintf("Warning: backend input is empty, will use the default value: %s", defaultBackend))
backend = defaultBackend
} else {
if !validBackend(ai.Backends, backend) {
color.Red("Error: Backend AI accepted values are '%v'", strings.Join(ai.Backends, ", "))
os.Exit(1)
}
}
6. Backend Validation
The final piece of the puzzle is the backend validation. The code ensures that the specified backend matches the supported backends in the ai
package (ai.Backends
), and OpenAI is part of this list. https://github.com/k8sgpt-ai/k8sgpt/blob/8cd3b2985e4cd61711497fb0436e72b6b8aa3162/cmd/auth/add.go#L66C1-L75C4
// check if backend is not empty and a valid value
if backend == "" {
color.Yellow(fmt.Sprintf("Warning: backend input is empty, will use the default value: %s", defaultBackend))
backend = defaultBackend
} else {
if !validBackend(ai.Backends, backend) {
color.Red("Error: Backend AI accepted values are '%v'", strings.Join(ai.Backends, ", "))
os.Exit(1)
}
}
So to sum up the code path responsible for OpenAI as the default backend
auth.go
- Registers the subcommands and initializes theauth
command.default.go
- Handles setting and verifying the default backend, ensuring OpenAI remains the default if none is set.list.go
- Displays the default provider, treating OpenAI as the fallback.add.go
- Initializes the backend, setting it to OpenAI by default if unspecified.
These code sections collectively ensure that OpenAI is treated as the default backend in the auth
command.
k8sgpt supports multiple providers, offering flexibility and compatibility to suit diverse use cases.
Providers Available
- The output lists a wide range of AI providers that K8sGPT supports. Here’s a summary :
If you want to activate one of these providers, you can do so with the following command:
k8sgpt auth add <provider> — key <API_KEY>
Alternatively, you can use k8sgpt auth add, which defaults to OpenAI as the provider and prompts you to enter your OpenAI key.
k8sgpt auth add
Warning: backend input is empty, will use the default value: openai
Warning: model input is empty, will use the default value: gpt-3.5-turbo
Enter openai Key: openai added to the AI backend provider list
The provided key is stored in K8sGPT’s configuration file. For example /Users/plakhera/Library/Application Support/k8sgpt/k8sgpt.yaml.
If you’re unsure how to obtain or generate a key, visit OpenAI’s API Keys page https://platform.openai.com/api-keys to create one. Alternatively, you can use the k8sgpt generate command, which generates a key for authentication with your backend. This command opens a browser to complete the process. k8sgpt generate
Opening: https://beta.openai.com/account/api-keys to generate a key for openai
Please copy the generated key and run k8sgpt auth add
to add it to your config file
NOTE: The key is stored in plain text. Ensure the file has appropriate permissions, allowing access only to privileged users.
NOTE: If you don’t specify a model (e.g., — model gpt-3.5-turbo), it defaults to gpt-3.5-turbo, which is one of OpenAI’s available models. Also, these warnings are informational, indicating that K8sGPT is falling back to its default settings for both the backend and the model.
If you’re curious about where k8sgpt picks its default model, you’re not alone. In the add.go
file. defaultModel
is defined as a string variable with the value "gpt-3.5-turbo"
.
const (
defaultBackend = "openai"
defaultModel = "gpt-3.5-turbo"
)
Since this is a relatively old model and GPT-4o offers better performance and cost-efficiency compared to GPT-3.5-turbo, I have already created a pull request to address this https://github.com/k8sgpt-ai/k8sgpt/pull/1332
- To verify the activation
k8sgpt auth list
Default:
> openai
Active:
> openai
Unused:
> localai
> ollama
> azureopenai
> cohere
> amazonbedrock
> amazonsagemaker
> google
> noopai
> huggingface
> googlevertexai
> oci
> ibmwatsonxai
- To remove a provider from K8sGPT, you can use the following command:
k8sgpt auth remove -b openai
openai deleted from the AI backend provider list
This command removes the specified provider from K8sGPT’s backend provider list.
- After removal, you can verify the list of available providers:
k8sgpt auth list
Default:
> openai
Active:
Unused:
> openai
> localai
> ollama
> azureopenai
> cohere
> amazonbedrock
> amazonsagemaker
> google
> noopai
> huggingface
> googlevertexai
> oci
> ibmwatsonxai
The removed provider will no longer appear in the list.
Also, you can add multiple providers like openai and ollama to K8sGPT. However, only one provider will be actively used at any given time when you run a command like k8sgpt explain. For example k8sgpt auth add — backend openai — model gpt-4o
k8sgpt auth add — backend ollama — model llama3
Verify Configured Providers:
k8sgpt auth list
Default:
> openai
Active:
> openai
> ollama
Unused:
> localai
> azureopenai
> cohere
> amazonbedrock
> amazonsagemaker
> google
> noopai
> huggingface
> googlevertexai
> oci
> ibmwatsonxai
In Part 4 of this series, we’ll dive into the k8sgpt analyze
command and uncover how it identifies and resolves issues in your Kubernetes cluster.
Conclusion
The k8sgpt auth command is a powerful feature that enables seamless integration with various AI backend providers. Through this exploration, we’ve delved into its inner workings, including how the default provider is determined, the available subcommands, and the process of adding, listing, and removing providers. The default configuration, which falls back to OpenAI, ensures ease of use while offering the flexibility to switch or add other supported providers.
By understanding the mechanics behind the auth command, you can optimize their debugging workflows and experiment with multiple AI backends to suit their specific needs. Whether leveraging the default OpenAI provider or integrating advanced alternatives like Ollama or Hugging Face, k8sgpt offers the adaptability required for efficient Kubernetes debugging.
As you move forward, consider exploring additional commands and features in k8sgpt to utilize this AI-powered Kubernetes tool fully. With its robust capabilities and growing support for diverse AI models, k8sgpt stands out as a versatile Kubernetes troubleshooting and management solution.