diff --git a/.cursor/rules/zz_generated.base-llm-rules.mdc b/.cursor/rules/zz_generated.base-llm-rules.mdc deleted file mode 100644 index cc304936..00000000 --- a/.cursor/rules/zz_generated.base-llm-rules.mdc +++ /dev/null @@ -1,61 +0,0 @@ ---- -description: Instructions for AI/LLM assistants -alwaysApply: true ---- - -# Instructions for AI/LLM assistants - -You are an AI assistant acting as an expert software developer and platform engineer working on Giant Swarm platform components. Your task is to act as a pair programmer and help others working in this codebase to keep the code delightful to work with. This includes ensuring that the code adheres to Giant Swarm's quality standards, keeping the project well-architected and organized, and maintaining supporting documentation, diagrams, and rules for other AI assistants. - -# Persona: Senior Giant Swarm Platform Engineer - -- **Technical Depth**: You are a domain expert in Go (formerly, golang), Helm, Kubernetes APIs and development, software design patterns, software architecture, Go application security, software testing, and software performance optimization, -- **Problem-Solver**: You approach issues methodically, prioritizing safety and stability. You first investigate deeply with the tools provided to you, before suggesting changes. You find and fix the root cause, not the symptoms. -- **Clear Communicator**: You explain complex topics clearly and provide actionable steps. -- **Collaborative**: You guide users, suggest diagnostic paths, and help them think through problems. -- **Best Practices**: You adhere to Giant Swarm operational and technical standards. - -# Reviewer Guidelines - -## Core Behaviors - -- Unless directed by the user, never use or recommend external linters, code analysis, or other tooling which isn't already recommended in Giant Swarm agent rules or style guides. -- Always adhere to the central coding guidelines and best practices maintained at: @https://github.com/giantswarm/fmt/ -- Prioritize readability, maintainability, and security. -- Write comprehensive tests and documentation. -- If documentation is available in the `docs` folder, keep this up-to-date when changing code. -- Maintain the main README.md file for correctness. -- If a changelog is available as CHANGELOG.md, add your changes to it. - -## Release Management - -- Follow the changelog and release guidelines from @https://github.com/giantswarm/fmt/tree/main/releases -- Use semantic versioning and conventional commits - - -## Language-Specific Guidelines - -Additional language-specific rules can be found in the general style guide and in the other rules files in this repository. - - -### Go Development - -- Go code must always adhere to the Go language-specific development guidelines and patterns rules in this repository. - -### Go Application Security - -- Ensure all Go dependencies are up to date. -- Follow best security practices for Go applications. - - ---- - -For detailed guidelines and examples, always refer to: @https://github.com/giantswarm/fmt/ - - - diff --git a/.cursor/rules/zz_generated.go-llm-rules.mdc b/.cursor/rules/zz_generated.go-llm-rules.mdc deleted file mode 100644 index cc328416..00000000 --- a/.cursor/rules/zz_generated.go-llm-rules.mdc +++ /dev/null @@ -1,35 +0,0 @@ ---- -description: Go language-specific development guidelines and patterns -globs: **/*.go,go.mod,go.sum ---- - -These guidelines apply to Go code and supplement any other general development instructions or workflows. - -# Go Code Guidelines - -- All Go code is expected to adhere to the rules outlined in the Giant Swarm style guide located at @https://github.com/giantswarm/fmt/tree/main/go . -- Always fetch and fully understand the style guide before reviewing any code. -- The Giant Swarm style guide linked above must always be considered definitive. ALL rules and provisions contained in the style guide MUST be met. -- When applying the style guide, always list all instances of a particular issue, grouped by the finding type. - -## Formatting - -CI will perform the following check for proper code formatting and sorting/grouping of imports: - -```bash -go install golang.org/x/tools/cmd/goimports@latest && \ -if [[ -n $(goimports -local github.com/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME} -l .) ]]; -then - goimports -local github.com/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME} -d . && exit 1; -fi -``` - -When creating or modifying Go files, perform the according check and mitigate any issues before committing. - - - diff --git a/.github/workflows/pre_commit_go.yaml b/.github/workflows/pre_commit_go.yaml index 6916278e..efae54d5 100644 --- a/.github/workflows/pre_commit_go.yaml +++ b/.github/workflows/pre_commit_go.yaml @@ -8,18 +8,34 @@ on: push: branches: [main] +permissions: {} + jobs: pre-commit: runs-on: ubuntu-24.04 + permissions: + contents: read steps: - name: Check out code uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2 - name: Set up Python environment uses: actions/setup-python@42375524e23c412d93fb67b49958b491fce71c38 # v5.4.0 - name: Set up Go environment + id: setup-go uses: actions/setup-go@f111f3307d8850f501ac008e886eec1fd1932a34 # v5.3.0 with: go-version: "1.27" + cache: false + - name: Cache Go modules, Go builds and golangci-lint + uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0 + with: + path: | + ~/go/pkg/mod + ~/.cache/go-build + ~/.cache/golangci-lint + key: pre-commit-go-${{ runner.os }}-${{ steps.setup-go.outputs.go-version }}-${{ hashFiles('**/go.sum') }} + restore-keys: | + pre-commit-go-${{ runner.os }}-${{ steps.setup-go.outputs.go-version }}- - name: Install goimports run: | go install golang.org/x/tools/cmd/goimports@v0.42.0 diff --git a/.github/workflows/zz_generated.add-team-labels.yaml b/.github/workflows/zz_generated.add-team-labels.yaml index 5abac1b9..750ff9bd 100644 --- a/.github/workflows/zz_generated.add-team-labels.yaml +++ b/.github/workflows/zz_generated.add-team-labels.yaml @@ -4,6 +4,10 @@ on: issues: types: [assigned] +# GITHUB_TOKEN gets no permissions by default. The workflow uses the +# project-automation App's token. +permissions: {} + jobs: build_user_list: name: Get yaml config of GS users @@ -15,7 +19,7 @@ jobs: # single-file permission on the two mapping files, nothing else there. - name: Mint App token for the user mapping id: app-token - uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 + uses: actions/create-github-app-token@f418cff399f7aef4c2f0ad47e25ca7a6abbdd6fc # v3.4.0 with: client-id: ${{ secrets.PROJECT_AUTOMATION_CLIENT_ID }} private-key: ${{ secrets.PROJECT_AUTOMATION_PRIVATE_KEY }} @@ -66,7 +70,7 @@ jobs: - name: Mint App token for the label id: app-token if: ${{ env.LABEL != '' && env.LABEL != 'null' && env.LABEL != null }} - uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 + uses: actions/create-github-app-token@f418cff399f7aef4c2f0ad47e25ca7a6abbdd6fc # v3.4.0 with: client-id: ${{ secrets.PROJECT_AUTOMATION_CLIENT_ID }} private-key: ${{ secrets.PROJECT_AUTOMATION_PRIVATE_KEY }} diff --git a/.github/workflows/zz_generated.add-to-project-board.yaml b/.github/workflows/zz_generated.add-to-project-board.yaml index 3a974490..100c6ad8 100644 --- a/.github/workflows/zz_generated.add-to-project-board.yaml +++ b/.github/workflows/zz_generated.add-to-project-board.yaml @@ -7,6 +7,10 @@ on: - assigned - labeled +# GITHUB_TOKEN gets no permissions by default. The workflow uses the +# project-automation App's token. +permissions: {} + jobs: build_user_list: name: Get yaml config of GS users @@ -19,7 +23,7 @@ jobs: # single-file permission on the two mapping files, nothing else there. - name: Mint App token for the mappings id: app-token - uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 + uses: actions/create-github-app-token@f418cff399f7aef4c2f0ad47e25ca7a6abbdd6fc # v3.4.0 with: client-id: ${{ secrets.PROJECT_AUTOMATION_CLIENT_ID }} private-key: ${{ secrets.PROJECT_AUTOMATION_PRIVATE_KEY }} @@ -79,7 +83,7 @@ jobs: - name: Mint App token for the board id: app-token if: ${{ env.BOARD != 'null' && env.BOARD != '' && env.BOARD != null }} - uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 + uses: actions/create-github-app-token@f418cff399f7aef4c2f0ad47e25ca7a6abbdd6fc # v3.4.0 with: client-id: ${{ secrets.PROJECT_AUTOMATION_CLIENT_ID }} private-key: ${{ secrets.PROJECT_AUTOMATION_PRIVATE_KEY }} @@ -87,7 +91,7 @@ jobs: permission-organization-projects: write - name: Add issue to personal board if: ${{ env.BOARD != 'null' && env.BOARD != '' && env.BOARD != null }} - uses: actions/add-to-project@5afcf98fcd03f1c2f92c3c83f58ae24323cc57fd # v2.0.0 + uses: actions/add-to-project@f2c1f1fa649a891be84948ea89b5dfba1526ef05 # v2.0.1 with: project-url: ${{ env.BOARD }} github-token: ${{ steps.app-token.outputs.token }} @@ -114,39 +118,133 @@ jobs: - name: Mint App token for the board id: app-token if: ${{ env.BOARD != 'null' && env.BOARD != '' && env.BOARD != null }} - uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 + uses: actions/create-github-app-token@f418cff399f7aef4c2f0ad47e25ca7a6abbdd6fc # v3.4.0 with: client-id: ${{ secrets.PROJECT_AUTOMATION_CLIENT_ID }} private-key: ${{ secrets.PROJECT_AUTOMATION_PRIVATE_KEY }} permission-issues: read permission-organization-projects: write - name: Add issue to team board + id: add-to-board if: ${{ env.BOARD != 'null' && env.BOARD != '' && env.BOARD != null }} - uses: actions/add-to-project@5afcf98fcd03f1c2f92c3c83f58ae24323cc57fd # v2.0.0 + uses: actions/add-to-project@f2c1f1fa649a891be84948ea89b5dfba1526ef05 # v2.0.1 with: project-url: ${{ env.BOARD }} github-token: ${{ steps.app-token.outputs.token }} + # The roadmap board's Team field is a single select whose options carry + # emoji ("Honey Badger 🦡"); a label matches the option with the same + # letters, so team/honeybadger and team/honey-badger both match. + - name: Set the roadmap Team field from the team label + if: >- + steps.add-to-board.outputs.itemId != '' && + env.BOARD == 'https://github.com/orgs/giantswarm/projects/273' && + startsWith(github.event.label.name, 'team/') && + github.event.label.name != 'team/null' + env: + GH_TOKEN: ${{ steps.app-token.outputs.token }} + ITEM_ID: ${{ steps.add-to-board.outputs.itemId }} + LABEL: ${{ github.event.label.name }} + run: | + set -euo pipefail + team=$(printf '%s' "${LABEL#team/}" | tr -cd '[:alpha:]' | tr '[:upper:]' '[:lower:]') + read -r project field option < <(gh api graphql -F number=273 -f query=' + query($number: Int!) { + organization(login: "giantswarm") { + projectV2(number: $number) { + id + field(name: "Team") { ... on ProjectV2SingleSelectField { id options { id name } } } + } + } + }' | jq -r --arg team "$team" '.data.organization.projectV2 as $p + | ($p.field.options[] | select((.name | ascii_downcase | gsub("[^a-z]"; "")) == $team) | .id) as $o + | [$p.id, $p.field.id, $o] | @tsv') || true + if [ -z "${option:-}" ]; then + echo "::warning::No roadmap Team option matches ${LABEL}" + exit 0 + fi + gh api graphql -f project="$project" -f item="$ITEM_ID" -f field="$field" -f option="$option" -f query=' + mutation($project: ID!, $item: ID!, $field: ID!, $option: String!) { + updateProjectV2ItemFieldValue(input: {projectId: $project, itemId: $item, fieldId: $field, value: {singleSelectOptionId: $option}}) { + projectV2Item { id } + } + }' > /dev/null + echo "Team set from ${LABEL}" + + # The repository's owning team is the single team of the CODEOWNERS default + # rule, which align-files writes from the team file that declares it. Its + # label, added as the App, fires `labeled`, and add_to_team_board puts the + # issue on the team's board. An issue opened with a team label keeps it. + add_owner_team_label: + name: Add the owning team's label to a new issue + runs-on: ubuntu-latest + if: >- + github.event.action == 'opened' && + !contains(join(github.event.issue.labels.*.name, ' '), 'team/') + permissions: + contents: read + outputs: + team: ${{ steps.owner.outputs.team }} + steps: + - name: Find the owning team in CODEOWNERS + id: owner + env: + GH_TOKEN: ${{ github.token }} + run: | + set -euo pipefail + codeowners="" + for path in .github/CODEOWNERS CODEOWNERS docs/CODEOWNERS; do + if codeowners=$(gh api -H "Accept: application/vnd.github.raw" "repos/${GITHUB_REPOSITORY}/contents/${path}" 2>/dev/null); then + break + fi + codeowners="" + done + # The last matching rule wins, as GitHub reads CODEOWNERS. + rule=$(printf '%s\n' "$codeowners" | sed 's/#.*//' | awk '$1 == "*"' | tail -n 1) + teams=$(printf '%s\n' "$rule" | grep -oE '@giantswarm/team-[a-z0-9-]+' | sed 's|@giantswarm/team-||' | sort -u || true) + if [ "$(printf '%s' "$teams" | grep -c .)" = 1 ]; then + echo "Owning team: ${teams}" + echo "team=${teams}" >> "$GITHUB_OUTPUT" + else + echo "No single owning team in CODEOWNERS" + fi + - name: Mint App token for the label + id: app-token + if: steps.owner.outputs.team != '' + uses: actions/create-github-app-token@f418cff399f7aef4c2f0ad47e25ca7a6abbdd6fc # v3.4.0 + with: + client-id: ${{ secrets.PROJECT_AUTOMATION_CLIENT_ID }} + private-key: ${{ secrets.PROJECT_AUTOMATION_PRIVATE_KEY }} + permission-issues: write + - name: Apply the owning team's label + if: steps.owner.outputs.team != '' + env: + GH_TOKEN: ${{ steps.app-token.outputs.token }} + LABEL: team/${{ steps.owner.outputs.team }} + ISSUE: ${{ github.event.issue.number }} + run: gh api "repos/${GITHUB_REPOSITORY}/issues/${ISSUE}/labels" -f "labels[]=${LABEL}" > /dev/null - # An issue opened with a label or an assignee also fires `labeled` or - # `assigned`, which add it above; only a bare one is added here. + # A repository without a single owning team: a bare issue still reaches the + # roadmap board's inbox. An issue opened with a label or an assignee also + # fires `labeled` or `assigned`, which add it above. add_unlabelled_to_roadmap_board: name: Add unlabelled, unassigned issue to roadmap board runs-on: ubuntu-latest + needs: add_owner_team_label if: >- - github.event.action == 'opened' && + needs.add_owner_team_label.outputs.team == '' && join(github.event.issue.labels.*.name, '') == '' && join(github.event.issue.assignees.*.login, '') == '' steps: - name: Mint App token for the board id: app-token - uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0 + uses: actions/create-github-app-token@f418cff399f7aef4c2f0ad47e25ca7a6abbdd6fc # v3.4.0 with: client-id: ${{ secrets.PROJECT_AUTOMATION_CLIENT_ID }} private-key: ${{ secrets.PROJECT_AUTOMATION_PRIVATE_KEY }} permission-issues: read permission-organization-projects: write - name: Add issue to roadmap board - uses: actions/add-to-project@5afcf98fcd03f1c2f92c3c83f58ae24323cc57fd # v2.0.0 + uses: actions/add-to-project@f2c1f1fa649a891be84948ea89b5dfba1526ef05 # v2.0.1 with: project-url: https://github.com/orgs/giantswarm/projects/273 github-token: ${{ steps.app-token.outputs.token }} diff --git a/.github/workflows/zz_generated.nested_go_modules.yaml b/.github/workflows/zz_generated.nested_go_modules.yaml new file mode 100644 index 00000000..9f169fbd --- /dev/null +++ b/.github/workflows/zz_generated.nested_go_modules.yaml @@ -0,0 +1,145 @@ +# DO NOT EDIT. Generated with: +# +# devctl +# +# https://github.com/giantswarm/devctl/blob/7dc11ced4fb89a44c36dfe176401b7506769d4c0/pkg/gen/input/workflows/internal/file/nested_go_modules.yaml.template +# + +name: Nested Go modules + +on: + pull_request: {} + merge_group: {} + push: + branches: ['main'] + workflow_dispatch: + inputs: + module: + description: 'Tracked nested module directory, for example k8s' + type: string + required: true + version: + description: 'Module version, for example v0.1.0' + type: string + required: true + +permissions: {} + +concurrency: + group: nested-go-modules-${{ github.event.pull_request.number || github.ref }} + cancel-in-progress: ${{ github.event_name == 'pull_request' }} + +jobs: + modules: + runs-on: ubuntu-26.04 + permissions: + contents: read + outputs: + paths: ${{ steps.discover.outputs.paths }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - id: discover + shell: bash + run: | + set -euo pipefail + paths=() + while IFS= read -r -d '' file; do + directory=${file%/go.mod} + [[ "$directory" =~ ^[a-zA-Z0-9_-]+(/[a-zA-Z0-9_-]+)*$ ]] || continue + case "/$directory/" in + */testdata/*|*/vendor/*|*/node_modules/*) continue ;; + esac + [[ -f "$file" && ! -L "$file" ]] || continue + paths+=("$directory") + done < <(git ls-files -z -- '*/go.mod') + printf 'paths=%s\n' "$(printf '%s\0' "${paths[@]}" | jq -Rsc 'split("\u0000") | map(select(length > 0)) | sort')" >> "$GITHUB_OUTPUT" + + test: + needs: modules + if: needs.modules.outputs.paths != '[]' + runs-on: ubuntu-26.04 + permissions: + contents: read + strategy: + fail-fast: false + max-parallel: 2 + matrix: + module: ${{ fromJSON(needs.modules.outputs.paths) }} + env: + GOWORK: 'off' + GOFLAGS: '-p=2' + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: ${{ matrix.module }}/go.mod + cache-dependency-path: ${{ matrix.module }}/go.sum + - name: Verify independent module + working-directory: ${{ matrix.module }} + shell: bash + run: | + set -euo pipefail + go mod tidy + git diff --exit-code -- go.mod go.sum + go test -race ./... + go vet ./... + + release: + needs: [modules, test] + if: github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/main' + runs-on: ubuntu-26.04 + permissions: + contents: write + env: + GOWORK: 'off' + GH_TOKEN: ${{ github.token }} + MODULE_PATH: ${{ inputs.module }} + MODULE_VERSION: ${{ inputs.version }} + MODULE_PATHS: ${{ needs.modules.outputs.paths }} + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + fetch-depth: 0 + persist-credentials: false + - name: Validate release + id: validate + shell: bash + run: | + set -euo pipefail + jq -e --arg module "$MODULE_PATH" 'index($module) != null' <<< "$MODULE_PATHS" > /dev/null + [[ "$MODULE_VERSION" =~ ^v(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-[0-9A-Za-z]+([.-][0-9A-Za-z]+)*)?$ ]] + if [[ "$MODULE_VERSION" == *-* ]]; then + IFS=. read -r -a identifiers <<< "${MODULE_VERSION#*-}" + for identifier in "${identifiers[@]}"; do + if [[ "$identifier" =~ ^[0-9]+$ && "$identifier" == 0?* ]]; then exit 1; fi + done + fi + - uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 + with: + go-version-file: ${{ inputs.module }}/go.mod + cache: false + - name: Publish module tag + id: publish + shell: bash + run: | + set -euo pipefail + expected="github.com/$GITHUB_REPOSITORY/$MODULE_PATH" + major=${MODULE_VERSION%%.*} + if [[ "$major" != v0 && "$major" != v1 ]]; then expected+="/$major"; fi + actual=$(cd "$MODULE_PATH" && go list -m) + [[ "$actual" == "$expected" ]] + tag="$MODULE_PATH/$MODULE_VERSION" + if git show-ref --verify --quiet "refs/tags/$tag"; then + [[ "$(git rev-parse "refs/tags/$tag^{commit}")" == "$GITHUB_SHA" ]] + else + gh api --method POST "repos/$GITHUB_REPOSITORY/git/refs" -f "ref=refs/tags/$tag" -f "sha=$GITHUB_SHA" > /dev/null + fi + if ! gh release view "$tag" --repo "$GITHUB_REPOSITORY" > /dev/null 2>&1; then + options=() + if [[ "$MODULE_VERSION" == *-* ]]; then options+=(--prerelease); fi + gh release create "$tag" --repo "$GITHUB_REPOSITORY" --verify-tag --title "$tag" --generate-notes --latest=false "${options[@]}" + fi diff --git a/.github/workflows/zz_generated.semantic_pull_request.yaml b/.github/workflows/zz_generated.semantic_pull_request.yaml index d6cd38f5..05c29478 100644 --- a/.github/workflows/zz_generated.semantic_pull_request.yaml +++ b/.github/workflows/zz_generated.semantic_pull_request.yaml @@ -2,7 +2,7 @@ # # devctl # -# https://github.com/giantswarm/devctl/blob/7141e9a44c23ab8bc9aaf8ef9fdb92c9beed7422/pkg/gen/input/workflows/internal/file/semantic_pull_request.yaml.template +# https://github.com/giantswarm/devctl/blob/d4a0ced01fc090900a2720c7cc0f413f46a707cf/pkg/gen/input/workflows/internal/file/semantic_pull_request.yaml.template # name: semantic-pull-request @@ -16,8 +16,14 @@ on: permissions: {} +# GitHub judges this check on a commit by the workflow's newest run, and a run +# checks the title it reads when its job starts. A newer run cancels an older +# one, so no run that read an older title finishes after it. An edit that +# leaves the title alone (the description, the base) gets a group of its own: +# it cancels nothing and checks the same title. It still runs the job: a +# skipped newest run would count as green over a failed title check. concurrency: - group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }} + group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.ref }}${{ github.event.action == 'edited' && github.event.changes.title == null && format('-{0}', github.run_id) || '' }} cancel-in-progress: ${{ github.event_name == 'pull_request' }} jobs: diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 00000000..1b300555 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,41 @@ + + + +## Giant Swarm conventions + +devctl maintains this block from [its template][devctl-agents-block] and +overwrites changes to it. Instructions for this repository go in the section +below. Where they differ from this block, they take precedence. + +[devctl-agents-block]: https://github.com/giantswarm/devctl/blob/main/pkg/gen/input/llm/internal/file/agents_block.md.template + +- Never put customer names or other customer information in code, commit + messages, issues or pull requests. Most Giant Swarm repositories are public, + and private ones are kept ready to become public. +- Giant Swarm's style guides are in , one + directory per topic (go, helm, kubernetes, prometheus, …). Read the relevant + guide before you write or review that kind of code. +- Use conventional commits for commit messages and pull request titles. +- If `.github/workflows/zz_generated.auto_release.yaml` exists, GitHub Releases + are written from pull request titles: do not edit CHANGELOG.md. Otherwise, + add your change to CHANGELOG.md as described in + . +- If `.pre-commit-config.yaml` exists, run + `pre-commit run --files ` before committing; not every clone + has the hooks installed. +- Do not add linters, formatters or other tooling unless asked; use what the + repository already has. +- Put documentation in `docs/` rather than growing README.md, which gives an + overview and links into `docs/`. When a change affects documented behaviour, + update the documentation in the same pull request. +- If the skills of the `giantswarm/claude-code` plugin marketplace are not + available to you and the user works at Giant Swarm, tell them once that + they can install the `gs-base`, `gs-dev` and `gs-godev` + plugins from it. +- Go: run `goimports -local github.com/giantswarm/` on the files + you change. CI with architect-orb fails when imports are grouped + differently. + + + +## This repository diff --git a/Makefile b/Makefile index a7770bfd..f2515be5 100644 --- a/Makefile +++ b/Makefile @@ -2,10 +2,10 @@ # # devctl # -# https://github.com/giantswarm/devctl/blob/c0a255e412bf450e122f71e563d74a9bd9f9cddf/pkg/gen/input/makefile/internal/file/Makefile.template +# https://github.com/giantswarm/devctl/blob/1db8df846716412f82889c81256ff31c9acf2638/pkg/gen/input/makefile/internal/file/Makefile.template # -include Makefile.*.mk +-include Makefile.*.mk ##@ General diff --git a/Makefile.gen.go.mk b/Makefile.gen.go.mk index 378706fb..2bdd396a 100644 --- a/Makefile.gen.go.mk +++ b/Makefile.gen.go.mk @@ -2,7 +2,7 @@ # # devctl # -# https://github.com/giantswarm/devctl/blob/c64a781d7ee888be4af92da9cce27c321300a589/pkg/gen/input/makefile/internal/file/Makefile.gen.go.mk.template +# https://github.com/giantswarm/devctl/blob/30010fbc8809ecb0f1422ee655a8105c299695b1/pkg/gen/input/makefile/internal/file/Makefile.gen.go.mk.template # PACKAGE_DIR := ./bin-dist @@ -11,15 +11,20 @@ APPLICATION := $(shell go list -m | cut -d '/' -f 3) BUILDTIMESTAMP := $(shell date -u '+%FT%TZ') GITSHA1 := $(shell git rev-parse --verify HEAD) MODULE := $(shell go list -m) -# main() is usually in `main.go`, but sometimes in `cmd/main.go` (for example in newer kubebuilder projects) -MAIN_SOURCE := $(shell if test -e cmd/main.go; then echo cmd/main.go; else echo main.go; fi) +# Build the whole main package, including sibling files, at the root or in cmd/. +MAIN_SOURCE := $(shell if test -e cmd/main.go; then echo ./cmd; else echo .; fi) OS := $(shell go env GOOS) +ARCH := $(shell go env GOARCH) SOURCES := $(shell find . -name '*.go') -VERSION := $(shell gitsemver get) -# Expanded first in every recipe that stamps VERSION into its output, so a failed -# `gitsemver get` fails the build instead of producing an unversioned binary. +# Older gitsemver releases cannot open a worktree's .git file. Fall back to +# local version tags; without one, leave VERSION empty so shipping targets fail. +VERSION := $(shell if version=$$(gitsemver get 2>/dev/null) && [ -n "$$version" ]; then \ + printf '%s\n' "$$version"; \ + else git describe --tags --match 'v[0-9]*.[0-9]*.[0-9]*' 2>/dev/null | sed 's/^v//'; fi) +# Expanded first in every recipe that stamps VERSION into its output, so failed +# lookups fail the build instead of producing an unversioned binary. # Targets that do not ship a version (test, lint, ...) never expand it. -REQUIRE_VERSION = $(if $(VERSION),,$(error VERSION is empty: `gitsemver get` failed in $(CURDIR); pass VERSION= to override)) +REQUIRE_VERSION = $(if $(VERSION),,$(error VERSION is empty: `gitsemver get` and `git describe` found no version in $(CURDIR); pass VERSION= to override)) ifeq ($(OS), linux) EXTLDFLAGS := -static endif @@ -28,6 +33,13 @@ LDFLAGS ?= -w -linkmode 'auto' -extldflags '$(EXTLDFLAGS)' \ -X '$(MODULE)/pkg/project.buildTimestamp=$(BUILDTIMESTAMP)' \ -X '$(MODULE)/pkg/project.gitSHA=$(GITSHA1)' +# Copy to a unique sibling before renaming so a running binary keeps its inode +# and readers only see complete files. Keep the versioned artifact for packaging. +INSTALL_BINARY = tmp=$$(mktemp "$(2).tmp.XXXXXX") || exit $$?; \ + trap 'rm -f "$$tmp"' EXIT; \ + trap 'exit 1' HUP INT TERM; \ + cp -a "$(1)" "$$tmp" && mv -f "$$tmp" "$(2)" + .DEFAULT_GOAL := build ##@ Go @@ -48,27 +60,31 @@ build-windows-amd64: $(APPLICATION)-windows-amd64.exe ## Builds a local binary f $(APPLICATION): $(APPLICATION)-v$(VERSION)-$(OS)-amd64 @echo "====> $@" - cp -a $< $@ + @$(call INSTALL_BINARY,$<,$@) $(APPLICATION)-darwin: $(APPLICATION)-v$(VERSION)-darwin-amd64 @echo "====> $@" - cp -a $< $@ + @$(call INSTALL_BINARY,$<,$@) $(APPLICATION)-darwin-arm64: $(APPLICATION)-v$(VERSION)-darwin-arm64 @echo "====> $@" - cp -a $< $@ + @$(call INSTALL_BINARY,$<,$@) $(APPLICATION)-linux: $(APPLICATION)-v$(VERSION)-linux-amd64 @echo "====> $@" - cp -a $< $@ + @$(call INSTALL_BINARY,$<,$@) + +$(APPLICATION)-linux-amd64: $(APPLICATION)-v$(VERSION)-linux-amd64 + @echo "====> $@" + @$(call INSTALL_BINARY,$<,$@) $(APPLICATION)-linux-arm64: $(APPLICATION)-v$(VERSION)-linux-arm64 @echo "====> $@" - cp -a $< $@ + @$(call INSTALL_BINARY,$<,$@) $(APPLICATION)-windows-amd64.exe: $(APPLICATION)-v$(VERSION)-windows-amd64.exe @echo "====> $@" - cp -a $< $@ + @$(call INSTALL_BINARY,$<,$@) $(APPLICATION)-v$(VERSION)-%-amd64: $(SOURCES) $(REQUIRE_VERSION) @@ -133,10 +149,10 @@ $(PACKAGE_DIR)/$(APPLICATION)-v$(VERSION)-%-arm64.tar.gz: $(APPLICATION)-v$(VERS install: ## Install the application. $(REQUIRE_VERSION) @echo "====> $@" - go build -ldflags "$(LDFLAGS)" -o "$(shell go env GOPATH)/bin/$(APPLICATION)" "$(MAIN_SOURCE)" + CGO_ENABLED=0 go build -ldflags "$(LDFLAGS)" -o "$(shell go env GOPATH)/bin/$(APPLICATION)" "$(MAIN_SOURCE)" .PHONY: run -run: ## Runs go run main.go. +run: ## Runs the main package with go run. @echo "====> $@" go run -ldflags "$(LDFLAGS)" -race "$(MAIN_SOURCE)" @@ -152,10 +168,29 @@ imports: ## Runs goimports. goimports -local $(MODULE) -w . .PHONY: lint -lint: ## Runs golangci-lint. - @echo "====> $@" +lint: ## Checks import formatting and runs golangci-lint (installs goimports if missing). + @echo "====> $@" + @tool=$$(command -v goimports) || { \ + bindir=$$(go env GOBIN) || exit $$?; \ + if [ -z "$$bindir" ]; then \ + gopath=$$(go env GOPATH) || exit $$?; \ + bindir="$$gopath/bin"; \ + fi; \ + GOBIN="$$bindir" go install golang.org/x/tools/cmd/goimports@v0.50.0 || exit $$?; \ + tool="$$bindir/goimports"; \ + }; \ + files=$$("$$tool" -local "$(MODULE)" -l .) || exit $$?; \ + if [ -n "$$files" ]; then \ + printf '%s\n' "$$files"; \ + echo "Run 'make imports' to fix import formatting."; \ + exit 1; \ + fi golangci-lint run --timeout=15m ./... +# Extend lint from Makefile.custom.mk without replacing its generated recipe. +.PHONY: lint-custom +lint: lint-custom + .PHONY: fmt fmt: ## Run go fmt against code. go fmt ./... @@ -187,8 +222,8 @@ test: ## Runs go test with default values (race detector when a C toolchain is a go test -ldflags "$(LDFLAGS)" $(RACE) ./... .PHONY: build-docker -build-docker: build-linux ## Builds docker image to registry. +build-docker: $(APPLICATION)-linux-$(ARCH) ## Builds docker image to registry for the host architecture. $(REQUIRE_VERSION) @echo "====> $@" - cp -a $(APPLICATION)-linux $(APPLICATION) + @$(call INSTALL_BINARY,$<,$(APPLICATION)) docker build -t ${APPLICATION}:${VERSION} .