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

Bazel CRD and RBAC role generation #543

Merged

Conversation

randomvariable
Copy link
Member

What this PR does / why we need it:
Moves CRD and RBAC yaml generation directly into Bazel.
Our current set of RBAC were not automatically generated as intended from Kubebuilder.
The annotations have been put into the actuators to make this happen.

Eventually, this should move to Cluster API and/or Kubebuilder.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Extracted from #371

Special notes for your reviewer:

Please confirm that if this PR changes any image versions, then that's the sole change this PR makes.

Release note:


@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Feb 2, 2019
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: randomvariable

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

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 2, 2019
@k8s-ci-robot k8s-ci-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Feb 2, 2019
@randomvariable randomvariable force-pushed the bazel-crd-generation branch 3 times, most recently from f149b7b to 9abb7ea Compare February 2, 2019 10:14
@randomvariable randomvariable changed the title Bazel CRD generation Bazel CRD and RBAC role generation Feb 2, 2019
Copy link
Contributor

@chuckha chuckha left a comment

Choose a reason for hiding this comment

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

working through this one, off to a good start!


def _install_impl(ctx):

cmd = ""
Copy link
Contributor

@chuckha chuckha Feb 4, 2019

Choose a reason for hiding this comment

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

An alternative implementation to consider:

def _install_impl(ctx):
    copy_format = "cp -f {} {}"
    touch_format = "touch {}"
    cmds = []

    for s in ctx.attr.srcs:
        files = s.files.to_list()
        file = files[0].path
        root_file = paths.relativize(file, "bazel-out/k8-fastbuild/genfiles/")
        cmds.append(copy_format.format(file, root_file))

    cmds.append(touch_format.append(ctx.outputs.record.path))
    cmd = " && ".join(cmd)
    ...

If you want to be really safe, the last line should be:

# Remove empty items from cmdList to avoid an erroneous " && && "
" && ".join(filter(None, cmdList))

I'm also looking into how to make this work on a Darwin system. It's failing for me with this error:

 salazar:cluster-api-provider-aws cha$ make crds
bazel build //config:config_install
WARNING: failed to raise resource limit 8 to 524288: Invalid argument
Starting local Bazel server and connecting to it...
INFO: Invocation ID: 456b2a2b-7895-4f8f-865b-45226a773acc
ERROR: /Users/cha/go/src/sigs.k8s.io/cluster-api-provider-aws/config/BUILD.bazel:17:1: in install rule //config:config_install: 
Traceback (most recent call last):
	File "/Users/cha/go/src/sigs.k8s.io/cluster-api-provider-aws/config/BUILD.bazel", line 17
		install(name = 'config_install')
	File "/Users/cha/go/src/sigs.k8s.io/cluster-api-provider-aws/build/install.bzl", line 28, in _install_impl
		paths.relativize(file, "bazel-out/k8-fastbuild/genf...")
	File "/private/var/tmp/_bazel_cha/b7ce3b297716e241b109da3b2c60846f/external/bazel_skylib/lib/paths.bzl", line 186, in paths.relativize
		fail(("Path '%s' is not beneath '%s'"...)))
Path 'bazel-out/darwin-fastbuild/genfiles/config/rbac/rbac_role.yaml' is not beneath 'bazel-out/k8-fastbuild/genfiles/'
ERROR: Analysis of target '//config:config_install' failed; build aborted: Analysis of target '//config:config_install' failed; build aborted
INFO: Elapsed time: 13.016s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (358 packages loaded, 9342 targets configured)
make: *** [crds] Error 1

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for checking this out, was interested how it'd behave on os x. Will dig deeper, or more likely, split it out into a separate PR

for s in ctx.attr.srcs:
files = s.files.to_list()
file = files[0].path
root_file = paths.relativize(file,"bazel-out/k8-fastbuild/genfiles/")
Copy link
Contributor

Choose a reason for hiding this comment

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

