| 1 |
/* |
| 2 |
Copyright 2016 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 |
|
| 17 |
// import-boss enforces import restrictions in a given repository. |
| 18 |
// |
| 19 |
// When a package is verified, import-boss looks for files called |
| 20 |
// ".import-restrictions" in all directories between the package and |
| 21 |
// the $GOPATH/src. |
| 22 |
// |
| 23 |
// All imports of the package are checked against each "rule" in the |
| 24 |
// found restriction files, climbing up the directory tree until the |
| 25 |
// import matches one of the rules. |
| 26 |
// |
| 27 |
// If the import does not match any of the rules, it is accepted. |
| 28 |
// |
| 29 |
// In analogy, all incoming imports of the package are checked against each |
| 30 |
// "inverse rule" in the found restriction files, climbing up the directory |
| 31 |
// tree until the import matches one of the rules. |
| 32 |
// |
| 33 |
// If the incoming import does not match any of the inverse rules, it is accepted. |
| 34 |
// |
| 35 |
// A rule consists of three parts: |
| 36 |
// * A SelectorRegexp, to select the import paths that the rule applies to. |
| 37 |
// * A list of AllowedPrefixes |
| 38 |
// * A list of ForbiddenPrefixes |
| 39 |
// An import passes a rule of a matching selector if it matches at least one |
| 40 |
// allowed prefix, but no forbidden prefix. |
| 41 |
// |
| 42 |
// An example file looks like this: |
| 43 |
// |
| 44 |
// { |
| 45 |
// "Rules": [ |
| 46 |
// { |
| 47 |
// "SelectorRegexp": "k8s[.]io", |
| 48 |
// "AllowedPrefixes": [ |
| 49 |
// "k8s.io/gengo/examples", |
| 50 |
// "k8s.io/kubernetes/third_party" |
| 51 |
// ], |
| 52 |
// "ForbiddenPrefixes": [ |
| 53 |
// "k8s.io/kubernetes/pkg/third_party/deprecated" |
| 54 |
// ] |
| 55 |
// }, |
| 56 |
// { |
| 57 |
// "SelectorRegexp": "^unsafe$", |
| 58 |
// "AllowedPrefixes": [ |
| 59 |
// ], |
| 60 |
// "ForbiddenPrefixes": [ |
| 61 |
// "" |
| 62 |
// ] |
| 63 |
// } |
| 64 |
// ], |
| 65 |
// "InverseRules": [{ |
| 66 |
// "SelectorRegexp": "k8s[.]io", |
| 67 |
// "AllowedPrefixes": [ |
| 68 |
// "k8s.io/same-repo", |
| 69 |
// "k8s.io/kubernetes/pkg/legacy" |
| 70 |
// ], |
| 71 |
// "ForbiddenPrefixes": [ |
| 72 |
// "k8s.io/kubernetes/pkg/legacy/subpkg" |
| 73 |
// ] |
| 74 |
// }, |
| 75 |
// { |
| 76 |
// "SelectorRegexp": "k8s[.]io", |
| 77 |
// "Transitive": true, |
| 78 |
// "AllowedPrefixes": [ |
| 79 |
// "k8s.io/ |
| 80 |
// ], |
| 81 |
// "ForbiddenPrefixes": [ |
| 82 |
// "k8s.io/kubernetes/cmd/kubelet", |
| 83 |
// "k8s.io/kubernetes/cmd/kubectl" |
| 84 |
// ], |
| 85 |
// ] |
| 86 |
// } |
| 87 |
// |
| 88 |
// Note the second (non-inverse) rule explicitly matches the unsafe package, and forbids it |
| 89 |
// ("" is a prefix of everything). |
| 90 |
// |
| 91 |
// An import from another package passes an inverse rule with a matching selector if |
| 92 |
// it matches at least one allowed prefix, but no forbidden prefix. |
| 93 |
// |
| 94 |
// Note that the second InverseRule is transitive, the first only applies to direct imports. |
| 95 |
package main |
| 96 |
|
| 97 |
import ( |
| 98 |
"os" |
| 99 |
|
| 100 |
"k8s.io/gengo/args" |
| 101 |
"k8s.io/gengo/examples/import-boss/generators" |
| 102 |
|
| 103 |
"k8s.io/klog/v2" |
| 104 |
) |
| 105 |
|
| 106 |
func main() { |
| 107 |
klog.InitFlags(nil) |
| 108 |
arguments := args.Default() |
| 109 |
if err := arguments.Execute( |
| 110 |
generators.NameSystems(), |
| 111 |
generators.DefaultNameSystem(), |
| 112 |
generators.Packages, |
| 113 |
); err != nil { |
| 114 |
klog.Errorf("Error: %v", err) |
| 115 |
os.Exit(1) |
| 116 |
} |
| 117 |
klog.V(2).Info("Completed successfully.") |
| 118 |
} |