pkg/generator: add auto-gen for apis/../register.go#36
pkg/generator: add auto-gen for apis/../register.go#36fanminshi merged 1 commit intooperator-framework:masterfrom fanminshi:gen_apis_register
Conversation
|
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
} |
| ) | ||
|
|
||
| const ( | ||
| {{.Kind}}Kind = "{{.Kind}}" |
There was a problem hiding this comment.
I don't think we would need XXXKind and XXXPlural at the beginning.
There was a problem hiding this comment.
I am mirroring template from #13 (comment)
maybe once we figure out what is needed and not needed. I can modify this pr again.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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..
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
|
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
} |
| ) | ||
|
|
||
| const ( | ||
| Version = "{{.Version}}" |
There was a problem hiding this comment.
Make it local version? Don't expose it?
There was a problem hiding this comment.
yeah, just realized that.
|
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
} |
No description provided.