Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
version: '2'
linters:
default: none
enable:
- asasalint
- asciicheck
- bidichk
- bodyclose
- canonicalheader
- contextcheck
- copyloopvar
- decorder
- dogsled
- dupword
- durationcheck
- errcheck
- errchkjson
- errname
- exhaustive
- exptostd
- forbidigo
- ginkgolinter
- gocheckcompilerdirectives
- gochecksumtype
- gocritic
- goheader
- goprintffuncname
- gosec
- gosmopolitan
- govet
- grouper
- importas
- ineffassign
- interfacebloat
- intrange
- loggercheck
- makezero
- mirror
- misspell
- modernize
- musttag
- nakedret
- nilerr
- nilnil
- noctx
- nolintlint
- nosprintfhostport
- predeclared
- promlinter
- protogetter
- reassign
- sloglint
- staticcheck
- tagalign
- testableexamples
- unconvert
- unparam
- unused
- usestdlibvars
- usetesting
- wastedassign
formatters:
enable: [gci, gofmt]
settings:
gci:
sections:
- standard # Standard section: captures all standard packages.
- default # Default section: contains all imports that could not be matched to another section type.
- localmodule # Local module section: contains all local packages.
- blank # Blank section: contains all blank imports. This section is not present unless explicitly enabled.
- dot # Dot section: contains all dot imports. This section is not present unless explicitly enabled.
custom-order: true
exclusions:
generated: lax
paths: [third_party, builtin$, examples$]
8 changes: 5 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
FROM --platform=$BUILDPLATFORM golang:1.27.3 AS builder
ARG TARGETOS
ARG TARGETARCH
ARG VERSION=dev

WORKDIR /app

Expand All @@ -11,11 +12,12 @@ RUN go mod download
COPY main.go main.go
COPY controllers/ controllers/

RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o probemgr main.go
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} \
go build -a -ldflags "-s -w -X main.version=${VERSION}" -o probe-controller main.go

FROM gcr.io/distroless/static:nonroot
WORKDIR /
COPY --from=builder /app/probemgr .
COPY --from=builder /app/probe-controller .
USER 65532:65532

ENTRYPOINT ["/probemgr"]
ENTRYPOINT ["/probe-controller"]
68 changes: 58 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,49 @@
.DEFAULT_GOAL=help
SHELL:=/bin/bash
# See: https://clarkgrubb.com/makefile-style-guide
MAKEFLAGS += --warn-undefined-variables --no-builtin-rules
SHELL := /usr/bin/env bash

# The `--norc` option prevents "PS1: unbound" errors.
# If Bash thinks it is being run with its standard input connected to a network
# connection (such as via SSH or via Docker), it reads and executes commands
# from ~/.bashrc, regardless of whether it thinks it is in interactive mode.
# Bash does not set PS1 in non-interactive environments. But on Ubuntu 24.04 the
# default /etc/bash.bashrc file assumes that PS1 is set.
#
# See https://www.gnu.org/software/bash/manual/bash.html#Invoked-by-remote-shell-daemon
.SHELLFLAGS := --norc -euo pipefail -c
.DEFAULT_GOAL := help
.DELETE_ON_ERROR:
.SUFFIXES:

# Set empty value for MAKECMDGOALS to prevent the "warning: undefined variable 'MAKECMDGOALS'"
# warning from happening when running make without arguments
MAKECMDGOALS ?=

##
# Git and versioning information
##

git_version := $(shell git describe --tags --always --match='v*' --abbrev=14 --dirty)
VERSION ?= $(git_version)

##
# Global vars and dirs
##
bin_dir := _bin

$(bin_dir):
@mkdir -p $@

go_srcs := $(shell find . -name '*.go' -not -name '*_test.go' -not -path './$(bin_dir)/*')
binary := $(bin_dir)/probe-controller
ldflags := -s -w -X main.version=$(VERSION)

##@ Linting and static checks

.PHONY: vet
.PHONY: lint vet

