Skip to content

Commit

Permalink
Merge pull request redpanda-data#13396 from joejulian/fluxcd-controll…
Browse files Browse the repository at this point in the history
…er-factories

use the new controller factory shims for fluxcd
  • Loading branch information
joejulian authored Oct 17, 2023
2 parents f80d202 + a87fdab commit 5dc0c66
Show file tree
Hide file tree
Showing 133 changed files with 3,964 additions and 2,223 deletions.
6 changes: 1 addition & 5 deletions src/go/k8s/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ FROM --platform=$BUILDPLATFORM public.ecr.aws/docker/library/golang:1.21.2 as bu
ARG TARGETARCH
ARG TARGETOS

# Copy the rpk as a close dependency
WORKDIR /workspace
COPY rpk/ rpk/

WORKDIR /workspace/k8s
# Copy the Go Modules manifests
COPY k8s/go.mod go.mod
Expand All @@ -20,7 +16,7 @@ RUN go mod download
COPY k8s/ .

# build
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} GO111MODULE=on go build -a -o manager main.go && \
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} GO111MODULE=on go build -a -o manager cmd/main.go && \
CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} GO111MODULE=on go build -a -o configurator cmd/configurator/main.go

# Use distroless as minimal base image to package the manager binary
Expand Down
10 changes: 5 additions & 5 deletions src/go/k8s/Dockerfile.dockerignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
**
!rpk/
!k8s/apis
!k8s/api
!k8s/cmd
!k8s/config
!k8s/controllers
!k8s/go.mod
!k8s/go.sum
!k8s/main.go
!k8s/hack
!k8s/internal
!k8s/pkg
!k8s/webhooks
!k8s/go.mod
!k8s/go.sum
56 changes: 38 additions & 18 deletions src/go/k8s/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
OPERATOR_IMG_LATEST ?= "localhost/redpanda-operator:dev"
CONFIGURATOR_IMG_LATEST ?= "localhost/configurator:dev"
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"
CRD_OPTIONS ?= "crd"

# default redpanda image to load
REDPANDA_IMG ?= "localhost/redpanda:dev"
Expand Down Expand Up @@ -33,23 +33,36 @@ ifneq ($(TEST_NAME), )
TEST_ONLY_FLAG = --test $(TEST_NAME)
endif

all: manager
all: build

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

# Build manager binary
manager: generate vet
go build -o bin/manager main.go
.PHONY: manager
manager: manifests generate fmt vet
go build -o bin/manager cmd/main.go

# Build manager binary
.PHONY: configurator
configurator: manifests generate fmt vet
go build -o bin/configurator cmd/configurator/main.go

.PHONY: build
build: manager configurator

.PHONY: fmt
fmt: ## Run go fmt against code.
go fmt ./...

# Run against the configured Kubernetes cluster in ~/.kube/config
run: generate vet manifests
go run ./main.go
run: generate vet manifests fmt
go run ./cmd/main.go

# Run against the configured Kubernetes cluster in ~/.kube/config
run-v2: generate vet manifests
go run ./main.go --namespace=$(NAMESPACE) --log-level=debug
run-v2: generate vet manifests fmt
go run ./cmd/main.go --namespace=$(NAMESPACE) --log-level=debug

# Install CRDs into a cluster
install: manifests kustomize
Expand All @@ -73,14 +86,17 @@ undeploy:
$(KUSTOMIZE) build config/default | kubectl delete -f -

# Generate manifests e.g. CRD, RBAC etc.
.PHONY: manifests
manifests: controller-gen
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases output:rbac:artifacts:config=config/rbac/bases/operator

# Run go vet against code
.PHONY: vet
vet:
go vet ./...

# Generate code
.PHONY: generate
generate: controller-gen
$(CONTROLLER_GEN) object:headerFile="../../../licenses/boilerplate.go.txt" paths="./..."

Expand Down Expand Up @@ -157,7 +173,7 @@ GOLANGCI_LINT ?= $(LOCALBIN)/golangci-lint

## Tool Versions
KUSTOMIZE_VERSION ?= v4.5.7
CONTROLLER_TOOLS_VERSION ?= v0.4.1
CONTROLLER_TOOLS_VERSION ?= v0.13.0
KUTTL_VERSION ?= v0.15.0

GOLANGCI_LINT_VERSION ?= v1.54.2
Expand Down Expand Up @@ -212,14 +228,18 @@ gofumpt-lint: gofumpt-install
goimports: goimports-install
goimports -w .

.PHONY: golangci-lint-install
golangci-lint-install:
mkdir -p $(GOLANGCI_LINT)
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(GOLANGCI_LINT) v1.54.2

.PHONY: golangci-lint
GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
GOLANGCI_LINT_VERSION ?= v1.54.2
golangci-lint:
$(GOLANGCI_LINT)/golangci-lint run --go=1.21
@[ -f $(GOLANGCI_LINT) ] || { \
set -e ;\
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) $( GOLANGCI_LINT_VERSION) ;\
}

