Skip to content

Commit

Permalink
fix: add mock cluster creation to integration test (#21)
Browse files Browse the repository at this point in the history
* fix: add mock cluster creation to integration test
* fix: add kubebuilder controller implementation as a showcase 
add more sonar coverage
+semver: feature
  • Loading branch information
dnitsch authored Mar 1, 2023
1 parent 2a52682 commit 95dc4d9
Show file tree
Hide file tree
Showing 77 changed files with 4,488 additions and 1,160 deletions.
40 changes: 9 additions & 31 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,8 @@ permissions:
pull-requests: read

jobs:
set-version:
test-pr-ci:
runs-on: ubuntu-latest
container:
image: mcr.microsoft.com/dotnet/sdk:6.0
outputs:
semVer: ${{ steps.gitversion.outputs.semVer }}
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Install GitVersion
uses: gittools/actions/gitversion/[email protected]
with:
versionSpec: '5.x'
- name: Set SemVer Version
uses: gittools/actions/gitversion/[email protected]
id: gitversion

- name: echo VERSIONS
run: |
echo "REVISION -> $GITHUB_SHA"
echo "VERSION -> $GITVERSION_SEMVER"
test:
runs-on: ubuntu-latest
container:
image: golang:1.19-bullseye
needs: set-version
env:
SEMVER: ${{ needs.set-version.outputs.semVer }}
GIT_TAG: ${{ needs.set-version.outputs.semVer }}
Expand All @@ -43,18 +18,21 @@ jobs:
- uses: actions/checkout@v3
with:
fetch-depth: 1
- name: install deps
- name: git prep
run: |
apt update && apt install -y jq git
git config --global --add safe.directory "$GITHUB_WORKSPACE"
git config user.email ${{ github.actor }}[email protected]
git config user.name ${{ github.actor }}
- uses: actions/setup-go@v3
with:
go-version: '1.19.x'
- name: make test
run: |
export DOCKER_HOST=unix:///var/run/docker.sock
make REVISION=$GITHUB_SHA test
- name: Publish Junit style Test Report
uses: mikepenz/action-junit-report@v3
if: always() # always run even if the previous step fails
if: always()
with:
report_paths: '**/.coverage/report-junit.xml'
- name: Analyze with SonarCloud
Expand All @@ -66,5 +44,5 @@ jobs:
with:
args:
-Dsonar.projectVersion=${{ needs.set-version.outputs.semVer }}
-Dsonar.go.tests.reportPaths=/github/workspace/controller/.coverage/report-junit.xml,/github/workspace/seeder/.coverage/report-junit.xml
-Dsonar.go.coverage.reportPaths=/github/workspace/controller/.coverage/out,/github/workspace/seeder/.coverage/out
-Dsonar.go.tests.reportPaths=/github/workspace/controller/.coverage/report-junit.xml,/github/workspace/seeder/.coverage/report-junit.xml,/github/workspace/kubebuilder-controller/.coverage/report-junit.xml
-Dsonar.go.coverage.reportPaths=/github/workspace/controller/.coverage/out,/github/workspace/seeder/.coverage/out,/github/workspace/kubebuilder-controller/.coverage/out
6 changes: 2 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,8 @@ jobs:
with:
args:
-Dsonar.projectVersion=${{ needs.set-version.outputs.semVer }}
-Dsonar.go.tests.reportPaths=/github/workspace/controller/.coverage/report-junit.xml,/github/workspace/seeder/.coverage/report-junit.xml
-Dsonar.go.coverage.reportPaths=/github/workspace/controller/.coverage/out,/github/workspace/seeder/.coverage/out
-Dsonar.go.tests.reportPaths=/github/workspace/controller/.coverage/report-junit.xml,/github/workspace/seeder/.coverage/report-junit.xml,/github/workspace/kubebuilder-controller/.coverage/report-junit.xml
-Dsonar.go.coverage.reportPaths=/github/workspace/controller/.coverage/out,/github/workspace/seeder/.coverage/out,/github/workspace/kubebuilder-controller/.coverage/out

publish-binary:
name: tag repo and publish binary
Expand All @@ -89,8 +89,6 @@ jobs:
make GIT_TAG=$SEMVER REVISION=${{ github.sha }} PAT=${{ secrets.GITHUB_TOKEN }} build_ci
- name: release library
run: |
git config user.email ${{ github.actor }}[email protected]
git config user.name ${{ github.actor }}
make GIT_TAG=$SEMVER REVISION=${{ github.sha }} tag
- name: release cli
run: |
Expand Down
52 changes: 52 additions & 0 deletions .github/workflows/scratchpad._yml_
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
name: CI-ScratchPad-PR

on:
pull_request:
branches: [ master, main ]

jobs:
kind-test-scratch-pad:
runs-on: ubuntu-latest
container:
image: dnitsch/gha-dind:0.0.6
volumes:
- /var/run/docker.sock:/var/run/docker.sock
options: --privileged=true --group-add=docker
env:
GOVCS: false
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 1
- name: DL tools
run: |
curl -LO https://dl.k8s.io/release/v1.26.1/bin/linux/amd64/kubectl
install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
curl -Lo ./kind https://kind.sigs.k8s.io/dl/v0.17.0/kind-linux-amd64
install -o root -g root -m 0755 kind /usr/local/bin/kind
- name: Get KubeConfig
run: |
export KUBECONFIG=$HOME/.kube/config
export KIND_EXPERIMENTAL_DOCKER_NETWORK=${{ job.container.network }}
echo 'kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
networking:
apiServerAddress: "127.0.0.1"
apiServerPort: 6443' > $PWD/kind-config.yml

cat $PWD/kind-config.yml

kind create cluster --config=$PWD/kind-config.yml

echo "get internal"
kind get kubeconfig --internal
echo "get normal"
kind get kubeconfig

kubectl config set-cluster kind-kind --server=https://kind-control-plane:6443

kubectl get pods -n kube-system
kubectl cluster-info
kubectl apply -f ./kubebuilder-controller/config/crd/bases/seeder.dnitsch.net_reststrategies.yaml

kubectl get pods -n kube-system
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

# build dir
dist/
bin
testbin/*
Dockerfile.cross

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
Expand Down
27 changes: 20 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@ tag:
release:
OWNER=$(OWNER) NAME=$(NAME) PAT=$(PAT) VERSION=$(VERSION) . hack/release.sh

docker_release:
docker_build_raw_controller:
cd controller && \
docker build --build-arg REVISION=$(REVISION) --build-arg VERSION=$(VERSION) -t ghcr.io/dnitsch/reststrategy:$(VERSION) . && \
docker push ghcr.io/dnitsch/reststrategy:$(VERSION)
docker build --build-arg REVISION=$(REVISION) --build-arg VERSION=$(VERSION) -t ghcr.io/dnitsch/reststrategy:raw-$(VERSION) .

docker_build_kubebuilder_controller:
cd kubebuilder-controller && \
docker build --build-arg REVISION=$(REVISION) --build-arg VERSION=$(VERSION) -t ghcr.io/dnitsch/reststrategy:kubebuilder-$(VERSION) .

docker_release: docker_build_kubebuilder_controller docker_build_raw_controller
docker push ghcr.io/dnitsch/reststrategy:$(VERSION)

# for local development install all dependencies
# in workspace
Expand All @@ -50,13 +55,18 @@ test_seeder:
gocov convert seeder/.coverage/out | gocov-xml > seeder/.coverage/report-cobertura.xml

test_controller:
go test ./controller/... -v -mod=readonly -race -coverprofile=controller/.coverage/out | go-junit-report > controller/.coverage/report-junit.xml && \
go test ./controller/... -v -mod=readonly -race -timeout 10m0s -coverprofile=controller/.coverage/out | go-junit-report > controller/.coverage/report-junit.xml && \
gocov convert controller/.coverage/out | gocov-xml > controller/.coverage/report-cobertura.xml

test: test_prereq test_seeder test_controller
test_kubebuilder_controller:
go test ./kubebuilder-controller/... -v -mod=readonly -timeout 10m0s -race -coverprofile=kubebuilder-controller/.coverage/out | go-junit-report > kubebuilder-controller/.coverage/report-junit.xml && \
gocov convert kubebuilder-controller/.coverage/out | gocov-xml > kubebuilder-controller/.coverage/report-cobertura.xml

# running in CI on an alpine container without gcc so only running -race on local set up
test: test_prereq test_seeder test_controller test_kubebuilder_controller

test_prereq:
mkdir -p seeder/.coverage controller/.coverage
mkdir -p seeder/.coverage controller/.coverage kubebuilder-controller/.coverage
go install github.com/jstemmer/go-junit-report/v2@latest && \
go install github.com/axw/gocov/gocov@latest && \
go install github.com/AlekSi/gocov-xml@latest
Expand All @@ -69,4 +79,7 @@ coverage_seeder: test_seeder
go tool cover -html=seeder/.coverage/out

coverage_controller: test_controller
go tool cover -html=controller/.coverage/out
go tool cover -html=controller/.coverage/out

coverage_kubebuilder_controller: test_kubebuilder_controller
go tool cover -html=kubebuilder-controller/.coverage/out
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,13 @@ The templating function is provided already with a replaced value: `P4s$w0rd123!
This is a bit of experiment with controller structures and workspaces to see if some re-usable patterns can be gleamed and used in some code generation scaffolding.

At the very least a copy and paste into a new workspace of existing structure and Make tasks -> deleting following `seeder`, `controller/pkg/rstservice`, removing k8sutils should leave you with a fairly re-useable controller pkg which can be exchanged with other top level types coming from custom/new `apis` module.

## Controller generation

Using [kubebuilder](https://book.kubebuilder.io/quick-start.html) can be found in this dir [kubebuilder-controller](./kubebuilder-controller/README.md).

`kubebuilder init --domain dnitsch.net --repo github.com/dnitsch/reststrategy/controller`

`kubebuilder create api --plugins=base.go.kubebuilder.io/v3 --group seeder --version v1alpha1 --kind RestStrategy --controller --namespaced --plural reststrategies --resource --force`

`make manifests`
29 changes: 20 additions & 9 deletions apis/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,23 @@ require (
k8s.io/code-generator v0.26.1
)

require (
github.com/fatih/color v1.14.1 // indirect
github.com/go-logr/zerologr v1.2.3 // indirect
github.com/gobuffalo/flect v0.3.0 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/onsi/ginkgo/v2 v2.6.1 // indirect
github.com/rogpeppe/go-internal v1.9.0 // indirect
github.com/spf13/cobra v1.6.1 // indirect
k8s.io/apiextensions-apiserver v0.26.1 // indirect
)

require (
github.com/a8m/envsubst v1.3.1-0.20221106143949-dffd7176fbc4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dnitsch/simplelog v1.5.1 // indirect
github.com/dnitsch/simplelog v1.7.0 // indirect
github.com/emicklei/go-restful/v3 v3.10.1 // indirect
github.com/evanphx/json-patch v5.6.0+incompatible // indirect
github.com/go-logr/logr v1.2.3 // indirect
Expand All @@ -26,7 +39,6 @@ require (
github.com/google/gofuzz v1.2.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/kr/pretty v0.3.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
Expand All @@ -38,11 +50,11 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/spyzhov/ajson v0.7.2 // indirect
golang.org/x/mod v0.7.0 // indirect
golang.org/x/net v0.5.0 // indirect
golang.org/x/oauth2 v0.4.0 // indirect
golang.org/x/sys v0.4.0 // indirect
golang.org/x/term v0.4.0 // indirect
golang.org/x/text v0.6.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/oauth2 v0.5.0 // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/term v0.5.0 // indirect
golang.org/x/text v0.7.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.5.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
Expand All @@ -55,9 +67,8 @@ require (
k8s.io/klog/v2 v2.90.0 // indirect
k8s.io/kube-openapi v0.0.0-20230123231816-1cb3ae25d79a // indirect
k8s.io/utils v0.0.0-20230115233650-391b47cb4029 // indirect
sigs.k8s.io/controller-tools v0.11.3
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

replace github.com/dnitsch/reststrategy/seeder => ../seeder
Loading

0 comments on commit 95dc4d9

Please sign in to comment.