Skip to content

Commit 3ca1d0b

Browse files
Rick Rackowgeorgettica
authored andcommitted
operator-sdk: create boilerplate code
1 parent 709ff31 commit 3ca1d0b

30 files changed

+1088
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@
1313

1414
# Dependency directories (remove the comment below to include it)
1515
# vendor/
16+
17+
# binary for the operator
18+
bin/manager

Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Build the manager binary
2+
FROM golang:1.13 as builder
3+
4+
WORKDIR /workspace
5+
# Copy the Go Modules manifests
6+
COPY go.mod go.mod
7+
COPY go.sum go.sum
8+
# cache deps before building and copying source so that we don't need to re-download as much
9+
# and so that source changes don't invalidate our downloaded layer
10+
RUN go mod download
11+
12+
# Copy the go source
13+
COPY main.go main.go
14+
COPY api/ api/
15+
COPY controllers/ controllers/
16+
17+
# Build
18+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go
19+
20+
# Use distroless as minimal base image to package the manager binary
21+
# Refer to https://github.com/GoogleContainerTools/distroless for more details
22+
FROM gcr.io/distroless/static:nonroot
23+
WORKDIR /
24+
COPY --from=builder /workspace/manager .
25+
USER nonroot:nonroot
26+
27+
ENTRYPOINT ["/manager"]

Makefile

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# Current Operator version
2+
VERSION ?= 0.0.1
3+
# Default bundle image tag
4+
BUNDLE_IMG ?= controller-bundle:$(VERSION)
5+
# Options for 'bundle-build'
6+
ifneq ($(origin CHANNELS), undefined)
7+
BUNDLE_CHANNELS := --channels=$(CHANNELS)
8+
endif
9+
ifneq ($(origin DEFAULT_CHANNEL), undefined)
10+
BUNDLE_DEFAULT_CHANNEL := --default-channel=$(DEFAULT_CHANNEL)
11+
endif
12+
BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL)
13+
14+
# Image URL to use all building/pushing image targets
15+
IMG ?= controller:latest
16+
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
17+
CRD_OPTIONS ?= "crd:trivialVersions=true"
18+
19+
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
20+
ifeq (,$(shell go env GOBIN))
21+
GOBIN=$(shell go env GOPATH)/bin
22+
else
23+
GOBIN=$(shell go env GOBIN)
24+
endif
25+
26+
all: manager
27+
28+
# Run tests
29+
test: generate fmt vet manifests
30+
go test ./... -coverprofile cover.out
31+
32+
# Build manager binary
33+
manager: generate fmt vet
34+
go build -o bin/manager main.go
35+
36+
# Run against the configured Kubernetes cluster in ~/.kube/config
37+
run: generate fmt vet manifests
38+
go run ./main.go
39+
40+
# Install CRDs into a cluster
41+
install: manifests kustomize
42+
$(KUSTOMIZE) build config/crd | kubectl apply -f -
43+
44+
# Uninstall CRDs from a cluster
45+
uninstall: manifests kustomize
46+
$(KUSTOMIZE) build config/crd | kubectl delete -f -
47+
48+
# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
49+
deploy: manifests kustomize
50+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
51+
$(KUSTOMIZE) build config/default | kubectl apply -f -
52+
53+
# Generate manifests e.g. CRD, RBAC etc.
54+
manifests: controller-gen
55+
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
56+
57+
# Run go fmt against code
58+
fmt:
59+
go fmt ./...
60+
61+
# Run go vet against code
62+
vet:
63+
go vet ./...
64+
65+
# Generate code
66+
generate: controller-gen
67+
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
68+
69+
# Build the docker image
70+
docker-build: test
71+
docker build . -t ${IMG}
72+
73+
# Push the docker image
74+
docker-push:
75+
docker push ${IMG}
76+
77+
# find or download controller-gen
78+
# download controller-gen if necessary
79+
controller-gen:
80+
ifeq (, $(shell which controller-gen))
81+
@{ \
82+
set -e ;\
83+
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
84+
cd $$CONTROLLER_GEN_TMP_DIR ;\
85+
go mod init tmp ;\
86+
go get sigs.k8s.io/controller-tools/cmd/controller-gen@v0.3.0 ;\
87+
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
88+
}
89+
CONTROLLER_GEN=$(GOBIN)/controller-gen
90+
else
91+
CONTROLLER_GEN=$(shell which controller-gen)
92+
endif
93+
94+
kustomize:
95+
ifeq (, $(shell which kustomize))
96+
@{ \
97+
set -e ;\
98+
KUSTOMIZE_GEN_TMP_DIR=$$(mktemp -d) ;\
99+
cd $$KUSTOMIZE_GEN_TMP_DIR ;\
100+
go mod init tmp ;\
101+
go get sigs.k8s.io/kustomize/kustomize/v3@v3.5.4 ;\
102+
rm -rf $$KUSTOMIZE_GEN_TMP_DIR ;\
103+
}
104+
KUSTOMIZE=$(GOBIN)/kustomize
105+
else
106+
KUSTOMIZE=$(shell which kustomize)
107+
endif
108+
109+
# Generate bundle manifests and metadata, then validate generated files.
110+
bundle: manifests
111+
operator-sdk generate kustomize manifests -q
112+
kustomize build config/manifests | operator-sdk generate bundle -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
113+
operator-sdk bundle validate ./bundle
114+
115+
# Build the bundle image.
116+
bundle-build:
117+
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .

