Skip to content

Commit e8679e1

Browse files
authored
feat: automatically generate, create and update library repos (jsonnet-libs#39)
The last bits to automate all of this through Github Actions: * Create repositories for all libraries with Terraform * Per library: * Clone the repository * Remove all but .git * Generate the library and docs * Add, Commit and Push Renames s/k8s-alpha/k8s-libsonnet and takes the common approach of the -libsonnet suffix as discussed/voted for in the #tanka channel on Grafana's community Slack.
1 parent e5b527b commit e8679e1

26 files changed

+413
-146
lines changed

.github/workflows/main.yml

Lines changed: 92 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,110 @@
1-
# Generated by `make configure`
1+
# Generated by `make configure`, please do not edit manually.
22
"jobs":
3-
"libs_cert-manager":
4-
"name": "Generate libs/cert-manager Jsonnet library and docs"
3+
"cert-manager":
4+
"name": "Generate cert-manager Jsonnet library and docs"
5+
"needs": "terraform"
56
"runs-on": "ubuntu-latest"
67
"steps":
78
- "uses": "actions/checkout@v2"
8-
- "run": "make run INPUT_DIR=${PWD}/libs/cert-manager"
9-
"libs_cnrm":
10-
"name": "Generate libs/cnrm Jsonnet library and docs"
9+
- "if": "${{ github.ref == 'refs/heads/master' && github.repository == 'jsonnet-libs/k8s' }}"
10+
"run": |
11+
mkdir ~/.ssh
12+
echo "${{ secrets.DEPLOY_KEY }}" > ~/.ssh/id_rsa
13+
chmod 600 ~/.ssh/id_rsa
14+
export GEN_COMMIT=1
15+
- "run": "make build libs/cert-manager"
16+
"cnrm":
17+
"name": "Generate cnrm Jsonnet library and docs"
18+
"needs": "terraform"
1119
"runs-on": "ubuntu-latest"
1220
"steps":
1321
- "uses": "actions/checkout@v2"
14-
- "run": "make run INPUT_DIR=${PWD}/libs/cnrm"
15-
"libs_crossplane":
16-
"name": "Generate libs/crossplane Jsonnet library and docs"
22+
- "if": "${{ github.ref == 'refs/heads/master' && github.repository == 'jsonnet-libs/k8s' }}"
23+
"run": |
24+
mkdir ~/.ssh
25+
echo "${{ secrets.DEPLOY_KEY }}" > ~/.ssh/id_rsa
26+
chmod 600 ~/.ssh/id_rsa
27+
export GEN_COMMIT=1
28+
- "run": "make build libs/cnrm"
29+
"crossplane":
30+
"name": "Generate crossplane Jsonnet library and docs"
31+
"needs": "terraform"
1732
"runs-on": "ubuntu-latest"
1833
"steps":
1934
- "uses": "actions/checkout@v2"
20-
- "run": "make run INPUT_DIR=${PWD}/libs/crossplane"
21-
"libs_istio":
22-
"name": "Generate libs/istio Jsonnet library and docs"
35+
- "if": "${{ github.ref == 'refs/heads/master' && github.repository == 'jsonnet-libs/k8s' }}"
36+
"run": |
37+
mkdir ~/.ssh
38+
echo "${{ secrets.DEPLOY_KEY }}" > ~/.ssh/id_rsa
39+
chmod 600 ~/.ssh/id_rsa
40+
export GEN_COMMIT=1
41+
- "run": "make build libs/crossplane"
42+
"debugging":
43+
"name": "Debugging Github Action values"
44+
"runs-on": "ubuntu-latest"
45+
"steps":
46+
- "run": "echo isMaster? ${{ github.ref == 'refs/heads/master' && github.repository == 'jsonnet-libs/k8s' }}"
47+
- "run": "echo isPR? ${{ github.ref != 'refs/heads/master' && github.repository == 'jsonnet-libs/k8s' }}"
48+
- "run": "echo ${{ github.repository }}"
49+
- "run": "echo ${{ github.ref }}"
50+
- "run": "echo ${{ github.event_name }}"
51+
"istio":
52+
"name": "Generate istio Jsonnet library and docs"
53+
"needs": "terraform"
54+
"runs-on": "ubuntu-latest"
55+
"steps":
56+
- "uses": "actions/checkout@v2"
57+
- "if": "${{ github.ref == 'refs/heads/master' && github.repository == 'jsonnet-libs/k8s' }}"
58+
"run": |
59+
mkdir ~/.ssh
60+
echo "${{ secrets.DEPLOY_KEY }}" > ~/.ssh/id_rsa
61+
chmod 600 ~/.ssh/id_rsa
62+
export GEN_COMMIT=1
63+
- "run": "make build libs/istio"
64+
"k8s":
65+
"name": "Generate k8s Jsonnet library and docs"
66+
"needs": "terraform"
2367
"runs-on": "ubuntu-latest"
2468
"steps":
2569
- "uses": "actions/checkout@v2"
26-
- "run": "make run INPUT_DIR=${PWD}/libs/istio"
27-
"libs_k8s-alpha":
28-
"name": "Generate libs/k8s-alpha Jsonnet library and docs"
70+
- "if": "${{ github.ref == 'refs/heads/master' && github.repository == 'jsonnet-libs/k8s' }}"
71+
"run": |
72+
mkdir ~/.ssh
73+
echo "${{ secrets.DEPLOY_KEY }}" > ~/.ssh/id_rsa
74+
chmod 600 ~/.ssh/id_rsa
75+
export GEN_COMMIT=1
76+
- "run": "make build libs/k8s"
77+
"terraform":
78+
"name": "create repositories"
2979
"runs-on": "ubuntu-latest"
3080
"steps":
3181
- "uses": "actions/checkout@v2"
32-
- "run": "make run INPUT_DIR=${PWD}/libs/k8s-alpha"
82+
- "uses": "zendesk/setup-jsonnet@v7"
83+
- "run": "make tf/main.tf.json"
84+
- "uses": "hashicorp/setup-terraform@v1"
85+
"with":
86+
"cli_config_credentials_token": "${{ secrets.TF_API_TOKEN }}"
87+
- "env":
88+
"GITHUB_TOKEN": "${{ secrets.GITHUB_TOKEN }}"
89+
"TF_IN_AUTOMATION": "1"
90+
"run": "terraform init"
91+
"working-directory": "tf"
92+
- "env":
93+
"GITHUB_TOKEN": "${{ secrets.GITHUB_TOKEN }}"
94+
"TF_IN_AUTOMATION": "1"
95+
"run": "terraform validate -no-color"
96+
"working-directory": "tf"
97+
- "env":
98+
"GITHUB_TOKEN": "${{ secrets.GITHUB_TOKEN }}"
99+
"TF_IN_AUTOMATION": "1"
100+
"if": "${{ github.ref != 'refs/heads/master' && github.repository == 'jsonnet-libs/k8s' }}"
101+
"run": "terraform plan -no-color"
102+
"working-directory": "tf"
103+
- "env":
104+
"GITHUB_TOKEN": "${{ secrets.GITHUB_TOKEN }}"
105+
"TF_IN_AUTOMATION": "1"
106+
"if": "${{ github.ref == 'refs/heads/master' && github.repository == 'jsonnet-libs/k8s' }}"
107+
"run": "terraform apply -no-color -auto-approve"
108+
"working-directory": "tf"
33109
"on":
34110
- "push"

.gitignore

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
swagger.json
2-
jsonnetfile.json
3-
jsonnetfile.lock.json
4-
main.jsonnet
5-
spec.json
6-
vendor
7-
k8s
8-
.vscode/
1+
# k8s-gen binary name
2+
./k8s
3+
4+
# really want to make sure I don't commit that
5+
id_rsa
6+
7+
# default output directory
98
gen/
109

1110
# autogenerated

Dockerfile

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
FROM bitnami/kubectl:1.21.1 as kubectl
2-
FROM rancher/k3s:v1.21.1-k3s1 as k3s
3-
FROM mikefarah/yq:4.9.3 as yq2
4-
FROM jsonnetlibs/docsonnet:0.0.1 as docsonnet
5-
6-
FROM golang:1.16.4 as base
1+
FROM golang:1.16 as base
72

83
ENV GO111MODULE=on
94
WORKDIR /app
@@ -22,27 +17,27 @@ RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
2217
-ldflags='-s -w -extldflags "-static"' \
2318
-o k8s-gen .
2419

25-
FROM golang:1.16.4-alpine3.12 as jsonnet
20+
FROM golang:1.16-alpine3.14 as jsonnet
2621

2722
RUN apk add --no-cache git
2823
RUN go get github.com/google/go-jsonnet/cmd/jsonnet
2924

30-
FROM alpine:3.12
25+
FROM alpine:3.14
3126

3227
WORKDIR /app
3328

29+
RUN apk add --no-cache bash curl git
30+
3431
ENV KUBECONFIG=/app/kubeconfig/kube-config.yaml
3532
RUN chmod a+w /app
3633

37-
COPY --from=kubectl /opt/bitnami/kubectl/bin/kubectl /usr/local/bin/
38-
COPY --from=k3s /bin/k3s /usr/local/bin/
39-
COPY --from=yq2 /usr/bin/yq /usr/local/bin/yq2
40-
COPY --from=docsonnet /usr/local/bin/docsonnet /usr/local/bin/
34+
COPY --from=mikefarah/yq:4.9.6 /usr/bin/yq /usr/local/bin/yq2
35+
COPY --from=bitnami/kubectl:1.21.2 /opt/bitnami/kubectl/bin/kubectl /usr/local/bin/
36+
COPY --from=rancher/k3s:v1.21.2-k3s1 /bin/k3s /usr/local/bin/
37+
COPY --from=jsonnetlibs/docsonnet:0.0.3 /usr/local/bin/docsonnet /usr/local/bin/
4138
COPY --from=builder /app/k8s-gen /usr/local/bin/
4239
COPY --from=jsonnet /go/bin/jsonnet /usr/local/bin/
4340

44-
RUN apk add --no-cache bash curl
45-
4641
COPY scripts .
4742
COPY jsonnet jsonnet
4843
COPY LICENSE .

Makefile

Lines changed: 28 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,51 @@
1-
.PHONY: configure debug run build test push push-image
2-
31
IMAGE_NAME ?= k8s-gen
42
IMAGE_PREFIX ?= jsonnetlibs
5-
IMAGE_TAG ?= 0.0.1
3+
IMAGE_TAG ?= 0.0.3
64

7-
INPUT_DIR ?= ${PWD}/libs/k8s-alpha
85
OUTPUT_DIR ?= ${PWD}/gen
9-
10-
ABS_INPUT_DIR := $(shell realpath $(INPUT_DIR))
116
ABS_OUTPUT_DIR := $(shell realpath $(OUTPUT_DIR))
127

13-
LIBS=$(shell echo libs/* | sed "s/ /,/g")
8+
IMPORTS=$(shell find libs -name config.jsonnet | xargs -I {} echo "(import '{}'),")
9+
10+
.DEFAULT_GOAL: default
11+
default:
1412

1513
## Requires go-jsonnet for -c flag
16-
configure:
17-
jsonnet -c -m . -A "libs=$(LIBS)" -S jsonnet/github_action.jsonnet
14+
.PHONY: .github/workflows/main.yml
15+
.github/workflows/main.yml:
16+
jsonnet -c -m . -S -J . --tla-code "libs=[$(IMPORTS)]" jsonnet/github_action.jsonnet
17+
18+
## Requires go-jsonnet for -c flag
19+
.PHONY: tf/main.tf.json
20+
tf/main.tf.json:
21+
jsonnet -c -m . -S -J . --tla-code "libs=[$(IMPORTS)]" jsonnet/terraform.jsonnet
22+
23+
clean:
24+
rm -f .github/workflows/main.yml:
25+
rm -f tf/main.tf.json
26+
27+
configure: clean .github/workflows/main.yml tf/main.tf.json
1828

1929
debug: build
20-
mkdir -p $(ABS_OUTPUT_DIR) && \
21-
DEBUG=true bash bin/docker.sh \
22-
-v $(ABS_INPUT_DIR):/config \
23-
-v $(ABS_OUTPUT_DIR):/output \
24-
$(IMAGE_PREFIX)/$(IMAGE_NAME):$(IMAGE_TAG)
30+
make $@ DEBUG=true
2531

26-
run: build
32+
all: build libs/*
33+
34+
libs/*:
2735
mkdir -p $(ABS_OUTPUT_DIR) && \
2836
bash bin/docker.sh \
29-
-v $(ABS_INPUT_DIR):/config \
37+
-v $(shell realpath $@):/config \
3038
-v $(ABS_OUTPUT_DIR):/output \
39+
-e GEN_COMMIT=${GEN_COMMIT} \
3140
$(IMAGE_PREFIX)/$(IMAGE_NAME):$(IMAGE_TAG) /config /output
3241

33-
all:
34-
bash bin/all.sh $(ABS_OUTPUT_DIR)
35-
36-
3742
build:
3843
docker build -t $(IMAGE_PREFIX)/$(IMAGE_NAME):$(IMAGE_TAG) .
3944

40-
test: build
41-
42-
push: build test push-image
45+
push: build push-image
4346

4447
push-image:
4548
docker push $(IMAGE_PREFIX)/$(IMAGE_NAME):$(IMAGE_TAG)
4649
docker push $(IMAGE_PREFIX)/$(IMAGE_NAME):latest
50+
51+
.PHONY: clean configure debug run all libs/* build push push-image

0 commit comments

Comments
 (0)