Skip to content

Commit f1b6d8a

Browse files
committed
feat: nautobot operator
1 parent 8cb3182 commit f1b6d8a

Some content is hidden

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

67 files changed

+5059
-0
lines changed

go/rax/.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/

go/rax/.gitignore

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

go/rax/.golangci.yml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
version: "2"
2+
run:
3+
allow-parallel-runners: true
4+
linters:
5+
default: none
6+
enable:
7+
- dupl
8+
- errcheck
9+
- goconst
10+
- gocyclo
11+
- govet
12+
- ineffassign
13+
- lll
14+
- misspell
15+
- nakedret
16+
- prealloc
17+
- staticcheck
18+
- unconvert
19+
- unparam
20+
- unused
21+
exclusions:
22+
generated: lax
23+
rules:
24+
- linters:
25+
- lll
26+
path: api/*
27+
- linters:
28+
- dupl
29+
- lll
30+
path: internal/*
31+
paths:
32+
- third_party$
33+
- builtin$
34+
- examples$
35+
settings:
36+
staticcheck:
37+
dot-import-whitelist:
38+
- fmt
39+
- github.com/onsi/ginkgo/v2
40+
formatters:
41+
enable:
42+
- gofmt
43+
- goimports
44+
exclusions:
45+
generated: lax
46+
paths:
47+
- third_party$
48+
- builtin$
49+
- examples$

go/rax/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 docker.io/golang:1.23 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/ internal/
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"]

go/rax/Makefile

Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
# Image URL to use all building/pushing image targets
2+
IMG ?= controller:latest
3+
4+
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
5+
ifeq (,$(shell go env GOBIN))
6+
GOBIN=$(shell go env GOPATH)/bin
7+
else
8+
GOBIN=$(shell go env GOBIN)
9+
endif
10+
11+
# CONTAINER_TOOL defines the container tool to be used for building images.
12+
# Be aware that the target commands are only tested with Docker which is
13+
# scaffolded by default. However, you might want to replace it to use other
14+
# tools. (i.e. podman)
15+
CONTAINER_TOOL ?= docker
16+
17+
# Setting SHELL to bash allows bash commands to be executed by recipes.
18+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
19+
SHELL = /usr/bin/env bash -o pipefail
20+
.SHELLFLAGS = -ec
21+
22+
.PHONY: all
23+
all: build
24+
25+
##@ General
26+
27+
# The help target prints out all targets with their descriptions organized
28+
# beneath their categories. The categories are represented by '##@' and the
29+
# target descriptions by '##'. The awk command is responsible for reading the
30+
# entire set of makefiles included in this invocation, looking for lines of the
31+
# file as xyz: ## something, and then pretty-format the target and help. Then,
32+
# if there's a line with ##@ something, that gets pretty-printed as a category.
33+
# More info on the usage of ANSI control characters for terminal formatting:
34+
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
35+
# More info on the awk command:
36+
# http://linuxcommand.org/lc3_adv_awk.php
37+
38+
.PHONY: help
39+
help: ## Display this help.
40+
@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)
41+
42+
##@ Development
43+
44+
.PHONY: manifests
45+
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
46+
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
47+
48+
.PHONY: generate
49+
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
50+
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
51+
52+
.PHONY: fmt
53+
fmt: ## Run go fmt against code.
54+
go fmt ./...
55+
56+
.PHONY: vet
57+
vet: ## Run go vet against code.
58+
go vet ./...
59+
60+
.PHONY: test
61+
test: manifests generate fmt vet setup-envtest ## Run tests.
62+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test $$(go list ./... | grep -v /e2e) -coverprofile cover.out
63+
64+
# TODO(user): To use a different vendor for e2e tests, modify the setup under 'tests/e2e'.
65+
# The default setup assumes Kind is pre-installed and builds/loads the Manager Docker image locally.
66+
# CertManager is installed by default; skip with:
67+
# - CERT_MANAGER_INSTALL_SKIP=true
68+
.PHONY: test-e2e
69+
test-e2e: manifests generate fmt vet ## Run the e2e tests. Expected an isolated environment using Kind.
70+
@command -v $(KIND) >/dev/null 2>&1 || { \
71+
echo "Kind is not installed. Please install Kind manually."; \
72+
exit 1; \
73+
}
74+
@$(KIND) get clusters | grep -q 'kind' || { \
75+
echo "No Kind cluster is running. Please start a Kind cluster before running the e2e tests."; \
76+
exit 1; \
77+
}
78+
go test ./test/e2e/ -v -ginkgo.v
79+
80+
.PHONY: lint
81+
lint: golangci-lint ## Run golangci-lint linter
82+
$(GOLANGCI_LINT) run
83+
84+
.PHONY: lint-fix
85+
lint-fix: golangci-lint ## Run golangci-lint linter and perform fixes
86+
$(GOLANGCI_LINT) run --fix
87+
88+
.PHONY: lint-config
89+
lint-config: golangci-lint ## Verify golangci-lint linter configuration
90+
$(GOLANGCI_LINT) config verify
91+
92+
##@ Build
93+
94+
.PHONY: build
95+
build: manifests generate fmt vet ## Build manager binary.
96+
go build -o bin/manager cmd/main.go
97+
98+
.PHONY: run
99+
run: manifests generate fmt vet ## Run a controller from your host.
100+
go run ./cmd/main.go
101+
102+
# If you wish to build the manager image targeting other platforms you can use the --platform flag.
103+
# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
104+
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
105+
.PHONY: docker-build
106+
docker-build: ## Build docker image with the manager.
107+
$(CONTAINER_TOOL) build -t ${IMG} .
108+
109+
.PHONY: docker-push
110+
docker-push: ## Push docker image with the manager.
111+
$(CONTAINER_TOOL) push ${IMG}
112+
113+
# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
114+
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
115+
# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
116+
# - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/
117+
# - 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)
118+
# To adequately provide solutions that are compatible with multiple platforms, you should consider using this option.
119+
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
120+
.PHONY: docker-buildx
121+
docker-buildx: ## Build and push docker image for the manager for cross-platform support
122+
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
123+
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
124+
- $(CONTAINER_TOOL) buildx create --name rax-builder
125+
$(CONTAINER_TOOL) buildx use rax-builder
126+
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
127+
- $(CONTAINER_TOOL) buildx rm rax-builder
128+
rm Dockerfile.cross
129+
130+
.PHONY: build-installer
131+
build-installer: manifests generate kustomize ## Generate a consolidated YAML with CRDs and deployment.
132+
mkdir -p dist
133+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
134+
$(KUSTOMIZE) build config/default > dist/install.yaml
135+
136+
##@ Deployment
137+
138+
ifndef ignore-not-found
139+
ignore-not-found = false
140+
endif
141+
142+
.PHONY: install
143+
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
144+
$(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f -
145+
146+
.PHONY: uninstall
147+
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.
148+
$(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
149+
150+
.PHONY: deploy
151+
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
152+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
153+
$(KUSTOMIZE) build config/default | $(KUBECTL) apply -f -
154+
155+
.PHONY: undeploy
156+
undeploy: kustomize ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
157+
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
158+
159+
##@ Dependencies
160+
161+
## Location to install dependencies to
162+
LOCALBIN ?= $(shell pwd)/bin
163+
$(LOCALBIN):
164+
mkdir -p $(LOCALBIN)
165+
166+
## Tool Binaries
167+
KUBECTL ?= kubectl
168+
KIND ?= kind
169+
KUSTOMIZE ?= $(LOCALBIN)/kustomize
170+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
171+
ENVTEST ?= $(LOCALBIN)/setup-envtest
172+
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
173+
174+
## Tool Versions
175+
KUSTOMIZE_VERSION ?= v5.6.0
176+
CONTROLLER_TOOLS_VERSION ?= v0.17.2
177+
#ENVTEST_VERSION is the version of controller-runtime release branch to fetch the envtest setup script (i.e. release-0.20)
178+
ENVTEST_VERSION ?= $(shell go list -m -f "{{ .Version }}" sigs.k8s.io/controller-runtime | awk -F'[v.]' '{printf "release-%d.%d", $$2, $$3}')
179+
#ENVTEST_K8S_VERSION is the version of Kubernetes to use for setting up ENVTEST binaries (i.e. 1.31)
180+
ENVTEST_K8S_VERSION ?= $(shell go list -m -f "{{ .Version }}" k8s.io/api | awk -F'[v.]' '{printf "1.%d", $$3}')
181+
GOLANGCI_LINT_VERSION ?= v1.63.4
182+
183+
.PHONY: kustomize
184+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
185+
$(KUSTOMIZE): $(LOCALBIN)
186+
$(call go-install-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v5,$(KUSTOMIZE_VERSION))
187+
188+
.PHONY: controller-gen
189+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary.
190+
$(CONTROLLER_GEN): $(LOCALBIN)
191+
$(call go-install-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen,$(CONTROLLER_TOOLS_VERSION))
192+
193+
.PHONY: setup-envtest
194+
setup-envtest: envtest ## Download the binaries required for ENVTEST in the local bin directory.
195+
@echo "Setting up envtest binaries for Kubernetes version $(ENVTEST_K8S_VERSION)..."
196+
@$(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path || { \
197+
echo "Error: Failed to set up envtest binaries for version $(ENVTEST_K8S_VERSION)."; \
198+
exit 1; \
199+
}
200+
201+
.PHONY: envtest
202+
envtest: $(ENVTEST) ## Download setup-envtest locally if necessary.
203+
$(ENVTEST): $(LOCALBIN)
204+
$(call go-install-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest,$(ENVTEST_VERSION))
205+
206+
.PHONY: golangci-lint
207+
golangci-lint: $(GOLANGCI_LINT) ## Download golangci-lint locally if necessary.
208+
$(GOLANGCI_LINT): $(LOCALBIN)
209+
$(call go-install-tool,$(GOLANGCI_LINT),github.com/golangci/golangci-lint/cmd/golangci-lint,$(GOLANGCI_LINT_VERSION))
210+
211+
# go-install-tool will 'go install' any package with custom target and name of binary, if it doesn't exist
212+
# $1 - target path with name of binary
213+
# $2 - package url which can be installed
214+
# $3 - specific version of package
215+
define go-install-tool
216+
@[ -f "$(1)-$(3)" ] || { \
217+
set -e; \
218+
package=$(2)@$(3) ;\
219+
echo "Downloading $${package}" ;\
220+
rm -f $(1) || true ;\
221+
GOBIN=$(LOCALBIN) go install $${package} ;\
222+
mv $(1) $(1)-$(3) ;\
223+
} ;\
224+
ln -sf $(1)-$(3) $(1)
225+
endef

go/rax/PROJECT

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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: rax.io
6+
layout:
7+
- go.kubebuilder.io/v4
8+
projectName: rax
9+
repo: github.com/rackerlabs/understack/go/sync
10+
resources:
11+
- api:
12+
crdVersion: v1
13+
namespaced: true
14+
controller: true
15+
domain: rax.io
16+
group: sync
17+
kind: Nautobot
18+
path: github.com/rackerlabs/understack/go/sync/api/v1alpha1
19+
version: v1alpha1
20+
- api:
21+
crdVersion: v1
22+
namespaced: true
23+
controller: true
24+
domain: rax.io
25+
group: sync
26+
kind: GitRepoWatcher
27+
path: github.com/rackerlabs/understack/go/sync/api/v1alpha1
28+
version: v1alpha1
29+
version: "3"

0 commit comments

Comments
 (0)