From b450de91052b4f26afcdaf2335d2d5fbfc8bf184 Mon Sep 17 00:00:00 2001 From: Eugene Bosiakov Date: Sun, 17 Nov 2024 21:54:07 +0200 Subject: [PATCH 1/5] Release workflows --- .github/workflows/build-and-push-image.yml | 67 ++++++++++++++++++++++ .github/workflows/golangci-lint.yml | 42 ++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 .github/workflows/build-and-push-image.yml create mode 100644 .github/workflows/golangci-lint.yml diff --git a/.github/workflows/build-and-push-image.yml b/.github/workflows/build-and-push-image.yml new file mode 100644 index 0000000..b1efec9 --- /dev/null +++ b/.github/workflows/build-and-push-image.yml @@ -0,0 +1,67 @@ +name: Build Image and Publish to Dockerhub + +on: + release: + types: [ published ] + workflow_dispatch: + inputs: + tag: + description: 'Image tag' + required: true + default: 'test' +permissions: + contents: read + +jobs: + image: + name: Build Image from Dockerfile and binaries + runs-on: ubuntu-latest + steps: + # environment + - name: Checkout + uses: actions/checkout@v4 + with: + fetch-depth: '0' + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Get Image Tag Name + run: | + if [ x${{ github.event.inputs.tag }} == x"" ]; then + echo "TAG_NAME=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV + else + echo "TAG_NAME=${{ github.event.inputs.tag }}" >> $GITHUB_ENV + fi + + - name: Login to DockerHub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_PASSWORD }} + + # prepare image tags + - name: Prepare Image Tags + run: | + echo "TAG_MANAGER=akyriako78/rekuberate-io-sleepcycles:${{ env.TAG_NAME }}" >> $GITHUB_ENV + echo "TAG_RUNNER=akyriako78/rekuberate-io-sleepcycles:${{ env.TAG_NAME }}" >> $GITHUB_ENV + + - name: Build and push manager + uses: docker/build-push-action@v5 + with: + context: . + file: ./Dockerfile + platforms: linux/amd64,linux/arm/v7,linux/arm64 + push: true + tags: | + ${{ env.TAG_MANAGER }} + + - name: Build and push runner + uses: docker/build-push-action@v5 + with: + context: . + file: ./Dockerfile.runner + platforms: linux/amd64,linux/arm/v7,linux/arm64 + push: true + tags: | + ${{ env.TAG_RUNNER }} diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml new file mode 100644 index 0000000..6feab47 --- /dev/null +++ b/.github/workflows/golangci-lint.yml @@ -0,0 +1,42 @@ +name: golangci-lint +on: + push: + branches: + - master + - dev + pull_request: +permissions: + contents: read + # Optional: allow read access to pull request. Use with `only-new-issues` option. + pull-requests: read +jobs: + golangci: + name: lint + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 + with: + go-version: '1.23' + cache: false + - name: golangci-lint + uses: golangci/golangci-lint-action@v4 + with: + # Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version + version: v1.61 + + # Optional: golangci-lint command line arguments. + # args: --issues-exit-code=0 + + # Optional: show only new issues if it's a pull request. The default value is `false`. + # only-new-issues: true + + # Optional: if set to true then the all caching functionality will be complete disabled, + # takes precedence over all other caching options. + # skip-cache: true + + # Optional: if set to true then the action don't cache or restore ~/go/pkg. + # skip-pkg-cache: true + + # Optional: if set to true then the action don't cache or restore ~/.cache/go-build. + # skip-build-cache: true \ No newline at end of file From ff54fd6aca8b614b50f9d5546853970142f55749 Mon Sep 17 00:00:00 2001 From: Eugene Bosiakov Date: Mon, 18 Nov 2024 20:43:32 +0200 Subject: [PATCH 2/5] fix go lint --- controllers/sleepcycle_controller.go | 11 +++-------- runners/runner.go | 13 +++++-------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/controllers/sleepcycle_controller.go b/controllers/sleepcycle_controller.go index 61de63d..a639abb 100644 --- a/controllers/sleepcycle_controller.go +++ b/controllers/sleepcycle_controller.go @@ -19,6 +19,9 @@ package controllers import ( "context" "fmt" + "strings" + "time" + "github.com/go-logr/logr" "github.com/hashicorp/go-multierror" corev1alpha1 "github.com/rekuberate-io/sleepcycles/api/v1alpha1" @@ -34,8 +37,6 @@ import ( "sigs.k8s.io/controller-runtime/pkg/event" "sigs.k8s.io/controller-runtime/pkg/log" "sigs.k8s.io/controller-runtime/pkg/predicate" - "strings" - "time" ) const ( @@ -61,12 +62,6 @@ type runtimeObjectReconciler func( sleepcycle *corev1alpha1.SleepCycle, ) (int, int, error) -type runtimeObjectFinalizer func( - ctx context.Context, - req ctrl.Request, - original *corev1alpha1.SleepCycle, -) (ctrl.Result, error) - var ( eventFilters = builder.WithPredicates(predicate.Funcs{ UpdateFunc: func(e event.UpdateEvent) bool { diff --git a/runners/runner.go b/runners/runner.go index 46a72a8..59ac2d5 100644 --- a/runners/runner.go +++ b/runners/runner.go @@ -4,6 +4,11 @@ import ( "context" "flag" "fmt" + "os" + "strconv" + "strings" + "time" + "github.com/go-logr/logr" "github.com/pkg/errors" "go.uber.org/zap/zapcore" @@ -15,12 +20,8 @@ import ( typedv1core "k8s.io/client-go/kubernetes/typed/core/v1" "k8s.io/client-go/rest" "k8s.io/client-go/tools/record" - "os" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/log/zap" - "strconv" - "strings" - "time" ) var ( @@ -171,10 +172,6 @@ func run(ns string, cronjob *batchv1.CronJob, target string, kind string, target func syncReplicas(ctx context.Context, namespace string, cronjob *batchv1.CronJob, currentReplicas int32, targetReplicas int32) error { if currentReplicas != targetReplicas && currentReplicas > 0 { - if targetReplicas != 0 { - targetReplicas = currentReplicas - } - cronjob.Annotations["rekuberate.io/replicas"] = fmt.Sprint(currentReplicas) _, err := clientSet.BatchV1().CronJobs(namespace).Update(ctx, cronjob, metav1.UpdateOptions{}) if err != nil { From e39b6a38b7b25394d851877985332a3ba73912d7 Mon Sep 17 00:00:00 2001 From: Eugene Bosiakov Date: Mon, 18 Nov 2024 20:52:58 +0200 Subject: [PATCH 3/5] advanced go checks --- .github/workflows/golangci-lint.yml | 31 +++++++++++++--------- controllers/sleepcycle_runners_cronjobs.go | 9 ++++--- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 6feab47..4756a50 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -13,30 +13,37 @@ jobs: golangci: name: lint runs-on: ubuntu-latest + timeout-minutes: 5 + strategy: + fail-fast: true steps: - uses: actions/checkout@v4 + - uses: actions/setup-go@v5 with: go-version: '1.23' cache: false + - name: golangci-lint uses: golangci/golangci-lint-action@v4 with: - # Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version version: v1.61 + args: --timeout 5m + + - name: Go Format + run: gofmt -s -w . && git diff --exit-code - # Optional: golangci-lint command line arguments. - # args: --issues-exit-code=0 + - name: Go Vet + run: go vet ./... - # Optional: show only new issues if it's a pull request. The default value is `false`. - # only-new-issues: true + - name: Go Tidy + run: go mod tidy && git diff --exit-code - # Optional: if set to true then the all caching functionality will be complete disabled, - # takes precedence over all other caching options. - # skip-cache: true + - name: Go Mod + run: go mod download - # Optional: if set to true then the action don't cache or restore ~/go/pkg. - # skip-pkg-cache: true + - name: Go Mod Verify + run: go mod verify - # Optional: if set to true then the action don't cache or restore ~/.cache/go-build. - # skip-build-cache: true \ No newline at end of file + - name: Go Test + run: go test -v -count=1 -race -shuffle=on -coverprofile=coverage.txt ./... diff --git a/controllers/sleepcycle_runners_cronjobs.go b/controllers/sleepcycle_runners_cronjobs.go index 1387626..32ec74c 100644 --- a/controllers/sleepcycle_runners_cronjobs.go +++ b/controllers/sleepcycle_runners_cronjobs.go @@ -3,6 +3,9 @@ package controllers import ( "context" "fmt" + "strconv" + "strings" + "github.com/go-logr/logr" corev1alpha1 "github.com/rekuberate-io/sleepcycles/api/v1alpha1" batchv1 "k8s.io/api/batch/v1" @@ -10,8 +13,6 @@ import ( metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ctrl "sigs.k8s.io/controller-runtime" "sigs.k8s.io/controller-runtime/pkg/client" - "strconv" - "strings" ) var ( @@ -74,8 +75,8 @@ func (r *SleepCycleReconciler) createCronJob( } labels := make(map[string]string) - labels[OwnedBy] = fmt.Sprintf("%s", sleepcycle.Name) - labels[Target] = fmt.Sprintf("%s", targetMeta.Name) + labels[OwnedBy] = sleepcycle.Name + labels[Target] = targetMeta.Name labels[TargetKind] = targetKind annotations := make(map[string]string) From 66c053f190b3ee6f011ae8784e5fa36d562a2e6f Mon Sep 17 00:00:00 2001 From: Eugene Bosiakov Date: Mon, 18 Nov 2024 21:00:25 +0200 Subject: [PATCH 4/5] disable test + chart linting --- .github/workflows/golangci-lint.yml | 5 +-- .github/workflows/lint-helm-test.yaml | 45 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 .github/workflows/lint-helm-test.yaml diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 4756a50..96d67ab 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -45,5 +45,6 @@ jobs: - name: Go Mod Verify run: go mod verify - - name: Go Test - run: go test -v -count=1 -race -shuffle=on -coverprofile=coverage.txt ./... + # TODO: enable tests + # - name: Go Test + # run: go test -v -count=1 -race -shuffle=on -coverprofile=coverage.txt ./... diff --git a/.github/workflows/lint-helm-test.yaml b/.github/workflows/lint-helm-test.yaml new file mode 100644 index 0000000..fcf4f05 --- /dev/null +++ b/.github/workflows/lint-helm-test.yaml @@ -0,0 +1,45 @@ +name: Lint and Test Charts + +on: pull_request + +jobs: + lint-test: + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + + - name: Set up Helm + uses: azure/setup-helm@v4.2.0 + with: + version: v3.14.4 + + - uses: actions/setup-python@v5 + with: + python-version: '3.x' + check-latest: true + + - name: Set up chart-testing + uses: helm/chart-testing-action@v2.7.0 + + - name: Run chart-testing (list-changed) + id: list-changed + run: | + changed=$(ct list-changed --target-branch ${{ github.event.repository.default_branch }}) + if [[ -n "$changed" ]]; then + echo "changed=true" >> "$GITHUB_OUTPUT" + fi + + - name: Run chart-testing (lint) + if: steps.list-changed.outputs.changed == 'true' + run: ct lint --target-branch ${{ github.event.repository.default_branch }} + + - name: Create kind cluster + if: steps.list-changed.outputs.changed == 'true' + uses: helm/kind-action@v1.10.0 + + - name: Run chart-testing (install) + if: steps.list-changed.outputs.changed == 'true' + run: ct install --target-branch ${{ github.event.repository.default_branch }} From a5fbb905ff57687771b41d944e6156b84520b748 Mon Sep 17 00:00:00 2001 From: Eugene Bosiakov Date: Mon, 18 Nov 2024 21:03:39 +0200 Subject: [PATCH 5/5] wat --- .github/workflows/lint-helm-test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/lint-helm-test.yaml b/.github/workflows/lint-helm-test.yaml index fcf4f05..700984d 100644 --- a/.github/workflows/lint-helm-test.yaml +++ b/.github/workflows/lint-helm-test.yaml @@ -22,7 +22,7 @@ jobs: check-latest: true - name: Set up chart-testing - uses: helm/chart-testing-action@v2.7.0 + uses: helm/chart-testing-action@v2.6.1 - name: Run chart-testing (list-changed) id: list-changed