From 7799d9c1a56103490b0f6d15065acce7f36e3347 Mon Sep 17 00:00:00 2001 From: Chuck Ha Date: Mon, 22 Jul 2019 19:13:20 -0400 Subject: [PATCH] Use goreleaser * Keep version information very short * Build docker images with goreleaser * Adds github workflow Signed-off-by: Chuck Ha --- .github/main.workflow | 33 ------------------ .github/release.workflow | 21 +++++++++++ .gitignore | 6 ++++ .goreleaser.yml | 48 +++++++++++++++++++++++++ Dockerfile | 21 +++-------- cmd/capd-manager/main.go | 5 ++- cmd/versioninfo/versioninfo.go | 39 +++++++++------------ hack/set-workspace-status.sh | 64 +++------------------------------- hack/verify-docker-build.sh | 3 ++ hack/verify-goimports.sh | 2 +- scripts/build-binaries.sh | 14 ++++---- 11 files changed, 113 insertions(+), 143 deletions(-) delete mode 100644 .github/main.workflow create mode 100644 .github/release.workflow create mode 100644 .goreleaser.yml diff --git a/.github/main.workflow b/.github/main.workflow deleted file mode 100644 index 7fff950..0000000 --- a/.github/main.workflow +++ /dev/null @@ -1,33 +0,0 @@ -workflow "Docker image for master" { - on = "push" - resolves = ["push"] -} - -action "master" { - uses = "actions/bin/filter@master" - args = "branch master" -} - -action "Docker Registry" { - needs = ["master"] - uses = "actions/docker/login@86ff551d26008267bb89ac11198ba7f1d807b699" - secrets = ["DOCKER_USERNAME", "DOCKER_PASSWORD", "DOCKER_REGISTRY_URL"] -} - -action "build" { - needs = ["Docker Registry"] - uses = "actions/docker/cli@master" - args = "build -t base ." -} - -action "tag" { - needs = ["build"] - uses = "actions/docker/tag@master" - args = "base docker.pkg.github.com/kubernetes-sigs/cluster-api-provider-docker/manager --no-sha --no-latest" -} - -action "push" { - needs = ["tag"] - uses = "actions/docker/cli@master" - args = "push docker.pkg.github.com/kubernetes-sigs/cluster-api-provider-docker/manager" -} diff --git a/.github/release.workflow b/.github/release.workflow new file mode 100644 index 0000000..37c76b6 --- /dev/null +++ b/.github/release.workflow @@ -0,0 +1,21 @@ +workflow "Release" { + on = "push" + resolves = ["Setup Google Cloud"] +} + +action "Setup Google Cloud" { + uses = "actions/gcloud/auth@master" + secrets = ["GCLOUD_AUTH"] +} + +action "is-tag" { + uses = "actions/bin/filter@master" + args = "tag" +} + +action "goreleaser" { + uses = "docker://goreleaser/goreleaser" + secrets = [] + args = "release" + needs = ["is-tag", "Setup Google Cloud"] +} diff --git a/.gitignore b/.gitignore index 1581ba1..de3f266 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,9 @@ kind-test # vscode .vscode + +# goland +.idea + +# ignore build output +dist diff --git a/.goreleaser.yml b/.goreleaser.yml new file mode 100644 index 0000000..0cc7c84 --- /dev/null +++ b/.goreleaser.yml @@ -0,0 +1,48 @@ +before: + hooks: + - go mod download +builds: +- + id: capdctl + env: + - CGO_ENABLED=0 + main: ./cmd/capdctl/main.go + binary: capdctl + ldflags: + - -s + - -w + - -X sigs.k8s.io/cluster-api-provider-docker/cmd/versioninfo.version={{.Version}} + - -X sigs.k8s.io/cluster-api-provider-docker/cmd/versioninfo.shortHash={{.ShortCommit}} + goos: + - linux + - darwin + goarch: + - amd64 +- + id: capd-manager + env: + - CGO_ENABLED=0 + main: ./cmd/capd-manager/main.go + binary: capd-manager + goos: + - linux + goarch: + - amd64 +archives: +- replacements: + darwin: Darwin + linux: Linux + amd64: x86_64 +checksum: + name_template: 'checksums.txt' +snapshot: + name_template: "{{ .Tag }}-next" +dockers: + - goos: linux + goarch: amd64 + binaries: + - capd-manager + image_templates: + - "gcr.io/kubernetes1-226021/capd-manager:{{.Version}}" + - "gcr.io/kubernetes1-226021/capd-manager:{{.Major}}.{{.Minor}}" + - "gcr.io/kubernetes1-226021/capd-manager:latest" diff --git a/Dockerfile b/Dockerfile index dbdcd9d..3f585eb 100644 --- a/Dockerfile +++ b/Dockerfile @@ -12,22 +12,11 @@ # See the License for the specific language governing permissions and # limitations under the License. -FROM golang:1.12.6 -WORKDIR /cluster-api-provider-docker -ADD go.mod . -ADD go.sum . -RUN go mod download -RUN curl -L https://dl.k8s.io/v1.14.3/kubernetes-client-linux-amd64.tar.gz | tar xvz -ADD cmd cmd -ADD actuators actuators -ADD kind kind -ADD third_party third_party - -RUN go install -v ./cmd/capd-manager +FROM golang:1.12.7 +WORKDIR /tmp +RUN curl -L https://dl.k8s.io/v1.14.4/kubernetes-client-linux-amd64.tar.gz | tar xvz +RUN mv /tmp/kubernetes/client/bin/kubectl /usr/local/bin RUN curl https://get.docker.com | sh +COPY capd-manager /usr/local/bin -FROM golang:1.12.5 -COPY --from=0 /cluster-api-provider-docker/kubernetes/client/bin/kubectl /usr/local/bin -COPY --from=0 /go/bin/capd-manager /usr/local/bin -COPY --from=0 /usr/bin/docker /usr/local/bin ENTRYPOINT ["capd-manager"] diff --git a/cmd/capd-manager/main.go b/cmd/capd-manager/main.go index 27b9485..f33105c 100644 --- a/cmd/capd-manager/main.go +++ b/cmd/capd-manager/main.go @@ -72,13 +72,12 @@ func main() { os.Exit(1) } - clusterLogger := log.WithName("cluster-actuator") + clusterLogger := klogr.New().WithName("cluster-actuator") clusterActuator := actuators.Cluster{ Log: clusterLogger, } - machineLogger := log.WithName("machine-actuator") - + machineLogger := klogr.New().WithName("machine-actuator") machineActuator := actuators.Machine{ Core: k8sclientset.CoreV1(), ClusterAPI: cs.ClusterV1alpha1(), diff --git a/cmd/versioninfo/versioninfo.go b/cmd/versioninfo/versioninfo.go index adc3517..ce98cd5 100644 --- a/cmd/versioninfo/versioninfo.go +++ b/cmd/versioninfo/versioninfo.go @@ -18,34 +18,27 @@ package versioninfo import ( "fmt" - "strings" +) + +const ( + defaultVersion = "v0.0.0" + defaultShortHash = "0000000" ) var ( - // GitBranch is the branch from which this binary was built - GitBranch string - // GitReleaseTag is the git tag from which this binary is released - GitReleaseTag string - // GitReleaseCommit is the commit corresponding to the GitReleaseTag - GitReleaseCommit string - // GitTreeState indicates if the git tree, from which this binary was built, was clean or dirty - GitTreeState string - // GitCommit is the git commit at which this binary binary was built - GitCommit string - // GitMajor is the major version of the release - GitMajor string - // GitMinor is the minor version of the release - GitMinor string + // version is the version being released + version string + // ShortHash is the short form of the git hash of the commit being built + shortHash string ) // VersionInfo returns version information for the supplied binary func VersionInfo(binName string) string { - var vi strings.Builder - vi.WriteString(fmt.Sprintf("%s version info:\n", binName)) - vi.WriteString(fmt.Sprintf("GitReleaseTag: %q, Major: %q, Minor: %q, GitRelaseCommit: %q\n", GitReleaseTag, GitMajor, GitMinor, GitReleaseCommit)) - vi.WriteString(fmt.Sprintf("Git Branch: %q\n", GitBranch)) - vi.WriteString(fmt.Sprintf("Git commit: %q\n", GitCommit)) - vi.WriteString(fmt.Sprintf("Git tree state: %q\n", GitTreeState)) - - return vi.String() + if version == "" { + version = defaultVersion + } + if shortHash == "" { + shortHash = defaultShortHash + } + return fmt.Sprintf("%s %s+%s\n", binName, version, shortHash) } diff --git a/hack/set-workspace-status.sh b/hack/set-workspace-status.sh index 0264d15..a4d06b7 100755 --- a/hack/set-workspace-status.sh +++ b/hack/set-workspace-status.sh @@ -17,64 +17,8 @@ set -o errexit set -o nounset set -o pipefail -GIT_COMMIT="$(git describe --always --dirty --abbrev=14)" +# Override by passing in VERSION +VERSION=${VERSION:-$(git tag --sort taggerdate | tail -n 1)} -if git_status=$(git status --porcelain 2>/dev/null) && [[ -z ${git_status} ]]; then - GIT_TREE_STATE="clean" -else - GIT_TREE_STATE="dirty" -fi - -# mostly stolen from k8s.io/hack/lib/version.sh -# Use git describe to find the version based on tags. -if GIT_VERSION=$(git describe --tags --abbrev=14 2>/dev/null); then - # This translates the "git describe" to an actual semver.org - # compatible semantic version that looks something like this: - # v1.1.0-alpha.0.6+84c76d1142ea4d - # - # TODO: We continue calling this "git version" because so many - # downstream consumers are expecting it there. - DASHES_IN_VERSION=$(echo "${GIT_VERSION}" | sed "s/[^-]//g") - if [[ "${DASHES_IN_VERSION}" == "---" ]] ; then - # We have distance to subversion (v1.1.0-subversion-1-gCommitHash) - GIT_VERSION=$(echo "${GIT_VERSION}" | sed "s/-\([0-9]\{1,\}\)-g\([0-9a-f]\{14\}\)$/.\1\-\2/") - elif [[ "${DASHES_IN_VERSION}" == "--" ]] ; then - # We have distance to base tag (v1.1.0-1-gCommitHash) - GIT_VERSION=$(echo "${GIT_VERSION}" | sed "s/-g\([0-9a-f]\{14\}\)$/-\1/") - fi - if [[ "${GIT_TREE_STATE}" == "dirty" ]]; then - # git describe --dirty only considers changes to existing files, but - # that is problematic since new untracked .go files affect the build, - # so use our idea of "dirty" from git status instead. - GIT_VERSION+="-dirty" - fi - - - # Try to match the "git describe" output to a regex to try to extract - # the "major" and "minor" versions and whether this is the exact tagged - # version or whether the tree is between two tagged versions. - if [[ "${GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?([-].*)?([+].*)?$ ]]; then - GIT_MAJOR=${BASH_REMATCH[1]} - GIT_MINOR=${BASH_REMATCH[2]} - fi - - # If GIT_VERSION is not a valid Semantic Version, then refuse to build. - if ! [[ "${GIT_VERSION}" =~ ^v([0-9]+)\.([0-9]+)(\.[0-9]+)?(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$ ]]; then - GIT_VERSION=v0.0.0+${GIT_VERSION} - GIT_MAJOR=0 - GIT_MINOR=0 - fi -else - GIT_VERSION="UNKNOWN_GIT_VERSION" - GIT_MAJOR="UNKNOWN_GIT_MAJOR_VERSION" - GIT_MINOR="UNKNOWN_GIT_MINOR_VERSION" -fi - -if GIT_RELEASE_TAG=$(git describe --abbrev=0 --tags 2> /dev/null); then - GIT_RELEASE_COMMIT=$(git rev-list -n 1 ${GIT_RELEASE_TAG} | head -c 14) -else - GIT_RELEASE_TAG="UNKNOWN_RELEASE" - GIT_RELEASE_COMMIT="UNKNOWN_RELEAE_COMMIT" -fi - -GIT_BRANCH=$(git branch | grep \* | cut -d ' ' -f2) +# Override by passing in SHORT +SHORT=${SHORT:-$(git rev-parse --short "${VERSION}")} diff --git a/hack/verify-docker-build.sh b/hack/verify-docker-build.sh index 4865748..2990041 100755 --- a/hack/verify-docker-build.sh +++ b/hack/verify-docker-build.sh @@ -23,4 +23,7 @@ source "$(dirname "$0")/utils.sh" # check if manager docker image builds cd_root_path +export GO111MODULE=on +go mod download +go build -o capd-manager ./cmd/capd-manager docker build --file Dockerfile -t capd-manager:pr-verify . diff --git a/hack/verify-goimports.sh b/hack/verify-goimports.sh index df21c71..b4a30f9 100755 --- a/hack/verify-goimports.sh +++ b/hack/verify-goimports.sh @@ -47,7 +47,7 @@ go build > /dev/null popd > /dev/null # check for goimports diffs -diff=$(git ls-files | grep "\.go" | grep -v "\/vendor" | xargs "${BIN_PATH}/goimports" -d 2>&1) +diff=$(git ls-files | grep "\.go$" | xargs "${BIN_PATH}/goimports" -d 2>&1) if [[ -n "${diff}" ]]; then echo "${diff}" echo diff --git a/scripts/build-binaries.sh b/scripts/build-binaries.sh index 7fc4792..47c4b0b 100755 --- a/scripts/build-binaries.sh +++ b/scripts/build-binaries.sh @@ -16,6 +16,9 @@ set -o errexit set -o xtrace +VERSION=${VERSION:-} +SHORT=${SHORT:-} + # shellcheck source=/dev/null source "$(dirname "$0")/../hack/utils.sh" readonly REPO_PATH=$(get_root_path) @@ -28,15 +31,12 @@ readonly CAPDMGR_SRC=${REPO_PATH}/cmd/capd-manager readonly KIND_TEST_BIN=${REPO_PATH}/bin/kind-test readonly KIND_TEST_SRC=${REPO_PATH}/cmd/kind-test +# this sets VERSION and SHORT if they are not already set source "${REPO_PATH}/hack/set-workspace-status.sh" -LDFLAGS="-X sigs.k8s.io/cluster-api-provider-docker/cmd/versioninfo.GitBranch=${GIT_BRANCH} \ --X sigs.k8s.io/cluster-api-provider-docker/cmd/versioninfo.GitReleaseTag=${GIT_RELEASE_TAG} \ --X sigs.k8s.io/cluster-api-provider-docker/cmd/versioninfo.GitReleaseCommit=${GIT_RELEASE_COMMIT} \ --X sigs.k8s.io/cluster-api-provider-docker/cmd/versioninfo.GitTreeState=${GIT_TREE_STATE} \ --X sigs.k8s.io/cluster-api-provider-docker/cmd/versioninfo.GitCommit=${GIT_COMMIT} \ --X sigs.k8s.io/cluster-api-provider-docker/cmd/versioninfo.GitMajor=${GIT_MAJOR} \ --X sigs.k8s.io/cluster-api-provider-docker/cmd/versioninfo.GitMinor=${GIT_MINOR}" +LDFLAGS="-s -w \ +-X sigs.k8s.io/cluster-api-provider-docker/cmd/versioninfo.version=${VERSION} \ +-X sigs.k8s.io/cluster-api-provider-docker/cmd/versioninfo.shortHash=${SHORT}" # build capdctl go build -ldflags "${LDFLAGS}" -o ${CAPDCTL_BIN} ${CAPDCTL_SRC}