Skip to content

Commit

Permalink
Add schema generation (#54)
Browse files Browse the repository at this point in the history
* add schema generation

* update gh action

* test gh action with changed schema

* run make generate-schema

* update readme
  • Loading branch information
simongottschlag authored Jul 12, 2023
1 parent 6c9c215 commit 05bfbd2
Show file tree
Hide file tree
Showing 10 changed files with 3,273 additions and 4 deletions.
23 changes: 22 additions & 1 deletion .github/workflows/pr-validation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,27 @@ jobs:
echo 'run make fmt and commit changes'
exit 1
fi
generate-schema:
runs-on: ubuntu-latest
steps:
- name: Clone repo
uses: actions/checkout@v3
- name: Setup go
uses: actions/setup-go@v4
with:
go-version: "^1.20.5"
- name: Generate schema
run: |
make generate-schema
- name: Check if working tree is dirty
run: |
if [[ $(git status --porcelain) ]]; then
git diff
echo 'run make generate-schema and commit changes'
exit 1
fi
build:
runs-on: ubuntu-latest
steps:
Expand Down Expand Up @@ -121,4 +142,4 @@ jobs:
format: "table"
exit-code: "1"
ignore-unfixed: true
severity: "CRITICAL,HIGH"
severity: "CRITICAL,HIGH"
23 changes: 21 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,52 @@ ifneq (,$(wildcard $(TEST_ENV_FILE)))
export
endif

.PHONY: all
all: fmt vet lint test

.PHONY: lint
lint:
golangci-lint run ./...

.PHONY: fmt
fmt:
go fmt ./...

.PHONY: vet
vet:
go vet ./...

.PHONY: build
build:
CGO_ENABLED=0 go build -installsuffix 'static' -o bin/azcagit ./src/main.go

.PHONY: generate-schema
generate-schema:
go run ./generate_schema.go

.PHONY: test
test: fmt vet
go test --cover ./...

.PHONY: cover
cover:
mkdir -p .tmp
go test -timeout 5m -coverpkg=./src/... -coverprofile=.tmp/coverage.out ./src/...
go tool cover -html=.tmp/coverage.out

.PHONY: terraform-up
terraform-up:
cd test/terraform
terraform init
terraform apply -auto-approve -var-file="../../.tmp/lab.tfvars"

.PHONY: terraform-mr-up
terraform-mr-up:
cd test/terraform-multi-region
terraform init
terraform apply -auto-approve -var-file="../../.tmp/lab.tfvars"

.PHONY: run
run:
# AZURE_TENANT_ID=$${TENANT_ID} AZURE_CLIENT_ID=$${CLIENT_ID} AZURE_CLIENT_SECRET=$${CLIENT_SECRET} \
go run ./src \
Expand All @@ -62,13 +74,16 @@ run:
--notifications-enabled \
--environment $${ENV}

.PHONY: docker-build
docker-build:
docker build . -t $(IMG)

.PHONY: docker-run
docker-run: docker-build
docker run -it --rm -e AZURE_TENANT_ID=$${TENANT_ID} -e AZURE_CLIENT_ID=$${CLIENT_ID} -e AZURE_CLIENT_SECRET=$${CLIENT_SECRET} $(IMG) \
--debug \
--resource-group-name $${RG_NAME} \
--own-resource-group-name $${OWN_RG_NAME} \
--subscription-id $${SUB_ID} \
--managed-environment-id $${ME_ID} \
--key-vault-name $${KV_NAME} \
Expand All @@ -77,7 +92,11 @@ docker-run: docker-build
--reconcile-interval "10s" \
--git-url $${GIT_URL_AND_CREDS} \
--git-branch "main" \
--git-yaml-path "yaml/"
--git-yaml-path "yaml/" \
--notifications-enabled \
--environment $${ENV}

.PHONY: k6-http-get
k6-http-get:
k6 run -e LOAD_TEST_URI=$${LOAD_TEST_URI} test/k6/http_get.js
k6 run -e LOAD_TEST_URI=$${LOAD_TEST_URI} test/k6/http_get.js

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Platform is used for what we call "platform services", in this case the virtual

Tenant is used only to synchronize the Container Apps manifests. The Container Apps that are created by `azcagit` will reside here.

The manifests are in the same format as Kubernetes manifests ([Kubernetes Resource Model aka KRM](https://cloud.google.com/blog/topics/developers-practitioners/build-platform-krm-part-2-how-kubernetes-resource-model-works)), but with a hard coupling to the [Azure Container Apps specification](https://docs.microsoft.com/en-us/azure/templates/microsoft.app/containerapps?pivots=deployment-language-arm-template) for `spec.app` when using `kind: AzureContainerApp` and [Azure Container Jobs specification](https://learn.microsoft.com/en-us/azure/templates/microsoft.app/jobs?pivots=deployment-language-arm-template) for `spec.job` when using `kind: AzureContainerJob`.
The manifests are in the same format as Kubernetes manifests ([Kubernetes Resource Model aka KRM](https://cloud.google.com/blog/topics/developers-practitioners/build-platform-krm-part-2-how-kubernetes-resource-model-works)), but with a hard coupling to the [Azure Container Apps specification](https://docs.microsoft.com/en-us/azure/templates/microsoft.app/containerapps?pivots=deployment-language-arm-template) for `spec.app` when using `kind: AzureContainerApp` and [Azure Container Jobs specification](https://learn.microsoft.com/en-us/azure/templates/microsoft.app/jobs?pivots=deployment-language-arm-template) for `spec.job` when using `kind: AzureContainerJob`. Auto generated schemas can be found in the [schemas](schemas/) directory.

An example manifest of an app:

Expand Down
76 changes: 76 additions & 0 deletions generate_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
//go:build generateschema

package main

import (
"encoding/json"
"fmt"
"os"

"github.com/invopop/jsonschema"
"github.com/invopop/yaml"
"github.com/xenitab/azcagit/src/source"
)

func main() {
err := run()
if err != nil {
fmt.Fprintf(os.Stderr, "schema generation returned an error: %v\n", err)
os.Exit(1)
}
}

func run() error {
err := generateSchema(&source.SourceApp{}, "app")
if err != nil {
return err
}

err = generateSchema(&source.SourceJob{}, "job")
if err != nil {
return err
}

return nil
}

func generateSchema[T any](t T, name string) error {
schema := jsonschema.Reflect(&t)
schemaJson, err := schema.MarshalJSON()
if err != nil {
return err
}

var schemaJsonRaw interface{}
err = json.Unmarshal(schemaJson, &schemaJsonRaw)
if err != nil {
return err
}

prettySchemaJson, err := json.MarshalIndent(schemaJsonRaw, "", " ")
if err != nil {
return err
}

schemaYaml, err := yaml.JSONToYAML(schemaJson)
if err != nil {
return err
}

currDir, err := os.Getwd()
if err != nil {
return err
}

err = os.WriteFile(fmt.Sprintf("%s/schemas/%s.json", currDir, name), prettySchemaJson, 0644)
if err != nil {
return err
}

err = os.WriteFile(fmt.Sprintf("%s/schemas/%s.yaml", currDir, name), schemaYaml, 0644)
if err != nil {
return err
}

return nil
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ require (
github.com/go-logr/zapr v1.2.4
github.com/google/go-github/v41 v41.0.0
github.com/hashicorp/go-multierror v1.1.1
github.com/invopop/jsonschema v0.7.0
github.com/invopop/yaml v0.2.0
github.com/microsoft/azure-devops-go-api/azuredevops/v6 v6.0.1
github.com/stretchr/testify v1.8.4
github.com/whilp/git-urls v1.0.0
Expand Down Expand Up @@ -55,6 +57,7 @@ require (
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
github.com/kevinburke/ssh_config v1.2.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,14 @@ github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/go-multierror v1.1.1 h1:H5DkEtf6CXdFp0N0Em5UCwQpXMWke8IA0+lD48awMYo=
github.com/hashicorp/go-multierror v1.1.1/go.mod h1:iw975J/qwKPdAO1clOe2L8331t/9/fmwbPZ6JB6eMoM=
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0 h1:i462o439ZjprVSFSZLZxcsoAe592sZB1rci2Z8j4wdk=
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0/go.mod h1:N0Wam8K1arqPXNWjMo21EXnBPOPp36vB07FNRdD2geA=
github.com/imdario/mergo v0.3.16 h1:wwQJbIsHYGMUyLSPrEq1CT16AhnhNJQ51+4fdHUnCl4=
github.com/imdario/mergo v0.3.16/go.mod h1:WBLT9ZmE3lPoWsEzCh9LPo3TiwVN+ZKEjmz+hD27ysY=
github.com/invopop/jsonschema v0.7.0 h1:2vgQcBz1n256N+FpX3Jq7Y17AjYt46Ig3zIWyy770So=
github.com/invopop/jsonschema v0.7.0/go.mod h1:O9uiLokuu0+MGFlyiaqtWxwqJm41/+8Nj0lD7A36YH0=
github.com/invopop/yaml v0.2.0 h1:7zky/qH+O0DwAyoobXUqvVBwgBFRxKoQ/3FjcVpjTMY=
github.com/invopop/yaml v0.2.0/go.mod h1:2XuRLgs/ouIrW3XNzuNj7J3Nvu/Dig5MXvbCEdiBN3Q=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
Expand Down Expand Up @@ -147,6 +153,7 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.3.1-0.20190311161405-34c6fa2dc709/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
Expand Down Expand Up @@ -275,6 +282,7 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
nhooyr.io/websocket v1.8.7 h1:usjR2uOr/zjjkVMy0lW+PPohFok7PCow5sDjLgX4P4g=
Expand Down
Loading

0 comments on commit 05bfbd2

Please sign in to comment.