From 56fd8ea523ac414e7c21710e7bfee7f66b12de55 Mon Sep 17 00:00:00 2001 From: Mateusz Szostok Date: Thu, 27 Jul 2023 12:12:13 +0200 Subject: [PATCH] Add tpl approach for plugin Goreleaser config --- .github/workflows/pr-build.yaml | 13 ++ .goreleaser.plugin.tpl.yaml | 29 +++++ .goreleaser.plugin.yaml | 187 +++++++++++++++++++---------- Makefile | 10 +- hack/gen-plugin-index.go | 16 +-- hack/target/gen-goreleaser/main.go | 63 ++++++++++ test/helpers/plugin_server.go | 13 +- 7 files changed, 242 insertions(+), 89 deletions(-) create mode 100644 .goreleaser.plugin.tpl.yaml create mode 100644 hack/target/gen-goreleaser/main.go diff --git a/.github/workflows/pr-build.yaml b/.github/workflows/pr-build.yaml index c82f733d8..acf763604 100644 --- a/.github/workflows/pr-build.yaml +++ b/.github/workflows/pr-build.yaml @@ -157,6 +157,19 @@ jobs: echo '✔ No issues detected. Have a nice day :-)' fi + - name: Run generating Plugin GoReleaser file + run: make gen-plugins-goreleaser + + - name: Detect git changes + run: | + if [[ $(git diff --stat) != '' ]]; then + echo -e '❌ \033[0;31mGenerated GoReleaser file is outdated. Run 'make gen-plugins-goreleaser'.\033[0m' + git diff --color + exit 1 + else + echo '✔ No issues detected. Have a nice day :-)' + fi + integration-tests: name: Integration tests runs-on: ubuntu-latest diff --git a/.goreleaser.plugin.tpl.yaml b/.goreleaser.plugin.tpl.yaml new file mode 100644 index 000000000..34e6e7714 --- /dev/null +++ b/.goreleaser.plugin.tpl.yaml @@ -0,0 +1,29 @@ +# GoReleaser already creates Botkube artifacts in the ./dist folder. +# To not override them during release, we use a different folder +dist: plugin-dist + +before: + hooks: + - go mod download + +builds: + + - id: <.Name> + main: cmd/<.Type>/<.Name>/main.go + binary: <.Type>_<.Name>_{{ .Os }}_{{ .Arch }} + + no_unique_dist_dir: true + env: + - CGO_ENABLED=0 + goos: + - linux + - darwin + goarch: + - amd64 + - arm64 + goarm: + - 7 + + +snapshot: + name_template: 'v{{ .Version }}' diff --git a/.goreleaser.plugin.yaml b/.goreleaser.plugin.yaml index 30691527e..b6059342e 100644 --- a/.goreleaser.plugin.yaml +++ b/.goreleaser.plugin.yaml @@ -1,3 +1,5 @@ +# The code has been automatically generated and should not be modified directly. To update, run 'make gen-plugins-goreleaser' from the root directory of this repository. + # GoReleaser already creates Botkube artifacts in the ./dist folder. # To not override them during release, we use a different folder dist: plugin-dist @@ -7,112 +9,167 @@ before: - go mod download builds: + + - id: doctor + main: cmd/executor/doctor/main.go + binary: executor_doctor_{{ .Os }}_{{ .Arch }} + + no_unique_dist_dir: true + env: + - CGO_ENABLED=0 + goos: + - linux + - darwin + goarch: + - amd64 + - arm64 + goarm: + - 7 + - id: echo main: cmd/executor/echo/main.go binary: executor_echo_{{ .Os }}_{{ .Arch }} no_unique_dist_dir: true - env: &env + env: - CGO_ENABLED=0 - goos: &goos + goos: - linux - darwin - goarch: &goarch + goarch: - amd64 - arm64 - goarm: &goarm + goarm: - 7 - - - id: helm - main: cmd/executor/helm/main.go - binary: executor_helm_{{ .Os }}_{{ .Arch }} - - no_unique_dist_dir: true - env: *env - goos: *goos - goarch: *goarch - goarm: *goarm - + - id: exec main: cmd/executor/exec/main.go binary: executor_exec_{{ .Os }}_{{ .Arch }} no_unique_dist_dir: true - env: *env - goos: *goos - goarch: *goarch - goarm: *goarm - + env: + - CGO_ENABLED=0 + goos: + - linux + - darwin + goarch: + - amd64 + - arm64 + goarm: + - 7 + - id: gh main: cmd/executor/gh/main.go binary: executor_gh_{{ .Os }}_{{ .Arch }} no_unique_dist_dir: true - env: *env - goos: *goos - goarch: *goarch - goarm: *goarm - + env: + - CGO_ENABLED=0 + goos: + - linux + - darwin + goarch: + - amd64 + - arm64 + goarm: + - 7 + + - id: helm + main: cmd/executor/helm/main.go + binary: executor_helm_{{ .Os }}_{{ .Arch }} + no_unique_dist_dir: true + env: + - CGO_ENABLED=0 + goos: + - linux + - darwin + goarch: + - amd64 + - arm64 + goarm: + - 7 + - id: kubectl main: cmd/executor/kubectl/main.go binary: executor_kubectl_{{ .Os }}_{{ .Arch }} no_unique_dist_dir: true - env: *env - goos: *goos - goarch: *goarch - goarm: *goarm - - - id: doctor - main: cmd/executor/doctor/main.go - binary: executor_doctor_{{ .Os }}_{{ .Arch }} - - no_unique_dist_dir: true - env: *env - goos: *goos - goarch: *goarch - goarm: *goarm - + env: + - CGO_ENABLED=0 + goos: + - linux + - darwin + goarch: + - amd64 + - arm64 + goarm: + - 7 + - id: cm-watcher main: cmd/source/cm-watcher/main.go binary: source_cm-watcher_{{ .Os }}_{{ .Arch }} no_unique_dist_dir: true - env: *env - goos: *goos - goarch: *goarch - goarm: *goarm - - - id: prometheus - main: cmd/source/prometheus/main.go - binary: source_prometheus_{{ .Os }}_{{ .Arch }} + env: + - CGO_ENABLED=0 + goos: + - linux + - darwin + goarch: + - amd64 + - arm64 + goarm: + - 7 + + - id: keptn + main: cmd/source/keptn/main.go + binary: source_keptn_{{ .Os }}_{{ .Arch }} no_unique_dist_dir: true - env: *env - goos: *goos - goarch: *goarch - goarm: *goarm - + env: + - CGO_ENABLED=0 + goos: + - linux + - darwin + goarch: + - amd64 + - arm64 + goarm: + - 7 + - id: kubernetes main: cmd/source/kubernetes/main.go binary: source_kubernetes_{{ .Os }}_{{ .Arch }} no_unique_dist_dir: true - env: *env - goos: *goos - goarch: *goarch - goarm: *goarm - - - id: keptn - main: cmd/source/keptn/main.go - binary: source_keptn_{{ .Os }}_{{ .Arch }} + env: + - CGO_ENABLED=0 + goos: + - linux + - darwin + goarch: + - amd64 + - arm64 + goarm: + - 7 + + - id: prometheus + main: cmd/source/prometheus/main.go + binary: source_prometheus_{{ .Os }}_{{ .Arch }} no_unique_dist_dir: true - env: *env - goos: *goos - goarch: *goarch - goarm: *goarm + env: + - CGO_ENABLED=0 + goos: + - linux + - darwin + goarch: + - amd64 + - arm64 + goarm: + - 7 + snapshot: name_template: 'v{{ .Version }}' diff --git a/Makefile b/Makefile index 4d5464ba8..fbee91daa 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ .DEFAULT_GOAL := build -.PHONY: container-image test test-integration-slack test-integration-discord build pre-build publish lint lint-fix go-import-fmt system-check save-images load-and-push-images gen-grpc-resources gen-plugins-index build-plugins build-plugins-single +.PHONY: container-image test test-integration-slack test-integration-discord build pre-build publish lint lint-fix go-import-fmt system-check save-images load-and-push-images gen-grpc-resources gen-plugins-index build-plugins build-plugins-single gen-docs-cli gen-plugins-goreleaser # Show this help. help: @@ -32,13 +32,13 @@ build: pre-build @echo "Build completed successfully" # Build Botkube official plugins for all supported platforms. -build-plugins: pre-build +build-plugins: pre-build gen-plugins-goreleaser @echo "Building plugins binaries" @./hack/goreleaser.sh build_plugins @echo "Build completed successfully" # Build Botkube official plugins only for current GOOS and GOARCH. -build-plugins-single: pre-build +build-plugins-single: pre-build gen-plugins-goreleaser @echo "Building single target plugins binaries" @./hack/goreleaser.sh build_plugins_single @echo "Build completed successfully" @@ -94,7 +94,9 @@ gen-plugins-index: build-plugins gen-docs-cli: rm -f ./cmd/cli/docs/* go run -ldflags="-X go.szostok.io/version.name=botkube" cmd/cli/main.go gen-usage-docs -.PHONY: gen-docs-cli + +gen-plugins-goreleaser: + go run ./hack/target/gen-goreleaser/main.go # Pre-build checks pre-build: system-check diff --git a/hack/gen-plugin-index.go b/hack/gen-plugin-index.go index b6ea589b9..88bf81b59 100644 --- a/hack/gen-plugin-index.go +++ b/hack/gen-plugin-index.go @@ -2,13 +2,13 @@ package main import ( "flag" - "log" "os" "path/filepath" "github.com/sirupsen/logrus" "gopkg.in/yaml.v3" + "github.com/kubeshop/botkube/internal/loggerx" "github.com/kubeshop/botkube/internal/plugin" ) @@ -28,7 +28,7 @@ func main() { idxBuilder := plugin.NewIndexBuilder(logger) absBinsDir, err := filepath.Abs(*binsDir) - exitOnError("while resolving an absolute path of binaries folder", err) + loggerx.ExitOnError(err, "while resolving an absolute path of binaries folder") log := logger.WithFields(logrus.Fields{ "binDir": absBinsDir, @@ -38,18 +38,12 @@ func main() { log.Info("Building index..") idx, err := idxBuilder.Build(absBinsDir, *urlBasePath, *pluginNameFilter, false) - exitOnError("while building plugin index", err) + loggerx.ExitOnError(err, "while building plugin index") raw, err := yaml.Marshal(idx) - exitOnError("while marshaling index into YAML format", err) + loggerx.ExitOnError(err, "while marshaling index into YAML format") logger.WithField("output", *output).Info("Saving index file...") err = os.WriteFile(*output, raw, filePerm) - exitOnError("while saving index file", err) -} - -func exitOnError(context string, err error) { - if err != nil { - log.Fatalf("%s: %s", context, err) - } + loggerx.ExitOnError(err, "while saving index file") } diff --git a/hack/target/gen-goreleaser/main.go b/hack/target/gen-goreleaser/main.go new file mode 100644 index 000000000..95bd4af0f --- /dev/null +++ b/hack/target/gen-goreleaser/main.go @@ -0,0 +1,63 @@ +package main + +import ( + "fmt" + "os" + "text/template" + + "github.com/kubeshop/botkube/internal/loggerx" +) + +const ( + templateFile = "./.goreleaser.plugin.tpl.yaml" + outputFile = "./.goreleaser.plugin.yaml" + entrypoint = "./cmd" + filePerm = 0o644 + + fileNoEditHeader = "# The code has been automatically generated and should not be modified directly. To update, run 'make gen-plugins-goreleaser' from the root directory of this repository." +) + +type ( + Plugins []Plugin + Plugin struct { + Name string + Type string + } +) + +func main() { + executors, err := os.ReadDir(entrypoint + "/executor") + loggerx.ExitOnError(err, "collecting executors") + sources, err := os.ReadDir(entrypoint + "/source") + loggerx.ExitOnError(err, "collecting sources") + + var plugins Plugins + for _, d := range executors { + plugins = append(plugins, Plugin{ + Type: "executor", + Name: d.Name(), + }) + } + for _, d := range sources { + plugins = append(plugins, Plugin{ + Type: "source", + Name: d.Name(), + }) + } + + file, err := os.ReadFile(templateFile) + loggerx.ExitOnError(err, "reading tpl file") + + // Change delims to not interfere with the GoReleaser templates. + tpl, err := template.New("goreleaser").Delims("<", ">").Parse(string(file)) + loggerx.ExitOnError(err, "creating tpl processor") + + dst, err := os.OpenFile(outputFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, filePerm) + loggerx.ExitOnError(err, "open destination file file") + + fmt.Fprintln(dst, fileNoEditHeader) + fmt.Fprintln(dst) + + err = tpl.Execute(dst, plugins) + loggerx.ExitOnError(err, "while running tpl processor") +} diff --git a/test/helpers/plugin_server.go b/test/helpers/plugin_server.go index 141ec226c..bed96337a 100644 --- a/test/helpers/plugin_server.go +++ b/test/helpers/plugin_server.go @@ -6,12 +6,13 @@ import ( "path/filepath" "strconv" + "github.com/kubeshop/botkube/internal/loggerx" "github.com/kubeshop/botkube/test/fake" ) func main() { dir, err := os.Getwd() - exitOnErr(err) + loggerx.ExitOnError(err, "while getting current directory") host := os.Getenv("PLUGIN_SERVER_HOST") port := os.Getenv("PLUGIN_SERVER_PORT") @@ -22,7 +23,7 @@ func main() { port = "3010" } portInt, err := strconv.Atoi(port) - exitOnErr(err) + loggerx.ExitOnError(err, "while starting server") binDir := filepath.Join(dir, "plugin-dist") indexEndpoint, startServerFn := fake.NewPluginServer(fake.PluginConfig{ @@ -36,11 +37,5 @@ func main() { log.Printf("Service plugin binaries from %s\n", binDir) log.Printf("Botkube repository index URL: %s", indexEndpoint) err = startServerFn() - exitOnErr(err) -} - -func exitOnErr(err error) { - if err != nil { - log.Fatal(err) - } + loggerx.ExitOnError(err, "while starting server") }