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
28 changes: 27 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ ENVTEST_ARCH ?= $(LOCALARCH)

# API generation utilities
CONTROLLER_GEN_VERSION ?= v0.21.0
GOPLS_VERSION ?= v0.24.0

# Download source-controller CRDs
SOURCE_VER ?= $(shell go list -m all | grep github.com/fluxcd/source-controller/api | awk '{print $$2}')
Expand All @@ -44,7 +45,7 @@ all: manager

# Run 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 install-envtest download-crd-deps
test: tidy generate fix fmt vet unused manifests install-envtest download-crd-deps
KUBEBUILDER_ASSETS=$(KUBEBUILDER_ASSETS) go test ./... $(GO_TEST_ARGS) -coverprofile cover.out

# Build manager binary
Expand Down Expand Up @@ -115,6 +116,17 @@ 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
.PHONY: fix
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 @@ -123,6 +135,14 @@ fmt:
vet:
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: generate-api generate-json-schemas

Expand Down Expand Up @@ -162,6 +182,12 @@ manifests-release:
kustomize build config/release-tmp > ./build/source-watcher.deployment.yaml
rm -rf config/release-tmp

# Find or download gopls
GOPLS = $(shell pwd)/bin/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 = $(shell pwd)/bin/controller-gen
.PHONY: controller-gen
Expand Down
3 changes: 1 addition & 2 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/utils/ptr"
ctrlruntime "sigs.k8s.io/controller-runtime"
ctrlcache "sigs.k8s.io/controller-runtime/pkg/cache"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -177,7 +176,7 @@ func main() {
Logger: ctrlruntime.Log,
Controller: ctrlcfg.Controller{
MaxConcurrentReconciles: concurrent,
RecoverPanic: ptr.To(true),
RecoverPanic: new(true),
ReconciliationTimeout: reconciliationTimeout,
},
Client: ctrlclient.Options{
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ require (
k8s.io/apiextensions-apiserver 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/yaml v1.6.0
)
Expand Down Expand Up @@ -144,6 +143,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/kustomize/api v0.21.2 // indirect
sigs.k8s.io/kustomize/kyaml v0.21.2 // indirect
Expand Down
7 changes: 3 additions & 4 deletions internal/controller/artifactgenerator_authorization_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

. "github.com/onsi/gomega"
authzv1 "k8s.io/api/authorization/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/client/interceptor"
Expand Down Expand Up @@ -60,7 +59,7 @@ func TestAccessCheckerDeduplicates(t *testing.T) {
}).Build()

obj := &swapi.ArtifactGenerator{
ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "flux-system"},
Name: "test", Namespace: "flux-system",
}
checker := newAccessChecker(kubeClient, obj, "default-sa")

Expand Down Expand Up @@ -94,7 +93,7 @@ func TestAccessCheckerSameNamespaceUsesControllerCredentials(t *testing.T) {
}).Build()

obj := &swapi.ArtifactGenerator{
ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "flux-system"},
Name: "test", Namespace: "flux-system",
}
checker := newAccessChecker(kubeClient, obj, "default-sa")

Expand All @@ -118,7 +117,7 @@ func TestAccessCheckerDenied(t *testing.T) {
}).Build()

obj := &swapi.ArtifactGenerator{
ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "flux-system"},
Name: "test", Namespace: "flux-system",
}
checker := newAccessChecker(kubeClient, obj, "default-sa")

