Skip to content

Commit bbeb412

Browse files
florekszreigzmichaeljguarino
authored
feat(harness): add stackrun harness (#171)
* initial commit for stackrun harness * extend exec handling to support custom log sink and remotely cancellable context * small refactoring * add controller and executor to manage the execution of commands * small refactor * are pre/post run callbacks to update run/runstep status accordingly * initialize custom log sink for all executables * update console logs sink * add dockerfile to build harness image * simplify harness dockerfile * update console writer * add initial logic to gather terraform output and complete stack run * refactor state/output management and add more code documentation * go mod tidy * use nonroot tag * add app version to harness * add initial harness cd workflow * fix main branch name * disable test dependency * update gh action * update gh action and fix gitignore * update gh action * update console client and gh action * push initial harness images * listen for stack run status changes to see if it was cancelled * update gh action * fix terraform harness dockerfile * update gh action * add packages write permissions * fix nil pointer * wait for the subroutines to complete before finishing * add sleep command to harness image * fix: make console writer thread-safe * enforce exec to use the same writer for stdout/stderr to ensure proper output order * refactor code and add some documentation * simplify code and update docs/comments * fix tf state marshalling * do not try to use plan file if it does not exist * fix lint * fix agent build * fix agent dockerfile * update clientmock --------- Co-authored-by: Lukasz Zajaczkowski <[email protected]> Co-authored-by: michaeljguarino <[email protected]>
1 parent 9d3fb48 commit bbeb412

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

58 files changed

+2934
-153
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ jobs:
4242
with:
4343
go-version-file: go.mod
4444
check-latest: true
45-
- run: PATH=$PATH:$GOPATH/bin make build
45+
- run: PATH=$PATH:$GOPATH/bin make agent
4646
test:
4747
name: Unit test
4848
runs-on: ubuntu-latest
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
name: Publish Harness
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- "main"
7+
push:
8+
tags:
9+
- 'v*.*.*'
10+
11+
env:
12+
GOPATH: /home/runner/go/
13+
GOPROXY: "https://proxy.golang.org"
14+
15+
jobs:
16+
# TODO: Enable once some unit tests are added for harness
17+
# test:
18+
# name: Unit test
19+
# runs-on: ubuntu-latest
20+
# steps:
21+
# - uses: actions/checkout@v4
22+
# - uses: actions/setup-go@v4
23+
# with:
24+
# go-version-file: go.mod
25+
# check-latest: true
26+
# - run: PATH=$PATH:$GOPATH/bin make test
27+
28+
publish-harness-base:
29+
name: Build and push harness base container
30+
runs-on: ubuntu-20.04
31+
# needs: [test]
32+
permissions:
33+
contents: 'read'
34+
id-token: 'write'
35+
packages: 'write'
36+
outputs:
37+
version: ${{ steps.meta.outputs.version }}
38+
steps:
39+
- name: Checkout
40+
uses: actions/checkout@v4
41+
with:
42+
fetch-depth: 0
43+
- name: Docker meta
44+
id: meta
45+
uses: docker/metadata-action@v5
46+
with:
47+
# list of Docker images to use as base name for tags
48+
images: |
49+
ghcr.io/pluralsh/stackrun-harness-base
50+
docker.io/pluralsh/stackrun-harness-base
51+
tags: |
52+
type=semver,pattern={{version}},priority=1000
53+
type=sha,priority=800
54+
type=ref,event=pr,priority=600
55+
- name: Set up QEMU
56+
uses: docker/setup-qemu-action@v3
57+
- name: Set up Docker Buildx
58+
uses: docker/setup-buildx-action@v3
59+
- name: Login to GHCR
60+
uses: docker/login-action@v3
61+
with:
62+
registry: ghcr.io
63+
username: ${{ github.repository_owner }}
64+
password: ${{ secrets.GITHUB_TOKEN }}
65+
- name: Login to Docker
66+
uses: docker/login-action@v3
67+
with:
68+
username: mjgpluralsh
69+
password: ${{ secrets.DOCKER_ACCESS_TOKEN }}
70+
- name: Build and push
71+
uses: docker/build-push-action@v5
72+
with:
73+
context: "."
74+
file: "./hack/harness/base.Dockerfile"
75+
push: true
76+
tags: ${{ steps.meta.outputs.tags }}
77+
labels: ${{ steps.meta.outputs.labels }}
78+
platforms: linux/amd64,linux/arm64
79+
cache-from: type=gha
80+
cache-to: type=gha,mode=max
81+
build-args: |
82+
VERSION=${{ steps.meta.outputs.version }}
83+
84+
# TODO: Do we need that for harness?
85+
# - name: slack webhook
86+
# uses: 8398a7/action-slack@v3
87+
# with:
88+
# status: ${{ job.status }}
89+
# fields: workflow,job,repo,message,commit,author
90+
# env:
91+
# SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK }} # required
92+
# if: always()
93+
94+
publish-harness-terraform:
95+
name: Build and push harness terraform container
96+
runs-on: ubuntu-20.04
97+
needs: [publish-harness-base]
98+
env:
99+
TERRAFORM_VERSION: 1.8.2
100+
permissions:
101+
contents: write
102+
discussions: write
103+
pull-requests: write
104+
packages: write
105+
steps:
106+
- name: Checkout
107+
uses: actions/checkout@v4
108+
with:
109+
fetch-depth: 0
110+
- name: Docker meta
111+
id: meta
112+
uses: docker/metadata-action@v5
113+
with:
114+
# list of Docker images to use as base name for tags
115+
images: |
116+
ghcr.io/pluralsh/stackrun-harness
117+
docker.io/pluralsh/stackrun-harness
118+
tags: |
119+
type=semver,pattern={{version}},suffix=-terraform${{ env.TERRAFORM_VERSION }},priority=1000
120+
type=sha,suffix=-terraform${{ env.TERRAFORM_VERSION }},priority=800
121+
type=ref,event=pr,suffix=-terraform${{ env.TERRAFORM_VERSION }},priority=600
122+
- name: Set up QEMU
123+
uses: docker/setup-qemu-action@v3
124+
- name: Set up Docker Buildx
125+
uses: docker/setup-buildx-action@v3
126+
- name: Login to GHCR
127+
uses: docker/login-action@v3
128+
with:
129+
registry: ghcr.io
130+
username: ${{ github.repository_owner }}
131+
password: ${{ secrets.GITHUB_TOKEN }}
132+
- name: Login to Docker
133+
uses: docker/login-action@v3
134+
with:
135+
username: mjgpluralsh
136+
password: ${{ secrets.DOCKER_ACCESS_TOKEN }}
137+
- name: Build and push
138+
uses: docker/build-push-action@v5
139+
with:
140+
context: "."
141+
file: "./hack/harness/terraform.Dockerfile"
142+
push: true
143+
tags: ${{ steps.meta.outputs.tags }}
144+
labels: ${{ steps.meta.outputs.labels }}
145+
platforms: linux/amd64,linux/arm64
146+
cache-from: type=gha
147+
cache-to: type=gha,mode=max
148+
build-args: |
149+
TERRAFORM_IMAGE_TAG=${{ env.TERRAFORM_VERSION }}
150+
HARNESS_BASE_IMAGE_REPO=ghcr.io/pluralsh/stackrun-harness-base
151+
HARNESS_BASE_IMAGE_TAG=${{ needs.publish-harness-base.outputs.version }}
152+

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,6 @@ bin/
2525
# vendor/
2626

2727
notes.md
28+
29+
# Harness
30+
stackrun/**

Dockerfile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ COPY go.sum go.sum
1111
RUN go mod download
1212

1313
# Copy the go source
14-
COPY /cmd cmd/
14+
COPY /cmd/agent cmd/agent
1515
COPY /pkg pkg/
1616
COPY /api api/
1717
COPY /internal internal/
1818

1919
# Build
20-
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} GO111MODULE=on go build -a -o deployment-agent cmd/*
20+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=${TARGETARCH} GO111MODULE=on go build -a -o deployment-agent cmd/agent/**
2121

2222
FROM alpine:3.18
2323
WORKDIR /workspace
2424

2525
COPY --from=builder /workspace/deployment-agent .
2626
USER 65532:65532
27-
ENTRYPOINT ["/workspace/deployment-agent"]
27+
ENTRYPOINT ["/workspace/deployment-agent"]

Makefile

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,22 +58,51 @@ genmock: mockery ## generates mocks before running tests
5858

5959
##@ Run
6060

61-
.PHONY: run
62-
run: ## run
63-
go run cmd/*
61+
.PHONY: agent-run
62+
agent-run: ## run agent
63+
go run cmd/agent/**
6464

6565
##@ Build
6666

67-
.PHONY: build
68-
build: ## build
69-
go build -o bin/deployment-agent cmd/*
67+
.PHONY: agent
68+
agent: ## build agent
69+
go build -o bin/deployment-agent cmd/agent/**
70+
71+
.PHONY: harness
72+
harness: ## build stack run harness
73+
go build -o bin/stack-run-harness cmd/harness/main.go
7074

7175
docker-build: ## build image
7276
docker build -t ${IMG} .
7377

7478
docker-push: ## push image
7579
docker push ${IMG}
7680

81+
.PHONY: docker-build-harness-base
82+
docker-build-harness-base: ## build base docker harness image
83+
docker build \
84+
--build-arg=VERSION="0.0.0-dev" \
85+
-t harness-base \
86+
-f hack/harness/base.Dockerfile \
87+
.
88+
89+
.PHONY: docker-build-harness-terraform
90+
docker-build-harness-terraform: docker-build-harness-base ## build terraform docker harness image
91+
docker build \
92+
--build-arg=HARNESS_IMAGE_TAG="latest" \
93+
-t harness \
94+
-f hack/harness/terraform.Dockerfile \
95+
.
96+
97+
.PHONY: docker-run-harness
98+
docker-run-harness: docker-build-harness-terraform ## build and run terraform docker harness image
99+
docker run \
100+
harness:latest \
101+
--v=5 \
102+
--console-url=${PLURAL_CONSOLE_URL}/ext/gql \
103+
--console-token=${PLURAL_DEPLOY_TOKEN} \
104+
--stack-run-id=${PLURAL_STACK_RUN_ID}
105+
77106
velero-crds:
78107
@curl -L $(VELERO_CHART_URL) --output velero.tgz
79108
@tar zxvf velero.tgz velero/crds
@@ -140,4 +169,4 @@ echo "Downloading $(2)" ;\
140169
GOBIN=$(PROJECT_DIR)/bin go install $(2) ;\
141170
rm -rf $$TMP_DIR ;\
142171
}
143-
endef
172+
endef
File renamed without changes.

cmd/main.go renamed to cmd/agent/main.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,6 @@ import (
55

66
templatesv1 "github.com/open-policy-agent/frameworks/constraint/pkg/apis/templates/v1"
77
constraintstatusv1beta1 "github.com/open-policy-agent/gatekeeper/v3/apis/status/v1beta1"
8-
deploymentsv1alpha1 "github.com/pluralsh/deployment-operator/api/v1alpha1"
9-
"github.com/pluralsh/deployment-operator/internal/controller"
10-
"github.com/pluralsh/deployment-operator/pkg/client"
11-
"github.com/pluralsh/deployment-operator/pkg/log"
128
velerov1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
139
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1410
"k8s.io/apimachinery/pkg/runtime"
@@ -17,6 +13,11 @@ import (
1713
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
1814
ctrl "sigs.k8s.io/controller-runtime"
1915
"sigs.k8s.io/controller-runtime/pkg/healthz"
16+
17+
deploymentsv1alpha1 "github.com/pluralsh/deployment-operator/api/v1alpha1"
18+
"github.com/pluralsh/deployment-operator/internal/controller"
19+
"github.com/pluralsh/deployment-operator/pkg/client"
20+
"github.com/pluralsh/deployment-operator/pkg/log"
2021
)
2122

2223
var (
File renamed without changes.

0 commit comments

Comments
 (0)