Skip to content

Commit

Permalink
Add tests, remove :latest tag, update base version to 0.3.0, add envt…
Browse files Browse the repository at this point in the history
…est facilities, add test CRDs
  • Loading branch information
burmanm committed Oct 24, 2023
1 parent 5e1050a commit 63da254
Show file tree
Hide file tree
Showing 10 changed files with 12,254 additions and 11 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build-client.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
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
tags: k8ssandra/k8ssandra-client:${{ steps.vars.outputs.sha_short }}
platforms: linux/amd64,linux/arm64
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache
19 changes: 13 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION ?= 0.2.1
VERSION ?= 0.3.0

COMMIT := $(shell git rev-parse --short HEAD)
DATE := $(shell date +%Y%m%d)
Expand All @@ -9,11 +9,13 @@ IMAGE_TAG_BASE ?= $(ORG)/k8ssandra-client

# Image URL to use all building/pushing image targets
IMG ?= $(IMAGE_TAG_BASE):v$(VERSION)
IMG_LATEST ?= $(IMAGE_TAG_BASE):latest

SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec

# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.27.x

.PHONY: all
all: build

Expand Down Expand Up @@ -43,8 +45,8 @@ vet: ## Run go vet against code.
go vet ./...

.PHONY: test
test: fmt vet lint ## Run tests
go test -v ./...
test: fmt vet lint envtest ## Run tests
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test -v ./... -coverprofile cover.out

.PHONY: lint
lint: golangci-lint ## Run golangci-lint against code
Expand All @@ -56,11 +58,10 @@ 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} -t ${IMG} . --load -f cmd/kubectl-k8ssandra/Dockerfile
docker buildx build --build-arg VERSION=${VERSION} -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 All @@ -73,9 +74,15 @@ $(LOCALBIN):
## Tool binaries

GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint
ENVTEST ?= $(LOCALBIN)/setup-envtest

GOLINT_VERSION ?= 1.55.0

.PHONY: envtest
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
$(ENVTEST): $(LOCALBIN)
test -s $(ENVTEST) || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

.PHONY: golangci-lint
golangci-lint:
@if test -x $(LOCALBIN)/golangci-lint && ! $(LOCALBIN)/golangci-lint version | grep -q $(GOLINT_VERSION); then \
Expand Down
13 changes: 12 additions & 1 deletion internal/envtest/envtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import (
"testing"

cassdcapi "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1"
controlapi "github.com/k8ssandra/cass-operator/apis/control/v1alpha1"
k8ssandrataskapi "github.com/k8ssandra/k8ssandra-operator/apis/control/v1alpha1"

"github.com/k8ssandra/k8ssandra-client/pkg/kubernetes"
"k8s.io/kubectl/pkg/scheme"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -34,7 +37,7 @@ type Environment struct {
func NewEnvironment() *Environment {
env := &Environment{}
env.env = &envtest.Environment{
CRDDirectoryPaths: []string{filepath.Join(RootDir(), "config", "crd"), filepath.Join(RootDir(), "testfiles", "crd")},
CRDDirectoryPaths: []string{filepath.Join(RootDir(), "testfiles", "crd")},
ErrorIfCRDPathMissing: true,
}

Expand Down Expand Up @@ -65,6 +68,14 @@ func (e *Environment) start() {
panic(err)
}

if err := controlapi.AddToScheme(scheme.Scheme); err != nil {
panic(err)
}

if err := k8ssandrataskapi.AddToScheme(scheme.Scheme); err != nil {
panic(err)
}

//+kubebuilder:scaffold:scheme

k8sClient, err := client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expand Down
6 changes: 5 additions & 1 deletion pkg/tasks/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tasks
import (
"context"
"fmt"
"math/rand"
"time"

controlapi "github.com/k8ssandra/cass-operator/apis/control/v1alpha1"
Expand All @@ -17,7 +18,10 @@ func CreateClusterTask(ctx context.Context, kubeClient client.Client, command co
return nil, fmt.Errorf("clusterName and namespace must be specified")
}

generatedName := fmt.Sprintf("%s-%s-%d", kcName, command, time.Now().Unix())
generatedName := fmt.Sprintf("%s-%s-%d-%d", kcName, command, time.Now().Unix(), rand.Int31())
if len(generatedName) > 63 {
generatedName = generatedName[:63]
}
task := &k8ssandrataskapi.K8ssandraTask{
ObjectMeta: metav1.ObjectMeta{
Name: generatedName,
Expand Down
8 changes: 6 additions & 2 deletions pkg/tasks/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tasks
import (
"context"
"fmt"
"math/rand"
"time"

cassdcapi "github.com/k8ssandra/cass-operator/apis/cassandra/v1beta1"
Expand Down Expand Up @@ -170,7 +171,7 @@ func CreateRebuildTask(ctx context.Context, kubeClient client.Client, dc *cassdc

func rebuildArguments(rackName, podName, sourceDatacenter string) (*controlapi.JobArguments, error) {
args := commonArguments(rackName, podName)
if sourceDatacenter != "" {
if sourceDatacenter == "" {
return nil, fmt.Errorf("sourceDatacenter must be specified")
}
args.SourceDatacenter = sourceDatacenter
Expand Down Expand Up @@ -201,7 +202,10 @@ func commonArguments(rackName, podName string) *controlapi.JobArguments {
}

func CreateTask(ctx context.Context, kubeClient client.Client, command controlapi.CassandraCommand, dc *cassdcapi.CassandraDatacenter, args *controlapi.JobArguments) (*controlapi.CassandraTask, error) {
generatedName := fmt.Sprintf("%s-%s-%d", dc.Name, command, time.Now().Unix())
generatedName := fmt.Sprintf("%s-%s-%d-%d", dc.Name, command, time.Now().Unix(), rand.Int31())
if len(generatedName) > 63 {
generatedName = generatedName[:63]
}
task := &controlapi.CassandraTask{
ObjectMeta: metav1.ObjectMeta{
Name: generatedName,
Expand Down
Loading

0 comments on commit 63da254

Please sign in to comment.