Skip to content

pkg/generator: add auto-gen for apis/../register.go#36

Merged
fanminshi merged 1 commit intooperator-framework:masterfrom
fanminshi:gen_apis_register
Feb 21, 2018
Merged

pkg/generator: add auto-gen for apis/../register.go#36
fanminshi merged 1 commit intooperator-framework:masterfrom
fanminshi:gen_apis_register

Conversation

@fanminshi
Copy link
Copy Markdown
Contributor

No description provided.

@fanminshi
Copy link
Copy Markdown
Contributor Author

Manual test:

func TestGenRegister(t *testing.T) {
	buf := &bytes.Buffer{}
	if err := renderApisRegister(buf, "PlayService", "play.example.com", "v1alpha1"); err != nil {
		t.Error(err)
		return
	}
	if err := ioutil.WriteFile("./register.go", buf.Bytes(), 0644); err != nil {
		t.Error(err)
	}
}

Output:

package v1alpha1

import (
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/runtime/schema"
)

const (
	PlayServiceKind   = "PlayService"
	PlayServicePlural = "playservices"
	groupName         = "play.example.com"
)

var (
	SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
	AddToScheme   = SchemeBuilder.AddToSchemes
	// SchemeGroupVersion is the group version used to register these objects.
	SchemeGroupVersion = schema.GroupVersion{Group: groupName, Version: "v1alpha1"}
)

// addKnownTypes adds the set of types defined in this package to the supplied scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
	scheme.AddKnownTypes(SchemeGroupVersion,
		&PlayService{},
		&PlayServiceList{},
	)
	metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
	return nil
}

@fanminshi
Copy link
Copy Markdown
Contributor Author

cc/ @hongchaodeng @hasbro17

Comment thread pkg/generator/apis_tmpls.go Outdated
)

const (
{{.Kind}}Kind = "{{.Kind}}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I don't think we would need XXXKind and XXXPlural at the beginning.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I am mirroring template from #13 (comment)

maybe once we figure out what is needed and not needed. I can modify this pr again.

Copy link
Copy Markdown
Contributor Author

@fanminshi fanminshi Feb 21, 2018

Choose a reason for hiding this comment

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

I think XXXPlural is needed because cmd/main.go needs it to setup sdk.Watch(). and XXXKind is probably needed to setup deployment for custom resource deployment.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think XXXPlural is needed because cmd/main.go needs it to setup sdk.Watch().

sdk.Watch() doesn't need Plural.

XXXKind is probably needed to setup deployment for custom resource deployment.

What's "deployment for custom resource deployment"?

Please remove these two const unless they are needed.

Copy link
Copy Markdown
Contributor Author

@fanminshi fanminshi Feb 21, 2018

Choose a reason for hiding this comment

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

From my understanding of sdk.Watch(). resourcePluralName will be filled by xxxPlural.

// Watch watches for changes on the given resource.
// obj is an instance of the resource type, e.g. &Pod{}.
// resourcePluralName is the plural name of the resource, e.g. “pods”.
// resourceClient is the rest client for the resource, e.g. `kubeclient.CoreV1().RESTClient()`.
// opts provide more options for doing the watch.
// TODO: support opts for specifying label selector
func Watch(resourcePluralName, namespace string, obj runtime.Object, resourceClient rest.Interface) {
	informer := sdkInformer.New(resourcePluralName, namespace, obj, resourceClient)
	informers = append(informers, informer)
}

What's "deployment for custom resource deployment"?
On a second thought, probably no needed. I was thinking about creating a CR using xxxKind inside the stub.Handler which requires the usage of xxxKind.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The interface is outdated.
It would be

func Watch(apiVersion, kind, namespace string

You may argue that we would still want "Kind". But user already pass in the "Kind" when generating the API. And I'm not even sure we should generate that without knowing if user want to use it..

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I am thinking about generating the following inside cmd/main.go

package main

import (
 	"context"

 	sdk "github.com/coreos/operator-sdk/pkg/sdk"
	api "github.com/coreos/play/pkg/apis/play/v1alpha1"
	stub "github.com/coreos/play/pkg/stub"
)

 func main() {
 	namespace := "default"
 	sdk.Watch(api.Version, api.XXXKind, namespace)
  	sdk.Handle(&stub.Handler{})
  	sdk.Run(context.TODO())

as you can see, api.Version and api.XXXKind are constants inside api pkg. That's why I want to auto-generate them.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

	sdk.Watch(api.Version, api.XXXKind, namespace)

This is not to be generated, right?
By default, it should generate:

  sdk.Watch("apps/v1", "Deployment", "defalt")

Also wait on PR #34

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I see.

@fanminshi
Copy link
Copy Markdown
Contributor Author

output:

package v1alpha1

import (
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/runtime/schema"
)

const (
	Version = "v1alpha1"
	PlayServiceKind   = "PlayService"
	groupName         = "play.example.com"
)

var (
	SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
	AddToScheme   = SchemeBuilder.AddToSchemes
	// SchemeGroupVersion is the group version used to register these objects.
	SchemeGroupVersion = schema.GroupVersion{Group: groupName, Version: Version}
)

// addKnownTypes adds the set of types defined in this package to the supplied scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
	scheme.AddKnownTypes(SchemeGroupVersion,
		&PlayService{},
		&PlayServiceList{},
	)
	metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
	return nil
}

Comment thread pkg/generator/apis_tmpls.go Outdated
)

const (
Version = "{{.Version}}"
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Make it local version? Don't expose it?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yeah, just realized that.

Copy link
Copy Markdown
Contributor

@hongchaodeng hongchaodeng left a comment

Choose a reason for hiding this comment

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

LGTM

@fanminshi
Copy link
Copy Markdown
Contributor Author

latest output:

package v1alpha1

import (
	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime"
	"k8s.io/apimachinery/pkg/runtime/schema"
)

const (
	version   = "v1alpha1"
	groupName = "play.example.com"
)

var (
	SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)
	AddToScheme   = SchemeBuilder.AddToSchemes
	// SchemeGroupVersion is the group version used to register these objects.
	SchemeGroupVersion = schema.GroupVersion{Group: groupName, Version: version}
)

// addKnownTypes adds the set of types defined in this package to the supplied scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
	scheme.AddKnownTypes(SchemeGroupVersion,
		&PlayService{},
		&PlayServiceList{},
	)
	metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
	return nil
}

@fanminshi fanminshi merged commit 7875fc0 into operator-framework:master Feb 21, 2018
@fanminshi fanminshi mentioned this pull request Feb 22, 2018
21 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants