-
Notifications
You must be signed in to change notification settings - Fork 693
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add custom-analyzer cmd (#1207)
* feat: add custom analyzer management capability Introduced the ability to manage custom analyzers in the K8sGPT application, enabling users to add, deploy, and configure custom analyzers from various sources. This enhancement supports extending the application's analytical capabilities by integrating external analysis tools, thus offering more flexibility and customization options to meet specific user needs. Signed-off-by: Matthis Holleville <[email protected]> * feat: enhance custom analyzer management with removal functionality Introduced the ability to remove custom analyzers, streamlining the management process and ensuring flexibility in custom analyzer configuration. This enhancement addresses the need for dynamic customization and maintenance of analyzer setups, facilitating easier updates and modifications to the analysis environment. Signed-off-by: Matthis Holleville <[email protected]> * feat: add list command to customAnalyzer for displaying configured analyzers Implemented a new list command within the customAnalyzer module to enable users to view all configured custom analyzers. This enhancement aims to improve usability by providing a straightforward method for users to inspect their custom analyzer configurations directly from the command line. Signed-off-by: Matthis Holleville <[email protected]> * feat: add support for listing, adding, and removing custom analyzers This update introduces commands to manage custom analyzers in the k8sgpt tool, enhancing flexibility and control over analyzer configurations without the need for direct installation or docker dependency. Signed-off-by: Matthis Holleville <[email protected]> * feat: support private docker image authentication for custom analyzers Added authentication support for pulling private Docker images when adding custom analyzers, enhancing security and access control. Signed-off-by: Matthis Holleville <[email protected]> * feat: remove Docker custom analyzer installation Removed the installation and deployment functionality for custom analyzers, streamlining the process of adding analyzers. This change focuses on simplifying the configuration by eliminating the need for specifying installation types, package URLs, and authentication details for Docker images. The goal is to enhance user experience by making the addition of custom analyzers more straightforward and less error-prone. Signed-off-by: Matthis Holleville <[email protected]> * fix: remove unused packageUrl Signed-off-by: Matthis Holleville <[email protected]> * feat: update add command description to reflect broader functionality Signed-off-by: Matthis Holleville <[email protected]> * feat: Add name validation for custom analyzer creation To ensure the integrity and consistency of analyzer names, we introduced a validation step that checks the format of the name against a predefined regex pattern. This change aims to prevent the creation of analyzers with invalid names, enhancing the system's reliability and usability. Signed-off-by: Matthis Holleville <[email protected]> * feat: refactor customAnalyzer package for consistent naming Refactored the customAnalyzer package and its references to use consistent snake_case naming for improved code readability and alignment with Go naming conventions. Signed-off-by: Matthis Holleville <[email protected]> --------- Signed-off-by: Matthis Holleville <[email protected]> Signed-off-by: AlexsJones <[email protected]>
- Loading branch information
1 parent
fd08097
commit f50440f
Showing
9 changed files
with
345 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
Copyright 2023 The K8sGPT Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package customanalyzer | ||
|
||
import ( | ||
"os" | ||
|
||
"github.com/fatih/color" | ||
customAnalyzer "github.com/k8sgpt-ai/k8sgpt/pkg/custom_analyzer" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var ( | ||
name string | ||
url string | ||
port int | ||
) | ||
|
||
var addCmd = &cobra.Command{ | ||
Use: "add", | ||
Aliases: []string{"add"}, | ||
Short: "This command will add a custom analyzer from source", | ||
Long: "This command allows you to add/remote/list an existing custom analyzer.", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
err := viper.UnmarshalKey("custom_analyzers", &configCustomAnalyzer) | ||
if err != nil { | ||
color.Red("Error: %v", err) | ||
os.Exit(1) | ||
} | ||
analyzer := customAnalyzer.NewCustomAnalyzer() | ||
|
||
// Check if configuration is valid | ||
err = analyzer.Check(configCustomAnalyzer, name, url, port) | ||
if err != nil { | ||
color.Red("Error adding custom analyzer: %s", err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
configCustomAnalyzer = append(configCustomAnalyzer, customAnalyzer.CustomAnalyzerConfiguration{ | ||
Name: name, | ||
Connection: customAnalyzer.Connection{ | ||
Url: url, | ||
Port: port, | ||
}, | ||
}) | ||
|
||
viper.Set("custom_analyzers", configCustomAnalyzer) | ||
if err := viper.WriteConfig(); err != nil { | ||
color.Red("Error writing config file: %s", err.Error()) | ||
os.Exit(1) | ||
} | ||
color.Green("%s added to the custom analyzers config list", name) | ||
|
||
}, | ||
} | ||
|
||
func init() { | ||
addCmd.Flags().StringVarP(&name, "name", "n", "my-custom-analyzer", "Name of the custom analyzer.") | ||
addCmd.Flags().StringVarP(&url, "url", "u", "localhost", "URL for the custom analyzer connection.") | ||
addCmd.Flags().IntVarP(&port, "port", "r", 8085, "Port for the custom analyzer connection.") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
Copyright 2023 The K8sGPT Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package customanalyzer | ||
|
||
import ( | ||
customAnalyzer "github.com/k8sgpt-ai/k8sgpt/pkg/custom_analyzer" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var configCustomAnalyzer []customAnalyzer.CustomAnalyzerConfiguration | ||
|
||
// authCmd represents the auth command | ||
var CustomAnalyzerCmd = &cobra.Command{ | ||
Use: "custom-analyzer", | ||
Short: "Manage a custom analyzer", | ||
Long: `This command allows you to manage custom analyzers, including adding, removing, and listing them.`, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if len(args) == 0 { | ||
_ = cmd.Help() | ||
return | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
// add subcommand to add custom analyzer | ||
CustomAnalyzerCmd.AddCommand(addCmd) | ||
// remove subcomment to remove custom analyzer | ||
CustomAnalyzerCmd.AddCommand(removeCmd) | ||
// list subcomment to list custom analyzer | ||
CustomAnalyzerCmd.AddCommand(listCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
Copyright 2023 The K8sGPT Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package customanalyzer | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/fatih/color" | ||
customAnalyzer "github.com/k8sgpt-ai/k8sgpt/pkg/custom_analyzer" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var details bool | ||
|
||
var listCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "List configured custom analyzers", | ||
Long: "The list command displays a list of configured custom analyzers", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
|
||
// get custom_analyzers configuration | ||
err := viper.UnmarshalKey("custom_analyzers", &configCustomAnalyzer) | ||
if err != nil { | ||
color.Red("Error: %v", err) | ||
os.Exit(1) | ||
} | ||
|
||
// Get list of all Custom Analyers configured | ||
fmt.Print(color.YellowString("Active: \n")) | ||
for _, analyzer := range configCustomAnalyzer { | ||
fmt.Printf("> %s\n", color.GreenString(analyzer.Name)) | ||
if details { | ||
printDetails(analyzer) | ||
} | ||
} | ||
}, | ||
} | ||
|
||
func init() { | ||
listCmd.Flags().BoolVar(&details, "details", false, "Print custom analyzers configuration details") | ||
} | ||
|
||
func printDetails(analyzer customAnalyzer.CustomAnalyzerConfiguration) { | ||
fmt.Printf(" - Url: %s\n", analyzer.Connection.Url) | ||
fmt.Printf(" - Port: %d\n", analyzer.Connection.Port) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
Copyright 2023 The K8sGPT Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package customanalyzer | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
|
||
"github.com/fatih/color" | ||
"github.com/spf13/cobra" | ||
"github.com/spf13/viper" | ||
) | ||
|
||
var ( | ||
names string | ||
) | ||
|
||
var removeCmd = &cobra.Command{ | ||
Use: "remove", | ||
Short: "Remove custom analyzer(s)", | ||
Long: "The command to remove custom analyzer(s)", | ||
PreRun: func(cmd *cobra.Command, args []string) { | ||
// Ensure that the "names" flag is provided before running the command | ||
_ = cmd.MarkFlagRequired("names") | ||
}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
if names == "" { | ||
// Display an error message and show command help if "names" is not set | ||
color.Red("Error: names must be set.") | ||
_ = cmd.Help() | ||
return | ||
} | ||
// Split the provided names by comma | ||
inputCustomAnalyzers := strings.Split(names, ",") | ||
|
||
// Load the custom analyzers from the configuration file | ||
err := viper.UnmarshalKey("custom_analyzers", &configCustomAnalyzer) | ||
if err != nil { | ||
// Display an error message if the configuration cannot be loaded | ||
color.Red("Error: %v", err) | ||
os.Exit(1) | ||
} | ||
|
||
// Iterate over each input analyzer name | ||
for _, inputAnalyzer := range inputCustomAnalyzers { | ||
foundAnalyzer := false | ||
// Search for the analyzer in the current configuration | ||
for i, analyzer := range configCustomAnalyzer { | ||
if analyzer.Name == inputAnalyzer { | ||
foundAnalyzer = true | ||
|
||
// Remove the analyzer from the configuration list | ||
configCustomAnalyzer = append(configCustomAnalyzer[:i], configCustomAnalyzer[i+1:]...) | ||
color.Green("%s deleted from the custom analyzer list", analyzer.Name) | ||
break | ||
} | ||
} | ||
if !foundAnalyzer { | ||
// Display an error if the analyzer is not found in the configuration | ||
color.Red("Error: %s does not exist in configuration file. Please use k8sgpt custom-analyzer add.", inputAnalyzer) | ||
os.Exit(1) | ||
} | ||
} | ||
|
||
// Save the updated configuration back to the file | ||
viper.Set("custom_analyzers", configCustomAnalyzer) | ||
if err := viper.WriteConfig(); err != nil { | ||
// Display an error if the configuration cannot be written | ||
color.Red("Error writing config file: %s", err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
}, | ||
} | ||
|
||
func init() { | ||
// add flag for names | ||
removeCmd.Flags().StringVarP(&names, "names", "n", "", "Custom analyzers to remove (separated by a comma)") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.