diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c014e210..e2ca187d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,7 +2,7 @@ name: CI on: push: - branches: [ main ] + branches: [ master ] tags: - "*" pull_request: @@ -21,7 +21,7 @@ jobs: with: go-version: 1.23 - name: Build - run: make + run: make build docs: runs-on: ubuntu-latest @@ -37,41 +37,6 @@ jobs: # make gen-docs # git diff --exit-code - linux: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - name: Set up Go - uses: actions/setup-go@v4 - with: - go-version: 1.23 - - name: Build kg and kgctl for all Linux Architectures - run: make all-build - - darwin: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - name: Set up Go - uses: actions/setup-go@v4 - with: - go-version: 1.23 - - name: Build kgctl for Darwin amd64 - run: make OS=darwin ARCH=amd64 - - name: Build kgctl for Darwin arm64 - run: make OS=darwin ARCH=arm64 - - windows: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - name: Set up Go - uses: actions/setup-go@v4 - with: - go-version: 1.23 - - name: Build kgctl for Windows - run: make OS=windows - unit: runs-on: ubuntu-latest steps: @@ -94,25 +59,15 @@ jobs: - name: Run e2e Tests run: make e2e - lint: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - name: Set up Go - uses: actions/setup-go@v4 - with: - go-version: 1.23 - - name: Lint Code - run: make lint - - container: - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - name: Set up Go - uses: actions/setup-go@v4 - with: - go-version: 1.23 - - name: Container - run: make container + # https://github.com/castai/kilo/issues/12 + #lint: + # runs-on: ubuntu-latest + # steps: + # - uses: actions/checkout@v3 + # - name: Set up Go + # uses: actions/setup-go@v4 + # with: + # go-version: 1.23 + # - name: Lint Code + # run: make lint diff --git a/.github/workflows/goreleaser.yml b/.github/workflows/goreleaser.yml index 3e9b0f5d..7f086172 100644 --- a/.github/workflows/goreleaser.yml +++ b/.github/workflows/goreleaser.yml @@ -1,61 +1,47 @@ -name: Create and publish a Container image +name: goreleaser on: + pull_request: + paths: + - .github/workflows/goreleaser.yml + - .goreleaser.yaml push: - branches: - - master # Trigger the workflow on push to the master branch tags: - - 'v*' # Trigger the workflow on tags like v1.0.0, v2.0.0, etc. - pull_request: - branches: - - master # Trigger workflow when a PR targets the master branch + - 'v*' permissions: - contents: read - packages: write - id-token: write - -# Defines two custom environment variables for the workflow. These are used for the Container registry domain, and a name for the Docker image that this workflow builds. -env: - REGISTRY: ghcr.io - IMAGE_NAME: ${{ github.repository }} + contents: write jobs: - build-and-push-image: + goreleaser: runs-on: ubuntu-latest - permissions: - contents: read - packages: write - steps: - - name: Checkout repository - uses: actions/checkout@v4 - - # Logs in to the Container registry using GitHub token - - name: Log in to the Container registry - uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1 - with: - registry: ${{ env.REGISTRY }} - username: ${{ github.actor }} - password: ${{ secrets.GITHUB_TOKEN }} - - # Extract metadata (tags, labels) for Docker - - name: Extract metadata (tags, labels) for Docker - id: meta - uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7 - with: - images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} - - # Build and push Docker image - - name: Build and push Docker image - uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4 - with: - context: . - push: true - # Tag the image as 'latest' for master branch, versioned tags for versioned pushes, and PR-specific tags - tags: | - ${{ steps.meta.outputs.tags }} - ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest - ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${GITHUB_REF##*/} # This will use the tag from the push - ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:pr-${{ github.event.pull_request.number }} # PR-specific tag - labels: ${{ steps.meta.outputs.labels }} + - name: Checkout + uses: actions/checkout@v3 + with: + fetch-depth: 0 + - uses: actions/setup-go@v4 + with: + go-version: v1.23.0 + - name: SET LDFLAGS + run: echo "LDFLAGS=$(make ldflags)" >> $GITHUB_ENV + - name: Delete non-semver tags + run: 'git tag -d $(git tag -l | grep -v "^v")' + - name: Run GoReleaser on tag + if: github.event_name != 'pull_request' + uses: goreleaser/goreleaser-action@v4 + with: + distribution: goreleaser + version: latest + args: release --timeout 60m + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Run GoReleaser on pull request + if: github.event_name == 'pull_request' + uses: goreleaser/goreleaser-action@v4 + with: + distribution: goreleaser + version: latest + args: release --timeout 60m --snapshot + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/.github/workflows/images.yaml b/.github/workflows/images.yaml index 494cb11f..5aa7e0f1 100644 --- a/.github/workflows/images.yaml +++ b/.github/workflows/images.yaml @@ -6,6 +6,9 @@ on: - master # Trigger the workflow on push to the master branch tags: - 'v*' # Trigger the workflow on tags like v1.0.0, v2.0.0, etc. + pull_request: + branches: + - master # Trigger workflow when a PR targets the master branch permissions: contents: read @@ -49,9 +52,12 @@ jobs: with: context: . push: true - # Tag the image as 'latest' for master branch and as the version tag for versioned pushes + # Tag the image as 'latest' only for master branch, versioned tags for versioned pushes, and PR-specific tags tags: | + # Version tags for releases ${{ steps.meta.outputs.tags }} - ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest - ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${GITHUB_REF##*/} # This will use the tag from the push - labels: ${{ steps.meta.outputs.labels }} + + # Commit SHA for PR builds for precise versioning + ${{ github.event_name == 'pull_request' && env.REGISTRY && env.IMAGE_NAME && format('{0}/{1}:{2}', env.REGISTRY, env.IMAGE_NAME, github.sha.substring(0, 7)) }} + + labels: ${{ steps.meta.outputs.labels }} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 50b4ba67..653f1014 100644 --- a/.gitignore +++ b/.gitignore @@ -5,9 +5,5 @@ bin/ tmp/ e2e/kind.yaml* -hack/tools/controller-gen -hack/tools/controller-gen-v0.16.1 -hack/tools/openshift-goimports -hack/tools/openshift-goimports-c72f1dc2e3aacfa00aece3391d938c9bc734e791 -hack/tools/yaml-patch -hack/tools/yaml-patch-v0.0.11 +hack/tools/ +dist/ \ No newline at end of file diff --git a/.goreleaser.yaml b/.goreleaser.yaml index 865fc603..56d9069e 100644 --- a/.goreleaser.yaml +++ b/.goreleaser.yaml @@ -11,33 +11,18 @@ builds: ldflags: "{{ .Env.LDFLAGS }}" goos: - linux - - darwin - - windows goarch: - amd64 - arm64 - ignore: - - goos: darwin - goarch: ppc64le - - goos: windows - goarch: ppc64le - id: kilo-kgctl dir: cmd/kgctl binary: kilo-kgctl ldflags: "{{ .Env.LDFLAGS }}" goos: - linux - - darwin - - windows goarch: - amd64 - arm64 - ignore: - - goos: darwin - goarch: ppc64le - - goos: windows - goarch: ppc64le - archives: - id: kilo-plugin diff --git a/Makefile b/Makefile index 2207f18e..2f321138 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ export GO111MODULE=on -.PHONY: push container clean container-name container-latest push-latest fmt lint test unit gomodtidy generate crds codegen manifest manfest-latest manifest-annotate manifest manfest-latest manifest-annotate release e2e +.PHONY: push container clean push-latest fmt lint test unit gomodtidy generate crds codegen e2e OS ?= $(shell go env GOOS) ARCH ?= $(shell go env GOARCH) @@ -14,7 +14,7 @@ RELEASE_BINS := $(addprefix bin/release/kgctl-, $(addprefix linux-, $(ALL_ARCH)) PROJECT := kilo PKG := github.com/squat/$(PROJECT) REGISTRY ?= index.docker.io -IMAGE ?= squat/$(PROJECT) +IMAGE ?= castai/$(PROJECT) FULLY_QUALIFIED_IMAGE := $(REGISTRY)/$(IMAGE) TAG := $(shell git describe --abbrev=0 --tags HEAD 2>/dev/null) @@ -28,18 +28,13 @@ endif DIRTY := $(shell test -z "$$(git diff --shortstat 2>/dev/null)" || echo -dirty) VERSION := $(VERSION)$(DIRTY) -LDFLAGS := '-X $(PKG)/pkg/version.Version=$(VERSION)' +LDFLAGS := -X $(PKG)/pkg/version.Version=$(VERSION) SRC := $(shell find . -type f -name '*.go') GO_FILES ?= $$(find . -name '*.go') GO_PKGS ?= $$(go list ./...) -CONTROLLER_GEN_BINARY := bin/controller-gen -CLIENT_GEN_BINARY := bin/client-gen -DOCS_GEN_BINARY := bin/docs-gen -DEEPCOPY_GEN_BINARY := bin/deepcopy-gen -INFORMER_GEN_BINARY := bin/informer-gen -LISTER_GEN_BINARY := bin/lister-gen STATICCHECK_BINARY := bin/staticcheck +DOCS_GEN_BINARY := bin/docs-gen EMBEDMD_BINARY := bin/embedmd KIND_BINARY := $(shell pwd)/bin/kind KUBECTL_BINARY := $(shell pwd)/bin/kubectl @@ -109,37 +104,15 @@ tools: $(CONTROLLER_GEN) $(YAML_PATCH) $(OPENSHIFT_GOIMPORTS) ## Install tools .PHONY: tools build: WHAT ?= ./cmd/... -build: +build: mkdirbin GOOS=$(OS) GOARCH=$(ARCH) CGO_ENABLED=0 go build $(BUILDFLAGS) -ldflags="$(LDFLAGS)" -o bin $(WHAT) .PHONY: build goreleaser: LDFLAGS=$(LDFLAGS) goreleaser -build-%: - @$(MAKE) --no-print-directory OS=$(word 1,$(subst -, ,$*)) ARCH=$(word 2,$(subst -, ,$*)) build - -container-latest-%: - @$(MAKE) --no-print-directory ARCH=$* container-latest - -container-%: - @$(MAKE) --no-print-directory ARCH=$* container - -push-latest-%: - @$(MAKE) --no-print-directory ARCH=$* push-latest - -push-%: - @$(MAKE) --no-print-directory ARCH=$* push - -all-build: $(addprefix build-$(OS)-, $(ALL_ARCH)) - -all-container: $(addprefix container-, $(ALL_ARCH)) - -all-push: $(addprefix push-, $(ALL_ARCH)) - -all-container-latest: $(addprefix container-latest-, $(ALL_ARCH)) - -all-push-latest: $(addprefix push-latest-, $(ALL_ARCH)) +ldflags: + @echo $(LDFLAGS) generate: codegen crds @@ -172,12 +145,15 @@ lint: $(STATICCHECK_BINARY) echo "$$fmt_res"; \ exit 1; \ fi - + unit: go test --race ./... test: lint unit e2e +mkdirbin: + mkdir -p bin/ + $(KIND_BINARY): curl -Lo $@ https://kind.sigs.k8s.io/dl/v0.25.0/kind-linux-$(ARCH) chmod +x $@ @@ -190,12 +166,12 @@ $(BASH_UNIT): curl -Lo $@ https://raw.githubusercontent.com/pgrange/bash_unit/v2.3.1/bash_unit chmod +x $@ -e2e: container $(KIND_BINARY) $(KUBECTL_BINARY) $(BASH_UNIT) bin/$(OS)/$(ARCH)/kgctl - KILO_IMAGE=$(IMAGE):$(ARCH)-$(VERSION) KIND_BINARY=$(KIND_BINARY) KUBECTL_BINARY=$(KUBECTL_BINARY) KGCTL_BINARY=$(shell pwd)/bin/$(OS)/$(ARCH)/kgctl $(BASH_UNIT) $(BASH_UNIT_FLAGS) ./e2e/setup.sh ./e2e/multi-cluster.sh ./e2e/handlers.sh ./e2e/kgctl.sh ./e2e/teardown.sh +e2e: mkdirbin container $(KIND_BINARY) $(KUBECTL_BINARY) $(BASH_UNIT) build + KILO_IMAGE=$(IMAGE):$(ARCH)-$(VERSION) KIND_BINARY=$(KIND_BINARY) KUBECTL_BINARY=$(KUBECTL_BINARY) KGCTL_BINARY=$(shell pwd)/bin/kgctl $(BASH_UNIT) $(BASH_UNIT_FLAGS) ./e2e/setup.sh ./e2e/multi-cluster.sh ./e2e/handlers.sh ./e2e/kgctl.sh ./e2e/teardown.sh -tmp/help.txt: bin/$(OS)/$(ARCH)/kg +tmp/help.txt: build mkdir -p tmp - bin//$(OS)/$(ARCH)/kg --help 2>&1 | head -n -1 > $@ + bin/kg --help 2>&1 | head -n -1 > $@ docs/kg.md: $(EMBEDMD_BINARY) tmp/help.txt $(EMBEDMD_BINARY) -w $@ @@ -217,109 +193,22 @@ website/build/index.html: website/docs/README.md docs/api.md yarn --cwd website install yarn --cwd website build -container: .container-$(ARCH)-$(VERSION) container-name -.container-$(ARCH)-$(VERSION): bin/linux/$(ARCH)/kg bin/linux/$(ARCH)/kgctl Dockerfile - @i=0; for a in $(ALL_ARCH); do [ "$$a" = $(ARCH) ] && break; i=$$((i+1)); done; \ - ia=""; iv=""; \ - j=0; for a in $(DOCKER_ARCH); do \ - [ "$$i" -eq "$$j" ] && ia=$$(echo "$$a" | awk '{print $$1}') && iv=$$(echo "$$a" | awk '{print $$2}') && break; j=$$((j+1)); \ - done; \ - SHA=$$(docker manifest inspect $(BASE_IMAGE) | jq '.manifests[] | select(.platform.architecture == "'$$ia'") | if .platform | has("variant") then select(.platform.variant == "'$$iv'") else . end | .digest' -r); \ - docker build -t $(IMAGE):$(ARCH)-$(VERSION) --build-arg FROM=$(BASE_IMAGE)@$$SHA --build-arg GOARCH=$(ARCH) . - @docker images -q $(IMAGE):$(ARCH)-$(VERSION) > $@ - -container-latest: .container-$(ARCH)-$(VERSION) - @docker tag $(IMAGE):$(ARCH)-$(VERSION) $(FULLY_QUALIFIED_IMAGE):$(ARCH)-latest - @echo "container: $(IMAGE):$(ARCH)-latest" - -container-name: - @echo "container: $(IMAGE):$(ARCH)-$(VERSION)" - -manifest: .manifest-$(VERSION) manifest-name -.manifest-$(VERSION): Dockerfile $(addprefix push-, $(ALL_ARCH)) - @docker manifest create --amend $(FULLY_QUALIFIED_IMAGE):$(VERSION) $(addsuffix -$(VERSION), $(addprefix $(FULLY_QUALIFIED_IMAGE):, $(ALL_ARCH))) - @$(MAKE) --no-print-directory manifest-annotate-$(VERSION) - @docker manifest push $(FULLY_QUALIFIED_IMAGE):$(VERSION) > $@ - -manifest-latest: Dockerfile $(addprefix push-latest-, $(ALL_ARCH)) - @docker manifest rm $(FULLY_QUALIFIED_IMAGE):latest || echo no old manifest - @docker manifest create --amend $(FULLY_QUALIFIED_IMAGE):latest $(addsuffix -latest, $(addprefix $(FULLY_QUALIFIED_IMAGE):, $(ALL_ARCH))) - @$(MAKE) --no-print-directory manifest-annotate-latest - @docker manifest push $(FULLY_QUALIFIED_IMAGE):latest - @echo "manifest: $(IMAGE):latest" - -manifest-annotate: manifest-annotate-$(VERSION) - -manifest-annotate-%: - @i=0; \ - for a in $(ALL_ARCH); do \ - annotate=; \ - j=0; for da in $(DOCKER_ARCH); do \ - if [ "$$j" -eq "$$i" ] && [ -n "$$da" ]; then \ - annotate="docker manifest annotate $(FULLY_QUALIFIED_IMAGE):$* $(FULLY_QUALIFIED_IMAGE):$$a-$* --os linux --arch"; \ - k=0; for ea in $$da; do \ - [ "$$k" = 0 ] && annotate="$$annotate $$ea"; \ - [ "$$k" != 0 ] && annotate="$$annotate --variant $$ea"; \ - k=$$((k+1)); \ - done; \ - $$annotate; \ - fi; \ - j=$$((j+1)); \ - done; \ - i=$$((i+1)); \ - done - -manifest-name: - @echo "manifest: $(IMAGE):$(VERSION)" - -push: .push-$(ARCH)-$(VERSION) push-name -.push-$(ARCH)-$(VERSION): .container-$(ARCH)-$(VERSION) -ifneq ($(REGISTRY),index.docker.io) - @docker tag $(IMAGE):$(ARCH)-$(VERSION) $(FULLY_QUALIFIED_IMAGE):$(ARCH)-$(VERSION) -endif - @docker push $(FULLY_QUALIFIED_IMAGE):$(ARCH)-$(VERSION) - @docker images -q $(IMAGE):$(ARCH)-$(VERSION) > $@ - -push-latest: container-latest - @docker push $(FULLY_QUALIFIED_IMAGE):$(ARCH)-latest - @echo "pushed: $(IMAGE):$(ARCH)-latest" - -push-name: - @echo "pushed: $(IMAGE):$(ARCH)-$(VERSION)" +container: + docker build -t $(FULLY_QUALIFIED_IMAGE):latest -f Dockerfile . -release: $(RELEASE_BINS) -$(RELEASE_BINS): - @make OS=$(word 2,$(subst -, ,$(@F))) ARCH=$(word 3,$(subst -, ,$(@F))) - mkdir -p $(@D) - cp bin/$(word 2,$(subst -, ,$(@F)))/$(word 3,$(subst -, ,$(@F)))/kgctl $@ +container-linux: + FULLY_QUALIFIED_IMAGE=$(FULLY_QUALIFIED_IMAGE- + OS=linux ARCH=amd64 make container -clean: container-clean bin-clean +clean: bin-clean rm -rf .cache -container-clean: - rm -rf .container-* .manifest-* .push-* - bin-clean: rm -rf bin gomodtidy: go mod tidy -$(CONTROLLER_GEN_BINARY): - go build -o $@ sigs.k8s.io/controller-tools/cmd/controller-gen - -$(CLIENT_GEN_BINARY): - go build -o $@ k8s.io/code-generator/cmd/client-gen - -$(DEEPCOPY_GEN_BINARY): - go build -o $@ k8s.io/code-generator/cmd/deepcopy-gen - -$(INFORMER_GEN_BINARY): - go build -o $@ k8s.io/code-generator/cmd/informer-gen - -$(LISTER_GEN_BINARY): - go build -o $@ k8s.io/code-generator/cmd/lister-gen - $(DOCS_GEN_BINARY): cmd/docs-gen/main.go go build -o $@ ./cmd/docs-gen