Skip to content

Commit

Permalink
Add and fix golangci issues (#41)
Browse files Browse the repository at this point in the history
* Add GoLangCI and fix issues

* fix tests

* Fix release

* Add needed cp label

---------

Co-authored-by: zawachte <zwachtel11@gmail.com>
Co-authored-by: ekarlso <endre.karlson@xait.com>
  • Loading branch information
3 people authored Aug 8, 2023
1 parent 7dc6848 commit 7f5c26d
Show file tree
Hide file tree
Showing 50 changed files with 1,327 additions and 1,923 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/pr.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Pull Request Validation
on:
pull_request:
types: [opened, synchronize, reopened]
workflow_dispatch:

env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

jobs:
validate:
name: "Validate ${{ matrix.target }}"
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
target: ["lint", "test-bootstrap", "test-controlplane"]

steps:
- name: checkout
uses: actions/checkout@v3
with:
fetch-depth: 0

- uses: actions/setup-go@v4
with:
go-version-file: "./go.mod"

- name: ${{ matrix.target }}
run: make ${{ matrix.target }}
142 changes: 120 additions & 22 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -1,42 +1,140 @@
# workflow to release assets as part of the release
name: Upload Release Asset
on:
push:
# Sequence of patterns matched against refs/tags
tags:
- "v*" # Push events to matching v*, i.e. v1.0, v20.15.10

name: release
- "v*"
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
GO_VERSION: '1.20'

jobs:
build:
name: tag release
image:
name: Build and push ${{ matrix.target }} image
runs-on: ubuntu-latest
steps:
- name: Set env
run: echo "RELEASE_TAG=${GITHUB_REF:10}" >> $GITHUB_ENV
strategy:
matrix:
target: [bootsrap, controlplane]

- name: checkout code
steps:
- name: Checkout code
uses: actions/checkout@v3

- uses: actions/setup-go@v4
with:
fetch-depth: 0
go-version: "${{ env.GO_VERSION }}"
cache: true

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ matrix.target }}-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-${{ matrix.target }}-
${{ runner.os }}-buildx-
- name: Docker ${{ matrix.target }} metadata
id: meta
uses: docker/metadata-action@v4
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
images: ${{ env.REGISTRY }}/${{ matrix.target }}-${{ env.IMAGE_NAME }}
flavor: latest=false
tags: type=ref,event=tag

