Skip to content

Commit

Permalink
Make controller and client more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Feb 13, 2018
1 parent 63f413a commit dd81280
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 5 deletions.
22 changes: 21 additions & 1 deletion clientbase/object_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down
17 changes: 13 additions & 4 deletions controller/generic_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
Expand All @@ -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),
Expand Down
3 changes: 3 additions & 0 deletions types/reflection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit dd81280

Please sign in to comment.