Skip to content

Commit

Permalink
Run pre-commit with make
Browse files Browse the repository at this point in the history
Updating pre-commit hook with make. pre-commit-golang
repo is no longer maintained[1]. Instead of using
unmaintained repo we can use make to run pre-commit.

[1] dnephin/pre-commit-golang#98
  • Loading branch information
amolkahat committed Feb 27, 2023
1 parent acae6ba commit 413f49e
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 52 deletions.
42 changes: 21 additions & 21 deletions .github/create_bundle.sh
Original file line number Diff line number Diff line change
Expand Up @@ -34,32 +34,32 @@ cat "${CLUSTER_BUNDLE_FILE}" | grep "image:"
# error will be reported to the console.
set +e
for csv_image in $(cat "${CLUSTER_BUNDLE_FILE}" | grep "image:" | sed -e "s|.*image:||" | sort -u); do
digest_image=""
echo "CSV line: ${csv_image}"
digest_image=""
echo "CSV line: ${csv_image}"

# case where @ is in the csv_image image
if [[ "$csv_image" =~ .*"@".* ]]; then
delimeter='@'
else
delimeter=':'
fi
# case where @ is in the csv_image image
if [[ "$csv_image" =~ .*"@".* ]]; then
delimeter='@'
else
delimeter=':'
fi

base_image=$(echo $csv_image | cut -f 1 -d${delimeter})
tag_image=$(echo $csv_image | cut -f 2 -d${delimeter})
base_image=$(echo $csv_image | cut -f 1 -d${delimeter})
tag_image=$(echo $csv_image | cut -f 2 -d${delimeter})

