Skip to content

Commit da74a51

Browse files
authored
Merge pull request kubernetes-sigs#2451 from sanya301/license-checker
🌱 Add script for checking licenses
2 parents e395f98 + 422d8b2 commit da74a51

File tree

6 files changed

+78
-13
lines changed

6 files changed

+78
-13
lines changed

Makefile

+5-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ go-apidiff:
9898
##@ Tests
9999

100100
.PHONY: test
101-
test: test-unit test-integration test-testdata test-book ## Run the unit and integration tests (used in the CI)
101+
test: test-unit test-integration test-testdata test-book test-license ## Run the unit and integration tests (used in the CI)
102102

103103
.PHONY: test-unit
104104
TEST_PKGS := ./pkg/... ./test/e2e/utils/...
@@ -134,3 +134,7 @@ test-e2e-ci: ## Run the end-to-end tests (used in the CI)`
134134
.PHONY: test-book
135135
test-book: ## Run the cronjob tutorial's unit tests to make sure we don't break it
136136
cd ./docs/book/src/cronjob-tutorial/testdata/project && make test
137+
138+
.PHONY: test-license
139+
test-license: ## Run the license check
140+
./test/check-license.sh

docs/book/src/cronjob-tutorial/testdata/project/controllers/cronjob_controller_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
/*
2-
Ideally, we should have one `<kind>_controller_test.go` for each controller scaffolded and called in the `suite_test.go`.
3-
So, let's write our example test for the CronJob controller (`cronjob_controller_test.go.`)
4-
*/
5-
61
/*
72
83
Licensed under the Apache License, Version 2.0 (the "License");
@@ -19,6 +14,11 @@ limitations under the License.
1914
*/
2015
// +kubebuilder:docs-gen:collapse=Apache License
2116

17+
/*
18+
Ideally, we should have one `<kind>_controller_test.go` for each controller scaffolded and called in the `suite_test.go`.
19+
So, let's write our example test for the CronJob controller (`cronjob_controller_test.go.`)
20+
*/
21+
2222
/*
2323
As usual, we start with the necessary imports. We also define some utility variables.
2424
*/

docs/book/src/cronjob-tutorial/testdata/project/controllers/suite_test.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,3 @@
1-
/*
2-
When we created the CronJob API with `kubebuilder create api` in a [previous chapter](/cronjob-tutorial/new-api.md), Kubebuilder already did some test work for you.
3-
Kubebuilder scaffolded a `controllers/suite_test.go` file that does the bare bones of setting up a test environment.
4-
5-
First, it will contain the necessary imports.
6-
*/
7-
81
/*
92
Copyright 2022 The Kubernetes authors.
103
@@ -21,6 +14,14 @@ See the License for the specific language governing permissions and
2114
limitations under the License.
2215
*/
2316
// +kubebuilder:docs-gen:collapse=Apache License
17+
18+
/*
19+
When we created the CronJob API with `kubebuilder create api` in a [previous chapter](/cronjob-tutorial/new-api.md), Kubebuilder already did some test work for you.
20+
Kubebuilder scaffolded a `controllers/suite_test.go` file that does the bare bones of setting up a test environment.
21+
22+
First, it will contain the necessary imports.
23+
*/
24+
2425
package controllers
2526

2627
import (

pkg/plugins/golang/v3/scaffolds/internal/templates/api/webhook_suitetest.go

+16
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
Copyright 2022 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
117
package api
218

319
import (

test/check-license.sh

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/bin/bash
2+
3+
# Copyright 2021 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+
source $(dirname "$0")/common.sh
22+
23+
echo "Checking for license header..."
24+
allfiles=$(listFiles|grep -v ./internal/bindata/...)
25+
licRes=""
26+
for file in $allfiles; do
27+
if ! head -n4 "${file}" | grep -Eq "(Copyright|generated|GENERATED|Licensed)" ; then
28+
licRes="${licRes}\n"$(echo -e " ${file}")
29+
fi
30+
done
31+
if [ -n "${licRes}" ]; then
32+
echo -e "license header checking failed:\n${licRes}"
33+
exit 255
34+
fi

test/common.sh

+10
Original file line numberDiff line numberDiff line change
@@ -140,3 +140,13 @@ function is_installed {
140140
fi
141141
return 1
142142
}
143+
144+
function listPkgDirs() {
145+
go list -f '{{.Dir}}' ./... | grep -v generated
146+
}
147+
148+
#Lists all go files
149+
function listFiles() {
150+
# pipeline is much faster than for loop
151+
listPkgDirs | xargs -I {} find {} \( -name '*.go' -o -name '*.sh' \) | grep -v generated
152+
}

0 commit comments

Comments
 (0)