.PHONY: lint
lint: gofumpt-lint golangci-lint
lint: golangci-lint
$(GOLANGCI_LINT) run

.PHONY: lint-fix
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
$(GOLANGCI_LINT) run --fix
11 changes: 6 additions & 5 deletions src/go/k8s/PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
layout:
- go.kubebuilder.io/v3
- go.kubebuilder.io/v4
multigroup: true
projectName: redpanda-operator
repo: github.com/redpanda-data/redpanda/src/go/k8s
Expand All @@ -15,7 +15,7 @@ resources:
domain: vectorized.io
group: redpanda
kind: Cluster
path: github.com/redpanda-data/redpanda/src/go/k8s/apis/redpanda/v1alpha1
path: github.com/redpanda-data/redpanda/src/go/k8s/api/redpanda/v1alpha1
version: v1alpha1
webhooks:
webhookVersion: v1
Expand All @@ -26,7 +26,7 @@ resources:
domain: vectorized.io
group: redpanda
kind: Console
path: github.com/redpanda-data/redpanda/src/go/k8s/apis/redpanda/v1alpha1
path: github.com/redpanda-data/redpanda/src/go/k8s/api/redpanda/v1alpha1
version: v1alpha1
- api:
crdVersion: v1
Expand All @@ -35,14 +35,15 @@ resources:
domain: redpanda.com
group: cluster
kind: Redpanda
path: github.com/redpanda-data/redpanda/src/go/k8s/apis/redpanda/v1alpha1
path: github.com/redpanda-data/redpanda/src/go/k8s/api/redpanda/v1alpha1
plural: redpandas
version: v1alpha1
- api:
crdVersion: v1
namespaced: true
controller: true
group: cluster.redpanda.com
kind: Topic
path: github.com/redpanda-data/redpanda/src/go/k8s/apis/cluster.redpanda.com/v1alpha1
path: github.com/redpanda-data/redpanda/src/go/k8s/api/cluster.redpanda.com/v1alpha1
version: v1alpha1
version: "3"

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ type HelmUpgrade struct {
// Redpanda is the Schema for the redpanda API
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:resource:path=redpandas
// +kubebuilder:resource:shortName=rp
// +kubebuilder:printcolumn:name="Ready",type="string",JSONPath=".status.conditions[?(@.type==\"Ready\")].status",description=""
// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.conditions[?(@.type==\"Ready\")].message",description=""
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/utils/pointer"

"github.com/redpanda-data/redpanda/src/go/k8s/apis/vectorized/v1alpha1"
"github.com/redpanda-data/redpanda/src/go/k8s/api/vectorized/v1alpha1"
"github.com/redpanda-data/redpanda/src/go/k8s/pkg/resources/featuregates"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/redpanda-data/redpanda/src/go/k8s/pkg/resources/featuregates"
"github.com/redpanda-data/redpanda/src/go/k8s/pkg/utils"
Expand Down Expand Up @@ -197,23 +198,23 @@ func (r *Cluster) setDefaultAdditionalConfiguration() {
var _ webhook.Validator = &Cluster{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *Cluster) ValidateCreate() error {
func (r *Cluster) ValidateCreate() (admission.Warnings, error) {
log := ctrl.Log.WithName("Cluster.ValidateCreate").WithValues("namespace", r.Namespace, "name", r.Name)
log.Info("validating create")

allErrs := r.validateCommon(log)

if len(allErrs) == 0 {
return nil
return nil, nil
}

return apierrors.NewInvalid(
return nil, apierrors.NewInvalid(
r.GroupVersionKind().GroupKind(),
r.Name, allErrs)
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *Cluster) ValidateUpdate(old runtime.Object) error {
func (r *Cluster) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
log := ctrl.Log.WithName("Cluster.ValidateUpdate").WithValues("namespace", r.Namespace, "name", r.Name)
log.Info("validating update")

Expand All @@ -223,7 +224,7 @@ func (r *Cluster) ValidateUpdate(old runtime.Object) error {
// the clientCACertRef can fail with secret not found if the referenced secret
// has already been deleted as part of the cluster deletion.
if !r.GetDeletionTimestamp().IsZero() {
return nil
return nil, nil
}

oldCluster := old.(*Cluster)
Expand All @@ -236,10 +237,10 @@ func (r *Cluster) ValidateUpdate(old runtime.Object) error {
allErrs = append(allErrs, r.validateLicense(oldCluster)...)

if len(allErrs) == 0 {
return nil
return nil, nil
}

return apierrors.NewInvalid(
return nil, apierrors.NewInvalid(
r.GroupVersionKind().GroupKind(),
r.Name, allErrs)
}
Expand Down Expand Up @@ -1098,9 +1099,9 @@ func (r *Cluster) validateAdditionalConfiguration() field.ErrorList {
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *Cluster) ValidateDelete() error {
func (r *Cluster) ValidateDelete() (admission.Warnings, error) {
// this is a stub to implement the interface. We do not validate on delete.
return nil
return nil, nil
}

type listenersPorts struct {
Expand Down
Loading

0 comments on commit 5dc0c66

Please sign in to comment.