Skip to content

Commit

Permalink
Merge pull request #285 from sthaha/chore-format-shell-scripts
Browse files Browse the repository at this point in the history
chore: ensure all scripts are formatted the same
  • Loading branch information
sthaha authored Oct 19, 2023
2 parents 672efdf + bf6d2a3 commit 8cca9bb
Show file tree
Hide file tree
Showing 12 changed files with 255 additions and 247 deletions.
14 changes: 8 additions & 6 deletions .github/workflows/pr-checks.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ jobs:
make docs
git diff --exit-code
gofmt:
fmt:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main
- uses: actions/setup-go@main
with:
go-version-file: go.mod
- name: run gofmt test
run: ./automation/presubmit-tests/gofmt.sh
- name: Install all tools
uses: ./.github/tools-cache
- name: make fmt
run: make fmt && git diff --exit-code

escapes_detect:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -71,7 +73,7 @@ jobs:
run: make govulncheck

bundle:
needs: [docs, golangci, gofmt, vulnerability_detect, escapes_detect]
needs: [docs, golangci, fmt, vulnerability_detect, escapes_detect]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@main
Expand All @@ -87,7 +89,7 @@ jobs:
git diff --exit-code
build-images:
needs: [docs, golangci, gofmt, vulnerability_detect, escapes_detect]
needs: [docs, golangci, fmt, vulnerability_detect, escapes_detect]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand Down Expand Up @@ -115,7 +117,7 @@ jobs:
additional_tags: ${{ steps.additional_tags.outputs.result }}

e2e:
needs: [docs, golangci, gofmt, vulnerability_detect, escapes_detect]
needs: [docs, golangci, fmt, vulnerability_detect, escapes_detect]
env:
KIND_VERSION: "0.15.0"
GO111MODULE: "on"
Expand Down
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,11 @@ generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./pkg/..."

.PHONY: fmt
fmt: ## Run go fmt against code.
fmt: shfmt ## Run go fmt against code.
go fmt ./...
PATH=./tmp/bin:$$PATH \
shfmt -l -w ./**/*.sh \
./must-gather/gather* ./must-gather/common

.PHONY: vet
vet: ## Run go vet against code.
Expand Down Expand Up @@ -281,6 +284,10 @@ yq: ## Download yq locally if necessary
oc: ## Download oc locally if necessary
@./hack/tools.sh oc

.PHONY: shfmt
shfmt: ## Download shmft locally if necessary
@./hack/tools.sh shfmt

.PHONY: install-govulncheck
install-govulncheck: ## Download govulncheck locally if necessary
@./hack/tools.sh govulncheck
Expand Down
35 changes: 0 additions & 35 deletions automation/presubmit-tests/gofmt.sh

This file was deleted.

20 changes: 10 additions & 10 deletions hack/cluster-clean.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ source ./hack/common.sh
MANIFESTS_OUT_DIR=${MANIFESTS_OUT_DIR:-"_output/generated-manifest"}

