|
| 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) |
0 commit comments