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
29 changes: 27 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ SOURCE_CRD_VER=$(BUILD_DIR)/.src-crd-$(SOURCE_VER)
# API (doc) generation utilities
CONTROLLER_GEN_VERSION ?= v0.21.0
GEN_API_REF_DOCS_VERSION ?= e327d0730470cbd61b06300f81c5fcf91c23c113
GOPLS_VERSION ?= v0.24.0

all: manager

Expand All @@ -65,7 +66,7 @@ $(SOPS): ## Download latest sops binary if none is found.

# Run controller tests
KUBEBUILDER_ASSETS?="$(shell $(ENVTEST) --arch=$(ENVTEST_ARCH) use -i $(ENVTEST_KUBERNETES_VERSION) --bin-dir=$(ENVTEST_ASSETS_DIR) -p path)"
test: tidy generate fmt vet manifests api-docs download-crd-deps install-envtest $(SOPS)
test: fix tidy generate fmt vet unused manifests api-docs download-crd-deps install-envtest $(SOPS)
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) go test ./... $(GO_TEST_ARGS) -v -coverprofile cover.out

# Build manager binary
Expand Down Expand Up @@ -144,6 +145,16 @@ tidy:
cd api; rm -f go.sum; go mod tidy -compat=1.27
rm -f go.sum; go mod tidy -compat=1.27

# Run go fix against code until it makes no changes
fix:
@set -e; while :; do \
before=$$(git diff --no-ext-diff --no-color HEAD -- '*.go' | cksum); \
go fix ./...; \
( cd api && go fix ./... ); \
after=$$(git diff --no-ext-diff --no-color HEAD -- '*.go' | cksum); \
if [ "$$before" = "$$after" ]; then break; fi; \
done

# Run go fmt against code
fmt:
go fmt ./...
Expand All @@ -154,6 +165,14 @@ vet:
go vet ./...
cd api; go vet ./...

# Check for unused code and parameters
unused: gopls
@out=$$($(GOPLS) check -severity=info $$(git ls-files '*.go') | grep -E 'unused parameter:| is unused$$' || true); \
if [ -n "$$out" ]; then \
printf '%s\n' "$$out"; \
exit 1; \
fi

# Generate code
generate: controller-gen
cd api; $(CONTROLLER_GEN) object:headerFile="../hack/boilerplate.go.txt" paths="./..."
Expand Down Expand Up @@ -181,6 +200,12 @@ docker-push:
docker-deploy:
kubectl -n flux-system set image deployment/kustomize-controller manager=${IMG}

# Find or download gopls
GOPLS = $(GOBIN)/gopls
.PHONY: gopls
gopls: ## Download gopls locally if necessary.
$(call go-install-tool,$(GOPLS),golang.org/x/tools/gopls@$(GOPLS_VERSION))

# Find or download controller-gen
CONTROLLER_GEN = $(GOBIN)/controller-gen
.PHONY: controller-gen
Expand Down Expand Up @@ -234,7 +259,7 @@ fuzz-smoketest: fuzz-build
bash -c "/runner.sh"

# Run fuzz tests for the duration set in FUZZ_TIME.
fuzz-native:
fuzz-native:
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) \
FUZZ_TIME=$(FUZZ_TIME) \
./tests/fuzz/native_go_run.sh
8 changes: 0 additions & 8 deletions api/v1beta1/kustomization_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,3 @@ type KustomizationList struct {
metav1.ListMeta `json:"metadata,omitempty"`
Items []Kustomization `json:"items"`
}

