diff --git a/clientbase/object_client.go b/clientbase/object_client.go index e68f14d05..4e1f8efbc 100644 --- a/clientbase/object_client.go +++ b/clientbase/object_client.go @@ -31,6 +31,22 @@ func (u *UnstructuredObjectFactory) List() runtime.Object { 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 { restClient rest.Interface 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{ restClient: p.restClient, resource: p.resource, @@ -230,6 +246,10 @@ func (p *ObjectClient) Patch(name string, o runtime.Object, data []byte, subreso return result, err } +func (p *ObjectClient) ObjectFactory() ObjectFactory { + return p.Factory +} + type dynamicDecoder struct { factory ObjectFactory dec *json.Decoder diff --git a/controller/generic_controller.go b/controller/generic_controller.go index 265db5170..cf059dee3 100644 --- a/controller/generic_controller.go +++ b/controller/generic_controller.go @@ -10,8 +10,11 @@ import ( "github.com/rancher/norman/clientbase" "github.com/rancher/norman/types" "github.com/sirupsen/logrus" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" utilruntime "k8s.io/apimachinery/pkg/util/runtime" "k8s.io/apimachinery/pkg/util/wait" + "k8s.io/apimachinery/pkg/watch" "k8s.io/client-go/tools/cache" "k8s.io/client-go/util/workqueue" ) @@ -31,6 +34,12 @@ type GenericController interface { Start(ctx context.Context, threadiness int) error } +type ControllerBackend interface { + List(opts metav1.ListOptions) (runtime.Object, error) + Watch(opts metav1.ListOptions) (watch.Interface, error) + ObjectFactory() clientbase.ObjectFactory +} + type handlerDef struct { name string handler HandlerFunc @@ -46,13 +55,13 @@ type genericController struct { synced bool } -func NewGenericController(name string, objectClient *clientbase.ObjectClient) GenericController { +func NewGenericController(name string, genericClient ControllerBackend) GenericController { informer := cache.NewSharedIndexInformer( &cache.ListWatch{ - ListFunc: objectClient.List, - WatchFunc: objectClient.Watch, + ListFunc: genericClient.List, + WatchFunc: genericClient.Watch, }, - objectClient.Factory.Object(), resyncPeriod, cache.Indexers{}) + genericClient.ObjectFactory().Object(), resyncPeriod, cache.Indexers{}) rl := workqueue.NewMaxOfRateLimiter( workqueue.NewItemExponentialFailureRateLimiter(500*time.Millisecond, 1000*time.Second), diff --git a/types/reflection.go b/types/reflection.go index c2e8bb9ad..d8661d011 100644 --- a/types/reflection.go +++ b/types/reflection.go @@ -267,6 +267,9 @@ func (s *Schemas) readFields(schema *Schema, t reflect.Type) error { if fieldType.Kind() == reflect.Ptr { schemaField.Nullable = true fieldType = fieldType.Elem() + } else if fieldType.Kind() == reflect.Bool { + schemaField.Nullable = false + schemaField.Default = false } if err := applyTag(&field, &schemaField); err != nil {