Skip to content

Commit

Permalink
use kubebuilder go/v4 template
Browse files Browse the repository at this point in the history
resolve: #213

Signed-off-by: Yuji Ito <llamerada.jp@gmail.com>
  • Loading branch information
llamerada-jp committed Jan 11, 2024
1 parent 4a59fd7 commit a7b467f
Show file tree
Hide file tree
Showing 51 changed files with 374 additions and 636 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/e2e.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ jobs:
- uses: actions/setup-go@v5
with:
go-version-file: "go.mod"
- run: make -C e2e setup
- run: make -C e2e init-app-with-cert-manager
- run: make -C e2e test
- run: make -C test/e2e setup
- run: make -C test/e2e init-app-with-cert-manager
- run: make -C test/e2e test

e2e-k8s-without-cert-manager:
name: "e2e-k8s-without-cert-manager"
Expand All @@ -41,6 +41,6 @@ jobs:
- uses: actions/setup-go@v5
with:
go-version-file: "go.mod"
- run: make -C e2e setup
- run: make -C e2e init-app-without-cert-manager
- run: make -C e2e test
- run: make -C test/e2e setup
- run: make -C test/e2e init-app-without-cert-manager
- run: make -C test/e2e test
4 changes: 2 additions & 2 deletions .github/workflows/helm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ jobs:

- name: "Setup kind"
run: |
make -C e2e setup
make -C e2e init-cluster
make -C test/e2e setup
make -C test/e2e init-cluster
- name: "Run chart-testing (install)"
run: ct install --config ct.yaml
40 changes: 40 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
run:
deadline: 5m
allow-parallel-runners: true

issues:
# don't skip warning about doc comments
# don't exclude the default set of lint
exclude-use-default: false
# restore some of the defaults
# (fill in the rest as needed)
exclude-rules:
- path: "api/*"
linters:
- lll
- path: "internal/*"
linters:
- dupl
- lll
linters:
disable-all: true
enable:
- dupl
- errcheck
- exportloopref
- goconst
- gocyclo
- gofmt
- goimports
- gosimple
- govet
- ineffassign
- lll
- misspell
- nakedret
- prealloc
- staticcheck
- typecheck
- unconvert
- unparam
- unused
19 changes: 10 additions & 9 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
# Contributing Guide

