2023-07-31 11:03:27 +02:00
|
|
|
package metricsconfig
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"text/template"
|
|
|
|
|
|
|
|
"github.com/Masterminds/sprig/v3"
|
2023-09-06 16:44:50 +02:00
|
|
|
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/command"
|
2023-09-04 17:15:55 +02:00
|
|
|
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/create/templates"
|
2023-07-31 11:03:27 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
type namespaces struct {
|
|
|
|
Include []string `json:"include,omitempty"`
|
|
|
|
Exclude []string `json:"exclude,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type options struct {
|
|
|
|
Name string
|
|
|
|
Namespace string
|
|
|
|
Namespaces namespaces
|
|
|
|
}
|
|
|
|
|
|
|
|
func Command() *cobra.Command {
|
|
|
|
var path string
|
|
|
|
var options options
|
|
|
|
cmd := &cobra.Command{
|
2023-09-13 11:53:19 +02:00
|
|
|
Use: "metrics-config",
|
|
|
|
Short: command.FormatDescription(true, websiteUrl, false, description...),
|
|
|
|
Long: command.FormatDescription(false, websiteUrl, false, description...),
|
|
|
|
Example: command.FormatExamples(examples...),
|
|
|
|
Args: cobra.NoArgs,
|
|
|
|
SilenceUsage: true,
|
2023-09-12 23:47:03 +02:00
|
|
|
RunE: func(cmd *cobra.Command, _ []string) error {
|
2023-07-31 11:03:27 +02:00
|
|
|
tmpl, err := template.New("metricsconfig").Funcs(sprig.HermeticTxtFuncMap()).Parse(templates.MetricsConfigTemplate)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-09-12 23:47:03 +02:00
|
|
|
output := cmd.OutOrStdout()
|
2023-07-31 11:03:27 +02:00
|
|
|
if path != "" {
|
|
|
|
file, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
output = file
|
|
|
|
}
|
|
|
|
return tmpl.Execute(output, options)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
cmd.Flags().StringVarP(&path, "output", "o", "", "Output path (uses standard console output if not set)")
|
|
|
|
cmd.Flags().StringVarP(&options.Name, "name", "n", "kyverno-metrics", "Name")
|
|
|
|
cmd.Flags().StringVar(&options.Namespace, "namespace", "kyverno", "Namespace")
|
|
|
|
cmd.Flags().StringSliceVarP(&options.Namespaces.Include, "include", "i", []string{}, "Included namespaces")
|
|
|
|
cmd.Flags().StringSliceVarP(&options.Namespaces.Exclude, "exclude", "e", []string{}, "Excluded namespaces")
|
|
|
|
return cmd
|
|
|
|
}
|