From bf11d600f82e8ad35c1ed4c783a3d86b037bf351 Mon Sep 17 00:00:00 2001 From: Tuomo Tanskanen Date: Mon, 1 Jul 2024 08:21:02 +0300 Subject: [PATCH] fix hack scripts to run locally again Fix hack scripts to run locally again. We have some Git permissions issues, formatting issues, deprecated linters, bad Go configs, custom mount points etc. Now it should be cleaner, runs on Linux and Mac equally (zsh can't handle "${@}" for example)... Signed-off-by: Tuomo Tanskanen --- hack/generate.sh | 47 ++++++++++++++++++++++---------------------- hack/golint.sh | 20 ------------------- hack/gomod.sh | 8 +++++--- hack/manifestlint.sh | 32 ++++++++++++++++-------------- hack/markdownlint.sh | 8 +++++--- hack/shellcheck.sh | 22 +++++++++++---------- hack/unit.sh | 29 --------------------------- 7 files changed, 63 insertions(+), 103 deletions(-) delete mode 100755 hack/golint.sh delete mode 100755 hack/unit.sh diff --git a/hack/generate.sh b/hack/generate.sh index 6f1f1d23c7..db0c9861c5 100755 --- a/hack/generate.sh +++ b/hack/generate.sh @@ -2,35 +2,36 @@ # Ignore the rule that says we should always quote variables, because # in this script we *do* want globbing. -# shellcheck disable=SC2086 +# shellcheck disable=SC2086,SC2292 set -eux -IS_CONTAINER=${IS_CONTAINER:-false} -ARTIFACTS=${ARTIFACTS:-/tmp} +IS_CONTAINER="${IS_CONTAINER:-false}" +ARTIFACTS="${ARTIFACTS:-/tmp}" CONTAINER_RUNTIME="${CONTAINER_RUNTIME:-podman}" +WORKDIR="${WORKDIR:-/workdir}" if [ "${IS_CONTAINER}" != "false" ]; then - eval "$(go env)" - cd "${GOPATH}"/src/github.com/metal3-io/baremetal-operator - export XDG_CACHE_HOME="/tmp/.cache" + # we need to tell git its OK to use dir owned by someone else + git config --global safe.directory "${WORKDIR}" + export XDG_CACHE_HOME="/tmp/.cache" - INPUT_FILES="$(git ls-files config) $(git ls-files | grep zz_generated)" - cksum $INPUT_FILES > "$ARTIFACTS/lint.cksums.before" - export VERBOSE="--verbose" - make generate manifests - cksum $INPUT_FILES > "$ARTIFACTS/lint.cksums.after" - diff "$ARTIFACTS/lint.cksums.before" "$ARTIFACTS/lint.cksums.after" + INPUT_FILES="$(git ls-files config) $(git ls-files | grep zz_generated)" + cksum ${INPUT_FILES} > "${ARTIFACTS}/lint.cksums.before" + export VERBOSE="--verbose" + make generate manifests + cksum ${INPUT_FILES} > "${ARTIFACTS}/lint.cksums.after" + diff "${ARTIFACTS}/lint.cksums.before" "${ARTIFACTS}/lint.cksums.after" else - "${CONTAINER_RUNTIME}" run --rm \ - --env IS_CONTAINER=TRUE \ - --env DEPLOY_KERNEL_URL=http://172.22.0.1/images/ironic-python-agent.kernel \ - --env DEPLOY_RAMDISK_URL=http://172.22.0.1/images/ironic-python-agent.initramfs \ - --env IRONIC_ENDPOINT=http://localhost:6385/v1/ \ - --volume "${PWD}:/go/src/github.com/metal3-io/baremetal-operator:rw,z" \ - --entrypoint sh \ - --workdir /go/src/github.com/metal3-io/baremetal-operator \ - docker.io/golang:1.22 \ - /go/src/github.com/metal3-io/baremetal-operator/hack/generate.sh "${@}" -fi; + "${CONTAINER_RUNTIME}" run --rm \ + --env IS_CONTAINER=TRUE \ + --env DEPLOY_KERNEL_URL=http://172.22.0.1/images/ironic-python-agent.kernel \ + --env DEPLOY_RAMDISK_URL=http://172.22.0.1/images/ironic-python-agent.initramfs \ + --env IRONIC_ENDPOINT=http://localhost:6385/v1/ \ + --volume "${PWD}:${WORKDIR}:rw,z" \ + --entrypoint sh \ + --workdir "${WORKDIR}" \ + docker.io/golang:1.22 \ + "${WORKDIR}"/hack/generate.sh "$@" +fi diff --git a/hack/golint.sh b/hack/golint.sh deleted file mode 100755 index 1e565f0858..0000000000 --- a/hack/golint.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/bin/sh - -set -eux - -IS_CONTAINER=${IS_CONTAINER:-false} -CONTAINER_RUNTIME="${CONTAINER_RUNTIME:-podman}" - -if [ "${IS_CONTAINER}" != "false" ]; then - export XDG_CACHE_HOME="/tmp/.cache" - - make lint -else - "${CONTAINER_RUNTIME}" run -u "$(id -u)" --rm \ - --env IS_CONTAINER=TRUE \ - --volume "${PWD}:/go/src/github.com/metal3-io/baremetal-operator:rw,z" \ - --entrypoint sh \ - --workdir /go/src/github.com/metal3-io/baremetal-operator \ - docker.io/golang:1.22 \ - /go/src/github.com/metal3-io/baremetal-operator/hack/golint.sh "${@}" -fi; diff --git a/hack/gomod.sh b/hack/gomod.sh index 15c06decf6..ce8c48a1ab 100755 --- a/hack/gomod.sh +++ b/hack/gomod.sh @@ -4,11 +4,13 @@ # 2. Verify that running the above doesn't change go.mod and go.sum # # NOTE: This won't work unless the build environment has internet access +# shellcheck disable=SC2292 set -eux IS_CONTAINER=${IS_CONTAINER:-false} CONTAINER_RUNTIME="${CONTAINER_RUNTIME:-podman}" +WORKDIR="${WORKDIR:-/workdir}" if [ "${IS_CONTAINER}" != "false" ]; then export XDG_CACHE_HOME=/tmp/.cache @@ -36,9 +38,9 @@ if [ "${IS_CONTAINER}" != "false" ]; then else "${CONTAINER_RUNTIME}" run --rm \ --env IS_CONTAINER=TRUE \ - --volume "${PWD}:/workdir:ro,z" \ + --volume "${PWD}:{WORKDIR}:ro,z" \ --entrypoint sh \ - --workdir /workdir \ + --workdir "${WORKDIR}" \ docker.io/golang:1.22 \ - /workdir/hack/gomod.sh "$@" + "${WORKDIR}"/hack/gomod.sh "$@" fi diff --git a/hack/manifestlint.sh b/hack/manifestlint.sh index afded49296..2847ee7fef 100755 --- a/hack/manifestlint.sh +++ b/hack/manifestlint.sh @@ -1,9 +1,11 @@ #!/bin/sh +# shellcheck disable=SC2292 set -eux -IS_CONTAINER=${IS_CONTAINER:-false} +IS_CONTAINER="${IS_CONTAINER:-false}" CONTAINER_RUNTIME="${CONTAINER_RUNTIME:-podman}" +WORKDIR="${WORKDIR:-/workdir}" K8S_VERSION="${K8S_VERSION:-master}" # --strict: Disallow additional properties not in schema. @@ -24,19 +26,19 @@ if [ "${IS_CONTAINER}" != "false" ]; then { set +x; } 2>/dev/null echo "<-------------------------STARTING MANIFESTS VALIDATION CHECKS------------------------->" "${KUBECONFORM_PATH:-}"kubeconform --strict --ignore-missing-schemas \ - --kubernetes-version "${K8S_VERSION}" \ - --ignore-filename-pattern kustom --ignore-filename-pattern patch \ - --ignore-filename-pattern controller_manager_config \ - --output tap \ - config/ examples/ + --kubernetes-version "${K8S_VERSION}" \ + --ignore-filename-pattern kustom --ignore-filename-pattern patch \ + --ignore-filename-pattern controller_manager_config \ + --output tap \ + config/ examples/ echo "<-------------------------COMPLETED MANIFESTS VALIDATION CHECKS------------------------>" else - "${CONTAINER_RUNTIME}" run --rm \ - --env IS_CONTAINER=TRUE \ - --env KUBECONFORM_PATH="/" \ - --volume "${PWD}:/workdir:ro,z" \ - --entrypoint sh \ - --workdir /workdir \ - ghcr.io/yannh/kubeconform:v0.6.2-alpine@sha256:49b5f6b320d30c1b8b72a7abdf02740ac9dc36a3ba23b934d1c02f7b37e6e740 \ - /workdir/hack/manifestlint.sh "${@}" -fi; + "${CONTAINER_RUNTIME}" run --rm \ + --env IS_CONTAINER=TRUE \ + --env KUBECONFORM_PATH="/" \ + --volume "${PWD}:${WORKDIR}:ro,z" \ + --entrypoint sh \ + --workdir "${WORKDIR}" \ + ghcr.io/yannh/kubeconform:v0.6.2-alpine@sha256:49b5f6b320d30c1b8b72a7abdf02740ac9dc36a3ba23b934d1c02f7b37e6e740 \ + "${WORKDIR}"/hack/manifestlint.sh "$@" +fi diff --git a/hack/markdownlint.sh b/hack/markdownlint.sh index 5258d27417..37101ea199 100755 --- a/hack/markdownlint.sh +++ b/hack/markdownlint.sh @@ -1,10 +1,12 @@ #!/bin/sh # markdownlint-cli2 has config file(s) named .markdownlint-cli2.yaml in the repo +# shellcheck disable=SC2292 set -eux IS_CONTAINER="${IS_CONTAINER:-false}" CONTAINER_RUNTIME="${CONTAINER_RUNTIME:-podman}" +WORKDIR="${WORKDIR:-/workdir}" # all md files, but ignore .github if [ "${IS_CONTAINER}" != "false" ]; then @@ -12,9 +14,9 @@ if [ "${IS_CONTAINER}" != "false" ]; then else "${CONTAINER_RUNTIME}" run --rm \ --env IS_CONTAINER=TRUE \ - --volume "${PWD}:/workdir:ro,z" \ + --volume "${PWD}:${WORKDIR}:ro,z" \ --entrypoint sh \ - --workdir /workdir \ + --workdir "${WORKDIR}" \ docker.io/pipelinecomponents/markdownlint-cli2:0.9.0@sha256:71370df6c967bae548b0bfd0ae313ddf44bfad87da76f88180eff55c6264098c \ - /workdir/hack/markdownlint.sh "$@" + "${WORKDIR}"/hack/markdownlint.sh "$@" fi diff --git a/hack/shellcheck.sh b/hack/shellcheck.sh index e7e7c0ba19..e3543ae2fa 100755 --- a/hack/shellcheck.sh +++ b/hack/shellcheck.sh @@ -1,19 +1,21 @@ #!/bin/sh +# shellcheck disable=SC2292 set -eux -IS_CONTAINER=${IS_CONTAINER:-false} +IS_CONTAINER="${IS_CONTAINER:-false}" CONTAINER_RUNTIME="${CONTAINER_RUNTIME:-podman}" +WORKDIR="${WORKDIR:-/workdir}" if [ "${IS_CONTAINER}" != "false" ]; then - TOP_DIR="${1:-.}" - find "${TOP_DIR}" -path ./vendor -prune -o -name '*.sh' -type f -exec shellcheck -s bash {} \+ + TOP_DIR="${1:-.}" + find "${TOP_DIR}" -path ./vendor -prune -o -name '*.sh' -type f -exec shellcheck -s bash {} \+ else - "${CONTAINER_RUNTIME}" run --rm \ - --env IS_CONTAINER=TRUE \ - --volume "${PWD}:/workdir:ro,z" \ - --entrypoint sh \ - --workdir /workdir \ - docker.io/koalaman/shellcheck-alpine:v0.10.0@sha256:5921d946dac740cbeec2fb1c898747b6105e585130cc7f0602eec9a10f7ddb63 \ - /workdir/hack/shellcheck.sh "$@" + "${CONTAINER_RUNTIME}" run --rm \ + --env IS_CONTAINER=TRUE \ + --volume "${PWD}:${WORKDIR}:ro,z" \ + --entrypoint sh \ + --workdir "${WORKDIR}" \ + docker.io/koalaman/shellcheck-alpine:v0.10.0@sha256:5921d946dac740cbeec2fb1c898747b6105e585130cc7f0602eec9a10f7ddb63 \ + "${WORKDIR}"/hack/shellcheck.sh "$@" fi diff --git a/hack/unit.sh b/hack/unit.sh deleted file mode 100755 index b48549593d..0000000000 --- a/hack/unit.sh +++ /dev/null @@ -1,29 +0,0 @@ -#!/bin/sh - -set -eux - -IS_CONTAINER=${IS_CONTAINER:-false} -ARTIFACTS=${ARTIFACTS:-/tmp} -CONTAINER_RUNTIME="${CONTAINER_RUNTIME:-podman}" -TEST_FLAGS="${TEST_FLAGS:--v}" - -if [ "${IS_CONTAINER}" != "false" ]; then - eval "$(go env)" - cd "${GOPATH}"/src/github.com/metal3-io/baremetal-operator - export XDG_CACHE_HOME="/tmp/.cache" - export COVER_PROFILE="${ARTIFACTS}"/cover.out - TEST_FLAGS=${TEST_FLAGS} make -e unit-cover - -else - "${CONTAINER_RUNTIME}" run --rm \ - --env IS_CONTAINER=TRUE \ - --env TEST_FLAGS="${TEST_FLAGS}" \ - --env DEPLOY_KERNEL_URL=http://172.22.0.1/images/ironic-python-agent.kernel \ - --env DEPLOY_RAMDISK_URL=http://172.22.0.1/images/ironic-python-agent.initramfs \ - --env IRONIC_ENDPOINT=http://localhost:6385/v1/ \ - --volume "${PWD}:/go/src/github.com/metal3-io/baremetal-operator:rw,z" \ - --entrypoint sh \ - --workdir /go/src/github.com/metal3-io/baremetal-operator \ - docker.io/golang:1.22 \ - /go/src/github.com/metal3-io/baremetal-operator/hack/unit.sh "${@}" -fi;