Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an import verification tool #15620

Conversation

stevekuznetsov
Copy link
Contributor

In order to ensure that selected package trees in our source import only
from other package trees, we need a tool that will determine the imports
for a package tree and verify them all.

Signed-off-by: Steve Kuznetsov skuznets@redhat.com

/cc @deads2k @sttts

@openshift-merge-robot openshift-merge-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Aug 2, 2017
@stevekuznetsov
Copy link
Contributor Author

As we will soon see in the test output...

$ hack/verify-imports.sh 
[WARNING] No compiled `import-verifier` binary was found. Attempting to build one using:
[WARNING]   $ hack/build-go.sh tools/import-verifier
++ Building go targets for linux/amd64: tools/import-verifier
/home/skuznets/Documents/go/src/github.com/openshift/origin/hack/build-go.sh took 3 seconds
2017/08/02 14:20:24 Inspecting imports under pkg/image...
2017/08/02 14:20:25 -- validating imports for 58 packages in the tree
2017/08/02 14:20:25 -- found forbidden imports for pkg/image/admission:
2017/08/02 14:20:25 	pkg/quota/util
2017/08/02 14:20:25 -- found forbidden imports for pkg/image/admission/imagepolicy:
2017/08/02 14:20:25 	pkg/api/latest
2017/08/02 14:20:25 	pkg/api/meta
2017/08/02 14:20:25 	pkg/cmd/server/admission
2017/08/02 14:20:25 	pkg/cmd/server/api/latest
2017/08/02 14:20:25 	pkg/project/cache
2017/08/02 14:20:25 	pkg/build/apis/build
2017/08/02 14:20:25 -- found forbidden imports for pkg/image/admission/imagepolicy/api/install:
2017/08/02 14:20:25 	pkg/cmd/server/api
2017/08/02 14:20:25 -- found forbidden imports for pkg/image/apis/image/v1:
2017/08/02 14:20:25 	test/util/api
2017/08/02 14:20:25 	pkg/api
2017/08/02 14:20:25 	pkg/api/install
2017/08/02 14:20:25 -- found forbidden imports for pkg/image/apis/image/validation:
2017/08/02 14:20:25 	pkg/cmd/server/api
2017/08/02 14:20:25 	pkg/util/strings
2017/08/02 14:20:25 -- found forbidden imports for pkg/image/controller:
2017/08/02 14:20:25 	pkg/controller
2017/08/02 14:20:25 	pkg/api/install
2017/08/02 14:20:25 -- found forbidden imports for pkg/image/controller/trigger:
2017/08/02 14:20:25 	pkg/build/apis/build
2017/08/02 14:20:25 	pkg/build/generator
2017/08/02 14:20:25 	pkg/build/util
2017/08/02 14:20:25 	pkg/cmd/server/bootstrappolicy
2017/08/02 14:20:25 	pkg/deploy/apis/apps
2017/08/02 14:20:25 -- found forbidden imports for pkg/image/graph:
2017/08/02 14:20:25 	pkg/api/graph
2017/08/02 14:20:25 -- found forbidden imports for pkg/image/graph/nodes:
2017/08/02 14:20:25 	pkg/api/graph
2017/08/02 14:20:25 -- found forbidden imports for pkg/image/importer:
2017/08/02 14:20:25 	pkg/dockerregistry
2017/08/02 14:20:25 	pkg/api/install
2017/08/02 14:20:25 -- found forbidden imports for pkg/image/prune:
2017/08/02 14:20:25 	pkg/build/graph/nodes
2017/08/02 14:20:25 	pkg/build/util
2017/08/02 14:20:25 	pkg/deploy/apis/apps
2017/08/02 14:20:25 	pkg/deploy/graph/nodes
2017/08/02 14:20:25 	pkg/util/netutils
2017/08/02 14:20:25 	pkg/api/graph
2017/08/02 14:20:25 	pkg/api/kubegraph/nodes
2017/08/02 14:20:25 	pkg/build/apis/build
2017/08/02 14:20:25 -- found forbidden imports for pkg/image/registry/image/etcd:
2017/08/02 14:20:25 	pkg/util/restoptions
2017/08/02 14:20:25 	pkg/api/install
2017/08/02 14:20:25 	pkg/api/latest
2017/08/02 14:20:25 -- found forbidden imports for pkg/image/registry/imagestream:
2017/08/02 14:20:25 	pkg/authorization/apis/authorization
2017/08/02 14:20:25 	pkg/authorization/registry/subjectaccessreview
2017/08/02 14:20:25 -- found forbidden imports for pkg/image/registry/imagestream/etcd:
2017/08/02 14:20:25 	pkg/authorization/registry/subjectaccessreview
2017/08/02 14:20:25 	pkg/util/restoptions
2017/08/02 14:20:25 	pkg/api/install
2017/08/02 14:20:25 	pkg/api/latest
2017/08/02 14:20:25 	pkg/authorization/apis/authorization
2017/08/02 14:20:25 -- found forbidden imports for pkg/image/registry/imagestreamimage:
2017/08/02 14:20:25 	pkg/api/install
2017/08/02 14:20:25 	pkg/authorization/apis/authorization
2017/08/02 14:20:25 	pkg/authorization/registry/subjectaccessreview
2017/08/02 14:20:25 	pkg/util/restoptions
2017/08/02 14:20:25 -- found forbidden imports for pkg/image/registry/imagestreamimport:
2017/08/02 14:20:25 	pkg/dockerregistry
2017/08/02 14:20:25 	pkg/quota/util
2017/08/02 14:20:25 	pkg/authorization/apis/authorization
2017/08/02 14:20:25 	pkg/cmd/server/api
2017/08/02 14:20:25 -- found forbidden imports for pkg/image/registry/imagestreammapping:
2017/08/02 14:20:25 	pkg/api/install
2017/08/02 14:20:25 	pkg/authorization/apis/authorization
2017/08/02 14:20:25 	pkg/authorization/registry/subjectaccessreview
2017/08/02 14:20:25 	pkg/util/restoptions
2017/08/02 14:20:25 -- found forbidden imports for pkg/image/registry/imagestreamtag:
2017/08/02 14:20:25 	pkg/api
2017/08/02 14:20:25 	pkg/api/install
2017/08/02 14:20:25 	pkg/authorization/apis/authorization
2017/08/02 14:20:25 	pkg/authorization/registry/subjectaccessreview
2017/08/02 14:20:25 	pkg/util/restoptions
2017/08/02 14:20:25 -- found forbidden imports for pkg/image/trigger/annotations:
2017/08/02 14:20:25 	pkg/api/meta
2017/08/02 14:20:25 -- found forbidden imports for pkg/image/trigger/buildconfigs:
2017/08/02 14:20:25 	pkg/build/apis/build
2017/08/02 14:20:25 	pkg/build/util
2017/08/02 14:20:25 -- found forbidden imports for pkg/image/trigger/deploymentconfigs:
2017/08/02 14:20:25 	pkg/deploy/apis/apps
[ERROR] PID 11796: hack/verify-imports.sh:9: `import-verifier "${OS_ROOT}/hack/import-restrictions.json"` exited with status 1.
[INFO] 		Stack Trace: 
[INFO] 		  1: hack/verify-imports.sh:9: `import-verifier "${OS_ROOT}/hack/import-restrictions.json"`
[INFO]   Exiting with code 1.

