Skip to content

Commit 11b1653

Browse files
authored
Implement partial cleanups (#25)
1 parent cae217b commit 11b1653

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

+6406
-2473
lines changed

.dockerignore

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
2-
# Ignore build and test binaries.
3-
bin/
2+
# Ignore everything by default and re-include only needed files
3+
**
4+
5+
# Re-include Go source files (but not *_test.go)
6+
!**/*.go
7+
**/*_test.go
8+
9+
# Re-include Go module files
10+
!go.mod
11+
!go.sum

.github/workflows/lint.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,4 @@ jobs:
2020
- name: Run linter
2121
uses: golangci/golangci-lint-action@v8
2222
with:
23-
version: v2.1.6
23+
version: v2.4.0

Dockerfile

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,31 @@
1-
# Build
2-
FROM --platform=$BUILDPLATFORM golang:1.24-alpine AS build
3-
WORKDIR /build
1+
# Build the manager binary
2+
FROM golang:1.25 AS builder
3+
ARG TARGETOS
4+
ARG TARGETARCH
45

5-
# Dependency installation
6-
COPY go.mod go.sum ./
7-
RUN --mount=type=cache,target=/go/pkg/mod go mod download
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
813

9-
# Build the app from source
14+
# Copy the Go source (relies on .dockerignore to filter)
1015
COPY . .
11-
ARG TARGETOS TARGETARCH
12-
RUN --mount=type=cache,target=/root/.cache/go-build \
13-
--mount=type=cache,target=/go/pkg \
14-
CGO_ENABLED=0 GOOS=$TARGETOS GOARCH=$TARGETARCH go build -o manager cmd/main.go
1516

16-
# Runtime image
17-
FROM gcr.io/distroless/static:nonroot
17+
# Build
18+
# the GOARCH has no default value to allow the binary to be built according to the host where the command
19+
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
20+
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
21+
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
22+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager cmd/main.go
1823

19-
# Copy only the binary from the build stage to the final image
20-
COPY --from=build /build/manager /
24+
# Use distroless as minimal base image to package the manager binary
25+
# Refer to https://github.com/GoogleContainerTools/distroless for more details
26+
FROM gcr.io/distroless/static:nonroot
27+
WORKDIR /
28+
COPY --from=builder /workspace/manager .
29+
USER 65532:65532
2130

22-
# Set the entry point for the container
2331
ENTRYPOINT ["/manager"]

Makefile

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ setup-test-e2e: ## Set up a Kind cluster for e2e tests if it does not exist
8383

8484
.PHONY: test-e2e
8585
test-e2e: setup-test-e2e manifests generate fmt vet ## Run the e2e tests. Expected an isolated environment using Kind.
86-
KIND_CLUSTER=$(KIND_CLUSTER) go test ./test/e2e/ -v -ginkgo.v
86+
KIND=$(KIND) KIND_CLUSTER=$(KIND_CLUSTER) go test -tags=e2e ./test/e2e/ -v -ginkgo.v
8787
$(MAKE) cleanup-test-e2e
8888

8989
.PHONY: install-cert-manager
@@ -179,11 +179,13 @@ endif
179179

180180
.PHONY: install
181181
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
182-
$(KUSTOMIZE) build config/crd | $(KUBECTL) apply -f -
182+
@out="$$( $(KUSTOMIZE) build config/crd 2>/dev/null || true )"; \
183+
if [ -n "$$out" ]; then echo "$$out" | $(KUBECTL) apply -f -; else echo "No CRDs to install; skipping."; fi
183184

184185
.PHONY: uninstall
185186
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.
186-
$(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
187+
@out="$$( $(KUSTOMIZE) build config/crd 2>/dev/null || true )"; \
188+
if [ -n "$$out" ]; then echo "$$out" | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -; else echo "No CRDs to delete; skipping."; fi
187189

188190
.PHONY: deploy
189191
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
@@ -214,13 +216,13 @@ ENVTEST ?= $(LOCALBIN)/setup-envtest
214216
GOLANGCI_LINT = $(LOCALBIN)/golangci-lint
215217

216218
## Tool Versions
217-
KUSTOMIZE_VERSION ?= v5.6.0
218-
CONTROLLER_TOOLS_VERSION ?= v0.18.0
219+
KUSTOMIZE_VERSION ?= v5.7.1
220+
CONTROLLER_TOOLS_VERSION ?= v0.19.0
219221
#ENVTEST_VERSION is the version of controller-runtime release branch to fetch the envtest setup script (i.e. release-0.20)
220222
ENVTEST_VERSION ?= $(shell go list -m -f "{{ .Version }}" sigs.k8s.io/controller-runtime | awk -F'[v.]' '{printf "release-%d.%d", $$2, $$3}')
221223
#ENVTEST_K8S_VERSION is the version of Kubernetes to use for setting up ENVTEST binaries (i.e. 1.31)
222224
ENVTEST_K8S_VERSION ?= $(shell go list -m -f "{{ .Version }}" k8s.io/api | awk -F'[v.]' '{printf "1.%d", $$3}')
223-
GOLANGCI_LINT_VERSION ?= v2.1.6
225+
GOLANGCI_LINT_VERSION ?= v2.4.0
224226

225227
.PHONY: kustomize
226228
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary.
@@ -255,13 +257,13 @@ $(GOLANGCI_LINT): $(LOCALBIN)
255257
# $2 - package url which can be installed
256258
# $3 - specific version of package
257259
define go-install-tool
258-
@[ -f "$(1)-$(3)" ] || { \
260+
@[ -f "$(1)-$(3)" ] && [ "$$(readlink -- "$(1)" 2>/dev/null)" = "$(1)-$(3)" ] || { \
259261
set -e; \
260262
package=$(2)@$(3) ;\
261263
echo "Downloading $${package}" ;\
262-
rm -f $(1) || true ;\
264+
rm -f $(1) ;\
263265
GOBIN=$(LOCALBIN) go install $${package} ;\
264266
mv $(1) $(1)-$(3) ;\
265267
} ;\
266-
ln -sf $(1)-$(3) $(1)
268+
ln -sf $$(realpath $(1)-$(3)) $(1)
267269
endef

PROJECT

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# This file is used to track the info used to scaffold your project
33
# and allow the plugins properly work.
44
# More info: https://book.kubebuilder.io/reference/project-config.html
5-
cliVersion: 4.7.1
5+
cliVersion: 4.9.0
66
domain: agent.octopus.com
77
layout:
88
- go.kubebuilder.io/v4

api/v1beta1/clusterworkloadserviceaccount_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ type ClusterWorkloadServiceAccountList struct {
9999
Items []ClusterWorkloadServiceAccount `json:"items"`
100100
}
101101

102+
func (cwsa *ClusterWorkloadServiceAccount) GetConditions() *[]metav1.Condition {
103+
return &cwsa.Status.Conditions
104+
}
105+
102106
func init() {
103107
SchemeBuilder.Register(&ClusterWorkloadServiceAccount{}, &ClusterWorkloadServiceAccountList{})
104108
}

api/v1beta1/workloadserviceaccount_types.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ type WorkloadServiceAccountList struct {
113113
Items []WorkloadServiceAccount `json:"items"`
114114
}
115115

116+
func (wsa *WorkloadServiceAccount) GetConditions() *[]metav1.Condition {
117+
return &wsa.Status.Conditions
118+
}
119+
116120
func init() {
117121
SchemeBuilder.Register(&WorkloadServiceAccount{}, &WorkloadServiceAccountList{})
118122
}

cmd/main.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"context"
2021
"crypto/tls"
2122
"flag"
2223
"os"
@@ -50,6 +51,24 @@ import (
5051
// +kubebuilder:scaffold:imports
5152
)
5253

54+
type StartupReconciler struct {
55+
engine *rules.InMemoryEngine
56+
}
57+
58+
func (s *StartupReconciler) Start(ctx context.Context) error {
59+
setupLog.Info("Running initial full reconciliation")
60+
if err := s.engine.Reconcile(ctx); err != nil {
61+
setupLog.Error(err, "failed to run initial reconciliation")
62+
return err
63+
}
64+
setupLog.Info("Initial reconciliation completed successfully")
65+
return nil
66+
}
67+
68+
func (s *StartupReconciler) NeedLeaderElection() bool {
69+
return true
70+
}
71+
5372
var (
5473
scheme = runtime.NewScheme()
5574
setupLog = ctrl.Log.WithName("setup")
@@ -144,7 +163,7 @@ func main() {
144163

145164
// Metrics endpoint is enabled in 'config/default/kustomization.yaml'. The Metrics options configure the server.
146165
// More info:
147-
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.21.0/pkg/metrics/server
166+
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.22.1/pkg/metrics/server
148167
// - https://book.kubebuilder.io/reference/metrics.html
149168
metricsServerOptions := metricsserver.Options{
150169
BindAddress: metricsAddr,
@@ -156,7 +175,7 @@ func main() {
156175
// FilterProvider is used to protect the metrics endpoint with authn/authz.
157176
// These configurations ensure that only authorized users and service accounts
158177
// can access the metrics endpoint. The RBAC are configured in 'config/rbac/kustomization.yaml'. More info:
159-
// https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.21.0/pkg/metrics/filters#WithAuthenticationAndAuthorization
178+
// https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.22.1/pkg/metrics/filters#WithAuthenticationAndAuthorization
160179
metricsServerOptions.FilterProvider = filters.WithAuthenticationAndAuthorization
161180
}
162181

@@ -232,12 +251,16 @@ func main() {
232251

233252
var engine rules.InMemoryEngine
234253
if len(targetNamespaces) > 0 {
235-
engine = rules.NewInMemoryEngineWithNamespaces(mgr.GetClient(), targetNamespaces)
254+
engine = rules.NewInMemoryEngineWithNamespaces(mgr.GetClient(), mgr.GetScheme(), targetNamespaces)
236255
} else {
237-
engine = rules.NewInMemoryEngine(mgr.GetClient(), targetNamespaceRegex)
256+
engine = rules.NewInMemoryEngine(mgr.GetClient(), mgr.GetScheme(), targetNamespaceRegex)
238257
}
239258

240-
// Create the rules engine instance
259+
// Add startup runnable to perform initial full reconciliation
260+
if err := mgr.Add(&StartupReconciler{engine: &engine}); err != nil {
261+
setupLog.Error(err, "unable to add startup reconciler")
262+
os.Exit(1)
263+
}
241264

242265
// Create new prometheus metrics collector instance
243266
octopusMetricsCollector := metrics.NewOctopusMetricsCollector(mgr.GetClient(), &engine)

config/certificates-only/kustomization.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ resources:
55
- ./namespace.yaml
66
- ../webhook
77
- ../certmanager
8+
- ../local-debug
89

910
replacements:
1011
- source: # Uncomment the following block if you have any webhook

config/crd/bases/agent.octopus.com_clusterworkloadserviceaccounts.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ apiVersion: apiextensions.k8s.io/v1
33
kind: CustomResourceDefinition
44
metadata:
55
annotations:
6-
controller-gen.kubebuilder.io/version: v0.18.0
6+
controller-gen.kubebuilder.io/version: v0.19.0
77
name: clusterworkloadserviceaccounts.agent.octopus.com
88
spec:
99
group: agent.octopus.com

0 commit comments

Comments
 (0)