if [[ "$base_image:$tag_image" == "controller:latest" ]]; then
echo "$base_image:$tag_image becomes $OPERATOR_IMG_WITH_DIGEST"
sed -e "s|$base_image:$tag_image|$OPERATOR_IMG_WITH_DIGEST|g" -i "${CLUSTER_BUNDLE_FILE}"
else
digest_image=$(skopeo inspect docker://${base_image}${delimeter}${tag_image} | jq '.Digest' -r)
echo "Base image: $base_image"
if [ -n "$digest_image" ]; then
echo "$base_image${delimeter}$tag_image becomes $base_image@$digest_image"
sed -i "s|$base_image$delimeter$tag_image|$base_image@$digest_image|g" "${CLUSTER_BUNDLE_FILE}"
if [[ "$base_image:$tag_image" == "controller:latest" ]]; then
echo "$base_image:$tag_image becomes $OPERATOR_IMG_WITH_DIGEST"
sed -e "s|$base_image:$tag_image|$OPERATOR_IMG_WITH_DIGEST|g" -i "${CLUSTER_BUNDLE_FILE}"
else
echo "$base_image${delimeter}$tag_image not changed"
digest_image=$(skopeo inspect docker://${base_image}${delimeter}${tag_image} | jq '.Digest' -r)
echo "Base image: $base_image"
if [ -n "$digest_image" ]; then
echo "$base_image${delimeter}$tag_image becomes $base_image@$digest_image"
sed -i "s|$base_image$delimeter$tag_image|$base_image@$digest_image|g" "${CLUSTER_BUNDLE_FILE}"
else
echo "$base_image${delimeter}$tag_image not changed"
fi
fi
fi
done

echo "Resulting bundle file images:"
Expand Down
42 changes: 32 additions & 10 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,31 @@
repos:
- repo: local
hooks:
- id: golangci-lint
name: golangci-lint
language: golang
types: [go]
entry: make
args: ["golangci-lint"]
pass_filenames: false
- id: gofmt
name: gofmt
language: system
entry: make
args: ["fmt"]
pass_filenames: false
- id: govet
name: govet
language: system
entry: make
args: ["vet"]
pass_filenames: false
- id: gotidy
name: gotidy
language: system
entry: make
args: ["tidy"]
pass_filenames: false
- id: make-manifests
name: make-manifests
language: system
Expand All @@ -20,17 +45,8 @@ repos:
args: ['operator-lint']
pass_filenames: false

- repo: https://github.com/dnephin/pre-commit-golang
rev: v0.5.1
hooks:
- id: go-fmt
exclude: ^vendor
- id: go-vet
- id: go-mod-tidy
- id: go-lint

- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.3.0
rev: v4.4.0
hooks:
- id: check-added-large-files
- id: fix-byte-order-marker
Expand All @@ -51,3 +67,9 @@ repos:
- id: no-commit-to-branch
- id: trailing-whitespace
exclude: ^vendor

- repo: https://github.com/openstack/bashate.git
rev: 2.1.1
hooks:
- id: bashate
entry: bashate --error . --ignore=E006,E040,E011,E020,E012
14 changes: 13 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ help: ## Display this help.
##@ Development

.PHONY: manifests
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
manifests: gowork controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) rbac:roleName=manager-role crd$(CRDDESC_OVERRIDE) webhook paths="./..." output:crd:artifacts:config=config/crd/bases && \
rm -f apis/bases/* && cp -a config/crd/bases apis/

Expand All @@ -104,6 +104,18 @@ vet: gowork ## Run go vet against code.
go vet ./...
go vet ./apis/...

.PHONY: tidy
tidy: fmt
go mod tidy; \
pushd "$(LOCALBIN)/../apis"; \
go mod tidy; \
popd

.PHONY: golangci-lint
golangci-lint:
test -s $(LOCALBIN)/golangci-lint || curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s v1.51.2
$(LOCALBIN)/golangci-lint run --fix

.PHONY: test
test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
Expand Down
6 changes: 3 additions & 3 deletions hack/create_self_signed_cert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ DNS.2 = ${SERVICE}.${NAMESPACE}.svc.cluster.local
DNS.2 = ${SERVICE}.${NAMESPACE}.pod.cluster.local
EOF

openssl req -newkey rsa:4096 -days 3650 -nodes -x509 \
-keyout ${TMPDIR}/tls.key \
-out ${TMPDIR}/tls.crt -config ${TMPDIR}/tls.conf
openssl req -newkey rsa:4096 -days 3650 -nodes -x509 \
-keyout ${TMPDIR}/tls.key \
-out ${TMPDIR}/tls.crt -config ${TMPDIR}/tls.conf
34 changes: 17 additions & 17 deletions hack/pin-custom-bundle-dockerfile.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ cp custom-bundle.Dockerfile custom-bundle.Dockerfile.pinned

#loop over each openstack-k8s-operators go.mod entry
for MOD_PATH in $(go list -m -json all | jq -r '. | select(.Path | contains("openstack")) | .Replace // . |.Path' | grep -v apis | grep -v openstack-operator | grep -v lib-common); do
MOD_VERSION=$(go list -m -json all | jq -r ". | select(.Path | contains(\"openstack\")) | .Replace // . | select( .Path == \"$MOD_PATH\") | .Version")
MOD_VERSION=$(go list -m -json all | jq -r ". | select(.Path | contains(\"openstack\")) | .Replace // . | select( .Path == \"$MOD_PATH\") | .Version")

BASE=$(echo $MOD_PATH | sed -e 's|github.com/.*/\(.*\)-operator/.*|\1|')
BASE=$(echo $MOD_PATH | sed -e 's|github.com/.*/\(.*\)-operator/.*|\1|')

REF=$(echo $MOD_VERSION | sed -e 's|v0.0.0-[0-9]*-\(.*\)$|\1|')
GITHUB_USER=$(echo $MOD_PATH | sed -e 's|github.com/\(.*\)/.*-operator/.*$|\1|')
REPO_CURL_URL="https://quay.io/api/v1/repository/openstack-k8s-operators"
REPO_URL="quay.io/openstack-k8s-operators"
if [[ "$GITHUB_USER" != "openstack-k8s-operators" || "$BASE" == "$IMAGEBASE" ]]; then
if [[ "$IMAGENAMESPACE" != "openstack-k8s-operators" || "${IMAGEREGISTRY}" != "quay.io" ]]; then
REPO_CURL_URL="https://${IMAGEREGISTRY}/api/v1/repository/${IMAGENAMESPACE}"
REPO_URL="${IMAGEREGISTRY}/${IMAGENAMESPACE}"
else
REPO_CURL_URL="https://quay.io/api/v1/repository/${GITHUB_USER}"
REPO_URL="quay.io/${GITHUB_USER}"
fi
fi
REF=$(echo $MOD_VERSION | sed -e 's|v0.0.0-[0-9]*-\(.*\)$|\1|')
GITHUB_USER=$(echo $MOD_PATH | sed -e 's|github.com/\(.*\)/.*-operator/.*$|\1|')
REPO_CURL_URL="https://quay.io/api/v1/repository/openstack-k8s-operators"
REPO_URL="quay.io/openstack-k8s-operators"
if [[ "$GITHUB_USER" != "openstack-k8s-operators" ]]; then
if [[ "$IMAGENAMESPACE" != "openstack-k8s-operators" || "${IMAGEREGISTRY}" != "quay.io" ]]; then
REPO_CURL_URL="https://${IMAGEREGISTRY}/api/v1/repository/${IMAGENAMESPACE}"
REPO_URL="${IMAGEREGISTRY}/${IMAGENAMESPACE}"
else
REPO_CURL_URL="https://quay.io/api/v1/repository/${GITHUB_USER}"
REPO_URL="quay.io/${GITHUB_USER}"
fi
fi

SHA=$(curl -s ${REPO_CURL_URL}/$BASE-operator-bundle/tag/ | jq -r .tags[].name | sort -u | grep $REF)
sed -i custom-bundle.Dockerfile.pinned -e "s|quay.io/openstack-k8s-operators/${BASE}-operator-bundle.*|${REPO_URL}/${BASE}-operator-bundle:$SHA|"
SHA=$(curl -s ${REPO_CURL_URL}/$BASE-operator-bundle/tag/ | jq -r .tags[].name | sort -u | grep $REF)
sed -i custom-bundle.Dockerfile.pinned -e "s|quay.io/openstack-k8s-operators/${BASE}-operator-bundle.*|${REPO_URL}/${BASE}-operator-bundle:$SHA|"
done

0 comments on commit 413f49e

Please sign in to comment.