- [Ways to Contribute](#ways-to-contribute)
- [Find an Issue](#find-an-issue)
- [Ask for Help](#ask-for-help)
- [Pull Request Lifecycle](#pull-request-lifecycle)
- [Development Environment Setup](#development-environment-setup)
- [Sign Your Commits](#sign-your-commits)
- [DCO](#dco)
- [Pull Request Checklist](#pull-request-checklist)
- [Contributing Guide](#contributing-guide)
- [Ways to Contribute](#ways-to-contribute)
- [Find an Issue](#find-an-issue)
- [Ask for Help](#ask-for-help)
- [Pull Request Lifecycle](#pull-request-lifecycle)
- [Development Environment Setup](#development-environment-setup)
- [Sign Your Commits](#sign-your-commits)
- [DCO](#dco)
- [Pull Request Checklist](#pull-request-checklist)

Welcome! We are glad that you want to contribute to our project!

Expand Down Expand Up @@ -100,7 +101,7 @@ we suggest preparing a dedicated physical or virtual machine.
make test

# for end-to-end test
cd e2e
cd test/e2e
make setup
make init-cluster
make test
Expand Down
11 changes: 5 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
# Stage1: Build the pvc-autoresizer binary
FROM golang:1.20-bullseye as builder
FROM golang:1.20 as builder

WORKDIR /workspace

# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum
# Copy the go source
COPY main.go main.go
COPY cmd/ cmd/
COPY hooks/ hooks/
COPY metrics/ metrics/
COPY runners/ runners/
COPY hooks/ hooks/
COPY cmd/ cmd/

# Build
RUN CGO_ENABLED=0 go build -ldflags="-w -s" -a -o pvc-autoresizer main.go
RUN CGO_ENABLED=0 go build -ldflags="-w -s" -a -o pvc-autoresizer cmd/*.go

# Stage2: setup runtime container
FROM scratch
Expand Down
42 changes: 28 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ IMAGE_TAG ?= latest
IMAGE_PREFIX ?= ghcr.io/topolvm/

# Setting SHELL to bash allows bash commands to be executed by recipes.
# This is a requirement for 'setup-envtest.sh' in the test target.
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec
Expand Down Expand Up @@ -50,16 +49,12 @@ help: ## Display this help.
manifests: ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=controller webhook paths="./..." output:crd:artifacts:config=config/crd/bases

.PHONY: generate-api
generate-api: ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

.PHONY: generate-helm-docs
generate-helm-docs:
./bin/helm-docs -c charts/pvc-autoresizer/

.PHONY: generate
generate: manifests generate-api generate-helm-docs
generate: manifests generate-helm-docs

.PHONY: check-uncommitted
check-uncommitted: generate ## Check if latest generated artifacts are committed.
Expand All @@ -73,26 +68,38 @@ fmt: ## Run go fmt against code.
vet: ## Run go vet against code.
go vet ./...

.PHONY: test
test: manifests generate tools fmt vet ## Run tests.
$(shell go env GOPATH)/bin/staticcheck ./...
go install ./...
source <($(SETUP_ENVTEST) use -p env $(ENVTEST_K8S_VERSION)); \
go test -race -v -count 1 ./... --timeout=60s

GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
GOLANGCI_LINT_VERSION ?= v1.54.2
golangci-lint:
@[ -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: golangci-lint ## Run golangci-lint linter & yamllint
$(GOLANGCI_LINT) run

.PHONY: lint-fix
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
$(GOLANGCI_LINT) run --fix

##@ Build

.PHONY: build
build: generate-api ## Build manager binary.
go build -o $(BINDIR)/manager main.go
build: ## Build manager binary.
go build -o $(BINDIR)/manager cmd/*.go

.PHONY: run
run: manifests generate ## Run a controller from your host.
go run ./main.go

.PHONY: deploy
deploy: manifests ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cd $(shell pwd)/config/default && $(KUSTOMIZE) edit set image pvc-autoresizer=$(IMAGE_PREFIX)pvc-autoresizer:devel
$(KUSTOMIZE) build $(shell pwd)/config/default | $(KUBECTL) apply -f -
go run ./cmd/main.go

.PHONY: image
image: ## Build docker image.
Expand All @@ -106,6 +113,13 @@ tag: ## Set a docker tag to the image.
push: ## Push docker image.
docker push $(IMAGE_PREFIX)pvc-autoresizer:$(IMAGE_TAG)

##@ Deployment

.PHONY: deploy
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cd $(shell pwd)/config/default && $(KUSTOMIZE) edit set image pvc-autoresizer=$(IMAGE_PREFIX)pvc-autoresizer:devel
$(KUSTOMIZE) build $(shell pwd)/config/default | $(KUBECTL) apply -f -

##@ Chart Testing

.PHONY: ct-lint
Expand Down
6 changes: 5 additions & 1 deletion PROJECT
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Code generated by tool. DO NOT EDIT.
# This file is used to track the info used to scaffold your project
# and allow the plugins properly work.
# More info: https://book.kubebuilder.io/reference/project-config.html
domain: topolvm.io
layout:
- go.kubebuilder.io/v3
- go.kubebuilder.io/v4
projectName: pvc-autoresizer
repo: github.com/topolvm/pvc-autoresizer
version: "3"
8 changes: 4 additions & 4 deletions cmd/root.go → cmd/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package cmd
package main

import (
"fmt"
"os"
"time"

"github.com/spf13/cobra"
_ "k8s.io/client-go/plugin/pkg/client/auth"
//+kubebuilder:scaffold:imports
)

var config struct {
Expand All @@ -31,9 +33,7 @@ var rootCmd = &cobra.Command{
},
}

// Execute adds all child commands to the root command and sets flags appropriately.
// This is called by main.main(). It only needs to happen once to the rootCmd.
func Execute() {
func main() {
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
Expand Down
55 changes: 43 additions & 12 deletions cmd/run.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package main

import (
"net"
Expand All @@ -7,14 +7,19 @@ import (
"github.com/topolvm/pvc-autoresizer/hooks"
"github.com/topolvm/pvc-autoresizer/runners"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/healthz"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metricsserver "sigs.k8s.io/controller-runtime/pkg/metrics/server"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
// +kubebuilder:scaffold:imports
//+kubebuilder:scaffold:imports
)

var (
Expand All @@ -23,10 +28,10 @@ var (
)

func init() {
_ = clientgoscheme.AddToScheme(scheme)
utilruntime.Must(clientgoscheme.AddToScheme(scheme))

_ = corev1.AddToScheme(scheme)
// +kubebuilder:scaffold:scheme
utilruntime.Must(corev1.AddToScheme(scheme))
//+kubebuilder:scaffold:scheme
}

func subMain() error {
Expand All @@ -44,13 +49,39 @@ func subMain() error {
}

graceTimeout := 10 * time.Second

var pvcCacheTarget cache.ByObject
if len(config.namespaces) == 0 {
pvcCacheTarget = cache.ByObject{
Namespaces: map[string]cache.Config{
cache.AllNamespaces: {},
},
}
} else {
pvcCacheTarget = cache.ByObject{
Namespaces: map[string]cache.Config{},
}
for _, ns := range config.namespaces {
pvcCacheTarget.Namespaces[ns] = cache.Config{}
}
}

mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
Scheme: scheme,
Host: hookHost,
Port: hookPort,
CertDir: config.certDir,
MetricsBindAddress: config.metricsAddr,
Cache: cache.Options{Namespaces: config.namespaces},
Scheme: scheme,
WebhookServer: webhook.NewServer(webhook.Options{
Host: hookHost,
Port: hookPort,
CertDir: config.certDir,
}),
Metrics: metricsserver.Options{
BindAddress: config.metricsAddr,
},
Cache: cache.Options{
ByObject: map[client.Object]cache.ByObject{
&corev1.PersistentVolumeClaim{}: pvcCacheTarget,
&storagev1.StorageClass{}: {},
},
},
HealthProbeBindAddress: config.healthAddr,
LeaderElection: true,
LeaderElectionID: "49e22f61.topolvm.io",
Expand Down Expand Up @@ -96,7 +127,7 @@ func subMain() error {
return err
}

// +kubebuilder:scaffold:builder
//+kubebuilder:scaffold:builder

setupLog.Info("starting manager")
if err := mgr.Start(ctrl.SetupSignalHandler()); err != nil {
Expand Down
26 changes: 0 additions & 26 deletions config/certmanager/certificate.yaml

This file was deleted.

5 changes: 0 additions & 5 deletions config/certmanager/kustomization.yaml

This file was deleted.

Loading

0 comments on commit a7b467f

Please sign in to comment.