Skip to content

Commit bb78661

Browse files
generate api group and controller
1 parent 35f2f67 commit bb78661

40 files changed

+1899
-0
lines changed

.dockerignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
2+
# Ignore build and test binaries.
3+
bin/

.gitignore

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
2+
# Binaries for programs and plugins
3+
*.exe
4+
*.exe~
5+
*.dll
6+
*.so
7+
*.dylib
8+
bin/*
9+
Dockerfile.cross
10+
11+
# Test binary, build with `go test -c`
12+
*.test
13+
14+
# Output of the go coverage tool, specifically when used with LiteIDE
15+
*.out
16+
17+
# Kubernetes Generated files - skip generated files, except for vendored files
18+
19+
!vendor/**/zz_generated.*
20+
21+
# editor and IDE paraphernalia
22+
.idea
23+
.vscode
24+
*.swp
25+
*.swo
26+
*~

.golangci.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
run:
2+
deadline: 5m
3+
allow-parallel-runners: true
4+
5+
issues:
6+
# don't skip warning about doc comments
7+
# don't exclude the default set of lint
8+
exclude-use-default: false
9+
# restore some of the defaults
10+
# (fill in the rest as needed)
11+
exclude-rules:
12+
- path: "api/*"
13+
linters:
14+
- lll
15+
- path: "internal/*"
16+
linters:
17+
- dupl
18+
- lll
19+
linters:
20+
disable-all: true
21+
enable:
22+
- dupl
23+
- errcheck
24+
- exportloopref
25+
- goconst
26+
- gocyclo
27+
- gofmt
28+
- goimports
29+
- gosimple
30+
- govet
31+
- ineffassign
32+
- lll
33+
- misspell
34+
- nakedret
35+
- prealloc
36+
- staticcheck
37+
- typecheck
38+
- unconvert
39+
- unparam
40+
- unused

Dockerfile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Build the manager binary
2+
FROM golang:1.20 as builder
3+
ARG TARGETOS
4+
ARG TARGETARCH
5+
6+
WORKDIR /workspace
7+
# Copy the Go Modules manifests
8+
COPY go.mod go.mod
9+
COPY go.sum go.sum
10+
# cache deps before building and copying source so that we don't need to re-download as much
11+
# and so that source changes don't invalidate our downloaded layer
12+
RUN go mod download
13+
14+
# Copy the go source
15+
COPY cmd/main.go cmd/main.go
16+
COPY api/ api/
17+
COPY internal/controller/ internal/controller/
18+
19+
# Build
20+
# the GOARCH has not a default value to allow the binary be built according to the host where the command
21+
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
22+
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
23+
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
24+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go
25+
26+
# Use distroless as minimal base image to package the manager binary
27+
# Refer to https://github.com/GoogleContainerTools/distroless for more details
28+
FROM gcr.io/distroless/static:nonroot
29+
WORKDIR /
30+
COPY --from=builder /workspace/manager .
31+
USER 65532:65532
32+
33+
ENTRYPOINT ["/manager"]

