-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add basic commands for k8ssandra, stop/start/restart CassandraDatacen…
…ter. Add Github Actions to build docker image as well as run tests and lint
- Loading branch information
Showing
10 changed files
with
491 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
name: k8ssandra-client build and deploy | ||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: [ main ] | ||
jobs: | ||
build_and_test: | ||
env: | ||
CGO_ENABLED: 0 | ||
steps: | ||
- uses: actions/checkout@v2 | ||
if: github.event_name == 'pull_request' | ||
with: | ||
ref: ${{ github.event.pull_request.head.sha }} | ||
- uses: actions/checkout@v2 | ||
if: github.event_name != 'pull_request' | ||
- name: Set up Go 1.19 | ||
uses: actions/setup-go@v1 | ||
with: | ||
go-version: 1.19 | ||
- name: golangci-lint | ||
uses: golangci/golangci-lint-action@v3 | ||
with: | ||
version: latest | ||
# GHA requires longer timeout | ||
args: --timeout=10m | ||
# Optional: show only new issues if it's a pull request. The default value is `false`. | ||
only-new-issues: ${{ github.event_name == 'pull_request' }} | ||
skip-pkg-cache: true | ||
skip-build-cache: true | ||
- name: Unit Tests | ||
run: | | ||
make test | ||
build_docker_image: | ||
name: Build k8ssandra-client Docker Image | ||
runs-on: ubuntu-latest | ||
if: github.ref == 'refs/heads/master' | ||
steps: | ||
- name: Check out code into the Go module directory | ||
uses: actions/checkout@v2 | ||
- name: Set up Docker Buildx | ||
uses: docker/setup-buildx-action@v1 | ||
- name: Cache Docker layers | ||
uses: actions/cache@v2 | ||
with: | ||
path: /tmp/.buildx-cache | ||
key: ${{ runner.os }}-buildx-${{ github.sha }} | ||
restore-keys: | | ||
${{ runner.os }}-buildx- | ||
- name: Login to DockerHub | ||
uses: docker/login-action@v1 | ||
with: | ||
username: ${{ secrets.DOCKER_HUB_USERNAME }} | ||
password: ${{ secrets.DOCKER_HUB_PASSWORD }} | ||
- name: Set git parsed values | ||
id: vars | ||
run: | | ||
echo ::set-output name=sha_short::$(git rev-parse --short=8 ${{ github.sha }}) | ||
echo ::set-output name=tag_name::${GITHUB_REF#refs/tags/} | ||
- name: Build and push | ||
id: docker_build | ||
uses: docker/build-push-action@v2 | ||
with: | ||
file: cmd/kubectl-k8ssandra/Dockerfile | ||
push: ${{ github.event_name != 'pull_request' }} | ||
tags: k8ssandra/k8ssandra-client:${{ steps.vars.outputs.sha_short }}, k8ssandra/k8ssandra-client:latest | ||
platforms: linux/amd64 | ||
cache-from: type=local,src=/tmp/.buildx-cache | ||
cache-to: type=local,dest=/tmp/.buildx-cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
# k8ssandra-client | ||
# k8ssandra-client | ||
|
||
Work in progress, breaking changes will occur. This is intended to be a kubectl plugin to simplify usage of k8ssandra. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,206 @@ | ||
package operate | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/k8ssandra/k8ssandra-client/pkg/cassdcutil" | ||
"github.com/k8ssandra/k8ssandra-client/pkg/kubernetes" | ||
"github.com/spf13/cobra" | ||
"k8s.io/cli-runtime/pkg/genericclioptions" | ||
) | ||
|
||
var ( | ||
startExample = ` | ||
# start an existing datacenter that was stopped | ||
%[1]s start <datacenter> | ||
` | ||
|
||
stopExample = ` | ||
# shutdown an existing datacenter | ||
%[1]s stop <datacenter> | ||
# shutdown an existing datacenter and wait for all the pods to shutdown | ||
%[1]s stop <datacenter> --wait | ||
` | ||
|
||
restartExample = ` | ||
# request a rolling restart for datacenter | ||
%[1]s restart <datacenter> | ||
# request a rolling restart of a single rack called r1 | ||
%[1]s restart <datacenter> --rack r1 | ||
` | ||
|
||
errNoDatacenterDefined = fmt.Errorf("no target datacenter given") | ||
errRestartingStopped = fmt.Errorf("unable to do rolling restart to a stopped datacenter") | ||
) | ||
|
||
type options struct { | ||
configFlags *genericclioptions.ConfigFlags | ||
genericclioptions.IOStreams | ||
namespace string | ||
dcName string | ||
rackName string | ||
wait bool | ||
cassManager *cassdcutil.CassManager | ||
} | ||
|
||
func newOptions(streams genericclioptions.IOStreams) *options { | ||
return &options{ | ||
configFlags: genericclioptions.NewConfigFlags(true), | ||
IOStreams: streams, | ||
} | ||
} | ||
|
||
func NewStartCmd(streams genericclioptions.IOStreams) *cobra.Command { | ||
o := newOptions(streams) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "start [cluster]", | ||
Short: "restart an existing shutdown Cassandra cluster", | ||
Example: fmt.Sprintf(startExample, "kubectl k8ssandra"), | ||
SilenceUsage: true, | ||
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(false); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
fl := cmd.Flags() | ||
fl.BoolVarP(&o.wait, "wait", "w", false, "wait until all pods have started") | ||
fl.StringVar(&o.rackName, "rack", "", "restart only target rack") | ||
o.configFlags.AddFlags(fl) | ||
return cmd | ||
} | ||
|
||
func NewRestartCmd(streams genericclioptions.IOStreams) *cobra.Command { | ||
o := newOptions(streams) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "restart [cluster]", | ||
Short: "request rolling restart for an existing running Cassandra cluster", | ||
Example: fmt.Sprintf(restartExample, "kubectl k8ssandra"), | ||
SilenceUsage: true, | ||
RunE: func(c *cobra.Command, args []string) error { | ||
if err := o.Complete(c, args); err != nil { | ||
return err | ||
} | ||
if err := o.ValidateRestart(); err != nil { | ||
return err | ||
} | ||
if err := o.Restart(); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
fl := cmd.Flags() | ||
fl.BoolVarP(&o.wait, "wait", "w", false, "wait until all pods have restarted") | ||
o.configFlags.AddFlags(fl) | ||
return cmd | ||
} | ||
|
||
func NewStopCmd(streams genericclioptions.IOStreams) *cobra.Command { | ||
o := newOptions(streams) | ||
|
||
cmd := &cobra.Command{ | ||
Use: "stop [cluster]", | ||
Short: "shutdown running Cassandra cluster", | ||
Example: fmt.Sprintf(stopExample, "kubectl k8ssandra"), | ||
SilenceUsage: true, | ||
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(true); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
}, | ||
} | ||
|
||
fl := cmd.Flags() | ||
fl.BoolVarP(&o.wait, "wait", "w", false, "wait until all pods have terminated") | ||
o.configFlags.AddFlags(fl) | ||
return cmd | ||
} | ||
|
||
// Complete parses the arguments and necessary flags to options | ||
func (c *options) Complete(cmd *cobra.Command, args []string) error { | ||
var err error | ||
|
||
if len(args) < 1 { | ||
return errNoDatacenterDefined | ||
} | ||
|
||
c.dcName = args[0] | ||
|
||
c.namespace, _, err = c.configFlags.ToRawKubeConfigLoader().Namespace() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
restConfig, err := c.configFlags.ToRESTConfig() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
kubeClient, err := kubernetes.GetClientInNamespace(restConfig, c.namespace) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
c.cassManager = cassdcutil.NewManager(kubeClient) | ||
|
||
return nil | ||
} | ||
|
||
// Validate ensures that all required arguments and flag values are provided | ||
func (c *options) Validate() error { | ||
// Verify target cluster exists | ||
_, err := c.cassManager.CassandraDatacenter(c.dcName, c.namespace) | ||
if err != nil { | ||
// NotFound is still an error | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
// ValidateRestart ensures that all required arguments and flag values are provided | ||
func (c *options) ValidateRestart() error { | ||
// Verify target cluster exists | ||
dc, err := c.cassManager.CassandraDatacenter(c.dcName, c.namespace) | ||
if err != nil { | ||
// NotFound is still an error | ||
return err | ||
} | ||
if dc.Spec.Stopped { | ||
return errRestartingStopped | ||
} | ||
return nil | ||
} | ||
|
||
// Run either stops or starts the existing datacenter | ||
func (c *options) Run(stop bool) error { | ||
return c.cassManager.ModifyStoppedState(c.dcName, c.namespace, stop, c.wait) | ||
} | ||
|
||
// Restart creates a restart task for the cluster | ||
func (c *options) Restart() error { | ||
return c.cassManager.RestartDc(c.dcName, c.namespace, c.rackName, c.wait) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.