Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Config builder replacements #8

Merged
merged 12 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Question: Do we plan on creating a proper release process for this? Seems like we're just publishing tags that use the short sha (and latest).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes (we have to so that cass-operator can ship with correct versions), but that would come in a separate PR. We might also want to publish just a static binary for certain platforms if we add more CLI user features.

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