From 07b486658079195887d20865fec799b6201df285 Mon Sep 17 00:00:00 2001 From: Eguzki Astiz Lezaun Date: Wed, 18 Dec 2024 23:33:01 +0100 Subject: [PATCH] topology command: persist in local disk Signed-off-by: Eguzki Astiz Lezaun --- cmd/root.go | 11 +++++--- cmd/topology.go | 64 +++++++++++++++++++++++++++++++++++++++++++++ cmd/version.go | 6 ----- pkg/utils/scheme.go | 27 ------------------- 4 files changed, 72 insertions(+), 36 deletions(-) create mode 100644 cmd/topology.go delete mode 100644 pkg/utils/scheme.go diff --git a/cmd/root.go b/cmd/root.go index 6822ecc..c2dd710 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -16,6 +16,9 @@ limitations under the License. package cmd import ( + "context" + "os" + "github.com/spf13/cobra" logf "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/log/zap" @@ -32,6 +35,10 @@ func GetRootCmd(args []string) *cobra.Command { Use: "kuadrantctl", Short: "Kuadrant configuration command line utility", Long: "Kuadrant configuration command line utility", + PersistentPreRun: func(cmd *cobra.Command, args []string) { + logf.SetLogger(zap.New(zap.UseDevMode(verbose), zap.WriteTo(os.Stdout))) + cmd.SetContext(context.Background()) + }, } rootCmd.SetArgs(args) @@ -42,9 +49,7 @@ func GetRootCmd(args []string) *cobra.Command { rootCmd.AddCommand(versionCommand()) rootCmd.AddCommand(generateCommand()) - - loggerOpts := zap.Options{Development: verbose} - logf.SetLogger(zap.New(zap.UseFlagOptions(&loggerOpts))) + rootCmd.AddCommand(topologyCommand()) return rootCmd } diff --git a/cmd/topology.go b/cmd/topology.go new file mode 100644 index 0000000..192f9aa --- /dev/null +++ b/cmd/topology.go @@ -0,0 +1,64 @@ +package cmd + +import ( + "os" + + "github.com/spf13/cobra" + corev1 "k8s.io/api/core/v1" + "k8s.io/client-go/kubernetes/scheme" + "sigs.k8s.io/controller-runtime/pkg/client" + "sigs.k8s.io/controller-runtime/pkg/client/config" + logf "sigs.k8s.io/controller-runtime/pkg/log" +) + +var ( + topologyNS string + topologyOutputFile string +) + +func topologyCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "topology", + Short: "Read kuadrant topology", + Long: "Read kuadrant topology", + RunE: runTopology, + } + + cmd.Flags().StringVarP(&topologyNS, "namespace", "n", "kuadrant-system", "Topology's namespace") + cmd.Flags().StringVarP(&topologyOutputFile, "output", "o", "/dev/stdout", "Output file") + return cmd +} + +func runTopology(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + configuration, err := config.GetConfig() + if err != nil { + return err + } + + k8sClient, err := client.New(configuration, client.Options{Scheme: scheme.Scheme}) + if err != nil { + return err + } + + topologyKey := client.ObjectKey{Name: "topology", Namespace: topologyNS} + topologyConfigMap := &corev1.ConfigMap{} + err = k8sClient.Get(ctx, topologyKey, topologyConfigMap) + logf.Log.V(1).Info("reading topology", "object", client.ObjectKeyFromObject(topologyConfigMap), "error", err) + if err != nil { + return err + } + f, err := os.Create(topologyOutputFile) + logf.Log.V(1).Info("write topology topology to file", "file", topologyOutputFile, "error", err) + if err != nil { + return err + } + defer f.Close() + + _, err = f.WriteString(topologyConfigMap.Data["topology"]) + if err != nil { + return err + } + + return nil +} diff --git a/cmd/version.go b/cmd/version.go index 9390f08..71684e1 100644 --- a/cmd/version.go +++ b/cmd/version.go @@ -5,7 +5,6 @@ import ( "github.com/spf13/cobra" - "github.com/kuadrant/kuadrantctl/pkg/utils" "github.com/kuadrant/kuadrantctl/version" ) @@ -15,11 +14,6 @@ func versionCommand() *cobra.Command { Short: "Print the version number of kuadrantctl", Long: "Print the version number of kuadrantctl", RunE: func(cmd *cobra.Command, args []string) error { - err := utils.SetupScheme() - if err != nil { - return err - } - fmt.Printf("kuadrantctl %s (%s)\n", version.Version, version.GitHash) return nil }, diff --git a/pkg/utils/scheme.go b/pkg/utils/scheme.go deleted file mode 100644 index c500ac7..0000000 --- a/pkg/utils/scheme.go +++ /dev/null @@ -1,27 +0,0 @@ -package utils - -import ( - kuadrantoperator "github.com/kuadrant/kuadrant-operator/api/v1beta1" - operators "github.com/operator-framework/api/pkg/operators/v1alpha1" - apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1" - "k8s.io/client-go/kubernetes/scheme" -) - -func SetupScheme() error { - err := apiextensionsv1.AddToScheme(scheme.Scheme) - if err != nil { - return err - } - - err = operators.AddToScheme(scheme.Scheme) - if err != nil { - return err - } - - err = kuadrantoperator.AddToScheme(scheme.Scheme) - if err != nil { - return err - } - - return nil -}