Skip to content

Commit

Permalink
Merge pull request #781 from fabriziosestito/refactor/v3-to-v4
Browse files Browse the repository at this point in the history
refactor: kubebuilder v3 to v4
  • Loading branch information
fabriziosestito authored Jun 26, 2024
2 parents b79efed + ba1bb23 commit 0915fa0
Show file tree
Hide file tree
Showing 64 changed files with 150 additions and 143 deletions.
13 changes: 3 additions & 10 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,16 @@ issues:
# Disable 'funlen' linter for test functions.
# It's common for table-driven tests to be more than 60 characters long
source: "^func Test"
- path: internal/pkg/admission/policy-server-error.go
- path: internal/admission/policy-server-error.go
linters:
- deadcode
- unused
- path: internal/pkg/admissionregistration/cert.go
linters:
- gocritic
- path: internal/pkg/admission/validating-webhook.go
- path: internal/admission/validating-webhook.go
linters:
- dupl
- path: internal/pkg/admission/mutating-webhook.go
- path: internal/admission/mutating-webhook.go
linters:
- dupl
- path: pkg/apis/policies/v1alpha2/clusteradmissionpolicy_types.go
text: "got 'TypeMeta' want 'typeMeta'"
- path: pkg/apis/policies/v1alpha2/policyserver_types.go
text: "got 'TypeMeta' want 'typeMeta'"

linters:
enable-all: true
Expand Down
7 changes: 3 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ COPY go.sum go.sum
RUN go mod download

# Copy the go source
COPY *.go ./
COPY pkg/ pkg/
COPY controllers/ controllers/
COPY cmd/main.go cmd/main.go
COPY api/ api/
COPY internal/ internal/

# Build
RUN CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -a -o manager .
RUN CGO_ENABLED=0 GOOS=linux GO111MODULE=on go build -a -o manager cmd/main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
Expand Down
17 changes: 9 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ CONTROLLER_GEN_VER := v0.15.0
CONTROLLER_GEN_BIN := controller-gen
CONTROLLER_GEN := $(BIN_DIR)/$(CONTROLLER_GEN_BIN)

KUSTOMIZE_VER := v4.5.2
KUSTOMIZE_VER := v5.4.1
KUSTOMIZE_BIN := kustomize
KUSTOMIZE := $(BIN_DIR)/$(KUSTOMIZE_BIN)

Expand Down Expand Up @@ -74,7 +74,7 @@ $(CONTROLLER_GEN): ## Install controller-gen.
GOBIN=$(BIN_DIR) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_GEN_VER)

$(KUSTOMIZE): ## Install kustomize.
GOBIN=$(BIN_DIR) go install sigs.k8s.io/kustomize/kustomize/v4@$(KUSTOMIZE_VER)
GOBIN=$(BIN_DIR) go install sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VER)

$(SETUP_ENVTEST): ## Install setup-envtest.
GOBIN=$(BIN_DIR) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@$(SETUP_ENVTEST_VER)
Expand Down Expand Up @@ -114,13 +114,12 @@ setup-envtest: $(SETUP_ENVTEST) # Build setup-envtest

.PHONY: unit-tests
unit-tests: manifests generate fmt vet setup-envtest ## Run unit tests.
KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test ./internal/... -race -test.v -coverprofile=coverage/unit-tests/coverage-internal.txt -covermode=atomic
KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test ./pkg/... -race -test.v -coverprofile=coverage/unit-tests/coverage-pkg.txt -covermode=atomic
KUBEBUILDER_ASSETS="$(KUBEBUILDER_ASSETS)" go test $$(go list ./... | grep -v /internal/controller) -race -test.v -coverprofile=coverage/unit-tests/coverage.txt -covermode=atomic

.PHONY: setup-envtest integration-tests
integration-tests: manifests generate fmt vet setup-envtest ## Run integration tests.
ACK_GINKGO_DEPRECATIONS=2.12.0 K3S_TESTCONTAINER_VERSION="$(K3S_TESTCONTAINER_VERSION)" POLICY_SERVER_VERSION="$(POLICY_SERVER_VERSION)" \
go test -v ./controllers/... -ginkgo.v -ginkgo.progress -race -test.v \
go test -v ./internal/controller/... -ginkgo.v -ginkgo.progress -race -test.v \
-ginkgo.randomize-all -ginkgo.timeout=$(TEST_TIMEOUT) \
-coverprofile=coverage/integration-tests/coverage-controllers.txt \
-covermode=atomic -timeout=$(TEST_TIMEOUT) -coverpkg=all
Expand All @@ -132,11 +131,13 @@ generate-crds: $(KUSTOMIZE) manifests kustomize ## generate final crds with kust