Makefile

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
2+
# Image URL to use all building/pushing image targets
3+
IMG ?= controller:latest
4+
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
5+
ENVTEST_K8S_VERSION = 1.28.0
6+
7+
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
8+
ifeq (,$(shell go env GOBIN))
9+
GOBIN=$(shell go env GOPATH)/bin
10+
else
11+
GOBIN=$(shell go env GOBIN)
12+
endif
13+
14+
# CONTAINER_TOOL defines the container tool to be used for building images.
15+
# Be aware that the target commands are only tested with Docker which is
16+
# scaffolded by default. However, you might want to replace it to use other
17+
# tools. (i.e. podman)
18+
CONTAINER_TOOL ?= docker
19+
20+
# Setting SHELL to bash allows bash commands to be executed by recipes.
21+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
22+
SHELL = /usr/bin/env bash -o pipefail
23+
.SHELLFLAGS = -ec
24+
25+
.PHONY: all
26+
all: build
27+
28+
##@ General
29+
30+
# The help target prints out all targets with their descriptions organized
31+
# beneath their categories. The categories are represented by '##@' and the
32+
# target descriptions by '##'. The awk command is responsible for reading the
33+
# entire set of makefiles included in this invocation, looking for lines of the
34+
# file as xyz: ## something, and then pretty-format the target and help. Then,
35+
# if there's a line with ##@ something, that gets pretty-printed as a category.
36+
# More info on the usage of ANSI control characters for terminal formatting:
37+
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
38+
# More info on the awk command:
39+
# http://linuxcommand.org/lc3_adv_awk.php
40+
41+
.PHONY: help
42+
help: ## Display this help.
43+
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
44+
45+
##@ Development
46+
47+
.PHONY: manifests
48+
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
49+
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
50+
51+
.PHONY: generate
52+
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
53+
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
54+
55+
.PHONY: fmt
56+
fmt: ## Run go fmt against code.
57+
go fmt ./...
58+
59+
.PHONY: vet
60+
vet: ## Run go vet against code.
61+
go vet ./...
62+
63+
.PHONY: test
64+
test: manifests generate fmt vet envtest ## Run tests.
65+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
66+
67+
GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
68+
GOLANGCI_LINT_VERSION ?= v1.54.2
69+
golangci-lint:
70+
@[ -f $(GOLANGCI_LINT) ] || { \
71+
set -e ;\
72+
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) $(GOLANGCI_LINT_VERSION) ;\
73+
}
74+
75+
.PHONY: lint
76+
lint: golangci-lint ## Run golangci-lint linter & yamllint
77+
$(GOLANGCI_LINT) run
78+
79+
.PHONY: lint-fix
80+
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
81+
$(GOLANGCI_LINT) run --fix
82+
83+
##@ Build
84+
85+
.PHONY: build
86+
build: manifests generate fmt vet ## Build manager binary.
87+
go build -o bin/manager cmd/main.go
88+
89+
.PHONY: run
90+
run: manifests generate fmt vet ## Run a controller from your host.
91+
go run ./cmd/main.go
92+
93+
# If you wish to build the manager image targeting other platforms you can use the --platform flag.
94+
# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
95+
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
96+
.PHONY: docker-build
97+
docker-build: ## Build docker image with the manager.
98+
$(CONTAINER_TOOL) build -t ${IMG} .
99+
100+
.PHONY: docker-push
101+
docker-push: ## Push docker image with the manager.
102+
$(CONTAINER_TOOL) push ${IMG}
103+
104+
# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
105+
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
106+
# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
107+
# - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/
108+
# - be able to push the image to your registry (i.e. if you do not set a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
109+
# To adequately provide solutions that are compatible with multiple platforms, you should consider using this option.
110+
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
111+
.PHONY: docker-buildx
112+
docker-buildx: ## Build and push docker image for the manager for cross-platform support
113+
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
114+
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
115+
- $(CONTAINER_TOOL) buildx create --name project-v3-builder
116+
$(CONTAINER_TOOL) buildx use project-v3-builder
117+
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
118+
- $(CONTAINER_TOOL) buildx rm project-v3-builder
119+
rm Dockerfile.cross
120+
121+
##@ Deployment
122+
123+
ifndef ignore-not-found
124+
ignore-not-found = false
125+
endif
126+
127+
.PHONY: install
128+
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
129+
$(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f -
130+
131+
.PHONY: uninstall
132+
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
133+
$(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
134+
135+
.PHONY: deploy
136+
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
137+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
138+
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
139+
140+
.PHONY: undeploy
141+
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
142+
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
143+
144+
##@ Build Dependencies
145+
146+
## Location to install dependencies to
147+
LOCALBIN ?= $(shell pwd)/bin
148+
$(LOCALBIN):
149+
mkdir -p $(LOCALBIN)
150+
151+
## Tool Binaries
152+
KUBECTL ?= kubectl
153+
KUSTOMIZE ?= $(LOCALBIN)/kustomize
154+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
155+
ENVTEST ?= $(LOCALBIN)/setup-envtest
156+
157+
## Tool Versions
158+
KUSTOMIZE_VERSION ?= v5.2.1
159+
CONTROLLER_TOOLS_VERSION ?= v0.13.0
160+
161+
.PHONY: kustomize
162+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
163+
$(KUSTOMIZE): $(LOCALBIN)
164+
@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
165+
echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
166+
rm -rf $(LOCALBIN)/kustomize; \
167+
fi
168+
test -s $(LOCALBIN)/kustomize || GOBIN=$(LOCALBIN) GO111MODULE=on go install sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VERSION)
169+
170+
.PHONY: controller-gen
171+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
172+
$(CONTROLLER_GEN): $(LOCALBIN)
173+
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
174+
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
175+
176+
.PHONY: envtest
177+
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
178+
$(ENVTEST): $(LOCALBIN)
179+
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

PROJECT

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Code generated by tool. DO NOT EDIT.
2+
# This file is used to track the info used to scaffold your project
3+
# and allow the plugins properly work.
4+
# More info: https://book.kubebuilder.io/reference/project-config.html
5+
domain: kube-logging.dev
6+
layout:
7+
- go.kubebuilder.io/v4
8+
multigroup: true
9+
projectName: subscription-operator
10+
repo: github.com/kube-logging/subscription-operator
11+
resources:
12+
- api:
13+
crdVersion: v1
14+
namespaced: true
15+
controller: true
16+
domain: kube-logging.dev
17+
group: logging
18+
kind: Collector
19+
path: github.com/kube-logging/subscription-operator/api/logging/v1alpha1
20+
version: v1alpha1
21+
version: "3"

README.md

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# subscription-operator
2+
// TODO(user): Add simple overview of use/purpose
3+
4+
## Description
5+
// TODO(user): An in-depth paragraph about your project and overview of use
6+
7+
## Getting Started
8+
9+
### Prerequisites
10+
- go version v1.20.0+
11+
- docker version 17.03+.
12+
- kubectl version v1.11.3+.
13+
- Access to a Kubernetes v1.11.3+ cluster.
14+
15+
### To Deploy on the cluster
16+
**Build and push your image to the location specified by `IMG`:**
17+
18+
```sh
19+
make docker-build docker-push IMG=<some-registry>/subscription-operator:tag
20+
```
21+
22+
**NOTE:** This image ought to be published in the personal registry you specified.
23+
And it is required to have access to pull the image from the working environment.
24+
Make sure you have the proper permission to the registry if the above commands don’t work.
25+
26+
**Install the CRDs into the cluster:**
27+
28+
```sh
29+
make install
30+
```
31+
32+
**Deploy the Manager to the cluster with the image specified by `IMG`:**
33+
34+
```sh
35+
make deploy IMG=<some-registry>/subscription-operator:tag
36+
```
37+
38+
> **NOTE**: If you encounter RBAC errors, you may need to grant yourself cluster-admin
39+
privileges or be logged in as admin.
40+
41+
**Create instances of your solution**
42+
You can apply the samples (examples) from the config/sample:
43+
44+
```sh
45+
kubectl apply -k config/samples/
46+
```
47+
48+
>**NOTE**: Ensure that the samples has default values to test it out.
49+
50+
### To Uninstall
51+
**Delete the instances (CRs) from the cluster:**
52+
53+
```sh
54+
kubectl delete -k config/samples/
55+
```
56+
57+
**Delete the APIs(CRDs) from the cluster:**
58+
59+
```sh
60+
make uninstall
61+
```
62+
63+
**UnDeploy the controller from the cluster:**
64+
65+
```sh
66+
make undeploy
67+
```
68+
69+
## Contributing
70+
// TODO(user): Add detailed information on how you would like others to contribute to this project
71+
72+
**NOTE:** Run `make --help` for more information on all potential `make` targets
73+
74+
More information can be found via the [Kubebuilder Documentation](https://book.kubebuilder.io/introduction.html)
75+
76+
## License
77+
78+
Copyright 2023.
79+
80+
Licensed under the Apache License, Version 2.0 (the "License");
81+
you may not use this file except in compliance with the License.
82+
You may obtain a copy of the License at
83+
84+
http://www.apache.org/licenses/LICENSE-2.0
85+
86+
Unless required by applicable law or agreed to in writing, software
87+
distributed under the License is distributed on an "AS IS" BASIS,
88+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
89+
See the License for the specific language governing permissions and
90+
limitations under the License.
91+

0 commit comments

Comments
 (0)