Skip to content

Commit

Permalink
Merge branch 'main' into feat/schema-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
matthisholleville authored Aug 15, 2024
2 parents ded0e10 + 3148b5c commit 9fb3bd1
Show file tree
Hide file tree
Showing 13 changed files with 354 additions and 10 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/build_container.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ jobs:

steps:
- name: Check out code
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4

- name: Extract branch name
id: extract_branch
Expand Down Expand Up @@ -70,7 +70,7 @@ jobs:
RELEASE_REGISTRY: "localhost:5000/k8sgpt"
steps:
- name: Check out code
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4

- name: Set up Docker Buildx
id: buildx
Expand Down Expand Up @@ -115,7 +115,7 @@ jobs:
contents: read # Needed for checking out the repository
steps:
- name: Check out code
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4

- name: Login to GitHub Container Registry
uses: docker/login-action@0d4c9c5ea7693da7b068278f7b52bda2a190a446 # v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/golangci_lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4

- name: golangci-lint
uses: reviewdog/action-golangci-lint@7708105983c614f7a2725e2172908b7709d1c3e4 # v2
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
# Release-please creates a PR that tracks all changes
steps:
- name: Checkout
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4

- uses: google-github-actions/release-please-action@e4dc86ba9405554aeba3c6bb2d169500e7d3b4ee # v4.1.1
id: release
Expand All @@ -41,7 +41,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
with:
fetch-depth: 0
- name: Set up Go
Expand Down Expand Up @@ -76,7 +76,7 @@ jobs:
IMAGE_NAME: k8sgpt
steps:
- name: Checkout
uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4
with:
submodules: recursive

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4

- name: Set up Go
uses: actions/setup-go@0a12ed9d6a96ab950c8f026ed9f722fe0da7ef32 # v5
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,21 @@ This now gives the ability to pass through hostOS information ( from this analyz

_See the docs on how to write a custom analyzer_

_Listing custom analyzers configured_
```
k8sgpt custom-analyzer list
```

_Adding custom analyzer without install_
```
k8sgpt custom-analyzer add --name my-custom-analyzer --port 8085
```

_Removing custom analyzer_
```
k8sgpt custom-analyzer remove --names "my-custom-analyzer,my-custom-analyzer-2"
```

</details>

## Documentation
Expand Down
73 changes: 73 additions & 0 deletions cmd/customAnalyzer/add.go
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.")
}
43 changes: 43 additions & 0 deletions cmd/customAnalyzer/customAnalyzer.go
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)
}
60 changes: 60 additions & 0 deletions cmd/customAnalyzer/list.go
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)

}
90 changes: 90 additions & 0 deletions cmd/customAnalyzer/remove.go
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)")
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/k8sgpt-ai/k8sgpt/cmd/analyze"
"github.com/k8sgpt-ai/k8sgpt/cmd/auth"
"github.com/k8sgpt-ai/k8sgpt/cmd/cache"
customanalyzer "github.com/k8sgpt-ai/k8sgpt/cmd/customAnalyzer"
"github.com/k8sgpt-ai/k8sgpt/cmd/filters"
"github.com/k8sgpt-ai/k8sgpt/cmd/generate"
"github.com/k8sgpt-ai/k8sgpt/cmd/integration"
Expand Down Expand Up @@ -74,6 +75,7 @@ func init() {
rootCmd.AddCommand(integration.IntegrationCmd)
rootCmd.AddCommand(serve.ServeCmd)
rootCmd.AddCommand(cache.CacheCmd)
rootCmd.AddCommand(customanalyzer.CustomAnalyzerCmd)
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", fmt.Sprintf("Default config file (%s/k8sgpt/k8sgpt.yaml)", xdg.ConfigHome))
rootCmd.PersistentFlags().StringVar(&kubecontext, "kubecontext", "", "Kubernetes context to use. Only required if out-of-cluster.")
rootCmd.PersistentFlags().StringVar(&kubeconfig, "kubeconfig", "", "Path to a kubeconfig. Only required if out-of-cluster.")
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/docker/cli v26.1.4+incompatible // indirect
github.com/docker/distribution v2.8.3+incompatible // indirect
github.com/docker/docker v27.0.0+incompatible // indirect
github.com/docker/docker v27.1.1+incompatible // indirect
github.com/docker/docker-credential-helpers v0.8.2 // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-metrics v0.0.1 // indirect
Expand Down
Loading

0 comments on commit 9fb3bd1

Please sign in to comment.