Skip to content

Commit

Permalink
Add support for controller-gen and setup-envtest
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuckal777 authored and SuperSandro2000 committed Feb 12, 2024
1 parent d1a39b4 commit 2924636
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 8 deletions.
8 changes: 7 additions & 1 deletion internal/core/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ type ScanResult struct {
GoVersion string // from "go" directive in go.mod, e.g. "1.21"
GoDirectDependencies []module.Version // from "require" directive(s) in go.mod without the "// indirect" comment
HasBinInfo bool // whether we can produce linker instructions for "github.com/sapcc/go-api-declarations/bininfo"
UsesPostgres bool // wether postgres is used
UsesPostgres bool // whether postgres is used
KubernetesController bool // whether the repository contains a Kubernetes controller
}

const ModFilename = "go.mod"
Expand All @@ -47,6 +48,7 @@ func Scan() ScanResult {
var goDeps []module.Version
hasBinInfo := false
usesPostgres := false
kubernetesController := false

for _, v := range modFile.Require {
if !v.Indirect {
Expand All @@ -60,6 +62,9 @@ func Scan() ScanResult {
if v.Mod.Path == "github.com/lib/pq" {
usesPostgres = true
}
if v.Mod.Path == "sigs.k8s.io/controller-runtime" {
kubernetesController = true
}
}

return ScanResult{
Expand All @@ -68,6 +73,7 @@ func Scan() ScanResult {
GoDirectDependencies: goDeps,
HasBinInfo: hasBinInfo,
UsesPostgres: usesPostgres,
KubernetesController: kubernetesController,
}
}

Expand Down
50 changes: 43 additions & 7 deletions internal/makefile/makefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ endif
build.addDefinition("GO_BUILDFLAGS =%s", cfg.Variable("GO_BUILDFLAGS", defaultBuildFlags))
build.addDefinition("GO_LDFLAGS =%s", cfg.Variable("GO_LDFLAGS", strings.TrimSpace(defaultLdFlags)))
build.addDefinition("GO_TESTENV =%s", cfg.Variable("GO_TESTENV", ""))
if sr.KubernetesController {
build.addDefinition("TESTBIN=$(shell pwd)/testbin")
}
if sr.HasBinInfo {
build.addDefinition("")
build.addDefinition("# These definitions are overridable, e.g. to provide fixed version/commit values when")
Expand Down Expand Up @@ -172,13 +175,37 @@ endif
` go install github.com/google/addlicense@latest; fi`,
}...)
}
if sr.KubernetesController {
prepareStaticRecipe = append(prepareStaticRecipe, []string{
`@if ! hash controller-gen 2>/dev/null; then` +
` printf "\e[1;36m>> Installing controller-gen...\e[0m\n";` +
` go install sigs.k8s.io/controller-tools/cmd/controller-gen@latest; fi`,
`@if ! hash setup-envtest 2>/dev/null; then` +
` printf "\e[1;36m>> Installing setup-envtest...\e[0m\n";` +
` go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest; fi`,
}...)
}
test.addRule(rule{
description: "Install any tools required by static-check. This is used in CI before dropping privileges, you should probably install all the tools using your package manager",
phony: true,
target: "prepare-static-check",
recipe: prepareStaticRecipe,
})

if sr.KubernetesController {
components := strings.Split(sr.ModulePath, "/")
roleName := components[len(components)-1]
test.addRule(rule{
description: "Generate code for Kubernetes CRDs and deepcopy.",
target: "generate",
recipe: []string{
`@printf "\e[1;36m>> controller-gen\e[0m\n"`,
fmt.Sprintf(`@controller-gen crd rbac:roleName=%s paths="./..." output:crd:artifacts:config=crd`, roleName),
`@controller-gen object paths=./...`,
},
})
}

// add target to run golangci-lint
test.addRule(rule{
description: "Install and run golangci-lint. Installing is used in CI, but you should probably install golangci-lint using your package manager.",
Expand All @@ -192,20 +219,25 @@ endif
})

//add targets for `go test` incl. coverage report
test.addRule(rule{
testRule := rule{
description: "Run tests and generate coverage report.",
phony: true,
target: "build/cover.out",
// We use order only prerequisite because this target is used in CI.
orderOnlyPrerequisites: []string{"build"},
recipe: []string{
`@printf "\e[1;36m>> go test\e[0m\n"`,
fmt.Sprintf(
`@env $(GO_TESTENV) go test $(GO_BUILDFLAGS) -ldflags '%s $(GO_LDFLAGS)' -shuffle=on -p 1 -coverprofile=$@ -covermode=count -coverpkg=$(subst $(space),$(comma),$(GO_COVERPKGS)) $(GO_TESTPKGS)`,
makeDefaultLinkerFlags(path.Base(sr.MustModulePath()), sr),
),
},
})
}
goTest := fmt.Sprintf(`go test $(GO_BUILDFLAGS) -ldflags '%s $(GO_LDFLAGS)' -shuffle=on -p 1 -coverprofile=$@ -covermode=count -coverpkg=$(subst $(space),$(comma),$(GO_COVERPKGS)) $(GO_TESTPKGS)`, makeDefaultLinkerFlags(path.Base(sr.MustModulePath()), sr))
if sr.KubernetesController {
testRule.prerequisites = append(testRule.prerequisites, "generate")
testRule.recipe = append(testRule.recipe, `KUBEBUILDER_ASSETS="$(shell setup-envtest use 1.29.1 --bin-dir $(TESTBIN) -p path)" `+goTest)
} else {
testRule.recipe = append(testRule.recipe, `@env $(GO_TESTENV) `+goTest)
}

test.addRule(testRule)

test.addRule(rule{
description: "Generate an HTML file with source code annotations from the coverage report.",
Expand Down Expand Up @@ -261,7 +293,7 @@ endif
})
}

if isSAPCC {
if isSAPCC && cfg.GitHubWorkflow != nil {
// `go list .` does not work to get the package name because it requires a go file in the current directory
// but some packages like concourse-swift-resource or gatekeeper-addons only have subpackages
allGoFilesExpr := `$(patsubst $(shell awk '$$1 == "module" {print $$2}' go.mod)%,.%/*.go,$(shell go list ./...))`
Expand Down Expand Up @@ -369,6 +401,10 @@ func buildTargets(binaries []core.BinaryConfiguration, sr core.ScanResult) []rul
)},
}

if sr.KubernetesController {
r.prerequisites = append(r.prerequisites, "generate")
}

result = append(result, r)
allPrerequisites = append(allPrerequisites, r.target)
}
Expand Down

0 comments on commit 2924636

Please sign in to comment.