mirror of
https://github.com/rancher/norman.git
synced 2025-09-05 17:20:20 +00:00
Make controller and client more generic
This commit is contained in:
@@ -31,6 +31,22 @@ func (u *UnstructuredObjectFactory) List() runtime.Object {
|
|||||||
return &unstructured.UnstructuredList{}
|
return &unstructured.UnstructuredList{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type GenericClient interface {
|
||||||
|
UnstructuredClient() GenericClient
|
||||||
|
GroupVersionKind() schema.GroupVersionKind
|
||||||
|
Create(o runtime.Object) (runtime.Object, error)
|
||||||
|
GetNamespaced(namespace, name string, opts metav1.GetOptions) (runtime.Object, error)
|
||||||
|
Get(name string, opts metav1.GetOptions) (runtime.Object, error)
|
||||||
|
Update(name string, o runtime.Object) (runtime.Object, error)
|
||||||
|
DeleteNamespaced(namespace, name string, opts *metav1.DeleteOptions) error
|
||||||
|
Delete(name string, opts *metav1.DeleteOptions) error
|
||||||
|
List(opts metav1.ListOptions) (runtime.Object, error)
|
||||||
|
Watch(opts metav1.ListOptions) (watch.Interface, error)
|
||||||
|
DeleteCollection(deleteOptions *metav1.DeleteOptions, listOptions metav1.ListOptions) error
|
||||||
|
Patch(name string, o runtime.Object, data []byte, subresources ...string) (runtime.Object, error)
|
||||||
|
ObjectFactory() ObjectFactory
|
||||||
|
}
|
||||||
|
|
||||||
type ObjectClient struct {
|
type ObjectClient struct {
|
||||||
restClient rest.Interface
|
restClient rest.Interface
|
||||||
resource *metav1.APIResource
|
resource *metav1.APIResource
|
||||||
@@ -49,7 +65,7 @@ func NewObjectClient(namespace string, restClient rest.Interface, apiResource *m
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *ObjectClient) UnstructuredClient() *ObjectClient {
|
func (p *ObjectClient) UnstructuredClient() GenericClient {
|
||||||
return &ObjectClient{
|
return &ObjectClient{
|
||||||
restClient: p.restClient,
|
restClient: p.restClient,
|
||||||
resource: p.resource,
|
resource: p.resource,
|
||||||
@@ -230,6 +246,10 @@ func (p *ObjectClient) Patch(name string, o runtime.Object, data []byte, subreso
|
|||||||
return result, err
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (p *ObjectClient) ObjectFactory() ObjectFactory {
|
||||||
|
return p.Factory
|
||||||
|
}
|
||||||
|
|
||||||
type dynamicDecoder struct {
|
type dynamicDecoder struct {
|
||||||
factory ObjectFactory
|
factory ObjectFactory
|
||||||
dec *json.Decoder
|
dec *json.Decoder
|
||||||
|
@@ -10,8 +10,11 @@ import (
|
|||||||
"github.com/rancher/norman/clientbase"
|
"github.com/rancher/norman/clientbase"
|
||||||
"github.com/rancher/norman/types"
|
"github.com/rancher/norman/types"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||||
"k8s.io/apimachinery/pkg/util/wait"
|
"k8s.io/apimachinery/pkg/util/wait"
|
||||||
|
"k8s.io/apimachinery/pkg/watch"
|
||||||
"k8s.io/client-go/tools/cache"
|
"k8s.io/client-go/tools/cache"
|
||||||
"k8s.io/client-go/util/workqueue"
|
"k8s.io/client-go/util/workqueue"
|
||||||
)
|
)
|
||||||
@@ -31,6 +34,12 @@ type GenericController interface {
|
|||||||
Start(ctx context.Context, threadiness int) error
|
Start(ctx context.Context, threadiness int) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Backend interface {
|
||||||
|
List(opts metav1.ListOptions) (runtime.Object, error)
|
||||||
|
Watch(opts metav1.ListOptions) (watch.Interface, error)
|
||||||
|
ObjectFactory() clientbase.ObjectFactory
|
||||||
|
}
|
||||||
|
|
||||||
type handlerDef struct {
|
type handlerDef struct {
|
||||||
name string
|
name string
|
||||||
handler HandlerFunc
|
handler HandlerFunc
|
||||||
@@ -46,13 +55,13 @@ type genericController struct {
|
|||||||
synced bool
|
synced bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGenericController(name string, objectClient *clientbase.ObjectClient) GenericController {
|
func NewGenericController(name string, genericClient Backend) GenericController {
|
||||||
informer := cache.NewSharedIndexInformer(
|
informer := cache.NewSharedIndexInformer(
|
||||||
&cache.ListWatch{
|
&cache.ListWatch{
|
||||||
ListFunc: objectClient.List,
|
ListFunc: genericClient.List,
|
||||||
WatchFunc: objectClient.Watch,
|
WatchFunc: genericClient.Watch,
|
||||||
},
|
},
|
||||||
objectClient.Factory.Object(), resyncPeriod, cache.Indexers{})
|
genericClient.ObjectFactory().Object(), resyncPeriod, cache.Indexers{})
|
||||||
|
|
||||||
rl := workqueue.NewMaxOfRateLimiter(
|
rl := workqueue.NewMaxOfRateLimiter(
|
||||||
workqueue.NewItemExponentialFailureRateLimiter(500*time.Millisecond, 1000*time.Second),
|
workqueue.NewItemExponentialFailureRateLimiter(500*time.Millisecond, 1000*time.Second),
|
||||||
|
@@ -267,6 +267,9 @@ func (s *Schemas) readFields(schema *Schema, t reflect.Type) error {
|
|||||||
if fieldType.Kind() == reflect.Ptr {
|
if fieldType.Kind() == reflect.Ptr {
|
||||||
schemaField.Nullable = true
|
schemaField.Nullable = true
|
||||||
fieldType = fieldType.Elem()
|
fieldType = fieldType.Elem()
|
||||||
|
} else if fieldType.Kind() == reflect.Bool {
|
||||||
|
schemaField.Nullable = false
|
||||||
|
schemaField.Default = false
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := applyTag(&field, &schemaField); err != nil {
|
if err := applyTag(&field, &schemaField); err != nil {
|
||||||
|
Reference in New Issue
Block a user