Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 072e577

Browse files
authored
Merge pull request #6 from vshn/change/framework
Extend framework to support multiple functions
2 parents 3901795 + b1dca40 commit 072e577

20 files changed

+2014
-305
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@
33
/.github/release-notes.md
44

55
# Build
6-
# TODO: Adjust binary file name
7-
/go-bootstrap
6+
/appcat-comp-functions
87
*.out
98

109
# Docs

Dockerfile

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,25 @@
1+
FROM golang:1.20-alpine as Build
2+
3+
ARG INSTANCE=""
4+
5+
WORKDIR /app
6+
7+
COPY . ./
8+
RUN go mod download
9+
10+
RUN cd "cmd/$INSTANCE" && CGO_ENABLED=0 go build -o functionio .
11+
112
FROM docker.io/library/alpine:3.17 as runtime
213

14+
ARG INSTANCE=""
15+
316
RUN \
417
apk add --update --no-cache \
5-
bash \
6-
curl \
7-
ca-certificates \
8-
tzdata
9-
10-
# TODO: Adjust binary file name
11-
ENTRYPOINT ["go-bootstrap"]
12-
COPY go-bootstrap /usr/bin/
18+
bash \
19+
curl \
20+
ca-certificates \
21+
tzdata
1322

14-
USER 65536:0
23+
ENTRYPOINT ["functionio"]
24+
CMD ["--log-level", "1"]
25+
COPY --from=Build /app/cmd/$INSTANCE/functionio /usr/bin/

Makefile

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,30 @@ golangci_bin = $(go_bin)/golangci-lint
2323
help: ## Show this help
2424
@grep -E -h '\s##\s' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
2525

26+
## BUILD an instance
2627
.PHONY: build
2728
build: build-bin build-docker ## All-in-one build
2829

2930
.PHONY: build-bin
3031
build-bin: export CGO_ENABLED = 0
3132
build-bin: fmt vet ## Build binary
32-
@go build -o $(BIN_FILENAME) .
33+
@go build -o $(PROJECT_ROOT_DIR)/cmd/$(instance)/$(BIN_FILENAME) $(PROJECT_ROOT_DIR)/cmd/$(instance)
3334

3435
.PHONY: build-docker
35-
build-docker: build-bin ## Build docker image
36-
$(DOCKER_CMD) build -t $(CONTAINER_IMG) .
36+
build-docker: ## Build docker image
37+
$(DOCKER_CMD) build -t $(CONTAINER_IMG) --build-arg INSTANCE=$(instance) .
38+
39+
## BUILD all instances
40+
.PHONY: build-all
41+
build-all: build-bin-all build-docker-all ## All-in-one build for all instances
42+
43+
.PHONY: build-bin-all
44+
build-bin-all: recursive_target=build-bin
45+
build-bin-all: $(instances) ## Build binaries
46+
47+
.PHONY: build-docker-all
48+
build-docker-all: recursive_target=build-docker
49+
build-docker-all: $(instances) ## Build docker images
3750

3851
.PHONY: test
3952
test: test-go ## All-in-one test
@@ -68,8 +81,18 @@ generate: ## Generate additional code and artifacts
6881

6982
.PHONY: clean
7083
clean: kind-clean ## Cleans local build artifacts
71-
docker rmi $(CONTAINER_IMG) || true
84+
clean: recursive_target=.clean-build-img
85+
clean: $(instances)
7286
rm -rf docs/node_modules $(docs_out_dir) dist .cache $(WORK_DIR)
7387

88+
.PHONY: .clean-build-img
89+
.clean-build-img:
90+
docker rmi $(CONTAINER_IMG) -f || true
91+
rm $(PROJECT_ROOT_DIR)/cmd/$(instance)/$(BIN_FILENAME)
92+
7493
$(golangci_bin): | $(go_bin)
7594
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b "$(go_bin)"
95+
96+
.PHONY: $(instances)
97+
$(instances):
98+
$(MAKE) $(recursive_target) -e instance=$(basename $(@F))