##@ Build

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

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
go run . -deployments-namespace kubewarden --default-policy-server default
go run ./cmd/main.go -deployments-namespace kubewarden --default-policy-server default

docker-build: test ## Build docker image with the manager.
docker build -t ${IMG} .
Expand Down
14 changes: 7 additions & 7 deletions PROJECT
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
domain: kubewarden.io
layout:
- go.kubebuilder.io/v3
- go.kubebuilder.io/v4
multigroup: true
projectName: kubewarden-controller
repo: github.com/kubewarden/kubewarden-controller
Expand All @@ -12,7 +12,7 @@ resources:
domain: kubewarden.io
group: policies
kind: PolicyServer
path: github.com/kubewarden/kubewarden-controller/pkg/apis/policies/v1alpha2
path: github.com/kubewarden/kubewarden-controller/api/policies/v1alpha2
version: v1alpha2
webhooks:
defaulting: true
Expand All @@ -23,7 +23,7 @@ resources:
domain: kubewarden.io
group: policies
kind: ClusterAdmissionPolicy
path: github.com/kubewarden/kubewarden-controller/pkg/apis/policies/v1alpha2
path: github.com/kubewarden/kubewarden-controller/api/policies/v1alpha2
version: v1alpha2
webhooks:
defaulting: true
Expand All @@ -35,30 +35,30 @@ resources:
domain: kubewarden.io
group: policies
kind: AdmissionPolicy
path: github.com/kubewarden/kubewarden-controller/pkg/apis/policies/v1alpha2
path: github.com/kubewarden/kubewarden-controller/api/policies/v1alpha2
version: v1alpha2
- api:
crdVersion: v1
namespaced: true
domain: kubewarden.io
group: policies
kind: AdmissionPolicy
path: github.com/kubewarden/kubewarden-controller/pkg/apis/policies/v1
path: github.com/kubewarden/kubewarden-controller/api/policies/v1
version: v1
- api:
crdVersion: v1
namespaced: false
domain: kubewarden.io
group: policies
kind: ClusterAdmissionPolicy
path: github.com/kubewarden/kubewarden-controller/pkg/apis/policies/v1
path: github.com/kubewarden/kubewarden-controller/api/policies/v1
version: v1
- api:
crdVersion: v1
namespaced: false
domain: kubewarden.io
group: policies
kind: PolicyServer
path: github.com/kubewarden/kubewarden-controller/pkg/apis/policies/v1
path: github.com/kubewarden/kubewarden-controller/api/policies/v1
version: v1
version: "3"
3 changes: 1 addition & 2 deletions Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ kubectl_cmd = "kubectl"
if str(local("command -v " + kubectl_cmd + " || true", quiet = True)) == "":
fail("Required command '" + kubectl_cmd + "' not found in PATH")


# Install cert manager
load('ext://cert_manager', 'deploy_cert_manager')
deploy_cert_manager()
Expand Down Expand Up @@ -82,7 +81,7 @@ k8s_yaml(updated_install)
# binary is rebuilt and the hot swat wrapper takes care of the rest.
local_resource(
'manager',
"CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/manager ./",
"CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o bin/manager cmd/main.go",
deps = [
"main.go",
"go.mod",
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ package v1
import (
"fmt"

"github.com/kubewarden/kubewarden-controller/internal/pkg/constants"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/kubewarden/kubewarden-controller/internal/constants"
)

// log is for logging in this package.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

"github.com/stretchr/testify/require"

"github.com/kubewarden/kubewarden-controller/internal/pkg/constants"
"github.com/kubewarden/kubewarden-controller/internal/constants"
)

func TestAdmissionPolicyDefault(t *testing.T) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"

"github.com/kubewarden/kubewarden-controller/internal/pkg/constants"
"github.com/kubewarden/kubewarden-controller/internal/constants"
)

// log is for logging in this package.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

"github.com/stretchr/testify/require"

"github.com/kubewarden/kubewarden-controller/internal/pkg/constants"
"github.com/kubewarden/kubewarden-controller/internal/constants"
)