@stevekuznetsov
Copy link
Contributor Author

@deads2k give me a good review and I'll push it upstream too

func resolvePackageTree(treeBase string) ([]Package, error) {
cmd := "go"
args := []string{"list", "-json", fmt.Sprintf("%s...", treeBase)}
stdout, err := exec.Command(cmd, args...).Output()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, real bummer that we don't get a Go API for this, but grabbing the JSON and decoding it is good enough.

@stevekuznetsov
Copy link
Contributor Author

supersedes #15608

@@ -0,0 +1,9 @@
[
{
"baseImportPath" : "pkg/image",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we place that as .import-restrictions in the respective directories. Then the OWNER/REVIEWER logic automatically applies.

type ImportRestriction struct {
// BaseImportPath is the root of the package tree
// that is restricted by this configuration
BaseImportPath string `json:"baseImportPath"`
Copy link
Contributor

@sttts sttts Aug 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need exceptions for that path very soon. E.g.:

everything under staging/src/k8s.io/kube-gen, but do not apply the restrcitions to staging/src/k8s.io/kube-gen/test/....

Compare kubernetes/kubernetes@3d94b5f

@sttts
Copy link
Contributor

sttts commented Aug 3, 2017

@deads2k give me a good review and I'll push it upstream too

Compare my comment about the exception. And dotfiles please. We don't want to poke hack/ OWNERs for every subpackage.

@sttts
Copy link
Contributor

sttts commented Aug 3, 2017

Oh, overall looks very good! Well done!

// - is not of the base import path or a sub-package of it
// - is not of an allowed path or a sub-package of one
func (i *ImportRestriction) isForbidden(imp string) bool {
importsBelowRoot := strings.HasPrefix(imp, rootPackage)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it has the prefix, then its not below the root, is it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My variable name may not be the best here -- but github.com/openshift/origin/pkg/util has the prefix of github.com/openshift/origin/ and is thereby an import to something "below" the Origin root

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting. I read that as "under", not "below" when clearly they have very similar meanings.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@deads2k do you have a suggestion for a more technically clear name here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use "below" as well. But the choice of the name here probably not that critical ;-)

@deads2k
Copy link
Contributor

deads2k commented Aug 3, 2017

can we place that as .import-restrictions in the respective directories. Then the OWNER/REVIEWER logic automatically applies.

I'm disinclined to force additional requirements on this before its initial merge. @stevekuznetsov said he didn't want bash, I said that bash is what I had on and hand and I wasn't going to rewrite it for beauty, so @stevekuznetsov delivered an equivalent tool.

Also, I've heard rumors of bazel in our future and that comes with some kind of import protection, so this gets us over the initial hump.

@deads2k
Copy link
Contributor

deads2k commented Aug 3, 2017

Compare my comment about the exception. And dotfiles please. We don't want to poke hack/ OWNERs for every subpackage.

I'll find a better home for it before I start enforcing. It probably needs to be pretty restrictive since this is about forcing better structure on the repo.

@sttts I'm not clear on the exception. Why would I want to allow exceptions? It seems like that cold produce cycle problems for me.

@sttts
Copy link
Contributor

sttts commented Aug 3, 2017

I'm not clear on the exception.

We create clients in client-gen, but that package should not depend on apimachinery in general, only the test output.

@sttts
Copy link
Contributor

sttts commented Aug 3, 2017

I'm disinclined to force additional requirements

That "exception" requirement is fulfilled any moment in master, when my kube-gen dependency PR merges.

@stevekuznetsov
Copy link
Contributor Author

@deads2k @sttts pushed an updated commit to support ignored subtrees

// IgnoredSubTrees are roots of sub-trees of the
// BaseImportPath for which we do not want to enforce
// any import restrictions whatsoever
IgnoredSubTrees []string `json:"ignoredSubTrees,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 thanks!

@sttts
Copy link
Contributor

sttts commented Aug 3, 2017

/lgtm

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Aug 3, 2017
@stevekuznetsov
Copy link
Contributor Author

I'll make the verify soft-fail, so this can actually merge lol

In order to ensure that selected package trees in our source import only
from other package trees, we need a tool that will determine the imports
for a package tree and verify them all.

Signed-off-by: Steve Kuznetsov <skuznets@redhat.com>
@openshift-merge-robot openshift-merge-robot removed the lgtm Indicates that a PR is ready to be merged. label Aug 3, 2017
@sttts
Copy link
Contributor

sttts commented Aug 3, 2017

/lgtm

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Aug 3, 2017
@openshift-merge-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: stevekuznetsov, sttts
We suggest the following additional approver: liggitt

Assign the PR to them by writing /assign @liggitt in a comment when ready.

No associated issue. Update pull-request body to add a reference to an issue, or get approval with /approve no-issue

The full list of commands accepted by this bot can be found here.

Needs approval from an approver in each of these OWNERS Files:

You can indicate your approval by writing /approve in a comment
You can cancel your approval by writing /approve cancel in a comment

@stevekuznetsov
Copy link
Contributor Author

/retest

@stevekuznetsov
Copy link
Contributor Author

Actually ...

/retest-not-required

@deads2k @sttts want to approve this?

@stevekuznetsov
Copy link
Contributor Author

/test extended_conformance_install_update

@stevekuznetsov
Copy link
Contributor Author

/test extended_conformance_install_update

1 similar comment
@stevekuznetsov
Copy link
Contributor Author

/test extended_conformance_install_update

@openshift-merge-robot
Copy link
Contributor

Automatic merge from submit-queue

@openshift-merge-robot openshift-merge-robot merged commit ac7a49a into openshift:master Aug 4, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/internal-tools lgtm Indicates that a PR is ready to be merged. retest-not-required size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants