-
Notifications
You must be signed in to change notification settings - Fork 289
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
663 additions
and
214 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package config | ||
|
||
import "github.com/spf13/cobra" | ||
|
||
// NewCmd returns a new cobra.Command subcommand for config-related operations. | ||
func NewCmd() *cobra.Command { | ||
root := &cobra.Command{ | ||
Use: "config", | ||
Aliases: []string{"cfg"}, | ||
Short: "This command consists of multiple subcommands for working with Botkube configuration", | ||
} | ||
|
||
root.AddCommand( | ||
NewGet(), | ||
) | ||
return root | ||
} |
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,130 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"reflect" | ||
|
||
"github.com/spf13/cobra" | ||
"gopkg.in/yaml.v3" | ||
|
||
"github.com/kubeshop/botkube/internal/cli" | ||
"github.com/kubeshop/botkube/internal/cli/config" | ||
"github.com/kubeshop/botkube/internal/cli/heredoc" | ||
"github.com/kubeshop/botkube/internal/cli/printer" | ||
"github.com/kubeshop/botkube/internal/kubex" | ||
) | ||
|
||
type GetOptions struct { | ||
OmitEmpty bool | ||
Exporter config.ExporterOptions | ||
} | ||
|
||
// NewGet returns a cobra.Command for getting Botkube configuration. | ||
func NewGet() *cobra.Command { | ||
var opts GetOptions | ||
|
||
resourcePrinter := printer.NewForResource(os.Stdout, printer.WithJSON(), printer.WithYAML()) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "get", | ||
Short: "Displays Botkube configuration", | ||
Example: heredoc.WithCLIName(` | ||
# Show configuration for currently installed Botkube | ||
<cli> config get | ||
# Show configuration in JSON format | ||
<cli> config get -ojson | ||
# Save configuration in file | ||
<cli> config get > config.yaml | ||
`, cli.Name), | ||
RunE: func(cmd *cobra.Command, args []string) (err error) { | ||
status := printer.NewStatus(cmd.ErrOrStderr(), "Fetching Botkube configuration") | ||
defer func() { | ||
status.End(err == nil) | ||
}() | ||
|
||
k8sCfg, err := kubex.LoadRestConfigWithMetaInformation() | ||
if err != nil { | ||
return fmt.Errorf("while creating k8s config: %w", err) | ||
} | ||
|
||
err = status.InfoStructFields("Export details:", exportDetails{ | ||
ExporterVersion: opts.Exporter.Tag, | ||
K8sCtx: k8sCfg.CurrentContext, | ||
LookupNamespace: opts.Exporter.BotkubePodNamespace, | ||
LookupPodLabel: opts.Exporter.BotkubePodLabel, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
status.Step("Fetching configuration") | ||
cfg, botkubeVersionStr, err := config.GetFromCluster(cmd.Context(), k8sCfg.K8s, opts.Exporter) | ||
if err != nil { | ||
return fmt.Errorf("while getting configuration: %w", err) | ||
} | ||
status.End(true) | ||
|
||
var raw interface{} | ||
err = yaml.Unmarshal(cfg, &raw) | ||
if err != nil { | ||
return fmt.Errorf("while loading configuration: %w", err) | ||
} | ||
|
||
if opts.OmitEmpty { | ||
status.Step("Removing empty keys from configuration") | ||
raw = removeEmptyValues(raw) | ||
status.End(true) | ||
} | ||
|
||
status.Step("Exported Botkube configuration installed in version %s", botkubeVersionStr) | ||
status.End(true) | ||
|
||
return resourcePrinter.Print(raw) | ||
}, | ||
} | ||
|
||
flags := cmd.Flags() | ||
|
||
flags.BoolVar(&opts.OmitEmpty, "omit-empty-values", true, "Omits empty keys from printed configuration") | ||
|
||
opts.Exporter.RegisterFlags(flags) | ||
|
||
resourcePrinter.RegisterFlags(flags) | ||
|
||
return cmd | ||
} | ||
|
||
type exportDetails struct { | ||
K8sCtx string `pretty:"Kubernetes Context"` | ||
ExporterVersion string `pretty:"Exporter Version"` | ||
LookupNamespace string `pretty:"Lookup Namespace"` | ||
LookupPodLabel string `pretty:"Lookup Pod Label"` | ||
} | ||
|
||
func removeEmptyValues(obj any) any { | ||
switch v := obj.(type) { | ||
case map[string]any: | ||
newObj := make(map[string]any) | ||
for key, value := range v { | ||
if value != nil { | ||
newValue := removeEmptyValues(value) | ||
if newValue != nil { | ||
newObj[key] = newValue | ||
} | ||
} | ||
} | ||
if len(newObj) == 0 { | ||
return nil | ||
} | ||
return newObj | ||
default: | ||
val := reflect.ValueOf(v) | ||
if val.IsZero() { | ||
return nil | ||
} | ||
return obj | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
title: botkube config | ||
--- | ||
|
||
## botkube config | ||
|
||
This command consists of multiple subcommands for working with Botkube configuration | ||
|
||
### Options | ||
|
||
``` | ||
-h, --help help for config | ||
``` | ||
|
||
### Options inherited from parent commands | ||
|
||
``` | ||
-v, --verbose int/string[=simple] Prints more verbose output. Allowed values: 0 - disable, 1 - simple, 2 - trace (default 0 - disable) | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [botkube](botkube.md) - Botkube CLI | ||
* [botkube config get](botkube_config_get.md) - Displays Botkube configuration | ||
|
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,51 @@ | ||
--- | ||
title: botkube config get | ||
--- | ||
|
||
## botkube config get | ||
|
||
Displays Botkube configuration | ||
|
||
``` | ||
botkube config get [flags] | ||
``` | ||
|
||
### Examples | ||
|
||
``` | ||
# Show configuration for currently installed Botkube | ||
botkube config get | ||
# Show configuration in JSON format | ||
botkube config get -ojson | ||
# Save configuration in file | ||
botkube config get > config.yaml | ||
``` | ||
|
||
### Options | ||
|
||
``` | ||
--cfg-exporter-image-registry string Registry for the Config Exporter job image (default "ghcr.io") | ||
--cfg-exporter-image-repo string Repository for the Config Exporter job image (default "kubeshop/botkube-config-exporter") | ||
--cfg-exporter-image-tag string Tag of the Config Exporter job image (default "v9.99.9-dev") | ||
--cfg-exporter-poll-period duration Interval used to check if Config Exporter job was finished (default 1s) | ||
--cfg-exporter-timeout duration Maximum execution time for the Config Exporter job (default 1m0s) | ||
-h, --help help for get | ||
-l, --label string Label used for identifying the Botkube pod (default "app=botkube") | ||
-n, --namespace string Namespace of Botkube pod (default "botkube") | ||
--omit-empty-values Omits empty keys from printed configuration (default true) | ||
-o, --output string Output format. One of: json | yaml (default "yaml") | ||
``` | ||
|
||
### Options inherited from parent commands | ||
|
||
``` | ||
-v, --verbose int/string[=simple] Prints more verbose output. Allowed values: 0 - disable, 1 - simple, 2 - trace (default 0 - disable) | ||
``` | ||
|
||
### SEE ALSO | ||
|
||
* [botkube config](botkube_config.md) - This command consists of multiple subcommands for working with Botkube configuration | ||
|
Oops, something went wrong.