Skip to content

Commit

Permalink
Merge pull request #8 from burmanm/config-builder
Browse files Browse the repository at this point in the history
Config builder replacements
  • Loading branch information
burmanm authored Jul 26, 2023
2 parents a3116e5 + fd44045 commit bee01cc
Show file tree
Hide file tree
Showing 16 changed files with 3,759 additions and 3 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/build-client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,14 @@ jobs:
build_docker_image:
name: Build k8ssandra-client Docker Image
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/master'
if: github.ref == 'refs/heads/main'
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v3
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
uses: docker/setup-buildx-action@v2
- name: Set up QEMU
uses: docker/setup-qemu-action@v2
- name: Login to DockerHub
uses: docker/login-action@v2
with:
Expand All @@ -59,6 +61,7 @@ jobs:
id: docker_build
uses: docker/build-push-action@v3
with:
load: false
file: cmd/kubectl-k8ssandra/Dockerfile
push: ${{ github.event_name != 'pull_request' }}
tags: k8ssandra/k8ssandra-client:${{ steps.vars.outputs.sha_short }}, k8ssandra/k8ssandra-client:latest
Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,12 @@ build: test ## Build kubectl-k8ssandra

.PHONY: docker-build
docker-build: ## Build k8ssandra-client docker image
docker buildx build --build-arg VERSION=${VERSION} -t ${IMG_LATEST} . --load -f cmd/kubectl-k8ssandra/Dockerfile
docker buildx build --build-arg VERSION=${VERSION} -t ${IMG_LATEST} -t ${IMG} . --load -f cmd/kubectl-k8ssandra/Dockerfile

.PHONY: kind-load
kind-load: ## Load k8ssandra-client:latest to kind
kind load docker-image ${IMG_LATEST}
kind load docker-image ${IMG}

##@ Tools / Dependencies

Expand Down
83 changes: 83 additions & 0 deletions cmd/kubectl-k8ssandra/config/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package config

import (
"context"
"fmt"

"github.com/k8ssandra/k8ssandra-client/pkg/config"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

var (
configBuilderExample = `
# Process the config files from cass-operator input
%[1]s build [<args>]
`
)

type builderOptions struct {
configFlags *genericclioptions.ConfigFlags
genericclioptions.IOStreams

inputDir string
outputDir string
}

func newBuilderOptions(streams genericclioptions.IOStreams) *builderOptions {
return &builderOptions{
configFlags: genericclioptions.NewConfigFlags(true),
IOStreams: streams,
}
}

// NewCmd provides a cobra command wrapping newAddOptions
func NewBuilderCmd(streams genericclioptions.IOStreams) *cobra.Command {
o := newBuilderOptions(streams)

cmd := &cobra.Command{
Use: "build [flags]",
Short: "Build config files from cass-operator input",
Example: fmt.Sprintf(configBuilderExample, "kubectl k8ssandra config"),
RunE: func(c *cobra.Command, args []string) error {
if err := o.Complete(c, args); err != nil {
return err
}
if err := o.Validate(); err != nil {
return err
}
if err := o.Run(); err != nil {
return err
}

return nil
},
}

fl := cmd.Flags()
fl.StringVar(&o.inputDir, "input", "", "read config files from this directory instead of default")
fl.StringVar(&o.outputDir, "output", "", "write config files to this directory instead of default")
o.configFlags.AddFlags(fl)
return cmd
}

// Complete parses the arguments and necessary flags to options
func (c *builderOptions) Complete(cmd *cobra.Command, args []string) error {
// TODO Instead of pkg doing the Getenv parameters, we should probably do them here
// since it's related to the command line interface and shell. Makes it easier to
// refactor later to more sane input
return nil
}

// Validate ensures that all required arguments and flag values are provided
func (c *builderOptions) Validate() error {
return nil
}

// Run processes the input, creates a connection to Kubernetes and processes a secret to add the users
func (c *builderOptions) Run() error {
ctx := context.Background()

builder := config.NewBuilder(c.inputDir, c.outputDir)
return builder.Build(ctx)
}
37 changes: 37 additions & 0 deletions cmd/kubectl-k8ssandra/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package config

import (
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
)

type ClientOptions struct {
configFlags *genericclioptions.ConfigFlags
genericclioptions.IOStreams
}

// NewClientOptions provides an instance of ClientOptions with default values
func NewClientOptions(streams genericclioptions.IOStreams) *ClientOptions {
return &ClientOptions{
configFlags: genericclioptions.NewConfigFlags(true),
IOStreams: streams,
}
}

// NewCmd provides a cobra command wrapping ClientOptions
func NewCmd(streams genericclioptions.IOStreams) *cobra.Command {
o := NewClientOptions(streams)

cmd := &cobra.Command{
Use: "config [subcommand] [flags]",
}

// Add subcommands
cmd.AddCommand(NewBuilderCmd(streams))
// TODO Add the idea of allowing to modify cassandra-yaml with interactive editor from the
// command line

o.configFlags.AddFlags(cmd.Flags())

return cmd
}
2 changes: 2 additions & 0 deletions cmd/kubectl-k8ssandra/k8ssandra/k8ssandra.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
// "github.com/k8ssandra/k8ssandra-client/cmd/kubectl-k8ssandra/list"
// "github.com/k8ssandra/k8ssandra-client/cmd/kubectl-k8ssandra/migrate"
// "github.com/k8ssandra/k8ssandra-client/cmd/kubectl-k8ssandra/nodetool"
"github.com/k8ssandra/k8ssandra-client/cmd/kubectl-k8ssandra/config"
"github.com/k8ssandra/k8ssandra-client/cmd/kubectl-k8ssandra/operate"
"github.com/k8ssandra/k8ssandra-client/cmd/kubectl-k8ssandra/users"

Expand Down Expand Up @@ -49,6 +50,7 @@ func NewCmd(streams genericclioptions.IOStreams) *cobra.Command {
// cmd.AddCommand(migrate.NewCmd(streams))
cmd.AddCommand(users.NewCmd(streams))
// cmd.AddCommand(migrate.NewInstallCmd(streams))
cmd.AddCommand(config.NewCmd(streams))

// cmd.Flags().BoolVar(&o.listNamespaces, "list", o.listNamespaces, "if true, print the list of all namespaces in the current KUBECONFIG")
o.configFlags.AddFlags(cmd.Flags())
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/k8ssandra/k8ssandra-client
go 1.20

require (
github.com/adutra/goalesce v0.0.0-20221124153206-5643f911003d
github.com/charmbracelet/bubbles v0.16.1
github.com/charmbracelet/bubbletea v0.24.2
github.com/charmbracelet/lipgloss v0.7.1
Expand Down Expand Up @@ -34,6 +35,7 @@ require (
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/burmanm/definitions-parser v0.0.0-20230720114634-62c738b72e61 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chai2010/gettext-go v1.0.2 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
Expand Down
Loading

0 comments on commit bee01cc

Please sign in to comment.