Makefile.vars.mk

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,28 @@
11
## These are some common variables for Make
22

33
PROJECT_ROOT_DIR = .
4-
# TODO: Adjust project meta
5-
PROJECT_NAME ?= go-bootstrap
4+
PROJECT_NAME ?= appcat-comp-functions
65
PROJECT_OWNER ?= vshn
76

87
WORK_DIR = $(PWD)/.work
98

9+
## BUILD
10+
instance ?= vshn-postgres-func
11+
instances ?= vshn-postgres-func
12+
1013
## BUILD:go
11-
BIN_FILENAME ?= $(PROJECT_NAME)
14+
BIN_FILENAME ?= $(instance)
1215
go_bin ?= $(WORK_DIR)/bin
1316
$(go_bin):
1417
@mkdir -p $@
1518

1619
## BUILD:docker
1720
DOCKER_CMD ?= docker
1821

22+
## BUILD:docker VSHN Postgres
1923
IMG_TAG ?= latest
2024
# Image URL to use all building/pushing image targets
21-
CONTAINER_IMG ?= ghcr.io/$(PROJECT_OWNER)/$(PROJECT_NAME):$(IMG_TAG)
25+
CONTAINER_IMG ?= ghcr.io/$(PROJECT_OWNER)/$(BIN_FILENAME):$(IMG_TAG)
2226

2327

2428
## KIND:setup

README.md

Lines changed: 34 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,46 @@
1-
# go-bootstrap
1+
# appcat-comp-functions
22