Expand Down
25 changes: 9 additions & 16 deletions internal/controller/artifactgenerator_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,7 @@ func (r *ArtifactGeneratorReconciler) Reconcile(ctx context.Context, req ctrl.Re
// are retryable, as the RBAC bindings may be fixed without changing the
// ArtifactGenerator.
defer func() {
var accessErr *accessDeniedError
if errors.As(retErr, &accessErr) {
if accessErr, ok := errors.AsType[*accessDeniedError](retErr); ok {
gotkconditions.MarkFalse(obj,
gotkmeta.ReadyCondition,
swapi.AccessDeniedReason,
Expand Down Expand Up @@ -354,7 +353,7 @@ func (r *ArtifactGeneratorReconciler) reconcile(ctx context.Context,
// Load and parse the exported inputs. The staging directory is
// namespaced to match the artifact builder.
artifactDir := filepath.Join(tmpDir, namespace, oa.Name)
exportedInputs, err := r.loadExportedInputs(ctx, &oa, localSources, artifactDir)
exportedInputs, err := r.loadExportedInputs(&oa, localSources, artifactDir)
if err != nil {
msg := fmt.Sprintf("%s inputs failed: %s", oa.Name, err.Error())
gotkconditions.MarkFalse(obj,
Expand Down Expand Up @@ -763,16 +762,12 @@ func (r *ArtifactGeneratorReconciler) reconcileExternalArtifact(ctx context.Cont

// Create the ExternalArtifact object.
ea := &sourcev1.ExternalArtifact{
TypeMeta: metav1.TypeMeta{
APIVersion: sourcev1.GroupVersion.String(),
Kind: sourcev1.ExternalArtifactKind,
},
ObjectMeta: metav1.ObjectMeta{
Name: outputArtifact.Name,
Namespace: obj.GetArtifactNamespace(outputArtifact),
Labels: labels,
Annotations: annotations,
},
APIVersion: sourcev1.GroupVersion.String(),
Kind: sourcev1.ExternalArtifactKind,
Name: outputArtifact.Name,
Namespace: obj.GetArtifactNamespace(outputArtifact),
Labels: labels,
Annotations: annotations,
Spec: sourcev1.ExternalArtifactSpec{
SourceRef: &gotkmeta.NamespacedObjectKindReference{
APIVersion: swapi.GroupVersion.String(),
Expand Down Expand Up @@ -824,9 +819,7 @@ func (r *ArtifactGeneratorReconciler) reconcileExternalArtifact(ctx context.Cont
ExportedInputs: exportedInputs,
}
statusOpts := &client.SubResourcePatchOptions{
PatchOptions: client.PatchOptions{
FieldManager: r.ControllerName,
},
FieldManager: r.ControllerName,
}
if err := auth.ensureExternalArtifactVerb(ctx, namespace, "patch", "status"); err != nil {
return nil, nil, err
Expand Down
102 changes: 35 additions & 67 deletions internal/controller/artifactgenerator_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,14 +324,10 @@ func TestArtifactGeneratorReconciler_fetchSources(t *testing.T) {
}

generator := &swapi.ArtifactGenerator{
TypeMeta: metav1.TypeMeta{
Kind: swapi.ArtifactGeneratorKind,
APIVersion: swapi.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: objKey.Name,
Namespace: objKey.Namespace,
},
Kind: swapi.ArtifactGeneratorKind,
APIVersion: swapi.GroupVersion.String(),
Name: objKey.Name,
Namespace: objKey.Namespace,
Spec: swapi.ArtifactGeneratorSpec{
Sources: []swapi.SourceReference{
{
Expand All @@ -350,10 +346,10 @@ func TestArtifactGeneratorReconciler_fetchSources(t *testing.T) {

cleanup := func() {
testClient.Delete(context.Background(), &sourcev1.GitRepository{
ObjectMeta: metav1.ObjectMeta{Name: gitKey.Name, Namespace: gitKey.Namespace},
Name: gitKey.Name, Namespace: gitKey.Namespace,
})
testClient.Delete(context.Background(), &sourcev1.OCIRepository{
ObjectMeta: metav1.ObjectMeta{Name: ociKey.Name, Namespace: ociKey.Namespace},
Name: ociKey.Name, Namespace: ociKey.Namespace,
})
}

Expand All @@ -370,14 +366,10 @@ func TestArtifactGeneratorReconciler_fetchSources(t *testing.T) {
objKey := client.ObjectKey{Name: "test-generator", Namespace: "default"}

generator := &swapi.ArtifactGenerator{
TypeMeta: metav1.TypeMeta{
Kind: swapi.ArtifactGeneratorKind,
APIVersion: swapi.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: objKey.Name,
Namespace: objKey.Namespace,
},
Kind: swapi.ArtifactGeneratorKind,
APIVersion: swapi.GroupVersion.String(),
Name: objKey.Name,
Namespace: objKey.Namespace,
Spec: swapi.ArtifactGeneratorSpec{
Sources: []swapi.SourceReference{
{
Expand Down Expand Up @@ -413,14 +405,10 @@ func TestArtifactGeneratorReconciler_fetchSources(t *testing.T) {
}

generator := &swapi.ArtifactGenerator{
TypeMeta: metav1.TypeMeta{
Kind: swapi.ArtifactGeneratorKind,
APIVersion: swapi.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: objKey.Name,
Namespace: objKey.Namespace,
},
Kind: swapi.ArtifactGeneratorKind,
APIVersion: swapi.GroupVersion.String(),
Name: objKey.Name,
Namespace: objKey.Namespace,
Spec: swapi.ArtifactGeneratorSpec{
Sources: []swapi.SourceReference{
{
Expand All @@ -434,7 +422,7 @@ func TestArtifactGeneratorReconciler_fetchSources(t *testing.T) {

cleanup := func() {
testClient.Delete(context.Background(), &sourcev1.GitRepository{
ObjectMeta: metav1.ObjectMeta{Name: gitKey.Name, Namespace: gitKey.Namespace},
Name: gitKey.Name, Namespace: gitKey.Namespace,
})
}

Expand All @@ -458,14 +446,10 @@ func TestArtifactGeneratorReconciler_fetchSources(t *testing.T) {
}

generator := &swapi.ArtifactGenerator{
TypeMeta: metav1.TypeMeta{
Kind: swapi.ArtifactGeneratorKind,
APIVersion: swapi.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: objKey.Name,
Namespace: objKey.Namespace,
},
Kind: swapi.ArtifactGeneratorKind,
APIVersion: swapi.GroupVersion.String(),
Name: objKey.Name,
Namespace: objKey.Namespace,
Spec: swapi.ArtifactGeneratorSpec{
Sources: []swapi.SourceReference{
{
Expand All @@ -480,7 +464,7 @@ func TestArtifactGeneratorReconciler_fetchSources(t *testing.T) {

cleanup := func() {
testClient.Delete(context.Background(), &sourcev1.GitRepository{
ObjectMeta: metav1.ObjectMeta{Name: gitKey.Name, Namespace: gitKey.Namespace},
Name: gitKey.Name, Namespace: gitKey.Namespace,
})
}

Expand Down Expand Up @@ -713,14 +697,10 @@ func getArtifactGeneratorReconciler() *ArtifactGeneratorReconciler {

func getArtifactGenerator(objectKey client.ObjectKey) *swapi.ArtifactGenerator {
return &swapi.ArtifactGenerator{
TypeMeta: metav1.TypeMeta{
Kind: swapi.ArtifactGeneratorKind,
APIVersion: swapi.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: objectKey.Name,
Namespace: objectKey.Namespace,
},
Kind: swapi.ArtifactGeneratorKind,
APIVersion: swapi.GroupVersion.String(),
Name: objectKey.Name,
Namespace: objectKey.Namespace,
Spec: swapi.ArtifactGeneratorSpec{
Sources: []swapi.SourceReference{
{
Expand Down Expand Up @@ -766,14 +746,10 @@ func applyGitRepository(objKey client.ObjectKey, revision string, files []gotkte
}

repo := &sourcev1.GitRepository{
TypeMeta: metav1.TypeMeta{
Kind: sourcev1.GitRepositoryKind,
APIVersion: sourcev1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: objKey.Name,
Namespace: objKey.Namespace,
},
Kind: sourcev1.GitRepositoryKind,
APIVersion: sourcev1.GroupVersion.String(),
Name: objKey.Name,
Namespace: objKey.Namespace,
Spec: sourcev1.GitRepositorySpec{
URL: "https://github.com/test/repository",
Interval: metav1.Duration{Duration: time.Minute},
Expand Down Expand Up @@ -816,9 +792,7 @@ func applyGitRepository(objKey client.ObjectKey, revision string, files []gotkte
repo.Status = status

statusOpts := &client.SubResourcePatchOptions{
PatchOptions: client.PatchOptions{
FieldManager: "source-controller",
},
FieldManager: "source-controller",
}

return testClient.Status().Patch(context.Background(), repo, client.Apply, statusOpts)
Expand All @@ -831,14 +805,10 @@ func applyOCIRepository(objKey client.ObjectKey, revision string, files []gotkte
}

repo := &sourcev1.OCIRepository{
TypeMeta: metav1.TypeMeta{
Kind: sourcev1.OCIRepositoryKind,
APIVersion: sourcev1.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: objKey.Name,
Namespace: objKey.Namespace,
},
Kind: sourcev1.OCIRepositoryKind,
APIVersion: sourcev1.GroupVersion.String(),
Name: objKey.Name,
Namespace: objKey.Namespace,
Spec: sourcev1.OCIRepositorySpec{
URL: "oci://ghcr.io/test/repository",
Interval: &metav1.Duration{Duration: time.Minute},
Expand Down Expand Up @@ -883,9 +853,7 @@ func applyOCIRepository(objKey client.ObjectKey, revision string, files []gotkte
repo.Status = status

statusOpts := &client.SubResourcePatchOptions{
PatchOptions: client.PatchOptions{
FieldManager: "source-controller",
},
FieldManager: "source-controller",
}

return testClient.Status().Patch(context.Background(), repo, client.Apply, statusOpts)
Expand Down Expand Up @@ -1102,7 +1070,7 @@ func TestAccessCheckerServiceAccountSelection(t *testing.T) {
g := NewWithT(t)

obj := &swapi.ArtifactGenerator{
ObjectMeta: metav1.ObjectMeta{Name: "test", Namespace: "flux-system"},
Name: "test", Namespace: "flux-system",
}
checker := newAccessChecker(nil, obj, "default-sa")

Expand Down
Loading
Loading