From 86c2c9f745d82b1f534d844fa082c8a6a91b98d9 Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Wed, 8 Apr 2020 12:38:47 +0300 Subject: [PATCH 1/3] Implement CI with GitHub Actions --- .github/actions/kubebuilder/Dockerfile | 6 +++ .github/actions/kubebuilder/action.yml | 9 +++++ .github/actions/kubebuilder/entrypoint.sh | 12 ++++++ .github/actions/kustomize/Dockerfile | 6 +++ .github/actions/kustomize/action.yml | 9 +++++ .github/actions/kustomize/entrypoint.sh | 12 ++++++ .github/workflows/e2e.yaml | 46 +++++++++++++++++++++++ .gitignore | 3 +- Makefile | 8 +++- config/manager/deployment.yaml | 3 +- config/manager/kustomization.yaml | 3 +- hack/dev-deploy.sh | 24 ++++++++++++ 12 files changed, 136 insertions(+), 5 deletions(-) create mode 100644 .github/actions/kubebuilder/Dockerfile create mode 100644 .github/actions/kubebuilder/action.yml create mode 100644 .github/actions/kubebuilder/entrypoint.sh create mode 100644 .github/actions/kustomize/Dockerfile create mode 100644 .github/actions/kustomize/action.yml create mode 100644 .github/actions/kustomize/entrypoint.sh create mode 100644 .github/workflows/e2e.yaml create mode 100755 hack/dev-deploy.sh diff --git a/.github/actions/kubebuilder/Dockerfile b/.github/actions/kubebuilder/Dockerfile new file mode 100644 index 00000000..2ebd6334 --- /dev/null +++ b/.github/actions/kubebuilder/Dockerfile @@ -0,0 +1,6 @@ +FROM giantswarm/tiny-tools + +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] diff --git a/.github/actions/kubebuilder/action.yml b/.github/actions/kubebuilder/action.yml new file mode 100644 index 00000000..b582dbbd --- /dev/null +++ b/.github/actions/kubebuilder/action.yml @@ -0,0 +1,9 @@ +name: 'kubebuilder' +description: 'A GitHub Action to run kubebuilder commands' +author: 'Stefan Prodan' +branding: + icon: 'command' + color: 'blue' +runs: + using: 'docker' + image: 'Dockerfile' diff --git a/.github/actions/kubebuilder/entrypoint.sh b/.github/actions/kubebuilder/entrypoint.sh new file mode 100644 index 00000000..f7f2dad4 --- /dev/null +++ b/.github/actions/kubebuilder/entrypoint.sh @@ -0,0 +1,12 @@ +#!/bin/sh -l + +VERSION=2.3.1 + +curl -sL https://go.kubebuilder.io/dl/${VERSION}/linux/amd64 | tar -xz -C /tmp/ + +mkdir -p $GITHUB_WORKSPACE/kubebuilder +mv /tmp/kubebuilder_${VERSION}_linux_amd64/* $GITHUB_WORKSPACE/kubebuilder/ +ls -lh $GITHUB_WORKSPACE/kubebuilder/bin + +echo "::add-path::$GITHUB_WORKSPACE/kubebuilder/bin" +echo "::add-path::$RUNNER_WORKSPACE/$(basename $GITHUB_REPOSITORY)/kubebuilder/bin" diff --git a/.github/actions/kustomize/Dockerfile b/.github/actions/kustomize/Dockerfile new file mode 100644 index 00000000..2ebd6334 --- /dev/null +++ b/.github/actions/kustomize/Dockerfile @@ -0,0 +1,6 @@ +FROM giantswarm/tiny-tools + +COPY entrypoint.sh /entrypoint.sh +RUN chmod +x /entrypoint.sh + +ENTRYPOINT ["/entrypoint.sh"] diff --git a/.github/actions/kustomize/action.yml b/.github/actions/kustomize/action.yml new file mode 100644 index 00000000..bd53d1d7 --- /dev/null +++ b/.github/actions/kustomize/action.yml @@ -0,0 +1,9 @@ +name: 'kustomize' +description: 'A GitHub Action to run kustomize commands' +author: 'Stefan Prodan' +branding: + icon: 'command' + color: 'blue' +runs: + using: 'docker' + image: 'Dockerfile' diff --git a/.github/actions/kustomize/entrypoint.sh b/.github/actions/kustomize/entrypoint.sh new file mode 100644 index 00000000..9ca798a5 --- /dev/null +++ b/.github/actions/kustomize/entrypoint.sh @@ -0,0 +1,12 @@ +#!/bin/sh -l + +VERSION=3.1.0 +curl -sfLo kustomize https://github.com/kubernetes-sigs/kustomize/releases/download/v${VERSION}/kustomize_${VERSION}_linux_amd64 + +mkdir -p $GITHUB_WORKSPACE/bin +cp ./kustomize $GITHUB_WORKSPACE/bin +chmod +x $GITHUB_WORKSPACE/bin/kustomize +ls -lh $GITHUB_WORKSPACE/bin + +echo "::add-path::$GITHUB_WORKSPACE/bin" +echo "::add-path::$RUNNER_WORKSPACE/$(basename $GITHUB_REPOSITORY)/bin" diff --git a/.github/workflows/e2e.yaml b/.github/workflows/e2e.yaml new file mode 100644 index 00000000..e4f033ff --- /dev/null +++ b/.github/workflows/e2e.yaml @@ -0,0 +1,46 @@ +name: e2e + +on: + pull_request: + push: + branches: + - master + +jobs: + kind: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v2 + - name: Restore Go cache + uses: actions/cache@v1 + with: + path: ~/go/pkg/mod + key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go- + - name: Setup Go + uses: actions/setup-go@v2-beta + with: + go-version: 1.13.x + - name: Setup Kubernetes + uses: engineerd/setup-kind@v0.3.0 + - name: Setup Kustomize + uses: ./.github/actions/kustomize + - name: Setup Kubebuilder + uses: ./.github/actions/kubebuilder + - name: Run tests and build image + run: make docker-build IMG=test/source-controller:latest + env: + KUBEBUILDER_ASSETS: ${{ github.workspace }}/kubebuilder/bin + - name: Load test image + run: kind load docker-image test/source-controller:latest + - name: Deploy controller + run: make dev-deploy IMG=test/source-controller:latest + env: + KUBEBUILDER_ASSETS: ${{ github.workspace }}/kubebuilder/bin + - name: Debug failure + if: failure() + run: | + kubectl -n sourcer-system get all + kubectl -n sourcer-system logs deploy/sourcer-controller diff --git a/.gitignore b/.gitignore index 139ae323..6590b56f 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,5 @@ # Dependency directories (remove the comment below to include it) # vendor/ -bin/ \ No newline at end of file +bin/ +tmp/ \ No newline at end of file diff --git a/Makefile b/Makefile index 32d5aa22..fd97714e 100644 --- a/Makefile +++ b/Makefile @@ -35,9 +35,13 @@ uninstall: manifests # Deploy controller in the configured Kubernetes cluster in ~/.kube/config deploy: manifests - cd config/manager && kustomize edit set image controller=${IMG} + cd config/manager && kustomize edit set image fluxcd/sourcer=${IMG} kustomize build config/default | kubectl apply -f - +# Deploy controller dev image in the configured Kubernetes cluster in ~/.kube/config +dev-deploy: manifests + ./hack/dev-deploy.sh $(IMG) + # Generate manifests e.g. CRD, RBAC etc. manifests: controller-gen $(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role paths="./..." output:crd:artifacts:config=config/crd/bases @@ -78,3 +82,5 @@ CONTROLLER_GEN=$(GOBIN)/controller-gen else CONTROLLER_GEN=$(shell which controller-gen) endif + + diff --git a/config/manager/deployment.yaml b/config/manager/deployment.yaml index a021949f..4722eba7 100644 --- a/config/manager/deployment.yaml +++ b/config/manager/deployment.yaml @@ -21,7 +21,8 @@ spec: terminationGracePeriodSeconds: 10 containers: - name: manager - image: controller + image: fluxcd/sourcer + imagePullPolicy: IfNotPresent ports: - containerPort: 8080 name: http diff --git a/config/manager/kustomization.yaml b/config/manager/kustomization.yaml index 20f92b25..fee50a12 100644 --- a/config/manager/kustomization.yaml +++ b/config/manager/kustomization.yaml @@ -5,6 +5,5 @@ resources: - service.yaml - deployment.yaml images: -- name: controller - newName: fluxcd/sourcer +- name: fluxcd/sourcer newTag: 0.0.1-alpha.1 diff --git a/hack/dev-deploy.sh b/hack/dev-deploy.sh new file mode 100755 index 00000000..c9360a15 --- /dev/null +++ b/hack/dev-deploy.sh @@ -0,0 +1,24 @@ +#!/usr/bin/env bash + +set -o errexit + +IMG=$1 +TMP_DIR="$(git rev-parse --show-toplevel)/tmp" + +mkdir -p ${TMP_DIR} + +cat << EOF | tee ${TMP_DIR}/kustomization.yaml +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +namespace: sourcer-system +namePrefix: sourcer- +bases: +- ../config/crd +- ../config/rbac +- ../config/manager +EOF + +cd ${TMP_DIR} && kustomize edit set image fluxcd/sourcer=${IMG} +kustomize build ${TMP_DIR} | kubectl apply -f - +rm -rf ${TMP_DIR} +kubectl -n sourcer-system rollout status deploy/sourcer-controller --timeout=1m From 065825c12d174c44d23b668e252d41bfc788c9e4 Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Wed, 8 Apr 2020 14:26:07 +0300 Subject: [PATCH 2/3] Add smoke tests to CI --- .github/workflows/e2e.yaml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.github/workflows/e2e.yaml b/.github/workflows/e2e.yaml index e4f033ff..8cf84ba6 100644 --- a/.github/workflows/e2e.yaml +++ b/.github/workflows/e2e.yaml @@ -39,8 +39,15 @@ jobs: run: make dev-deploy IMG=test/source-controller:latest env: KUBEBUILDER_ASSETS: ${{ github.workspace }}/kubebuilder/bin + - name: Run smoke tests + run: | + kubectl apply -f ./config/samples + kubectl wait gitrepository/podinfo --for=condition=ready --timeout=1m + kubectl wait helmrepository/podinfo --for=condition=ready --timeout=1m - name: Debug failure if: failure() run: | + kubectl get gitrepositories -oyaml + kubectl get helmrepositories -oyaml kubectl -n sourcer-system get all kubectl -n sourcer-system logs deploy/sourcer-controller From d4b9e1780081c909e19a440849fe905f7f0c3d7d Mon Sep 17 00:00:00 2001 From: stefanprodan Date: Wed, 8 Apr 2020 15:14:32 +0300 Subject: [PATCH 3/3] Move dev deploy script to Makefile --- .github/workflows/e2e.yaml | 2 ++ .gitignore | 1 - Makefile | 10 +++++----- hack/dev-deploy.sh | 24 ------------------------ 4 files changed, 7 insertions(+), 30 deletions(-) delete mode 100755 hack/dev-deploy.sh diff --git a/.github/workflows/e2e.yaml b/.github/workflows/e2e.yaml index 8cf84ba6..3ef2c0d4 100644 --- a/.github/workflows/e2e.yaml +++ b/.github/workflows/e2e.yaml @@ -42,8 +42,10 @@ jobs: - name: Run smoke tests run: | kubectl apply -f ./config/samples + kubectl -n sourcer-system rollout status deploy/sourcer-controller --timeout=1m kubectl wait gitrepository/podinfo --for=condition=ready --timeout=1m kubectl wait helmrepository/podinfo --for=condition=ready --timeout=1m + kubectl -n sourcer-system logs deploy/sourcer-controller - name: Debug failure if: failure() run: | diff --git a/.gitignore b/.gitignore index 6590b56f..57c413ff 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,3 @@ # Dependency directories (remove the comment below to include it) # vendor/ bin/ -tmp/ \ No newline at end of file diff --git a/Makefile b/Makefile index fd97714e..efd4ac40 100644 --- a/Makefile +++ b/Makefile @@ -40,7 +40,10 @@ deploy: manifests # Deploy controller dev image in the configured Kubernetes cluster in ~/.kube/config dev-deploy: manifests - ./hack/dev-deploy.sh $(IMG) + mkdir -p config/dev && cp config/default/* config/dev + cd config/dev && kustomize edit set image fluxcd/sourcer=${IMG} + kustomize build config/dev | kubectl apply -f - + rm -rf config/dev # Generate manifests e.g. CRD, RBAC etc. manifests: controller-gen @@ -66,8 +69,7 @@ docker-build: test docker-push: docker push ${IMG} -# find or download controller-gen -# download controller-gen if necessary +# Find or download controller-gen controller-gen: ifeq (, $(shell which controller-gen)) @{ \ @@ -82,5 +84,3 @@ CONTROLLER_GEN=$(GOBIN)/controller-gen else CONTROLLER_GEN=$(shell which controller-gen) endif - - diff --git a/hack/dev-deploy.sh b/hack/dev-deploy.sh deleted file mode 100755 index c9360a15..00000000 --- a/hack/dev-deploy.sh +++ /dev/null @@ -1,24 +0,0 @@ -#!/usr/bin/env bash - -set -o errexit - -IMG=$1 -TMP_DIR="$(git rev-parse --show-toplevel)/tmp" - -mkdir -p ${TMP_DIR} - -cat << EOF | tee ${TMP_DIR}/kustomization.yaml -apiVersion: kustomize.config.k8s.io/v1beta1 -kind: Kustomization -namespace: sourcer-system -namePrefix: sourcer- -bases: -- ../config/crd -- ../config/rbac -- ../config/manager -EOF - -cd ${TMP_DIR} && kustomize edit set image fluxcd/sourcer=${IMG} -kustomize build ${TMP_DIR} | kubectl apply -f - -rm -rf ${TMP_DIR} -kubectl -n sourcer-system rollout status deploy/sourcer-controller --timeout=1m