Skip to content

Commit

Permalink
topology command: persist in local disk
Browse files Browse the repository at this point in the history
Signed-off-by: Eguzki Astiz Lezaun <[email protected]>
  • Loading branch information
eguzki committed Dec 19, 2024
1 parent 2bc1faf commit 07b4866
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 36 deletions.
11 changes: 8 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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)
Expand All @@ -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
}
64 changes: 64 additions & 0 deletions cmd/topology.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 0 additions & 6 deletions cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/spf13/cobra"

"github.com/kuadrant/kuadrantctl/pkg/utils"
"github.com/kuadrant/kuadrantctl/version"
)

Expand All @@ -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
},
Expand Down
27 changes: 0 additions & 27 deletions pkg/utils/scheme.go

This file was deleted.

0 comments on commit 07b4866

Please sign in to comment.