Skip to content

Commit b879946

Browse files
committed
Initial commit.
0 parents  commit b879946

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+3525
-0
lines changed

.dockerignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
2+
# Ignore all files which are not go type
3+
!**/*.go
4+
!**/*.mod
5+
!**/*.sum

.gitignore

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
# Binaries for programs and plugins
3+
*.exe
4+
*.exe~
5+
*.dll
6+
*.so
7+
*.dylib
8+
bin
9+
testbin/*
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+
*.swp
24+
*.swo
25+
*~

Dockerfile

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Build the manager binary
2+
FROM golang:1.15 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 65532:65532
26+
27+
ENTRYPOINT ["/manager"]

LICENSE

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Copyright 2021 Glints Intern Pte Ltd
2+
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.

Makefile

+200
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,200 @@
1+
# VERSION defines the project version for the bundle.
2+
# Update this value when you upgrade the version of your project.
3+
# To re-generate a bundle for another specific version without changing the standard setup, you can:
4+
# - use the VERSION as arg of the bundle target (e.g make bundle VERSION=0.0.2)
5+
# - use environment variables to overwrite this value (e.g export VERSION=0.0.2)
6+
VERSION ?= 0.0.1
7+
8+
# CHANNELS define the bundle channels used in the bundle.
9+
# Add a new line here if you would like to change its default config. (E.g CHANNELS = "preview,fast,stable")
10+
# To re-generate a bundle for other specific channels without changing the standard setup, you can:
11+
# - use the CHANNELS as arg of the bundle target (e.g make bundle CHANNELS=preview,fast,stable)
12+
# - use environment variables to overwrite this value (e.g export CHANNELS="preview,fast,stable")
13+
ifneq ($(origin CHANNELS), undefined)
14+
BUNDLE_CHANNELS := --channels=$(CHANNELS)
15+
endif
16+
17+
# DEFAULT_CHANNEL defines the default channel used in the bundle.
18+
# Add a new line here if you would like to change its default config. (E.g DEFAULT_CHANNEL = "stable")
19+
# To re-generate a bundle for any other default channel without changing the default setup, you can:
20+
# - use the DEFAULT_CHANNEL as arg of the bundle target (e.g make bundle DEFAULT_CHANNEL=stable)
21+
# - use environment variables to overwrite this value (e.g export DEFAULT_CHANNEL="stable")
22+
ifneq ($(origin DEFAULT_CHANNEL), undefined)
23+
BUNDLE_DEFAULT_CHANNEL := --default-channel=$(DEFAULT_CHANNEL)
24+
endif
25+
BUNDLE_METADATA_OPTS ?= $(BUNDLE_CHANNELS) $(BUNDLE_DEFAULT_CHANNEL)
26+
27+
# IMAGE_TAG_BASE defines the docker.io namespace and part of the image name for remote images.
28+
# This variable is used to construct full image tags for bundle and catalog images.
29+
#
30+
# For example, running 'make bundle-build bundle-push catalog-build catalog-push' will build and push both
31+
# glints.com/postgres-config-operator-bundle:$VERSION and glints.com/postgres-config-operator-catalog:$VERSION.
32+
IMAGE_TAG_BASE ?= glints.com/postgres-config-operator
33+
34+
# BUNDLE_IMG defines the image:tag used for the bundle.
35+
# You can use it as an arg. (E.g make bundle-build BUNDLE_IMG=<some-registry>/<project-name-bundle>:<tag>)
36+
BUNDLE_IMG ?= $(IMAGE_TAG_BASE)-bundle:v$(VERSION)
37+
38+
# Image URL to use all building/pushing image targets
39+
IMG ?= controller:latest
40+
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
41+
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"
42+
43+
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
44+
ifeq (,$(shell go env GOBIN))
45+
GOBIN=$(shell go env GOPATH)/bin
46+
else
47+
GOBIN=$(shell go env GOBIN)
48+
endif
49+
50+
# Setting SHELL to bash allows bash commands to be executed by recipes.
51+
# This is a requirement for 'setup-envtest.sh' in the test target.
52+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
53+
SHELL = /usr/bin/env bash -o pipefail
54+
.SHELLFLAGS = -ec
55+
56+
all: build
57+
58+
##@ General
59+
60+
# The help target prints out all targets with their descriptions organized
61+
# beneath their categories. The categories are represented by '##@' and the
62+
# target descriptions by '##'. The awk commands is responsible for reading the
63+
# entire set of makefiles included in this invocation, looking for lines of the
64+
# file as xyz: ## something, and then pretty-format the target and help. Then,
65+
# if there's a line with ##@ something, that gets pretty-printed as a category.
66+
# More info on the usage of ANSI control characters for terminal formatting:
67+
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
68+
# More info on the awk command:
69+
# http://linuxcommand.org/lc3_adv_awk.php
70+
71+
help: ## Display this help.
72+
@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)
73+
74+
##@ Development
75+
76+
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
77+
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
78+
79+
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
80+
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
81+
82+
fmt: ## Run go fmt against code.
83+
go fmt ./...
84+
85+
vet: ## Run go vet against code.
86+
go vet ./...
87+
88+
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
89+
test: manifests generate fmt vet ## Run tests.
90+
mkdir -p ${ENVTEST_ASSETS_DIR}
91+
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.8.3/hack/setup-envtest.sh
92+
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out
93+
94+
##@ Build
95+
96+
build: generate fmt vet ## Build manager binary.
97+
go build -o bin/manager main.go
98+
99+
run: manifests generate fmt vet ## Run a controller from your host.
100+
go run ./main.go
101+
102+
docker-build: test ## Build docker image with the manager.
103+
docker build -t ${IMG} .
104+
105+
docker-push: ## Push docker image with the manager.
106+
docker push ${IMG}
107+
108+
##@ Deployment
109+
110+
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
111+
$(KUSTOMIZE) build config/crd | kubectl apply -f -
112+
113+
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
114+
$(KUSTOMIZE) build config/crd | kubectl delete -f -
115+
116+
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
117+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
118+
$(KUSTOMIZE) build config/default | kubectl apply -f -
119+
120+
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
121+
$(KUSTOMIZE) build config/default | kubectl delete -f -
122+
123+
124+
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
125+
controller-gen: ## Download controller-gen locally if necessary.
126+
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected])
127+
128+
KUSTOMIZE = $(shell pwd)/bin/kustomize
129+
kustomize: ## Download kustomize locally if necessary.
130+
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected])
131+
132+
# go-get-tool will 'go get' any package $2 and install it to $1.
133+
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
134+
define go-get-tool
135+
@[ -f $(1) ] || { \
136+
set -e ;\
137+
TMP_DIR=$$(mktemp -d) ;\
138+
cd $$TMP_DIR ;\
139+
go mod init tmp ;\
140+
echo "Downloading $(2)" ;\
141+
GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\
142+
rm -rf $$TMP_DIR ;\
143+
}
144+
endef
145+
146+
.PHONY: bundle
147+
bundle: manifests kustomize ## Generate bundle manifests and metadata, then validate generated files.
148+
operator-sdk generate kustomize manifests -q
149+
cd config/manager && $(KUSTOMIZE) edit set image controller=$(IMG)
150+
$(KUSTOMIZE) build config/manifests | operator-sdk generate bundle -q --overwrite --version $(VERSION) $(BUNDLE_METADATA_OPTS)
151+
operator-sdk bundle validate ./bundle
152+
153+
.PHONY: bundle-build
154+
bundle-build: ## Build the bundle image.
155+
docker build -f bundle.Dockerfile -t $(BUNDLE_IMG) .
156+
157+
.PHONY: bundle-push
158+
bundle-push: ## Push the bundle image.
159+
$(MAKE) docker-push IMG=$(BUNDLE_IMG)
160+
161+
.PHONY: opm
162+
OPM = ./bin/opm
163+
opm: ## Download opm locally if necessary.
164+
ifeq (,$(wildcard $(OPM)))
165+
ifeq (,$(shell which opm 2>/dev/null))
166+
@{ \
167+
set -e ;\
168+
mkdir -p $(dir $(OPM)) ;\
169+
OS=$(shell go env GOOS) && ARCH=$(shell go env GOARCH) && \
170+
curl -sSLo $(OPM) https://github.com/operator-framework/operator-registry/releases/download/v1.15.1/$${OS}-$${ARCH}-opm ;\
171+
chmod +x $(OPM) ;\
172+
}
173+
else
174+
OPM = $(shell which opm)
175+
endif
176+
endif
177+
178+
# A comma-separated list of bundle images (e.g. make catalog-build BUNDLE_IMGS=example.com/operator-bundle:v0.1.0,example.com/operator-bundle:v0.2.0).
179+
# These images MUST exist in a registry and be pull-able.
180+
BUNDLE_IMGS ?= $(BUNDLE_IMG)
181+
182+
# The image tag given to the resulting catalog image (e.g. make catalog-build CATALOG_IMG=example.com/operator-catalog:v0.2.0).
183+
CATALOG_IMG ?= $(IMAGE_TAG_BASE)-catalog:v$(VERSION)
184+
185+
# Set CATALOG_BASE_IMG to an existing catalog image tag to add $BUNDLE_IMGS to that image.
186+
ifneq ($(origin CATALOG_BASE_IMG), undefined)
187+
FROM_INDEX_OPT := --from-index $(CATALOG_BASE_IMG)
188+
endif
189+
190+
# Build a catalog image by adding bundle images to an empty catalog using the operator package manager tool, 'opm'.
191+
# This recipe invokes 'opm' in 'semver' bundle add mode. For more information on add modes, see:
192+
# https://github.com/operator-framework/community-operators/blob/7f1438c/docs/packaging-operator.md#updating-your-existing-operator
193+
.PHONY: catalog-build
194+
catalog-build: opm ## Build a catalog image.
195+
$(OPM) index add --container-tool docker --mode semver --tag $(CATALOG_IMG) --bundles $(BUNDLE_IMGS) $(FROM_INDEX_OPT)
196+
197+
# Push the catalog image.
198+
.PHONY: catalog-push
199+
catalog-push: ## Push a catalog image.
200+
$(MAKE) docker-push IMG=$(CATALOG_IMG)

PROJECT

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
domain: glints.com
2+
layout:
3+
- go.kubebuilder.io/v3
4+
plugins:
5+
manifests.sdk.operatorframework.io/v2: {}
6+
scorecard.sdk.operatorframework.io/v2: {}
7+
projectName: postgres-config-operator
8+
repo: github.com/glints-dev/postgres-config-operator
9+
resources:
10+
- api:
11+
crdVersion: v1
12+
namespaced: true
13+
controller: true
14+
domain: glints.com
15+
group: postgres
16+
kind: PostgresConfig
17+
path: github.com/glints-dev/postgres-config-operator/api/v1alpha1
18+
version: v1alpha1
19+
version: "3"

README.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# PostgreSQL Configuration Operator
2+
3+
The Postgres Configuration Operator allows for declarative configuration of
4+
PostgreSQL instances using Custom Resource Definitions (CRDs).
5+
6+
This operator is written using the [Operator SDK](https://sdk.operatorframework.io/).
7+
8+
## Operator Features
9+
10+
- Creating and maintaining [publications](https://www.postgresql.org/docs/current/sql-createpublication.html)
11+
- (Coming soon) Creating and maintaining [roles](https://www.postgresql.org/docs/13/sql-createrole.html)
12+
13+
## Getting Started
14+
15+
As this operator is in early development, there are no releases at the moment.
16+
17+
To build a copy of the operator and push the resulting image to a Docker
18+
registry, use the following command:
19+
20+
```
21+
make docker-build docker-push IMG="localhost:5000/postgres-config-operator:latest"
22+
```
23+
24+
To deploy to a running Kubernetes cluster using the current context configured
25+
with `kubectl`, use:
26+
27+
```
28+
make deploy IMG="localhost:5000/postgres-config-operator:latest"
29+
```
30+
31+
## Development & Testing
32+
33+
Testing can be done locally by simply running:
34+
35+
```
36+
make test
37+
```

api/v1alpha1/groupversion_info.go

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2021.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package v1alpha1 contains API Schema definitions for the postgres v1alpha1 API group
18+
//+kubebuilder:object:generate=true
19+
//+groupName=postgres.glints.com
20+
package v1alpha1
21+
22+
import (
23+
"k8s.io/apimachinery/pkg/runtime/schema"
24+
"sigs.k8s.io/controller-runtime/pkg/scheme"
25+
)
26+
27+
var (
28+
// GroupVersion is group version used to register these objects
29+
GroupVersion = schema.GroupVersion{Group: "postgres.glints.com", Version: "v1alpha1"}
30+
31+
// SchemeBuilder is used to add go types to the GroupVersionKind scheme
32+
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}
33+
34+
// AddToScheme adds the types in this group-version to the given scheme.
35+
AddToScheme = SchemeBuilder.AddToScheme
36+
)

0 commit comments

Comments
 (0)