- name: Log in to the Container registry
uses: docker/login-action@v2
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Setup Env
run: |
DOCKER_BUILD_LDFLAGS="$(hack/version.sh)"
echo 'DOCKER_BUILD_LDFLAGS<<EOF' >> $GITHUB_ENV
echo $DOCKER_BUILD_LDFLAGS >> $GITHUB_ENV
echo 'EOF' >> $GITHUB_ENV
- name: Install go
uses: actions/setup-go@v4.0.1
- name: Build and push ${{ matrix.target }} image
uses: docker/build-push-action@v4
with:
go-version: "^1.20"
context: .
push: true
build-args: |
LDFLAGS=${{ env.DOCKER_BUILD_LDFLAGS }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
platforms: linux/amd64,linux/arm64
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max

- name: generate release artifacts
- name: Move cache
# Temp fix
# https://github.com/docker/build-push-action/issues/252
# https://github.com/moby/buildkit/issues/1896
run: |
make release
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache
release:
name: Release
runs-on: ubuntu-latest
needs:
- image
steps:
- uses: actions/checkout@v3

- uses: ./.github/actions/setup-go

- uses: actions/cache@v3
with:
path: hack/tools/bin
key: ${{ runner.os }}-tools-bin-release-${{ hashFiles('Makefile') }}
restore-keys: |
${{ runner.os }}-tools-bin-release-
${{ runner.os }}-tools-bin-
- name: generate release notes
- name: Docker ${{ matrix.target }} metadata
id: meta
uses: docker/metadata-action@v4
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
flavor: latest=false
tags: type=ref,event=tag

- name: manifest
run: make release-${{ matrix.target }}
env:
TAG: ${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.version'] }}

- name: Generate Release Notes
run: |
make release-notes
release_notes=$(gh api repos/{owner}/{repo}/releases/generate-notes -F tag_name=${{ github.ref }} --jq .body)
echo 'RELEASE_NOTES<<EOF' >> $GITHUB_ENV
echo "${release_notes}" >> $GITHUB_ENV
echo 'EOF' >> $GITHUB_ENV
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
OWNER: ${{ github.repository_owner }}
REPO: ${{ github.event.repository.name }}

- name: Release
- name: Create Release
id: create_release
uses: softprops/action-gh-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
draft: true
files: out/*
body_path: _releasenotes/release-notes-${{ env.RELEASE_TAG }}.md
files: out/release/*
body: ${{ env.RELEASE_NOTES }}
draft: false
prerelease: false
156 changes: 156 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
run:
skip-files:
- ".*zz_generated.*\\.go"
- "contrib/.*"
timeout: 5m
issue:
max-same-issues: 0
max-per-linter: 0
issues:
exclude-rules:
- path: "test/e2e/*"
linters:
- gosec
text: "G106:"
- linters:
- revive
text: "dot-imports"
path: ".*test.*"
- linters:
- stylecheck
text: "ST1001"
path: ".*test.*"

linters:
fast: true
disable-all: true
enable:
- asciicheck
- bodyclose
- depguard
- dogsled
- errcheck
- exportloopref
- gci
- goconst
- gocritic
- gocyclo
- godot
- gofmt
- goimports
- goprintffuncname
- gosec
- gosimple
- govet
- importas
- ineffassign
- misspell
- nakedret
- nilerr
- noctx
- nolintlint
- prealloc
- predeclared
- revive
- rowserrcheck
- staticcheck
- stylecheck
- thelper
- typecheck
- unconvert
- unparam
- unused
- whitespace

linters-settings:
depguard:
rules:
main:
allow:
- $gostd
- github.com/go-logr/logr
- github.com/coredns/corefile-migration/migration

- k8s.io/api
- k8s.io/apimachinery/pkg
- k8s.io/apiserver
- k8s.io/client-go
- k8s.io/klog/v2/klogr
- k8s.io/utils/pointer

- github.com/onsi/ginkgo
- github.com/onsi/gomega

- sigs.k8s.io/yaml
- sigs.k8s.io/controller-runtime
- sigs.k8s.io/cluster-api

- github.com/cluster-api-provider-k3s/cluster-api-k3s
gci:
sections:
- standard
- default
- prefix(github.com/cluster-api-provider-k3s/cluster-api-k3s)
gomoddirectives:
# List of allowed `replace` directives.
# Default: []
replace-allow-list:
- sigs.k8s.io/cluster-api
goimports:
# put imports beginning with prefix after 3rd-party packages;
# it's a comma-separated list of prefixes
local-prefixes: github.com/cluster-api-provider-k3s/cluster-api-k3s
godot:
# declarations - for top level declaration comments (default);
# toplevel - for top level comments;
# all - for all comments.
scope: toplevel
exclude:
- '^ \+.*'
- '^ ANCHOR.*'
gosec:
excludes:
- G307 # Deferring unsafe method "Close" on type "\*os.File"
- G108 # Profiling endpoint is automatically exposed on /debug/pprof
importas:
# Do not allow unaliased imports of aliased packages.
# Default: false
no-unaliased: true
alias:
# Kubernetes
- pkg: k8s.io/api/core/v1
alias: corev1
- pkg: k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1
alias: apiextensionsv1
- pkg: k8s.io/apimachinery/pkg/apis/meta/v1
alias: metav1
- pkg: k8s.io/apimachinery/pkg/api/errors
alias: apierrors
- pkg: k8s.io/apimachinery/pkg/util/errors
alias: kerrors
# Controller Runtime
- pkg: sigs.k8s.io/controller-runtime
alias: ctrl
nolintlint:
# Enable to require nolint directives to mention the specific linter being suppressed.
# Default: false
require-specific: true
revive:
rules:
- name: exported
arguments:
- disableStutteringCheck
- name: unused-parameter
disabled: true
staticcheck:
go: "1.20"
stylecheck:
go: "1.20"
tagliatelle:
case:
rules:
# Any struct tag type can be used.
# Support string case: `camel`, `pascal`, `kebab`, `snake`, `goCamel`, `goPascal`, `goKebab`, `goSnake`, `upper`, `lower`, `header`
json: goCamel
unused:
go: "1.20"
Loading

0 comments on commit 7f5c26d

Please sign in to comment.