Skip to content

Commit 7b208b6

Browse files
committed
create a file to hold documentation of import-boss in one place
1 parent 0f336e0 commit 7b208b6

File tree

2 files changed

+97
-42
lines changed

2 files changed

+97
-42
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
## Purpose
2+
3+
- `import-boss` enforces import restrictions against all pull requests submitted to the [k/k](https://github.com/kubernetes/kubernetes) repository. There are a number of `.import-restrictions` files that in the [k/k](https://github.com/kubernetes/kubernetes) repository, all of which are defined in `YAML` (or `JSON`) format.
4+
5+
## How does it work?
6+
7+
- When a directory is verified, `import-boss` looks for a file called `.import-restrictions`. If this file is not found, `import-boss` will go up to the parent directory until it finds this `.import-restrictions` file.
8+
9+
- Adding `.import-restrictions` files does not add them to CI runs. They need to be explicitly added to `hack/verify-import-boss.sh`. Once an `.import-restrictions` file is added, all of the sub-packages of this file's directory are added as well.
10+
11+
### What are Rules?
12+
13+
- If an `.import-restrictions` file is found, then all imports of the package are checked against each `rule` in the file. A `rule` consists of three parts:
14+
- A `SelectorRegexp`, to select the import paths that the rule applies to.
15+
- A list of `AllowedPrefixes`
16+
- A list of `ForbiddenPrefixes`
17+
18+
- An import is allowed if it matches at least one allowed prefix and does not match any forbidden prefixes. An example `.import-restrictions` file looks like this:
19+
20+
```json
21+
{
22+
"Rules": [
23+
{
24+
"SelectorRegexp": "k8s[.]io",
25+
"AllowedPrefixes": [
26+
"k8s.io/gengo/examples",
27+
"k8s.io/kubernetes/third_party"
28+
],
29+
"ForbiddenPrefixes": [
30+
"k8s.io/kubernetes/pkg/third_party/deprecated"
31+
]
32+
},
33+
{
34+
"SelectorRegexp": "^unsafe$",
35+
"AllowedPrefixes": [
36+
],
37+
"ForbiddenPrefixes": [
38+
""
39+
]
40+
}
41+
]
42+
}
43+
```
44+
- Take note of `"SelectorRegexp": "k8s[.]io"` in the first block. This specifies that we are applying these rules to the `"k8s.io"` import path.
45+
- The second block explicitly matches the "unsafe" package, and forbids it ("" is a prefix of everything).
46+
47+
### What are Inverse Rules?
48+
49+
- In contrast to non-inverse rules, which are defined in importing packages, inverse rules are defined in imported packages.
50+
51+
- Inverse rules allow for fine-grained import restrictions for "private packages" where we don't want to spread use inside of [kubernetes/kubernetes](https://github.com/kubernetes/kubernetes).
52+
53+
- If an `.import-restrictions` file is found, then all imports of the package are checked against each `inverse rule` in the file. This check will continue, climbing up the directory tree, until a match is found and accepted.
54+
55+
- Inverse rules also have a boolean `transitive` option. When this option is true, the import rule is also applied to `transitive` imports.
56+
- `transitive` imports are dependencies not directly depended on by the code, but are needed to run the application. Use this option if you want to apply restrictions to those indirect dependencies.
57+
58+
```yaml
59+
rules:
60+
- selectorRegexp: k8s[.]io
61+
allowedPrefixes:
62+
- k8s.io/gengo/examples
63+
- k8s.io/kubernetes/third_party
64+
forbiddenPrefixes:
65+
- k8s.io/kubernetes/pkg/third_party/deprecated
66+
- selectorRegexp: ^unsafe$
67+
forbiddenPrefixes:
68+
- ""
69+
inverseRules:
70+
- selectorRegexp: k8s[.]io
71+
allowedPrefixes:
72+
- k8s.io/same-repo
73+
- k8s.io/kubernetes/pkg/legacy
74+
forbiddenPrefixes:
75+
- k8s.io/kubernetes/pkg/legacy/subpkg
76+
- selectorRegexp: k8s[.]io
77+
transitive: true
78+
forbiddenPrefixes:
79+
- k8s.io/kubernetes/cmd/kubelet
80+
- k8s.io/kubernetes/cmd/kubectl
81+
```
82+
83+
## How do I run import-boss within the k/k repo?
84+
85+
- In order to include _test.go files, make sure to pass in the `include-test-files` flag:
86+
```sh
87+
hack/verify-import-boss.sh --include-test-files=true
88+
```
89+
90+
- To include other directories, pass in a directory or directories using the `input-dirs` flag:
91+
```sh
92+
hack/verify-import-boss.sh --input-dirs="k8s.io/kubernetes/test/e2e/framework/..."
93+
```
94+
95+
## Reference
96+
97+
- [import-boss](https://github.com/kubernetes/gengo/tree/master/examples/import-boss)

staging/src/k8s.io/code-generator/cmd/import-boss/main.go

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,48 +15,6 @@ limitations under the License.
1515
*/
1616

1717
// import-boss enforces import restrictions in a given repository.
18-
//
19-
// When a directory is verified, import-boss looks for a file called
20-
// ".import-restrictions". If this file is not found, parent directories will be
21-
// recursively searched.
22-
//
23-
// If an ".import-restrictions" file is found, then all imports of the package
24-
// are checked against each "rule" in the file. A rule consists of three parts:
25-
//
26-
// - A SelectorRegexp, to select the import paths that the rule applies to.
27-
//
28-
// - A list of AllowedPrefixes
29-
//
30-
// - A list of ForbiddenPrefixes
31-
//
32-
// An import is allowed if it matches at least one allowed prefix and does not
33-
// match any forbidden prefix. An example file looks like this:
34-
//
35-
// {
36-
// "Rules": [
37-
// {
38-
// "SelectorRegexp": "k8s[.]io",
39-
// "AllowedPrefixes": [
40-
// "k8s.io/gengo/examples",
41-
// "k8s.io/kubernetes/third_party"
42-
// ],
43-
// "ForbiddenPrefixes": [
44-
// "k8s.io/kubernetes/pkg/third_party/deprecated"
45-
// ]
46-
// },
47-
// {
48-
// "SelectorRegexp": "^unsafe$",
49-
// "AllowedPrefixes": [
50-
// ],
51-
// "ForbiddenPrefixes": [
52-
// ""
53-
// ]
54-
// }
55-
// ]
56-
// }
57-
//
58-
// Note the second block explicitly matches the unsafe package, and forbids it
59-
// ("" is a prefix of everything).
6018
package main
6119

6220
import (

0 commit comments

Comments
 (0)