lint: ## Run golangci-lint
@golangci-lint run

vet: ## Run go vet
@go vet ./...
Expand All @@ -13,21 +53,29 @@ vet: ## Run go vet
.PHONY: test test-cov-html

test: vet ## Run tests
@go test ./... -cover
@go test -cover ./...

test-cov-html: vet ## Run test coverage
@go test ./... -covermode=count -coverprofile=coverage.out \
@go test -covermode=count -coverprofile=coverage.out ./... \
&& go tool cover -html=coverage.out

##@ Build

.PHONY: docker lint-docker
.PHONY: build bake clean lint-docker

lint-docker:
@hadolint Dockerfile
build: $(binary) ## Build binary locally

$(binary): $(go_srcs) go.mod go.sum | $(bin_dir)
@CGO_ENABLED=0 go build -ldflags "$(ldflags)" -o $@ main.go

bake: lint-docker
@docker buildx bake image-local
bake: lint-docker ## Bake Docker image locally
@VERSION=$(VERSION) docker buildx bake image-local

clean: ## Clean all temporary files
@rm -rfv $(bin_dir) coverage.out

lint-docker: ## Run hadolint
@hadolint Dockerfile

##@ Helpers

Expand Down
31 changes: 22 additions & 9 deletions controllers/httproute_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package controllers
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"strconv"
"time"

ctrl "sigs.k8s.io/controller-runtime"
Expand Down Expand Up @@ -63,9 +64,8 @@ func (r *HTTPRouteWatcher) Reconcile(ctx context.Context, req ctrl.Request) (ctr
return ctrl.Result{}, nil
}

// Check if trigger annotation is enabled
// If annotation was removed, clean up the finalizer
if route.Annotations[triggerAnnotation] != "true" {
// If annotation was removed, clean up the finalizer
if controllerutil.ContainsFinalizer(&route, finalizerName) {
controllerutil.RemoveFinalizer(&route, finalizerName)
return ctrl.Result{}, r.Update(ctx, &route)
Expand All @@ -83,15 +83,18 @@ func (r *HTTPRouteWatcher) Reconcile(ctx context.Context, req ctrl.Request) (ctr
}

// Prevent dupe API calls on status-only updates
// TODO: hash spec fields to account for domain changes
currentGen := strconv.FormatInt(route.Generation, 10)
lastSyncedGen := route.Annotations[lastSyncedAnnotation]
if currentGen == lastSyncedGen {
currentHash, err := computeSpecHash(route.Spec)
if err != nil {
return ctrl.Result{}, err
}

lastSyncedHash := route.Annotations[lastSyncedAnnotation]
if currentHash == lastSyncedHash {
return ctrl.Result{}, nil
}

eventType := "UPDATED"
if lastSyncedGen == "" {
if lastSyncedHash == "" {
eventType = "CREATED"
}

Expand All @@ -110,7 +113,7 @@ func (r *HTTPRouteWatcher) Reconcile(ctx context.Context, req ctrl.Request) (ctr
if route.Annotations == nil {
route.Annotations = make(map[string]string)
}
route.Annotations[lastSyncedAnnotation] = currentGen
route.Annotations[lastSyncedAnnotation] = currentHash
if err := r.Update(ctx, &route); err != nil {
return ctrl.Result{}, err
}
Expand Down Expand Up @@ -153,6 +156,16 @@ func (r *HTTPRouteWatcher) callExternalAPI(ctx context.Context, payload WebhookP
return nil
}

// encode payload and return a 16-char hex digest
func computeSpecHash(spec gatewayv1.HTTPRouteSpec) (string, error) {
data, err := json.Marshal(spec)
if err != nil {
return "", fmt.Errorf("failed to marshal HTTPRoute spec: %w", err)
}
sum := sha256.Sum256(data)
return hex.EncodeToString(sum[:8]), nil
}

func (r *HTTPRouteWatcher) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&gatewayv1.HTTPRoute{}).
Expand Down
Loading
Loading