Skip to content

Commit ffee0a4

Browse files
authored
Merge pull request #6886 from oscr/add-dockerfile-lint
🌱 Add Dockerfile linter
2 parents bc177c6 + 0c66bcf commit ffee0a4

File tree

7 files changed

+62
-1
lines changed

7 files changed

+62
-1
lines changed

Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
# Build the manager binary
1818
# Run this with docker build --build-arg builder_image=<golang:x.y.z>
1919
ARG builder_image
20+
21+
# Ignore Hadolint rule "Always tag the version of an image explicitly."
22+
# It's an invalid finding since the image is explicitly set in the Makefile.
23+
# https://github.com/hadolint/hadolint/wiki/DL3006
24+
# hadolint ignore=DL3006
2025
FROM ${builder_image} as builder
2126
WORKDIR /workspace
2227

Makefile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ GO_APIDIFF_BIN := go-apidiff
111111
GO_APIDIFF := $(abspath $(TOOLS_BIN_DIR)/$(GO_APIDIFF_BIN)-$(GO_APIDIFF_VER))
112112
GO_APIDIFF_PKG := github.com/joelanford/go-apidiff
113113

114+
HADOLINT_VER := v2.10.0
115+
HADOLINT_FAILURE_THRESHOLD = warning
116+
114117
KPROMO_VER := v3.4.4
115118
KPROMO_BIN := kpromo
116119
KPROMO := $(abspath $(TOOLS_BIN_DIR)/$(KPROMO_BIN)-$(KPROMO_VER))
@@ -446,6 +449,11 @@ lint: $(GOLANGCI_LINT) ## Lint the codebase
446449
$(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_EXTRA_ARGS)
447450
cd $(TEST_DIR); $(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_EXTRA_ARGS)
448451
cd $(TOOLS_DIR); $(GOLANGCI_LINT) run -v $(GOLANGCI_LINT_EXTRA_ARGS)
452+
./scripts/ci-lint-dockerfiles.sh $(HADOLINT_VER) $(HADOLINT_FAILURE_THRESHOLD)
453+
454+
.PHONY: lint-dockerfiles
455+
lint-dockerfiles:
456+
./scripts/ci-lint-dockerfiles.sh $(HADOLINT_VER) $(HADOLINT_FAILURE_THRESHOLD)
449457

450458
.PHONY: lint-fix
451459
lint-fix: $(GOLANGCI_LINT) ## Lint the codebase and run auto-fixers if supported by the linter

cmd/clusterctl/Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
# Build the clusterctl binary
1818
# Run this with docker build --build-arg builder_image=<golang:x.y.z>
1919
ARG builder_image
20+
21+
# Ignore Hadolint rule "Always tag the version of an image explicitly."
22+
# It's an invalid finding since the image is explicitly set in the Makefile.
23+
# https://github.com/hadolint/hadolint/wiki/DL3006
24+
# hadolint ignore=DL3006
2025
FROM ${builder_image} as builder
2126
WORKDIR /workspace
2227

docs/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
FROM maven:3-jdk-8
3333

34-
RUN apt-get update && apt-get install -y --no-install-recommends graphviz fonts-symbola fonts-wqy-zenhei && rm -rf /var/lib/apt/lists/*
34+
RUN apt-get update && apt-get install -y --no-install-recommends graphviz=2.42.2-5 fonts-symbola=2.60-1.1 fonts-wqy-zenhei=0.9.45-8 && rm -rf /var/lib/apt/lists/*
3535
RUN wget -O /plantuml.jar http://sourceforge.net/projects/plantuml/files/plantuml.1.2019.6.jar/download
3636

3737
# By default, java writes a 'hsperfdata_<username>' directory in the work dir.

scripts/ci-lint-dockerfiles.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/bash
2+
3+
# Copyright 2022 The Kubernetes Authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -o errexit
18+
set -o nounset
19+
set -o pipefail
20+
21+
HADOLINT_VER=${1:-latest}
22+
HADOLINT_FAILURE_THRESHOLD=${2:-warning}
23+
24+
FILES=$(find -- * -name Dockerfile)
25+
while read -r file; do
26+
echo "Linting: ${file}"
27+
# Configure the linter to fail for warnings and errors. Can be set to: error | warning | info | style | ignore | none
28+
docker run --rm -i ghcr.io/hadolint/hadolint:"${HADOLINT_VER}" hadolint --failure-threshold "${HADOLINT_FAILURE_THRESHOLD}" - < "${file}"
29+
done <<< "${FILES}"

test/extension/Dockerfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
# Build the extension binary
1818
# Run this with docker build --build-arg builder_image=<golang:x.y.z>
1919
ARG builder_image
20+
21+
# Ignore Hadolint rule "Always tag the version of an image explicitly."
22+
# It's an invalid finding since the image is explicitly set in the Makefile.
23+
# https://github.com/hadolint/hadolint/wiki/DL3006
24+
# hadolint ignore=DL3006
2025
FROM ${builder_image} as builder
2126
WORKDIR /workspace
2227

test/infrastructure/docker/Dockerfile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616

1717
# Run this with docker build --build-arg builder_image=<golang:x.y.z>
1818
ARG builder_image
19+
20+
# Ignore Hadolint rule "Always tag the version of an image explicitly."
21+
# It's an invalid finding since the image is explicitly set in the Makefile.
22+
# https://github.com/hadolint/hadolint/wiki/DL3006
23+
# hadolint ignore=DL3006
1924
FROM ${builder_image} as builder
2025

2126
# Run this with docker build --build-arg goproxy=$(go env GOPROXY) to override the goproxy
@@ -58,6 +63,10 @@ RUN --mount=type=cache,target=/root/.cache/go-build \
5863
CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} go build -trimpath -a -o /workspace/manager main.go
5964

6065
# NOTE: CAPD can't use non-root because docker requires access to the docker socket
66+
67+
# Ignore Hadolint rule "Using latest is prone to errors if the image will ever update. Pin the version explicitly to a release tag."
68+
# https://github.com/hadolint/hadolint/wiki/DL3007
69+
# hadolint ignore=DL3007
6170
FROM gcr.io/distroless/static:latest
6271

6372
WORKDIR /

0 commit comments

Comments
 (0)