Skip to content

Commit

Permalink
Add tpl approach for plugin goreleaser config
Browse files Browse the repository at this point in the history
  • Loading branch information
mszostok committed Apr 15, 2023
1 parent bd64d77 commit a9da2e9
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 76 deletions.
38 changes: 38 additions & 0 deletions .goreleaser.plugin.tpl.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# 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:
<range .>
- 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
<end>

archives:
<range .>
- builds: [<.Name>]
id: <.Name>
files:
- none*
name_template: "{{ .Binary }}"
<end>

snapshot:
name_template: 'v{{ .Version }}'
168 changes: 125 additions & 43 deletions .goreleaser.plugin.yaml
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -7,82 +7,164 @@ before:
- go mod download

builds:

- 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: 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

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
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:
- 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

- 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
env:
- CGO_ENABLED=0
goos:
- linux
- darwin
goarch:
- amd64
- arm64
goarm:
- 7


archives:

- builds: [echo]
id: echo
files:
- none*
name_template: "{{ .Binary }}"

- builds: [gh]
id: gh
files:
- none*
name_template: "{{ .Binary }}"

- builds: [helm]
id: helm
files:
- none*
name_template: "{{ .Binary }}"

- builds: [kubectl]
id: kubectl
files:
- none*
name_template: "{{ .Binary }}"

- builds: [cm-watcher]
id: cm-watcher
files:
- none*
name_template: "{{ .Binary }}"

- builds: [kubernetes]
id: kubernetes
files:
- none*
name_template: "{{ .Binary }}"

- builds: [prometheus]
id: prometheus
files:
- none*
name_template: "{{ .Binary }}"


snapshot:
name_template: 'v{{ .Version }}'
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,14 @@ build: pre-build
# Build Botkube official plugins for all supported platforms.
build-plugins: pre-build
@echo "Building plugins binaries"
go run ./hack/target/gen-goreleaser/main.go
@./hack/goreleaser.sh build_plugins
@echo "Build completed successfully"

# Build Botkube official plugins only for current GOOS and GOARCH.
build-plugins-single: pre-build
@echo "Building single target plugins binaries"
go run ./hack/target/gen-goreleaser/main.go
@./hack/goreleaser.sh build_plugins_single
@echo "Build completed successfully"

Expand Down
6 changes: 2 additions & 4 deletions cmd/botkube/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"errors"
"fmt"
"log"
"net/http"
"time"

Expand Down Expand Up @@ -67,9 +66,8 @@ func main() {
ctx, cancelCtxFn := context.WithCancel(ctx)
defer cancelCtxFn()

if err := run(ctx); err != nil {
log.Fatal(err)
}
err := run(ctx)
loggerx.ExitOnError(err, "while running app")
}

// run wraps the main logic of the app to be able to properly clean up resources via deferred calls.
Expand Down
14 changes: 4 additions & 10 deletions cmd/source/cm-watcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"fmt"
"log"

"github.com/MakeNowJust/heredoc"
"github.com/hashicorp/go-plugin"
Expand All @@ -17,6 +16,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
watchtools "k8s.io/client-go/tools/watch"

"github.com/kubeshop/botkube/internal/loggerx"
"github.com/kubeshop/botkube/pkg/api"
"github.com/kubeshop/botkube/pkg/api/source"
"github.com/kubeshop/botkube/pkg/pluginx"
Expand Down Expand Up @@ -82,9 +82,9 @@ func (CMWatcher) Stream(ctx context.Context, in source.StreamInput) (source.Stre

func listenEvents(ctx context.Context, kubeConfig []byte, obj Object, sink chan<- []byte) {
config, err := clientcmd.RESTConfigFromKubeConfig(kubeConfig)
exitOnError(err)
loggerx.ExitOnError(err, "while creating kubeconfig")
clientset, err := kubernetes.NewForConfig(config)
exitOnError(err)
loggerx.ExitOnError(err, "while creating k8s client")

fieldSelector := fields.OneTermEqualSelector("metadata.name", obj.Name).String()
lw := &cache.ListWatch{
Expand All @@ -110,7 +110,7 @@ func listenEvents(ctx context.Context, kubeConfig []byte, obj Object, sink chan<
}

_, err = watchtools.UntilWithSync(ctx, lw, &corev1.ConfigMap{}, nil, infiniteWatch)
exitOnError(err)
loggerx.ExitOnError(err, "while watching for ConfigMaps")
}

func main() {
Expand All @@ -121,12 +121,6 @@ func main() {
})
}

func exitOnError(err error) {
if err != nil {
log.Fatal(err)
}
}

func jsonSchema() api.JSONSchema {
return api.JSONSchema{
Value: heredoc.Docf(`{
Expand Down
16 changes: 5 additions & 11 deletions hack/gen-plugin-index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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,
Expand All @@ -38,18 +38,12 @@ func main() {

log.Info("Building index..")
idx, err := idxBuilder.Build(absBinsDir, *urlBasePath, *pluginNameFilter)
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")
}
Loading

0 comments on commit a9da2e9

Please sign in to comment.