define a build_dir outside the for loop:

    build_dir = "bazel-out/{}-{}/genfiles".format(ctx.var["TARGET_CPU"], ctx.var["COMPILATION_MODE"])

and then this line can be

        root_file = paths.relativize(file, build_dir)

Copy link
Contributor

Choose a reason for hiding this comment

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

lol or just ctx.var["GENDIR"] thx Bazel

Copy link
Member Author

Choose a reason for hiding this comment

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

Omg.

@@ -41,6 +41,11 @@ import (
controllerError "sigs.k8s.io/cluster-api/pkg/controller/error"
)

//+kubebuilder:rbac:groups=awsprovider.k8s.io,resources=awsmachineproviderconfigs;awsmachineproviderstatuses,verbs=get;list;watch;create;update;patch;delete
Copy link
Contributor

@chuckha chuckha Feb 4, 2019

Choose a reason for hiding this comment

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

Are these being added so that the rbac rules are a bit more restrictive?

@chuckha
Copy link
Contributor

chuckha commented Feb 6, 2019

Should this PR also clean up the Makefile and remove the crd generation from the make manifests make target?

Copy link
Member

@vincepri vincepri left a comment

Choose a reason for hiding this comment

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

Some minor comments, otherwise this looks great!

Makefile Outdated
@@ -163,6 +163,10 @@ manifests: cmd/clusterctl/examples/aws/out/credentials ## Generate manifests for
manifests-dev: dep-ensure dep-install binaries-dev ## Builds development manifests
MANAGER_IMAGE=$(DEV_MANAGER_IMAGE) MANAGER_IMAGE_PULL_POLICY="Always" $(MAKE) manifests

.PHONY: crds
crds:
bazel build //config:config_install
Copy link
Member

Choose a reason for hiding this comment

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

😍

def _qualified_genfile(label):
return "$$GO_GENRULE_EXECROOT/$(location %s)" % label

def controller_gen(name, importpath, api, visibility, deps = []):
Copy link
Member

Choose a reason for hiding this comment

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

I would love to bring these upstream if that's even possible. We should also add some more comments around these functions and their objective.

Copy link
Member Author

Choose a reason for hiding this comment

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

100% agree. Have added comments for now, then I'll PR to the controller-runtime repo after we know we're happy with it.

for s in ctx.attr.srcs:
files = s.files.to_list()
file = files[0].path
root_file = paths.relativize(file,"bazel-out/k8-fastbuild/genfiles/")
Copy link
Member

Choose a reason for hiding this comment

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

Space between arguments?

* Add annotations to controllers as required
* New controller_gen build rule for generating CRD and RBAC

Signed-off-by: Naadir Jeewa <[email protected]>
@randomvariable
Copy link
Member Author

Alright, taken out the install.bzl magic until I understand the sandboxing across Linux and OS X better.

@chuckha
Copy link
Contributor

chuckha commented Feb 7, 2019

@randomvariable Huh, which part do you want to understand more? It looked pretty good to me with that one fix for darwin architecture.

Also should this PR also clean up the Makefile and remove the crd generation from the make manifests make target? Or am I misunderstanding something?

@randomvariable
Copy link
Member Author

@chuckha Was short on time this week, so just want to do it as a separate PR now.

Have cleaned up the Makefile - deleted the CRD gen from make manifests as it's still user facing, added as a dep to make manifests-dev

@randomvariable randomvariable deleted the bazel-crd-generation branch February 8, 2019 10:19
@randomvariable randomvariable restored the bazel-crd-generation branch February 8, 2019 10:19
@randomvariable randomvariable reopened this Feb 8, 2019
@randomvariable
Copy link
Member Author

whoops. was cleaning up dead branches in my fork...

@chuckha
Copy link
Contributor

chuckha commented Feb 8, 2019

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 8, 2019
@k8s-ci-robot k8s-ci-robot merged commit b97293e into kubernetes-sigs:master Feb 8, 2019
@randomvariable randomvariable deleted the bazel-crd-generation branch February 21, 2019 01:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. 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.

4 participants