Skip to content

Commit b78cc09

Browse files
Fix copyright in the codegen
The script validates the copyright of every go file that we own as a codegen target. I think the most logical place to do this is in the make lint, however auto generated files inject a copyright from a static file, so we need to modify the auto generated code before the linting. So I see two options: 1- Verify everything in the codegen 2- Replace the autogenerated parts in the codegen and verify everything in the lint. Since the lint depends on the codegen, I decided that the simplicity of having just one script with just one call was more worthy than the correctness. Co-authored-by: Tom Wieczorek <[email protected]> Signed-off-by: Juan Luis de Sousa-Valadas Castaño <[email protected]>
1 parent 7bc5755 commit b78cc09

File tree

6 files changed

+88
-4
lines changed

6 files changed

+88
-4
lines changed

.github/workflows/lint.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ jobs:
3535
runs-on: ubuntu-latest
3636
steps:
3737
- uses: actions/checkout@v3
38+
with:
39+
fetch-depth: 0
3840

3941
- name: Prepare build environment
4042
run: .github/workflows/prepare-build-env.sh

.go-header.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright 2022 k0s authors
1+
Copyright {{YEAR}} k0s authors
22

33
Licensed under the Apache License, Version 2.0 (the "License");
44
you may not use this file except in compliance with the License.

.golangci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ linters-settings:
2626
min-confidence: 0
2727
goheader:
2828
template-path: .go-header.txt
29+
values:
30+
regexp:
31+
year: 202[0-9]
2932

3033
issues:
3134
max-issues-per-linter: 0

Makefile

+5-2
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,15 @@ codegen_targets += pkg/apis/autopilot.k0sproject.io/v1beta2/.controller-gen.stam
118118
pkg/apis/autopilot.k0sproject.io/v1beta2/.controller-gen.stamp: $(shell find pkg/apis/autopilot.k0sproject.io/v1beta2/ -maxdepth 1 -type f -name \*.go)
119119
pkg/apis/autopilot.k0sproject.io/v1beta2/.controller-gen.stamp: gen_output_dir = autopilot
120120

121-
pkg/apis/%/.controller-gen.stamp: .k0sbuild.docker-image.k0s hack/tools/Makefile.variables
121+
pkg/apis/%/.controller-gen.stamp: .k0sbuild.docker-image.k0s hack/tools/boilerplate.go.txt hack/tools/Makefile.variables
122122
rm -rf 'static/manifests/$(gen_output_dir)/CustomResourceDefinition'
123123
rm -f -- '$(dir $@)'zz_*.go
124124
CGO_ENABLED=0 $(GO) install sigs.k8s.io/controller-tools/cmd/controller-gen@v$(controller-gen_version)
125125
$(GO_ENV) controller-gen \
126126
crd \
127127
paths="./$(dir $@)..." \
128128
output:crd:artifacts:config=./static/manifests/$(gen_output_dir)/CustomResourceDefinition \
129-
object
129+
object:headerFile=hack/tools/boilerplate.go.txt
130130
touch -- '$@'
131131

132132
# Note: k0s.k0sproject.io omits the version from the output directory, so this needs
@@ -153,6 +153,7 @@ pkg/apis/%/.client-gen.stamp: .k0sbuild.docker-image.k0s hack/tools/boilerplate.
153153
--output-package=github.com/k0sproject/k0s/pkg/apis/$(gen_output_dir)/
154154
touch -- '$@'
155155

156+
156157
codegen_targets += static/zz_generated_assets.go
157158
static_asset_dirs := static/manifests static/misc
158159
static/zz_generated_assets.go: .k0sbuild.docker-image.k0s hack/tools/Makefile.variables
@@ -212,6 +213,7 @@ bindata: static/zz_generated_assets.go pkg/assets/zz_generated_offsets_$(TARGET_
212213
.PHONY: lint
213214
lint: GOLANGCI_LINT_FLAGS ?=
214215
lint: .k0sbuild.docker-image.k0s go.sum codegen
216+
hack/copyright.sh
215217
CGO_ENABLED=0 $(GO) install github.com/golangci/golangci-lint/cmd/golangci-lint@v$(golangci-lint_version)
216218
$(GO_ENV) golangci-lint run --verbose $(GOLANGCI_LINT_FLAGS) $(GO_DIRS)
217219

@@ -277,6 +279,7 @@ clean: clean-gocache clean-docker-image clean-airgap-image-bundles
277279
-rm -f pkg/assets/zz_generated_offsets_*.go k0s k0s.exe .bins.*stamp bindata* static/zz_generated_assets.go
278280
-rm -rf $(K0S_GO_BUILD_CACHE)
279281
-find pkg/apis -type f \( -name .client-gen.stamp -or -name .controller-gen.stamp \) -delete
282+
-rm -f hack/.copyright.stamp
280283
-$(MAKE) -C docs clean
281284
-$(MAKE) -C embedded-bins clean
282285
-$(MAKE) -C inttest clean

hack/copyright.sh

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2023 k0s 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 -eu
18+
19+
RESULT=0
20+
FIX=${FIX:=n}
21+
22+
get_year(){
23+
# The -1 has to be added because if a file has been added, removed and added
24+
# again we'll get two lines with both dates. We only pick the newest one.
25+
YEAR=$(git log -1 --diff-filter=A --pretty=format:%ad --date=format:%Y -- "$1")
26+
if [ -z "$YEAR" ]; then
27+
echo "FATAL!! $1 doesn't seem to be commited in the repo" 1>&2
28+
exit 1
29+
fi
30+
echo $YEAR
31+
}
32+
33+
has_basic_copyright(){
34+
FILE=$1
35+
grep -q -F "Copyright k0s authors" "$FILE"
36+
}
37+
38+
has_date_copyright(){
39+
DATE=$1
40+
FILE=$2
41+
grep -q -F "Copyright $DATE k0s authors" "$FILE"
42+
}
43+
44+
# Deliberately do not search in docs as the date of the matches for the
45+
# Copyright notice aren't related to the date of the document.
46+
for i in $(find cmd hack internal inttest pkg static -type f -name '*.go' -not -name 'zz_generated*'); do
47+
DATE=$(get_year $i)
48+
49+
# codegen gets the header from a static file, so instead we'll replace it every time.
50+
# Also fix every file if FIX=y
51+
if [[ "$FIX" == 'y' ]] ; then
52+
sed -i "s/Copyright 20../Copyright $DATE/" "$i"
53+
fi
54+
55+
if [[ $i == "pkg/apis/k0s.k0sproject.io/clientset/"* ]] || [[ $i == "pkg/apis/autopilot.k0sproject.io/v1beta2/clientset/"* ]]; then
56+
if ! has_basic_copyright "$i"; then
57+
echo "ERROR: $i doesn't have a proper copyright notice" 1>&2
58+
RESULT=1
59+
fi
60+
else
61+
if ! has_date_copyright "$DATE" "$i"; then
62+
echo "ERROR: $i doesn't have a proper copyright notice" 1>&2
63+
RESULT=1
64+
fi
65+
fi
66+
done
67+
68+
if [[ "$RESULT" != 0 ]]; then
69+
if [[ "$FIX" == 'y' ]]; then
70+
echo "hack/copyright.sh can't fix the problem automatically. Manual intervention is required"
71+
else
72+
echo "Retry running the script with FIX=y hack/copyright.sh to see if can be fixed automatically"
73+
fi
74+
fi
75+
76+
exit $RESULT

hack/tools/boilerplate.go.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright 2022 k0s authors
2+
Copyright k0s authors
33

44
Licensed under the Apache License, Version 2.0 (the "License");
55
you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)