PROJECT

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
domain: openshift.io
2+
layout: go.kubebuilder.io/v2
3+
repo: github.com/RiRa12621/openshift-route-monitor-operator
4+
version: 3-alpha
5+
plugins:
6+
go.operator-sdk.io/v2-alpha: {}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# The following manifests contain a self-signed issuer CR and a certificate CR.
2+
# More document can be found at https://docs.cert-manager.io
3+
# WARNING: Targets CertManager 0.11 check https://docs.cert-manager.io/en/latest/tasks/upgrading/index.html for
4+
# breaking changes
5+
apiVersion: cert-manager.io/v1alpha2
6+
kind: Issuer
7+
metadata:
8+
name: selfsigned-issuer
9+
namespace: system
10+
spec:
11+
selfSigned: {}
12+
---
13+
apiVersion: cert-manager.io/v1alpha2
14+
kind: Certificate
15+
metadata:
16+
name: serving-cert # this name should match the one appeared in kustomizeconfig.yaml
17+
namespace: system
18+
spec:
19+
# $(SERVICE_NAME) and $(SERVICE_NAMESPACE) will be substituted by kustomize
20+
dnsNames:
21+
- $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc
22+
- $(SERVICE_NAME).$(SERVICE_NAMESPACE).svc.cluster.local
23+
issuerRef:
24+
kind: Issuer
25+
name: selfsigned-issuer
26+
secretName: webhook-server-cert # this secret will not be prefixed, since it's not managed by kustomize
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
resources:
2+
- certificate.yaml
3+
4+
configurations:
5+
- kustomizeconfig.yaml
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# This configuration is for teaching kustomize how to update name ref and var substitution
2+
nameReference:
3+
- kind: Issuer
4+
group: cert-manager.io
5+
fieldSpecs:
6+
- kind: Certificate
7+
group: cert-manager.io
8+
path: spec/issuerRef/name
9+
10+
varReference:
11+
- kind: Certificate
12+
group: cert-manager.io
13+
path: spec/commonName
14+
- kind: Certificate
15+
group: cert-manager.io
16+
path: spec/dnsNames

config/default/kustomization.yaml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Adds namespace to all resources.
2+
namespace: openshift-route-monitor-operator-system
3+
4+
# Value of this field is prepended to the
5+
# names of all resources, e.g. a deployment named
6+
# "wordpress" becomes "alices-wordpress".
7+
# Note that it should also match with the prefix (text before '-') of the namespace
8+
# field above.
9+
namePrefix: openshift-route-monitor-operator-
10+
11+
# Labels to add to all resources and selectors.
12+
#commonLabels:
13+
# someName: someValue
14+
15+
bases:
16+
- ../crd
17+
- ../rbac
18+
- ../manager
19+
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
20+
# crd/kustomization.yaml
21+
#- ../webhook
22+
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
23+
#- ../certmanager
24+
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
25+
#- ../prometheus
26+
27+
patchesStrategicMerge:
28+
# Protect the /metrics endpoint by putting it behind auth.
29+
# If you want your controller-manager to expose the /metrics
30+
# endpoint w/o any authn/z, please comment the following line.
31+
- manager_auth_proxy_patch.yaml
32+
33+
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
34+
# crd/kustomization.yaml
35+
#- manager_webhook_patch.yaml
36+
37+
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'.
38+
# Uncomment 'CERTMANAGER' sections in crd/kustomization.yaml to enable the CA injection in the admission webhooks.
39+
# 'CERTMANAGER' needs to be enabled to use ca injection
40+
#- webhookcainjection_patch.yaml
41+
42+
# the following config is for teaching kustomize how to do var substitution
43+
vars:
44+
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER' prefix.
45+
#- name: CERTIFICATE_NAMESPACE # namespace of the certificate CR
46+
# objref:
47+
# kind: Certificate
48+
# group: cert-manager.io
49+
# version: v1alpha2
50+
# name: serving-cert # this name should match the one in certificate.yaml
51+
# fieldref:
52+
# fieldpath: metadata.namespace
53+
#- name: CERTIFICATE_NAME
54+
# objref:
55+
# kind: Certificate
56+
# group: cert-manager.io
57+
# version: v1alpha2
58+
# name: serving-cert # this name should match the one in certificate.yaml
59+
#- name: SERVICE_NAMESPACE # namespace of the service
60+
# objref:
61+
# kind: Service
62+
# version: v1
63+
# name: webhook-service
64+
# fieldref:
65+
# fieldpath: metadata.namespace
66+
#- name: SERVICE_NAME
67+
# objref:
68+
# kind: Service
69+
# version: v1
70+
# name: webhook-service
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# This patch inject a sidecar container which is a HTTP proxy for the
2+
# controller manager, it performs RBAC authorization against the Kubernetes API using SubjectAccessReviews.
3+
apiVersion: apps/v1
4+
kind: Deployment
5+
metadata:
6+
name: controller-manager
7+
namespace: system
8+
spec:
9+
template:
10+
spec:
11+
containers:
12+
- name: kube-rbac-proxy
13+
image: gcr.io/kubebuilder/kube-rbac-proxy:v0.5.0
14+
args:
15+
- "--secure-listen-address=0.0.0.0:8443"
16+
- "--upstream=http://127.0.0.1:8080/"
17+
- "--logtostderr=true"
18+
- "--v=10"
19+
ports:
20+
- containerPort: 8443
21+
name: https
22+
- name: manager
23+
args:
24+
- "--metrics-addr=127.0.0.1:8080"
25+
- "--enable-leader-election"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: controller-manager
5+
namespace: system
6+
spec:
7+
template:
8+
spec:
9+
containers:
10+
- name: manager
11+
ports:
12+
- containerPort: 9443
13+
name: webhook-server
14+
protocol: TCP
15+
volumeMounts:
16+
- mountPath: /tmp/k8s-webhook-server/serving-certs
17+
name: cert
18+
readOnly: true
19+
volumes:
20+
- name: cert
21+
secret:
22+
defaultMode: 420
23+
secretName: webhook-server-cert

0 commit comments

Comments
 (0)