function main() {
echo "Cleaning up ..."
echo "Cleaning up ..."

if [ ! -d "${MANIFESTS_OUT_DIR}" ]; then
echo "Directory ${MANIFESTS_OUT_DIR} DOES NOT exists. Run make generate first."
exit
fi
if [ ! -d "${MANIFESTS_OUT_DIR}" ]; then
echo "Directory ${MANIFESTS_OUT_DIR} DOES NOT exists. Run make generate first."
exit
fi

# Ignore errors because some clusters might not have prometheus operator
kubectl delete --ignore-not-found=true -f ${MANIFESTS_OUT_DIR}/*.yaml || true
# Ignore errors because some clusters might not have prometheus operator
kubectl delete --ignore-not-found=true -f ${MANIFESTS_OUT_DIR}/*.yaml || true

sleep 2
sleep 2

echo "Done $0"
echo "Done $0"
}

main "$@"
main "$@"
23 changes: 11 additions & 12 deletions hack/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

set -e


_registry_port="5001"
_registry_name="kind-registry"

Expand All @@ -42,21 +41,21 @@ IMAGE_TAG=${IMAGE_TAG:-devel}
PLATFORM=$(uname -m)
case ${PLATFORM} in
x86_64* | i?86_64* | amd64*)
ARCH="amd64"
;;
ARCH="amd64"
;;
ppc64le)
ARCH="ppc64le"
;;
ARCH="ppc64le"
;;
aarch64* | arm64*)
ARCH="arm64"
;;
ARCH="arm64"
;;
*)
echo "invalid Arch, only support x86_64, ppc64le, aarch64"
exit 1
;;
echo "invalid Arch, only support x86_64, ppc64le, aarch64"
exit 1
;;
esac

# the cluster kind is a kubernetes cluster
if [ ${CLUSTER_PROVIDER} = "kind" ]; then
CLUSTER_PROVIDER="kubernetes"
fi
CLUSTER_PROVIDER="kubernetes"
fi
68 changes: 34 additions & 34 deletions hack/integration_cluster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,32 @@ NGINX_HTTP_PORT=${ENV_NETWORK_INGRESS_HTTP_PORT:-80}
NGINX_HTTPS_PORT=${ENV_NETWORK_INGRESS_HTTPS_PORT:-443}

main() {
if [[ $# -lt 1 ]] ; then
exit 0
else
MODE=$1
shift
fi
if [[ $# -lt 1 ]]; then
exit 0
else
MODE=$1
shift
fi

if [ "${MODE}" == "kind" ]; then
kindTest
fi
if [ "${MODE}" == "kind" ]; then
kindTest
fi
}

function kindTest() {
echo "install kind"
go install sigs.k8s.io/kind@v0.12.0
echo "install kind"
go install sigs.k8s.io/kind@v0.12.0

echo "Starting kind with cluster name \"${CLUSTER_NAME}\""
echo "Starting kind with cluster name \"${CLUSTER_NAME}\""

local reg_name=${LOCAL_REGISTRY_NAME}
local reg_port=${LOCAL_REGISTRY_PORT}
local ingress_http_port=${NGINX_HTTP_PORT}
local ingress_https_port=${NGINX_HTTPS_PORT}
docker rm -f ${reg_name}
kind delete cluster --name $CLUSTER_NAME
local reg_name=${LOCAL_REGISTRY_NAME}
local reg_port=${LOCAL_REGISTRY_PORT}
local ingress_http_port=${NGINX_HTTP_PORT}
local ingress_https_port=${NGINX_HTTPS_PORT}
docker rm -f ${reg_name}
kind delete cluster --name $CLUSTER_NAME

cat <<EOF | kind create cluster -v=6 --name $CLUSTER_NAME --config=-
cat <<EOF | kind create cluster -v=6 --name $CLUSTER_NAME --config=-
---
kind: Cluster
apiVersion: kind.x-k8s.io/v1alpha4
Expand All @@ -62,21 +62,21 @@ containerdConfigPatches:
endpoint = ["http://${reg_name}:${reg_port}"]
EOF

echo "Launching container registry \"${LOCAL_REGISTRY_NAME}\" at localhost:${LOCAL_REGISTRY_PORT}"
running="$(docker inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)"
if [ "${running}" != 'true' ]; then
docker run \
-d --restart=always -p "127.0.0.1:${reg_port}:5000" --name "${reg_name}" \
registry:2
fi
echo "Launching container registry \"${LOCAL_REGISTRY_NAME}\" at localhost:${LOCAL_REGISTRY_PORT}"
running="$(docker inspect -f '{{.State.Running}}' "${reg_name}" 2>/dev/null || true)"
if [ "${running}" != 'true' ]; then
docker run \
-d --restart=always -p "127.0.0.1:${reg_port}:5000" --name "${reg_name}" \
registry:2
fi

# connect the registry to the cluster network
# (the network may already be connected)
docker network connect "kind" "${reg_name}" || true
# connect the registry to the cluster network
# (the network may already be connected)
docker network connect "kind" "${reg_name}" || true

# Document the local registry
# https://github.com/kubernetes/enhancements/tree/master/keps/sig-cluster-lifecycle/generic/1755-communicating-a-local-registry
cat <<EOF | kubectl apply -f -
# Document the local registry
# https://github.com/kubernetes/enhancements/tree/master/keps/sig-cluster-lifecycle/generic/1755-communicating-a-local-registry
cat <<EOF | kubectl apply -f -
---
apiVersion: v1
kind: ConfigMap
Expand All @@ -89,7 +89,7 @@ data:
help: "https://kind.sigs.k8s.io/docs/user/local-registry/"
EOF

echo "Complete start kind"
echo "Complete start kind"
}

main $*
main $*
37 changes: 37 additions & 0 deletions hack/tools.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ declare -r OPERATOR_SDK_VERSION=${OPERATOR_SDK_VERSION:-v1.27.0}
declare -r YQ_VERSION=${YQ_VERSION:-v4.34.2}
declare -r CRDOC_VERSION=${CRDOC_VERSION:-v0.6.2}
declare -r OC_VERSION=${OC_VERSION:-4.13.0}
declare -r SHFMT_VERSION=${SHFMT_VERSION:-v3.7.0}

# install
declare -r KUSTOMIZE_INSTALL_SCRIPT="https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
Expand Down Expand Up @@ -123,6 +124,42 @@ install_yq() {
ok "yq was installed successfully"
}

go_install() {
local pkg="$1"
local version="$2"
shift 2

info "installing $pkg version: $version"

GOBIN=$LOCAL_BIN \
go install "$pkg@$version" || {
fail "failed to install $pkg - $version"
return 1
}
ok "$pkg - $version was installed successfully"

}

validate_verison() {
local cmd="$1"
local version_arg="$2"
local version_regex="$3"
shift 3

command -v "$cmd" >/dev/null 2>&1 || return 1
[[ $(eval "$cmd $version_arg" | grep -o "$version_regex") =~ $version_regex ]] || {
return 1
}

ok "$cmd installed successfully"
}
install_shfmt() {
validate_verison shfmt --version "$SHFMT_VERSION" && {
return 0
}
go_install mvdan.cc/sh/v3/cmd/shfmt "$SHFMT_VERSION"
}

install_crdoc() {
local version_regex="version $CRDOC_VERSION"

Expand Down
24 changes: 12 additions & 12 deletions must-gather/common
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,31 @@ set -eu -o pipefail
export KEPLER_NS="openshift-kepler-operator"
export LOGFILE_NAME="gather-debug.log"

get_timestamp(){
date -u +"%Y-%m-%d %H:%M:%SZ"
get_timestamp() {
date -u +"%Y-%m-%d %H:%M:%SZ"
}

log_stdout() {
echo "$(get_timestamp) $*"
echo "$(get_timestamp) $*"
}

log_file() {
echo "$(get_timestamp) $*" >> "$LOGFILE_PATH"
echo "$(get_timestamp) $*" >>"$LOGFILE_PATH"
}

log_stdout_and_file() {
echo "$(get_timestamp) $*" | tee -a "$LOGFILE_PATH"
echo "$(get_timestamp) $*" | tee -a "$LOGFILE_PATH"
}

log(){
log_stdout_and_file "$@"
log() {
log_stdout_and_file "$@"
}

run() {
length=$(($#-1))
cmd=${@:1:$length}
gather_file="${@: -1}"
length=$(($# - 1))
cmd=${@:1:$length}
gather_file="${@: -1}"

log_file "$cmd"
$cmd >> "$gather_file" 2>> "$LOGFILE_PATH" || true
log_file "$cmd"
$cmd >>"$gather_file" 2>>"$LOGFILE_PATH" || true
}
Loading

0 comments on commit 8cca9bb

Please sign in to comment.