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 support for creating core type controllers #132

Merged
merged 3 commits into from
May 14, 2018

Conversation

Liujingfang1
Copy link
Contributor

@Liujingfang1 Liujingfang1 commented May 7, 2018

@k8s-ci-robot k8s-ci-robot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels May 7, 2018
@Liujingfang1 Liujingfang1 force-pushed the tmpcore branch 2 times, most recently from b6db00e to 17a7a2a Compare May 7, 2018 17:46
@Liujingfang1 Liujingfang1 changed the title [WIP] Add support for creating core type controllers Add support for creating core type controllers May 7, 2018
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label May 7, 2018
@Liujingfang1 Liujingfang1 force-pushed the tmpcore branch 2 times, most recently from 23c8f1d to 2b25ee4 Compare May 7, 2018 22:51
@Liujingfang1
Copy link
Contributor Author

Add parseDomain from file directly and remove a lot of if statements. PTAL

Copy link
Contributor

@droot droot left a comment

Choose a reason for hiding this comment

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

Took a quick look, have couple of comments.

glog.Fatal(err)
}

comments := Comments(lines)
Copy link
Contributor

Choose a reason for hiding this comment

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

May be, we can move this logic in the scanner loop itself itself. Ignore the line if its not a comment, else invoke Comments(line) and check for domain ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I updated this function to ignore the lines not a comment.
Comments(line) is left after all comments are get since Comments is the same type of []string.

@@ -42,11 +42,17 @@ import (
"github.com/kubernetes-sigs/kubebuilder/pkg/controller"
"github.com/kubernetes-sigs/kubebuilder/pkg/controller/types"
"k8s.io/client-go/tools/record"

{{if .CoreType}}
Copy link
Contributor

Choose a reason for hiding this comment

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

What does CoreType means here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should be ControllerOnly, will fix 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.

Done

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I changed it back to CoreType in create controller command. It makes sense now compared with ControllerOnly.


func AddInit(cmd *cobra.Command) {
cmd.AddCommand(repoCmd)
repoCmd.Flags().StringVar(&domain, "domain", "", "domain for the API groups")
repoCmd.Flags().StringVar(&copyright, "copyright", filepath.Join("hack", "boilerplate.go.txt"), "Location of copyright boilerplate file.")
repoCmd.Flags().BoolVar(&bazel, "bazel", false, "if true, setup Bazel workspace artifacts")
repoCmd.Flags().BoolVar(&controllerOnly, "controller-only", false, "if true, setup controller only")
Copy link
Contributor

Choose a reason for hiding this comment

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

Can I add API resource to a project which has been initialized as "controller-only" ?

If yes, then does this flag makes sense ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It won't work since the pkg/inject won't build

Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm.... it will be great if we can remove this restriction.
Ideal workflow is - to be able to not to take this flag as input for init command and then be able to add resources for non-core type and controllers for core-types.

Lets discuss today to explore if we can make this happen.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree with you, it's nice to remove the restriction.
The divergence comes from the different dependencies for core type controller and CRD behaviors. Having a flag in init command, we can easily distinguish the different dependencies.
Let's discuss to see if there's better solution.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As discussed offline, we keep this flag for now. Later when the dynamic client is added in the kube builder library, we will try to remove this flag.

@@ -61,6 +62,7 @@ func AddCreateResource(cmd *cobra.Command) {
createResourceCmd.Flags().BoolVar(&nonNamespacedKind, "non-namespaced", false, "if set, the API kind will be non namespaced")
createResourceCmd.Flags().BoolVar(&controller, "controller", true, "if true, generate the controller code for the resource")
createResourceCmd.Flags().BoolVar(&generate, "generate", true, "generate source code")
createResourceCmd.Flags().BoolVar(&controllerOnly, "controller-only", false, "skip resources, generate controller only")
Copy link
Contributor

Choose a reason for hiding this comment

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

hmm.... add a resource with --controller-only looks a bit confusing. "Add controller" should address this use case right ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I didn't realize that it might cause confusion. Do you suggest to add a new command like add controller or create controller? This new command will have almost the same flags and need to call the same functions as current create resource. I'm not sure if it worth to do it.

Copy link
Contributor

Choose a reason for hiding this comment

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

yeah I think create controller makes sense. We don't actually want to create a resource in this case.

@Liujingfang1 Liujingfang1 force-pushed the tmpcore branch 6 times, most recently from b295792 to ed6ab6b Compare May 9, 2018 17:44
@Liujingfang1
Copy link
Contributor Author

Add a subcommand create controller. Now we can create core type controller by
kubebuilder create controller --group <group> --version <version> --kind <kind> --core-type

Copy link
Contributor

@droot droot left a comment

Choose a reason for hiding this comment

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

Looks good to me. Was wondering if we can improve the test coverage ?

@droot
Copy link
Contributor

droot commented May 10, 2018

@Liujingfang1 travis tests are failing on this one.

@Liujingfang1
Copy link
Contributor Author

I updated the test script in travis-ci. Seems it's not properly added. I'll fix it.

@Liujingfang1
Copy link
Contributor Author

Change the command in test.sh as

kubebuilder create controller --group insect --version v1beta1 --kind Wasp
kubebuilder create controller --group ant --version v1beta1 --kind Ant

@Liujingfang1 Liujingfang1 force-pushed the tmpcore branch 2 times, most recently from 13a802b to 57bd34d Compare May 11, 2018 16:45
@Liujingfang1
Copy link
Contributor Author

Add two different workflows in test.sh

  1. Create a coretype controller after creating a CRD resource
  kubebuilder create resource --group ant --version v1beta1 --kind Ant
  kubebuilder create controller --group apps --version v1beta2 --kind Deployment --core-type
  1. Create coretype controller only
    kubebuilder init repo --domain sample.kubernetes.io --controller-only
    kubebuilder create controller --group apps --version v1beta2 --kind Deployment --core-type

Note that for the type Deployment, the object of this type must have some fields nonempty. For those fields, I add a post step to update the controller test file.

@Liujingfang1 Liujingfang1 force-pushed the tmpcore branch 2 times, most recently from 190b139 to 23a42ba Compare May 11, 2018 17:13
@Liujingfang1
Copy link
Contributor Author

@droot @pwittrock All tests passed, PTAL

Copy link
Contributor

@droot droot left a comment

Choose a reason for hiding this comment

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

Thanks for working on the tests for this one.
Have some more comments.

"k8s.io/client-go/kubernetes/scheme",
"rscheme " + "\"" + repo + "/pkg/client/clientset/versioned/scheme\""}...
)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Keeping track of these imports feels like a lot of hand-written magic :)
Another possible approach could be ...we add all the imports and then run goimports to remove extra imports.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As discussed offline, Let goimports add imports could be risky. We may only allow goimports to remove extra imports. It can be done in a different PR.