func trimString(str string, limit int) string {
if len(str) <= limit {
return str
}

return str[0:limit] + "..."
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,10 @@ require (
github.com/ory/dockertest/v3 v3.12.0
github.com/spf13/pflag v1.0.10
github.com/spiffe/go-spiffe/v2 v2.9.0
golang.org/x/net v0.61.0
golang.org/x/oauth2 v0.37.0
k8s.io/api v0.37.1
k8s.io/apimachinery v0.37.1
k8s.io/client-go v0.37.1
k8s.io/utils v0.0.0-20260707023825-cf1189d6abe3
sigs.k8s.io/controller-runtime v0.25.2
sigs.k8s.io/kustomize/api v0.21.2
sigs.k8s.io/kustomize/kyaml v0.21.2
Expand Down Expand Up @@ -254,6 +252,7 @@ require (
go.yaml.in/yaml/v3 v3.0.5 // indirect
golang.org/x/crypto v0.58.0 // indirect
golang.org/x/exp v0.0.0-20260824195058-e88cd73687aa // indirect
golang.org/x/net v0.61.0 // indirect
golang.org/x/sync v0.24.0 // indirect
golang.org/x/sys v0.49.0 // indirect
golang.org/x/term v0.47.0 // indirect
Expand All @@ -275,6 +274,7 @@ require (
k8s.io/klog/v2 v2.140.0 // indirect
k8s.io/kube-openapi v0.0.0-20260821135717-be32def86098 // indirect
k8s.io/kubectl v0.37.1 // indirect
k8s.io/utils v0.0.0-20260707023825-cf1189d6abe3 // indirect
sigs.k8s.io/json v0.0.0-20250730193827-2d320260d730 // indirect
sigs.k8s.io/randfill v1.0.0 // indirect
sigs.k8s.io/structured-merge-diff/v6 v6.4.2 // indirect
Expand Down
6 changes: 2 additions & 4 deletions internal/controller/kustomization_acl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,8 @@ stringData:
Namespace: id,
}
kustomization := &kustomizev1.Kustomization{
ObjectMeta: metav1.ObjectMeta{
Name: kustomizationKey.Name,
Namespace: kustomizationKey.Namespace,
},
Name: kustomizationKey.Name,
Namespace: kustomizationKey.Namespace,
Spec: kustomizev1.KustomizationSpec{
Interval: metav1.Duration{Duration: reconciliationInterval},
Path: "./",
Expand Down
6 changes: 2 additions & 4 deletions internal/controller/kustomization_commit_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ data:
// newKustomization creates a Kustomization referencing the test artifact.
newKustomization := func() *kustomizev1.Kustomization {
return &kustomizev1.Kustomization{
ObjectMeta: metav1.ObjectMeta{
Name: "commit-status-" + randStringRunes(5),
Namespace: id,
},
Name: "commit-status-" + randStringRunes(5),
Namespace: id,
Spec: kustomizev1.KustomizationSpec{
Interval: metav1.Duration{Duration: time.Hour},
Path: "./",
Expand Down
24 changes: 8 additions & 16 deletions internal/controller/kustomization_configuration_error_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,8 @@ data: {}
Namespace: id,
}
kustomization := &kustomizev1.Kustomization{
ObjectMeta: metav1.ObjectMeta{
Name: kustomizationKey.Name,
Namespace: kustomizationKey.Namespace,
},
Name: kustomizationKey.Name,
Namespace: kustomizationKey.Namespace,
Spec: kustomizev1.KustomizationSpec{
TargetNamespace: id,
Interval: metav1.Duration{Duration: 2 * time.Minute},
Expand All @@ -100,10 +98,8 @@ data: {}
HealthCheckExprs: []kustomize.CustomHealthCheck{{
APIVersion: "v1",
Kind: "ConfigMap",
HealthCheckExpressions: kustomize.HealthCheckExpressions{
InProgress: "foo.",
Current: "true",
},
InProgress: "foo.",
Current: "true",
}},
},
}
Expand Down Expand Up @@ -134,10 +130,8 @@ data: {}
Namespace: id,
}
kustomization := &kustomizev1.Kustomization{
ObjectMeta: metav1.ObjectMeta{
Name: kustomizationKey.Name,
Namespace: kustomizationKey.Namespace,
},
Name: kustomizationKey.Name,
Namespace: kustomizationKey.Namespace,
Spec: kustomizev1.KustomizationSpec{
TargetNamespace: id,
Interval: metav1.Duration{Duration: 2 * time.Minute},
Expand All @@ -155,10 +149,8 @@ data: {}
}

sa := &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
Namespace: id,
},
Name: "foo",
Namespace: id,
}
g.Expect(k8sClient.Create(context.Background(), sa)).To(Succeed())
t.Cleanup(func() { _ = k8sClient.Delete(context.Background(), sa) })
Expand Down
5 changes: 2 additions & 3 deletions internal/controller/kustomization_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"crypto/tls"
"errors"
"fmt"
"maps"
"os"
"strings"
"time"
Expand Down Expand Up @@ -455,9 +456,7 @@ func (r *KustomizationReconciler) reconcile(
// be redacted from any apply error surfaced below.
substituteVars := make(map[string]string)
buildCtx := generator.ContextWithSecretVarsCollector(ctx, func(vars map[string]string) {
for k, v := range vars {
substituteVars[k] = v
}
maps.Copy(substituteVars, vars)
})

// Build the Kustomize overlay and decrypt secrets if needed.
Expand Down
4 changes: 2 additions & 2 deletions internal/controller/kustomization_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestKustomizationReconciler_StagedApply(t *testing.T) {

namespaceName := "kust-" + randStringRunes(5)
namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: namespaceName},
Name: namespaceName,
}
g.Expect(k8sClient.Create(ctx, namespace)).ToNot(HaveOccurred())
t.Cleanup(func() {
Expand Down Expand Up @@ -104,7 +104,7 @@ func TestKustomizationReconciler_deleteBeforeFinalizer(t *testing.T) {

namespaceName := "kust-" + randStringRunes(5)
namespace := &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{Name: namespaceName},
Name: namespaceName,
}
g.Expect(k8sClient.Create(ctx, namespace)).ToNot(HaveOccurred())
t.Cleanup(func() {
Expand Down
44 changes: 15 additions & 29 deletions internal/controller/kustomization_decryptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestKustomizationReconciler_Decryptor(t *testing.T) {
g.Expect(err).NotTo(HaveOccurred(), "failed to create vault client")

// create a master key on the vault transit engine
path, data := "sops/keys/vault", map[string]interface{}{"type": "rsa-4096"}
path, data := "sops/keys/vault", map[string]any{"type": "rsa-4096"}
_, err = cli.Logical().Write(path, data)
g.Expect(err).NotTo(HaveOccurred(), "failed to write key")

Expand Down Expand Up @@ -92,10 +92,8 @@ func TestKustomizationReconciler_Decryptor(t *testing.T) {
Namespace: id,
}
sopsSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: sopsSecretKey.Name,
Namespace: sopsSecretKey.Namespace,
},
Name: sopsSecretKey.Name,
Namespace: sopsSecretKey.Namespace,
StringData: map[string]string{
"pgp.asc": string(pgpKey),
"age.agekey": string(ageKey),
Expand All @@ -111,10 +109,8 @@ func TestKustomizationReconciler_Decryptor(t *testing.T) {
Namespace: id,
}
kustomization := &kustomizev1.Kustomization{
ObjectMeta: metav1.ObjectMeta{
Name: kustomizationKey.Name,
Namespace: kustomizationKey.Namespace,
},
Name: kustomizationKey.Name,
Namespace: kustomizationKey.Namespace,
Spec: kustomizev1.KustomizationSpec{
Interval: metav1.Duration{Duration: 2 * time.Minute},
Path: "./",
Expand Down Expand Up @@ -216,10 +212,8 @@ func TestKustomizationReconciler_Decryptor(t *testing.T) {
g.Expect(err).NotTo(HaveOccurred())

globalSOPSSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: sopsAgeSecret,
Namespace: namespace,
},
Name: sopsAgeSecret,
Namespace: namespace,
StringData: map[string]string{
"identity.agekey": string(ageKey),
},
Expand All @@ -244,10 +238,8 @@ func TestKustomizationReconciler_Decryptor(t *testing.T) {
Namespace: namespace,
}
kustomization := &kustomizev1.Kustomization{
ObjectMeta: metav1.ObjectMeta{
Name: kustomizationKey.Name,
Namespace: kustomizationKey.Namespace,
},
Name: kustomizationKey.Name,
Namespace: kustomizationKey.Namespace,
Spec: kustomizev1.KustomizationSpec{
Interval: metav1.Duration{Duration: 2 * time.Minute},
Path: "./",
Expand Down Expand Up @@ -290,10 +282,8 @@ func TestKustomizationReconciler_Decryptor(t *testing.T) {
g.Expect(err).NotTo(HaveOccurred())

globalSOPSSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: sopsAgeSecret,
Namespace: namespace,
},
Name: sopsAgeSecret,
Namespace: namespace,
StringData: map[string]string{
"identity.agekey": string(ageGlobalKey),
},
Expand All @@ -305,10 +295,8 @@ func TestKustomizationReconciler_Decryptor(t *testing.T) {
Namespace: namespace,
}
localSOPSSecret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: localSOPSSecretKey.Name,
Namespace: localSOPSSecretKey.Namespace,
},
Name: localSOPSSecretKey.Name,
Namespace: localSOPSSecretKey.Namespace,
StringData: map[string]string{
"pgp.asc": string(pgpKey),
"age.agekey": string(ageKey),
Expand Down Expand Up @@ -336,10 +324,8 @@ func TestKustomizationReconciler_Decryptor(t *testing.T) {
Namespace: namespace,
}
kustomization := &kustomizev1.Kustomization{
ObjectMeta: metav1.ObjectMeta{
Name: kustomizationKey.Name,
Namespace: kustomizationKey.Namespace,
},
Name: kustomizationKey.Name,
Namespace: kustomizationKey.Namespace,
Spec: kustomizev1.KustomizationSpec{
Interval: metav1.Duration{Duration: 2 * time.Minute},
Path: "./",
Expand Down
6 changes: 2 additions & 4 deletions internal/controller/kustomization_deletion_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,8 @@ data:
Namespace: id,
}
kustomization := &kustomizev1.Kustomization{
ObjectMeta: metav1.ObjectMeta{
Name: kustomizationKey.Name,
Namespace: kustomizationKey.Namespace,
},
Name: kustomizationKey.Name,
Namespace: kustomizationKey.Namespace,
Spec: kustomizev1.KustomizationSpec{
Interval: metav1.Duration{Duration: reconciliationInterval},
Path: "./",
Expand Down
22 changes: 8 additions & 14 deletions internal/controller/kustomization_dependson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,8 @@ spec:
Namespace: id,
}
kustomization := &kustomizev1.Kustomization{
ObjectMeta: metav1.ObjectMeta{
Name: kustomizationKey.Name,
Namespace: kustomizationKey.Namespace,
},
Name: kustomizationKey.Name,
Namespace: kustomizationKey.Namespace,
Spec: kustomizev1.KustomizationSpec{
Interval: metav1.Duration{Duration: time.Hour},
Path: "./",
Expand Down Expand Up @@ -227,10 +225,8 @@ data:
Namespace: id,
}
kustomization := &kustomizev1.Kustomization{
ObjectMeta: metav1.ObjectMeta{
Name: kustomizationKey.Name,
Namespace: kustomizationKey.Namespace,
},
Name: kustomizationKey.Name,
Namespace: kustomizationKey.Namespace,
Spec: kustomizev1.KustomizationSpec{
Interval: metav1.Duration{Duration: time.Hour},
Path: "./",
Expand Down Expand Up @@ -263,12 +259,10 @@ data:

// Create a dependency Kustomization with matching annotations
dependency := &kustomizev1.Kustomization{
ObjectMeta: metav1.ObjectMeta{
Name: depID,
Namespace: id,
Annotations: map[string]string{
"app/version": "v1.2.3",
},
Name: depID,
Namespace: id,
Annotations: map[string]string{
"app/version": "v1.2.3",
},
Spec: kustomizev1.KustomizationSpec{
Suspend: true, // Suspended dependency should work with readyExpr and AdditiveCELDependencyCheck disabled
Expand Down
Loading
Loading