func TestClusterAdmissionPolicyDefault(t *testing.T) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"context"
"fmt"

"github.com/kubewarden/kubewarden-controller/internal/pkg/constants"
"github.com/kubewarden/kubewarden-controller/internal/pkg/policyserver"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/api/resource"
Expand All @@ -35,6 +33,9 @@ import (
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"

"github.com/kubewarden/kubewarden-controller/internal/constants"
"github.com/kubewarden/kubewarden-controller/internal/policyserver"
)

// log is for logging in this package.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ import (
"context"
"testing"

"github.com/kubewarden/kubewarden-controller/internal/pkg/constants"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"

"github.com/kubewarden/kubewarden-controller/internal/constants"
)

func TestDefault(t *testing.T) {
Expand Down

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

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

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

18 changes: 9 additions & 9 deletions main.go → cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ import (
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

controllers "github.com/kubewarden/kubewarden-controller/controllers"
"github.com/kubewarden/kubewarden-controller/internal/pkg/admission"
"github.com/kubewarden/kubewarden-controller/internal/pkg/constants"
"github.com/kubewarden/kubewarden-controller/internal/pkg/metrics"
policiesv1 "github.com/kubewarden/kubewarden-controller/pkg/apis/policies/v1"
"github.com/kubewarden/kubewarden-controller/pkg/apis/policies/v1alpha2"
policiesv1 "github.com/kubewarden/kubewarden-controller/api/policies/v1"
"github.com/kubewarden/kubewarden-controller/api/policies/v1alpha2"
"github.com/kubewarden/kubewarden-controller/internal/admission"
"github.com/kubewarden/kubewarden-controller/internal/constants"
"github.com/kubewarden/kubewarden-controller/internal/controller"
"github.com/kubewarden/kubewarden-controller/internal/metrics"
//+kubebuilder:scaffold:imports
)

Expand Down Expand Up @@ -186,7 +186,7 @@ func main() {
TracingEnabled: enableTracing,
}

if err = (&controllers.PolicyServerReconciler{
if err = (&controller.PolicyServerReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Log: ctrl.Log.WithName("policy-server-reconciler"),
Expand All @@ -197,7 +197,7 @@ func main() {
return
}

if err = (&controllers.AdmissionPolicyReconciler{
if err = (&controller.AdmissionPolicyReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Log: ctrl.Log.WithName("admission-policy-reconciler"),
Expand All @@ -208,7 +208,7 @@ func main() {
return
}

if err = (&controllers.ClusterAdmissionPolicyReconciler{
if err = (&controller.ClusterAdmissionPolicyReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Log: ctrl.Log.WithName("cluster-admission-policy-reconciler"),
Expand Down
5 changes: 3 additions & 2 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ coverage:
default:
threshold: 1%
ignore:
- pkg/apis/policies/v1/zz_generated.deepcopy.go
- pkg/apis/policies/v1alpha2
- api/policies/v1/zz_generated.deepcopy.go
- api/policies/v1alpha2
- cmd
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ import (
"fmt"
"path/filepath"

"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

policiesv1 "github.com/kubewarden/kubewarden-controller/pkg/apis/policies/v1"
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"github.com/kubewarden/kubewarden-controller/internal/pkg/constants"
policiesv1 "github.com/kubewarden/kubewarden-controller/api/policies/v1"
"github.com/kubewarden/kubewarden-controller/internal/constants"
)

//+kubebuilder:rbac:groups=admissionregistration.k8s.io,resources=mutatingwebhookconfigurations,verbs=create;delete;list;patch;watch
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ import (
"errors"
"fmt"

policiesv1 "github.com/kubewarden/kubewarden-controller/pkg/apis/policies/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"github.com/kubewarden/kubewarden-controller/internal/pkg/certs"
"github.com/kubewarden/kubewarden-controller/internal/pkg/constants"
policiesv1 "github.com/kubewarden/kubewarden-controller/api/policies/v1"
"github.com/kubewarden/kubewarden-controller/internal/certs"
"github.com/kubewarden/kubewarden-controller/internal/constants"
)

func (r *Reconciler) fetchOrInitializePolicyServerCASecret(ctx context.Context, policyServer *policiesv1.PolicyServer, caSecret *corev1.Secret) error {
Expand Down
Loading

0 comments on commit 0915fa0

Please sign in to comment.