{{if .CoreType}}
"{{ .Repo }}/pkg/inject"
"{{ .Repo }}/pkg/inject/args"
{{else}}
"{{ .Repo }}/pkg/client/clientset/versioned"
Copy link
Contributor

Choose a reason for hiding this comment

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

may be just have the clienset/versioned guarded ?

var nonNamespacedKind bool
var generate bool
var CoreType bool

Copy link
Contributor

Choose a reason for hiding this comment

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

We should move away from this pattern of defining pkg level globals for command options. Currently all commands are implemented like this but its an anti-pattern where push down cobra.command and these globals too deep in to the business logic.

Idea is to define a separate struct for command args:
type controllerArgs struct { namespaced bool coreType bool generate bool }
Bind above struct field to the command-args. So filling of struct is delegated to cobra with some basic validation.

Then have validate() method on controllerArgs

And then execution should NOT take cobra.cmd or args as input like we do today.

Not a deal breaker, but wanted to bring it out (can be handled in separate PR but will be good if we can do it in current PR because then we can point people to the pattern here to refactor commands):

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can do it in this PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done


func AddInit(cmd *cobra.Command) {
cmd.AddCommand(repoCmd)
repoCmd.Flags().StringVar(&domain, "domain", "", "domain for the API groups")
repoCmd.Flags().StringVar(&copyright, "copyright", filepath.Join("hack", "boilerplate.go.txt"), "Location of copyright boilerplate file.")
repoCmd.Flags().BoolVar(&bazel, "bazel", false, "if true, setup Bazel workspace artifacts")
repoCmd.Flags().BoolVar(&controllerOnly, "controller-only", false, "if true, setup controller only")
Copy link
Contributor

Choose a reason for hiding this comment

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

Hmm.... it will be great if we can remove this restriction.
Ideal workflow is - to be able to not to take this flag as input for init command and then be able to add resources for non-core type and controllers for core-types.

Lets discuss today to explore if we can make this happen.

test.sh Outdated
export PATH=/tmp/kubebuilder/bin/:$PATH
export TEST_ASSET_KUBECTL=/tmp/kubebuilder/bin/kubectl
export TEST_ASSET_KUBE_APISERVER=/tmp/kubebuilder/bin/kube-apiserver
export TEST_ASSET_ETCD=/tmp/kubebuilder/bin/etcd
Copy link
Contributor

Choose a reason for hiding this comment

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

we can set these env once ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

}

function update_controller_test {
# Update import
Copy link
Contributor

Choose a reason for hiding this comment

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

Its not very clear what are we doing here ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Explained offline. Also added a comment for explanation.

@Liujingfang1 Liujingfang1 force-pushed the tmpcore branch 3 times, most recently from 52c95aa to e61782f Compare May 11, 2018 20:49
Copy link
Contributor

@droot droot left a comment

Choose a reason for hiding this comment

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

LGTM.
Lets check with @pwittrock if he has any ideas to workaround the restriction of "not being able to add API resource to a project created using --controller-only flag during init"

@pwittrock pwittrock merged commit 391d011 into kubernetes-sigs:master May 14, 2018
@Liujingfang1 Liujingfang1 deleted the tmpcore branch June 1, 2018 23:15
luffyao pushed a commit to luffyao/kubebuilder that referenced this pull request Aug 6, 2020
…rence-markers-rbac

zh-translation:/reference/markers/rbac.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. 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.

(L) Support for creating controllers for core types
4 participants