From 46447e9b821fbda7e42e9480141743a361236fc8 Mon Sep 17 00:00:00 2001 From: dtfranz Date: Thu, 23 Feb 2023 10:50:30 -0800 Subject: [PATCH] Operator Controller Release Adds a release github action and implements goreleaser, borrowing heavily from rukpak but stripped down a bit. Signed-off-by: dtfranz --- .dockerignore | 3 +- .github/workflows/release.yaml | 63 +++++++++++++++++++ .gitignore | 5 ++ .goreleaser.template.yml | 51 +++++++++++++++ Dockerfile | 35 ++--------- Makefile | 30 ++++++++- .../bundles_and_dependencies.go | 4 +- .../required_package/required_package.go | 4 +- .../{util => utils}/predicates/predicates.go | 0 .../predicates/predicates_test.go | 2 +- .../{util => utils}/sort/sort.go | 0 .../{util => utils}/sort/sort_test.go | 2 +- internal/version/version.go | 8 +++ 13 files changed, 165 insertions(+), 42 deletions(-) create mode 100644 .github/workflows/release.yaml create mode 100644 .goreleaser.template.yml rename internal/resolution/variable_sources/{util => utils}/predicates/predicates.go (100%) rename internal/resolution/variable_sources/{util => utils}/predicates/predicates_test.go (98%) rename internal/resolution/variable_sources/{util => utils}/sort/sort.go (100%) rename internal/resolution/variable_sources/{util => utils}/sort/sort_test.go (99%) create mode 100644 internal/version/version.go diff --git a/.dockerignore b/.dockerignore index 0f046820f..25c8e56f3 100644 --- a/.dockerignore +++ b/.dockerignore @@ -1,4 +1,3 @@ # More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file -# Ignore build and test binaries. -bin/ +# Ignore test binaries. testbin/ diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml new file mode 100644 index 000000000..28b318b96 --- /dev/null +++ b/.github/workflows/release.yaml @@ -0,0 +1,63 @@ +name: release + +on: + workflow_dispatch: + push: + branches: + - 'main' + tags: + - 'v*' + pull_request: + branches: + - main + +jobs: + goreleaser: + name: goreleaser + runs-on: ubuntu-latest + steps: + - name: Check out code into the Go module directory + uses: actions/checkout@v2 + with: + fetch-depth: 0 + + - name: Install Go + uses: actions/setup-go@v3 + with: + go-version-file: "go.mod" + + - name: Docker Login + if: ${{ github.event_name != 'pull_request' }} + uses: docker/login-action@v1 + with: + registry: quay.io + username: ${{ secrets.QUAY_USERNAME }} + password: ${{ secrets.QUAY_PASSWORD }} + + - name: Set the release related variables + run: | + if [[ $GITHUB_REF == refs/tags/* ]]; then + # Release tags. + echo IMAGE_TAG="${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV + echo GORELEASER_ARGS="--rm-dist" >> $GITHUB_ENV + echo DISABLE_RELEASE_PIPELINE=false >> $GITHUB_ENV + elif [[ $GITHUB_REF == refs/heads/* ]]; then + # Branch build. + echo IMAGE_TAG="$(echo "${GITHUB_REF#refs/heads/}" | sed -r 's|/+|-|g')" >> $GITHUB_ENV + echo GORELEASER_ARGS="--rm-dist --skip-validate" >> $GITHUB_ENV + elif [[ $GITHUB_REF == refs/pull/* ]]; then + # PR build. + echo IMAGE_TAG="pr-$(echo "${GITHUB_REF}" | sed -E 's|refs/pull/([^/]+)/?.*|\1|')" >> $GITHUB_ENV + else + echo IMAGE_TAG="$(git describe --tags --always)" >> $GITHUB_ENV + fi + + - name: Generate the operator-controller release manifests + if: ${{ startsWith(github.ref, 'refs/tags/v') }} + run: | + make quickstart VERSION=${GITHUB_REF#refs/tags/} + + - name: Run goreleaser + run: make release + env: + GITHUB_TOKEN: ${{ github.token }} diff --git a/.gitignore b/.gitignore index e917e5cef..a53106959 100644 --- a/.gitignore +++ b/.gitignore @@ -15,6 +15,11 @@ Dockerfile.cross # Output of the go coverage tool, specifically when used with LiteIDE *.out +# Release output +dist/** +.goreleaser.yml +operator-controller.yaml + # Kubernetes Generated files - skip generated files, except for vendored files !vendor/**/zz_generated.* diff --git a/.goreleaser.template.yml b/.goreleaser.template.yml new file mode 100644 index 000000000..686c28379 --- /dev/null +++ b/.goreleaser.template.yml @@ -0,0 +1,51 @@ +env: +- GOPROXY=https://proxy.golang.org|direct +- GO111MODULE=on +- CGO_ENABLED=0 +- PKG=github.com/operator-framework/operator-controller/internal/version +before: + hooks: + - go mod tidy + - go mod download +builds: + - id: operator-controller + main: ./ + binary: bin/manager + tags: $GO_BUILD_TAGS + goos: + - linux + ldflags: + - -X {{ .Env.PKG }}.GitCommit={{ .ShortCommit }} +dockers: +- image_templates: + - "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}" + dockerfile: Dockerfile + goos: linux +docker_manifests: +- name_template: "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}" + image_templates: + - "{{ .Env.IMAGE_REPO }}:{{ .Env.IMAGE_TAG }}" +checksum: + name_template: 'checksums.txt' +snapshot: + name_template: "{{ incpatch .Version }}-next" +changelog: + use: github-native + skip: $DISABLE_RELEASE_PIPELINE +release: + disable: $DISABLE_RELEASE_PIPELINE + extra_files: + - glob: 'operator-controller.yaml' + header: | + ## Installation + + ```bash + kubectl apply -f https://github.com/cert-manager/cert-manager/releases/download/{{ .Env.CERT_MGR_VERSION }}/cert-manager.yaml + kubectl wait --for=condition=Available --namespace=cert-manager deployment/cert-manager-webhook --timeout=60s + kubectl apply -f https://github.com/operator-framework/rukpak/releases/latest/download/rukpak.yaml + kubectl wait --for=condition=Available --namespace=rukpak-system deployment/core --timeout=60s + kubectl wait --for=condition=Available --namespace=rukpak-system deployment/helm-provisioner --timeout=60s + kubectl wait --for=condition=Available --namespace=rukpak-system deployment/rukpak-webhooks --timeout=60s + kubectl apply -f https://github.com/operator-framework/operator-controller/releases/download/{{ .Tag }}/operator-controller.yaml + kubectl wait --for=condition=Available --namespace=operator-controller-system deployment/operator-controller --timeout=60s + ``` diff --git a/Dockerfile b/Dockerfile index 8f0d54e8f..7d509b43d 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,34 +1,7 @@ -# Build the manager binary -FROM golang:1.19 as builder -ARG TARGETOS -ARG TARGETARCH +FROM gcr.io/distroless/static:debug-nonroot -WORKDIR /workspace -# Copy the Go Modules manifests -COPY go.mod go.mod -COPY go.sum go.sum -# cache deps before building and copying source so that we don't need to re-download as much -# and so that source changes don't invalidate our downloaded layer -RUN go mod download - -# Copy the go source -COPY main.go main.go -COPY api/ api/ -COPY controllers/ controllers/ -COPY internal/ internal/ - -# Build -# the GOARCH has not a default value to allow the binary be built according to the host where the command -# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO -# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore, -# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform. -RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager main.go - -# Use distroless as minimal base image to package the manager binary -# Refer to https://github.com/GoogleContainerTools/distroless for more details -FROM gcr.io/distroless/static:nonroot WORKDIR / -COPY --from=builder /workspace/manager . -USER 65532:65532 -ENTRYPOINT ["/manager"] +COPY manager manager + +EXPOSE 8080 diff --git a/Makefile b/Makefile index 69f3a22bf..a9bd61a7e 100644 --- a/Makefile +++ b/Makefile @@ -95,10 +95,10 @@ kind-cluster-cleanup: kind ## Delete the kind cluster .PHONY: build build: manifests generate fmt vet ## Build manager binary. - go build -o bin/manager main.go + CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o bin/manager main.go .PHONY: run -run: docker-build kind-cluster kind-load cert-mgr rukpak install deploy wait ## Build the operator-controller then deploy it into a new kind cluster. +run: build docker-build kind-cluster kind-load cert-mgr rukpak install deploy wait ## Build the operator-controller then deploy it into a new kind cluster. .PHONY: wait wait: @@ -109,7 +109,7 @@ wait: # More info: https://docs.docker.com/develop/develop-images/build_enhancements/ .PHONY: docker-build docker-build: generate ## Build docker image with the operator-controller. - docker build -t ${IMG} . + docker build -t ${IMG} -f Dockerfile ./bin/ .PHONY: docker-push docker-push: ## Push docker image with the manager. @@ -132,6 +132,24 @@ docker-buildx: test ## Build and push docker image for the manager for cross-pla - docker buildx rm project-v3-builder rm Dockerfile.cross +########### +# Release # +########### + +##@ release: + +export DISABLE_RELEASE_PIPELINE ?= true +substitute: + envsubst < .goreleaser.template.yml > .goreleaser.yml + +release: GORELEASER_ARGS ?= --snapshot --rm-dist +release: goreleaser substitute ## Run goreleaser + $(GORELEASER) $(GORELEASER_ARGS) + +quickstart: VERSION ?= $(shell git describe --abbrev=0 --tags) +quickstart: generate ## Generate the installation release manifests + kubectl kustomize config/default | sed "s/:latest/:$(VERSION)/g" > operator-controller.yaml + ##@ Deployment ifndef ignore-not-found @@ -178,6 +196,7 @@ KUSTOMIZE ?= $(LOCALBIN)/kustomize CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen KIND ?= $(LOCALBIN)/kind GINKGO ?= $(LOCALBIN)/ginkgo +GORELEASER := $(LOCALBIN)/goreleaser ENVTEST ?= $(LOCALBIN)/setup-envtest ## Tool Versions @@ -194,6 +213,11 @@ ginkgo: $(GINKGO) ## Download ginkgo locally if necessary. $(GINKGO): $(LOCALBIN) test -s $(LOCALBIN)/ginkgo || GOBIN=$(LOCALBIN) go install github.com/onsi/ginkgo/v2/ginkgo@v2.1.4 +.PHONY: goreleaser +goreleaser: $(GORELEASER) ## Builds a local copy of goreleaser +$(GORELEASER): $(LOCALBIN) + test -s $(LOCALBIN)/goreleaser || GOBIN=$(LOCALBIN) go install github.com/goreleaser/goreleaser@v1.11.2 + KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" .PHONY: kustomize kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading. diff --git a/internal/resolution/variable_sources/bundles_and_dependencies/bundles_and_dependencies.go b/internal/resolution/variable_sources/bundles_and_dependencies/bundles_and_dependencies.go index 730ed1504..9749341e2 100644 --- a/internal/resolution/variable_sources/bundles_and_dependencies/bundles_and_dependencies.go +++ b/internal/resolution/variable_sources/bundles_and_dependencies/bundles_and_dependencies.go @@ -12,8 +12,8 @@ import ( olmentity "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/entity" "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/required_package" - "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/util/predicates" - entitysort "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/util/sort" + "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/utils/predicates" + entitysort "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/utils/sort" ) type BundleVariable struct { diff --git a/internal/resolution/variable_sources/required_package/required_package.go b/internal/resolution/variable_sources/required_package/required_package.go index 36dbb4b5c..f05104ca5 100644 --- a/internal/resolution/variable_sources/required_package/required_package.go +++ b/internal/resolution/variable_sources/required_package/required_package.go @@ -9,8 +9,8 @@ import ( "github.com/operator-framework/deppy/pkg/deppy/input" olmentity "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/entity" - "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/util/predicates" - "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/util/sort" + "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/utils/predicates" + "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/utils/sort" ) type RequiredPackageVariable struct { diff --git a/internal/resolution/variable_sources/util/predicates/predicates.go b/internal/resolution/variable_sources/utils/predicates/predicates.go similarity index 100% rename from internal/resolution/variable_sources/util/predicates/predicates.go rename to internal/resolution/variable_sources/utils/predicates/predicates.go diff --git a/internal/resolution/variable_sources/util/predicates/predicates_test.go b/internal/resolution/variable_sources/utils/predicates/predicates_test.go similarity index 98% rename from internal/resolution/variable_sources/util/predicates/predicates_test.go rename to internal/resolution/variable_sources/utils/predicates/predicates_test.go index 58d5e38f3..87cac3c59 100644 --- a/internal/resolution/variable_sources/util/predicates/predicates_test.go +++ b/internal/resolution/variable_sources/utils/predicates/predicates_test.go @@ -10,7 +10,7 @@ import ( "github.com/operator-framework/operator-registry/alpha/property" olmentity "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/entity" - "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/util/predicates" + "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/utils/predicates" ) func TestPredicates(t *testing.T) { diff --git a/internal/resolution/variable_sources/util/sort/sort.go b/internal/resolution/variable_sources/utils/sort/sort.go similarity index 100% rename from internal/resolution/variable_sources/util/sort/sort.go rename to internal/resolution/variable_sources/utils/sort/sort.go diff --git a/internal/resolution/variable_sources/util/sort/sort_test.go b/internal/resolution/variable_sources/utils/sort/sort_test.go similarity index 99% rename from internal/resolution/variable_sources/util/sort/sort_test.go rename to internal/resolution/variable_sources/utils/sort/sort_test.go index 4ca179eeb..f63a0e657 100644 --- a/internal/resolution/variable_sources/util/sort/sort_test.go +++ b/internal/resolution/variable_sources/utils/sort/sort_test.go @@ -9,7 +9,7 @@ import ( "github.com/operator-framework/deppy/pkg/deppy/input" "github.com/operator-framework/operator-registry/alpha/property" - entitysort "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/util/sort" + entitysort "github.com/operator-framework/operator-controller/internal/resolution/variable_sources/utils/sort" ) func TestSort(t *testing.T) { diff --git a/internal/version/version.go b/internal/version/version.go new file mode 100644 index 000000000..74b12efed --- /dev/null +++ b/internal/version/version.go @@ -0,0 +1,8 @@ +package version + +// GitCommit indicates which commit the rukpak binaries were built from +var GitCommit string + +func String() string { + return GitCommit +}