3-
[![Build](https://img.shields.io/github/actions/workflow/status/vshn/go-bootstrap/.github/workflows/test.yml?branch=master)][build]
4-
![Go version](https://img.shields.io/github/go-mod/go-version/vshn/go-bootstrap)
5-
[![Version](https://img.shields.io/github/v/release/vshn/go-bootstrap)][releases]
6-
[![GitHub downloads](https://img.shields.io/github/downloads/vshn/go-bootstrap/total)][releases]
3+
[![Build](https://img.shields.io/github/actions/workflow/status/vshn/appcat-comp-functions/.github/workflows/test.yml?branch=master)][build]
4+
![Go version](https://img.shields.io/github/go-mod/go-version/vshn/appcat-comp-functions)
5+
[![Version](https://img.shields.io/github/v/release/vshn/appcat-comp-functions)][releases]
6+
[![GitHub downloads](https://img.shields.io/github/downloads/vshn/appcat-comp-functions/total)][releases]
77

8-
[build]: https://github.com/vshn/go-bootstrap/actions?query=workflow%3ATest
9-
[releases]: https://github.com/vshn/go-bootstrap/releases
8+
[build]: https://github.com/vshn/appcat-comp-functions/actions?query=workflow%3ATest
9+
[releases]: https://github.com/vshn/appcat-comp-functions/releases
10+
## Repository structure
1011

11-
Template repository for common Go setups
12+
This repository will build different docker images for different services. For that reason some folder structure is bound to the name of the service.
1213

13-
## Features
14-
15-
* GitHub Workflows
16-
- Build (Go & Docker image)
17-
- Test
18-
- Lint (Go & GolangCI)
19-
- Release (Goreleaser & Changelog generator)
20-
21-
* GitHub issue templates
22-
- PR template
23-
- Issue templates using GitHub issue forms
24-
25-
* Goreleaser
26-
- Go build for `amd64`, `armv8`
27-
- Docker build for `latest` and `vx.y.z` tags
28-
- Push Docker image to GitHub's registry `ghcr.io`
29-
30-
* Antora documentation
31-
- Build default documentation with VSHN styling
32-
- Publish to GitHub Pages by default (opt-out)
33-
- Automated with GitHub workflows to build in `master` branch and (pre-)releases.
34-
- Available `make` targets are prefixed with `docs-`
35-
36-
* Local Kubernetes environment
37-
- Setup Kubernetes-In-Docker (kind)
38-
- Prepares a kubeconfig file in `.kind/`
39-
- Optionally install NGINX as ingress controller
40-
- Available `make` targets are prefixed with `kind-`
41-
42-
* CLI and logging framework
43-
- To help get you started with CLI subcommands, flags and environment variables
44-
- If you don't need subcommands, remove `example_command.go` and adjust `cli.App` settings in `main.go`
45-
46-
## TODO's after generating from this template
47-
48-
TIP: You can search for these tasks using `grep -n -r "TODO:" .`
49-
50-
1. `go.mod`: Adjust module name.
51-
1. `.goreleaser.yml`: Adjust Docker image location in `dockers` and `docker_manifests` parameters.
52-
1. `.gitignore`: Adjust binary file name.
53-
1. `Dockerfile`: Adjust binary file name.
54-
1. `Makefile.vars.mk`: Adjust project meta.
55-
1. `.github/ISSUE_TEMPLATE/config.yml` (optional): Enable forwarding questions to GitHub Discussions or other page.
56-
1. `docs/antora.yml`: Adjust project meta.
57-
1. `docs/antora-playbook.yml`: Adjust project meta.
58-
1. `docs/modules/pages/index.adoc`: Edit start page.
59-
1. `docs/modules/nav.adoc`: Edit navigation.
60-
1. `main.go`: Adjust variables.
61-
1. Edit this README (including badges links)
62-
1. Start hacking in `example_command.go`.
63-
64-
After completing a task, you can remove the comment in the files.
65-
66-
## Other repository settings
67-
68-
1. GitHub Settings
69-
- "Options > Wiki" (disable)
70-
- "Options > Allow auto-merge" (enable)
71-
- "Options > Automatically delete head branches" (enable)
72-
- "Collaborators & Teams > Add Teams and users to grant maintainer permissions
73-
- "Branches > Branch protection rules":
74-
- Branch name pattern: `master`
75-
- Require status check to pass before merging: `["lint"]` (you may need to push come commits first)
76-
- "Pages > Source": Branch `gh-pages`
77-
78-
1. GitHub Issue labels
79-
- "Issues > Labels > New Label" for the following labels with color suggestions:
80-
- `change` (`#D93F0B`)
81-
- `dependency` (`#ededed`)
82-
- `breaking` (`#FBCA04`)
83-
84-
## Antora documentation
85-
86-
This template comes with an Antora documentation module to help you create Asciidoctor documentation.
87-
By default, it is automatically published to GitHub Pages in `gh-pages` branch, however it can also be included in external Antora playbooks.
88-
89-
### Setup GitHub Pages
14+
```
15+
.
16+
├── cmd
17+
│ ├── vshn-postgres-func
18+
│ └── vshn-redis-func
19+
├── kind
20+
├── functions
21+
│ ├── vshn-common-func
22+
│ ├── vshn-postgres-func
23+
│ └── vshn-redis-func
24+
├── runtime
25+
└── test
26+
```
9027

91-
Once you generated a new repository using this template, the initial commit automatically runs a Job that creates the documentation in the `gh-pages` branch.
92-
All you need to do is then to enable Pages in the settings.
28+
- `./cmd` contains the entry point boilerplate for each service.
29+
- `./pkg/functions` contains the actual logic for the function. Each transform should be in its own package.
9330

94-
The `gh-pages` branch is a parent-less commit that only contains the Antora-generated files.
31+
## Add a new function
9532

96-
However, if that's not the case or if you are setting up Antora in an existing repository, here's how you can achieve the same, but make sure to **commit or stash current changes first!**
97-
```bash
98-
current_branch=$(git rev-parse --abbrev-ref HEAD)
99-
initial_commit=$(git rev-list --max-parents=0 HEAD | tail -n 1)
100-
git switch --create gh-pages $initial_commit
101-
git rm -r --cached .
102-
git commit -m "Prepare gh-pages branch"
103-
git push --set-upstream origin gh-pages
104-
git switch -f $current_branch
105-
```
33+
The framework is designed to easily add new composition functions to any AppCat service.
10634

107-
And you're done!
108-
GitHub automatically recognizes activity and sets up Pages if there's a `gh-pages` branch.
35+
To add a new function to PostgreSQL by VSHN:
10936

110-
---
37+
- Create a new package under `./pkg/functions/vshn-postgres-func`
38+
- Add the transform function to the list in `./cmd/vshn-postgres-func`
39+
- implement the actual `transform()` function by using the helper functions from `io.go`
11140

112-
If you want to skip deployment to GitHub Pages you need to delete specific files and references:
113-
`rm -f .github/workflows/docs.yml docs/package*.json docs/antora-playbook.yml docs/antora-build.mk`.
114-
Also don't forget to delete the branch and disable Pages in the repository settings.
41+
This architecture allows us to run all the functions with a single command. But for debugging and development purpose it's possible to run each function seperately, by using the `--function` flag.
11542

116-
---
43+
## Manually testing a function
44+
To test a function you can leverage the FunctionIO file in the `./test` folder.
11745

118-
If you want to remove documentation completely simply run `rm -rf docs .github/workflows/docs.yml`.
46+
`cat test/function-io.yaml | go run cmd/vshn-postgres-func/main.go --function myfunction > test.yaml`

cmd/vshn-postgres-func/main.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
vp "github.com/vshn/appcat-comp-functions/functions/vshn-postgres-func"
6+
"github.com/vshn/appcat-comp-functions/runtime"
7+
"os"
8+
9+
"github.com/urfave/cli/v2"
10+
vshnv1 "github.com/vshn/component-appcat/apis/vshn/v1"
11+
)
12+
13+
var postgresFunctions = []runtime.Transform[vshnv1.VSHNPostgreSQL, *vshnv1.VSHNPostgreSQL]{
14+
{
15+
Name: "dummy",
16+
TransformFunc: vp.Transform,
17+
},
18+
}
19+
20+
func main() {
21+
app := newApp()
22+
err := app.Run(os.Args)
23+
// If required flags aren't set, it will return with error before we could set up logging
24+
if err != nil {
25+
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
26+
os.Exit(1)
27+
}
28+
}
29+
30+
func newApp() *cli.App {
31+
app := &cli.App{
32+
Name: vp.AI.AppName,
33+
Usage: vp.AI.AppLongName,
34+
Version: fmt.Sprintf("%s, revision=%s, date=%s", vp.AI.Version, vp.AI.Commit, vp.AI.Date),
35+
Action: run,
36+
Flags: []cli.Flag{
37+
runtime.NewLogLevelFlag(),
38+
runtime.NewLogFormatFlag(),
39+
runtime.NewFunctionFlag(),
40+
},
41+
}
42+
return app
43+
}
44+
45+
func run(ctx *cli.Context) error {
46+
err := runtime.SetupLogging(vp.AI, ctx)
47+
if err != nil {
48+
return err
49+
}
50+
51+
_ = runtime.LogMetadata(ctx, vp.AI)
52+
53+
return runtime.RunCommand(ctx, postgresFunctions)
54+
}

docs/antora-playbook.yml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
site:
2-
# TODO: Adjust project meta
3-
title: Go Bootstrap
4-
start_page: go-bootstrap::index.adoc
5-
url: https://vshn.github.io/go-bootstrap
2+
title: AppCat Composition Functions
3+
start_page: appcat-comp-functions::index.adoc
4+
url: https://vshn.github.io/appcat-comp-functions
65
content:
76
sources:
87
- url: ../antora

docs/antora.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
# TODO: Adjust project meta
2-
name: go-bootstrap
1+
name: appcat-comp-functions
32
title: Go Bootstrap
43
version: master
54
start_page: ROOT:index.adoc

example_command.go

Lines changed: 0 additions & 37 deletions
This file was deleted.

0 commit comments

Comments
 (0)