Skip to content

Commit

Permalink
Add enable function and addFeature functions
Browse files Browse the repository at this point in the history
Added enable function to schemas. This allows filtering of schemas
which is a required part of feature flagging in rancher. Added
addFeature functions to controller template for adding handlers that
can be disabled if their associated feature is disabled.
  • Loading branch information
rmweir authored and Craig Jellick committed Jul 2, 2019
1 parent f574404 commit 2da1bd2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 0 deletions.
53 changes: 53 additions & 0 deletions generator/controller_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@ type {{.schema.CodeName}}Controller interface {
Informer() cache.SharedIndexInformer
Lister() {{.schema.CodeName}}Lister
AddHandler(ctx context.Context, name string, handler {{.schema.CodeName}}HandlerFunc)
AddFeatureHandler(ctx context.Context, enabled func() bool, name string, sync {{.schema.CodeName}}HandlerFunc)
AddClusterScopedHandler(ctx context.Context, name, clusterName string, handler {{.schema.CodeName}}HandlerFunc)
AddClusterScopedFeatureHandler(ctx context.Context, enabled func() bool, name, clusterName string, handler {{.schema.CodeName}}HandlerFunc)
Enqueue(namespace, name string)
Sync(ctx context.Context) error
Start(ctx context.Context, threadiness int) error
Expand All @@ -93,9 +95,13 @@ type {{.schema.CodeName}}Interface interface {
DeleteCollection(deleteOpts *metav1.DeleteOptions, listOpts metav1.ListOptions) error
Controller() {{.schema.CodeName}}Controller
AddHandler(ctx context.Context, name string, sync {{.schema.CodeName}}HandlerFunc)
AddFeatureHandler(ctx context.Context, enabled func() bool, name string, sync {{.schema.CodeName}}HandlerFunc)
AddLifecycle(ctx context.Context, name string, lifecycle {{.schema.CodeName}}Lifecycle)
AddFeatureLifecycle(ctx context.Context, enabled func() bool, name string, lifecycle {{.schema.CodeName}}Lifecycle)
AddClusterScopedHandler(ctx context.Context, name, clusterName string, sync {{.schema.CodeName}}HandlerFunc)
AddClusterScopedFeatureHandler(ctx context.Context, enabled func() bool, name, clusterName string, sync {{.schema.CodeName}}HandlerFunc)
AddClusterScopedLifecycle(ctx context.Context, name, clusterName string, lifecycle {{.schema.CodeName}}Lifecycle)
AddClusterScopedFeatureLifecycle(ctx context.Context, enabled func() bool, name, clusterName string, lifecycle {{.schema.CodeName}}Lifecycle)
}
type {{.schema.ID}}Lister struct {
Expand Down Expand Up @@ -156,6 +162,20 @@ func (c *{{.schema.ID}}Controller) AddHandler(ctx context.Context, name string,
})
}
func (c *{{.schema.ID}}Controller) AddFeatureHandler(ctx context.Context, enabled func() bool, name string, handler {{.schema.CodeName}}HandlerFunc) {
c.GenericController.AddHandler(ctx, name, func(key string, obj interface{}) (interface{}, error) {
if !enabled() {
return nil, nil
} else if obj == nil {
return handler(key, nil)
} else if v, ok := obj.(*{{.prefix}}{{.schema.CodeName}}); ok {
return handler(key, v)
} else {
return nil, nil
}
})
}
func (c *{{.schema.ID}}Controller) AddClusterScopedHandler(ctx context.Context, name, cluster string, handler {{.schema.CodeName}}HandlerFunc) {
resource.PutClusterScoped({{.schema.CodeName}}GroupVersionResource)
c.GenericController.AddHandler(ctx, name, func(key string, obj interface{}) (interface{}, error) {
Expand All @@ -169,6 +189,21 @@ func (c *{{.schema.ID}}Controller) AddClusterScopedHandler(ctx context.Context,
})
}
func (c *{{.schema.ID}}Controller) AddClusterScopedFeatureHandler(ctx context.Context, enabled func() bool, name, cluster string, handler {{.schema.CodeName}}HandlerFunc) {
resource.PutClusterScoped({{.schema.CodeName}}GroupVersionResource)
c.GenericController.AddHandler(ctx, name, func(key string, obj interface{}) (interface{}, error) {
if !enabled() {
return nil, nil
} else if obj == nil {
return handler(key, nil)
} else if v, ok := obj.(*{{.prefix}}{{.schema.CodeName}}); ok && controller.ObjectInCluster(cluster, obj) {
return handler(key, v)
} else {
return nil, nil
}
})
}
type {{.schema.ID}}Factory struct {
}
Expand Down Expand Up @@ -264,20 +299,38 @@ func (s *{{.schema.ID}}Client) AddHandler(ctx context.Context, name string, sync
s.Controller().AddHandler(ctx, name, sync)
}
func (s *{{.schema.ID}}Client) AddFeatureHandler(ctx context.Context, enabled func() bool, name string, sync {{.schema.CodeName}}HandlerFunc) {
s.Controller().AddFeatureHandler(ctx, enabled, name, sync)
}
func (s *{{.schema.ID}}Client) AddLifecycle(ctx context.Context, name string, lifecycle {{.schema.CodeName}}Lifecycle) {
sync := New{{.schema.CodeName}}LifecycleAdapter(name, false, s, lifecycle)
s.Controller().AddHandler(ctx, name, sync)
}
func (s *{{.schema.ID}}Client) AddFeatureLifecycle(ctx context.Context, enabled func() bool, name string, lifecycle {{.schema.CodeName}}Lifecycle) {
sync := New{{.schema.CodeName}}LifecycleAdapter(name, false, s, lifecycle)
s.Controller().AddFeatureHandler(ctx, enabled, name, sync)
}
func (s *{{.schema.ID}}Client) AddClusterScopedHandler(ctx context.Context, name, clusterName string, sync {{.schema.CodeName}}HandlerFunc) {
s.Controller().AddClusterScopedHandler(ctx, name, clusterName, sync)
}
func (s *{{.schema.ID}}Client) AddClusterScopedFeatureHandler(ctx context.Context, enabled func() bool, name, clusterName string, sync {{.schema.CodeName}}HandlerFunc) {
s.Controller().AddClusterScopedFeatureHandler(ctx, enabled, name, clusterName, sync)
}
func (s *{{.schema.ID}}Client) AddClusterScopedLifecycle(ctx context.Context, name, clusterName string, lifecycle {{.schema.CodeName}}Lifecycle) {
sync := New{{.schema.CodeName}}LifecycleAdapter(name+"_"+clusterName, true, s, lifecycle)
s.Controller().AddClusterScopedHandler(ctx, name, clusterName, sync)
}
func (s *{{.schema.ID}}Client) AddClusterScopedFeatureLifecycle(ctx context.Context, enabled func() bool, name, clusterName string, lifecycle {{.schema.CodeName}}Lifecycle) {
sync := New{{.schema.CodeName}}LifecycleAdapter(name+"_"+clusterName, true, s, lifecycle)
s.Controller().AddClusterScopedFeatureHandler(ctx, enabled, name, clusterName, sync)
}
type {{.schema.CodeName}}Indexer func(obj *{{.prefix}}{{.schema.CodeName}}) ([]string, error)
type {{.schema.CodeName}}ClientCache interface {
Expand Down
6 changes: 6 additions & 0 deletions parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ func Parse(rw http.ResponseWriter, req *http.Request, schemas *types.Schemas, ur
return result, err
}

if schema := result.Schema; schema.Enabled != nil {
if !schema.Enabled() {
return result, httperror.NewAPIError(httperror.ActionNotAvailable, "schema disabled "+schema.ID)
}
}

result.Type = result.Schema.ID

if err := ValidateMethod(result); err != nil {
Expand Down
12 changes: 12 additions & 0 deletions store/schema/schema_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ func NewSchemaStore() types.Store {
func (s *Store) ByID(apiContext *types.APIContext, schema *types.Schema, id string) (map[string]interface{}, error) {
for _, schema := range apiContext.Schemas.SchemasForVersion(*apiContext.Version) {
if strings.EqualFold(schema.ID, id) {
if schema.Enabled != nil {
if !schema.Enabled() {
return nil, httperror.NewAPIError(httperror.NotFound, "schema disabled")
}
}

schemaData := map[string]interface{}{}

data, err := json.Marshal(s.modifyForAccessControl(apiContext, *schema))
Expand Down Expand Up @@ -78,6 +84,12 @@ func (s *Store) List(apiContext *types.APIContext, schema *types.Schema, opt *ty
continue
}

if schema.Enabled != nil {
if !schema.Enabled() {
continue
}
}

if schema.CanList(apiContext) == nil || schema.CanGet(apiContext) == nil {
schemas = s.addSchema(apiContext, schema, schemaMap, schemas, included)
}
Expand Down
1 change: 1 addition & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ type Schema struct {
CollectionFilters map[string]Filter `json:"collectionFilters,omitempty"`
DynamicSchemaVersion string `json:"dynamicSchemaVersion,omitempty"`
Scope TypeScope `json:"-"`
Enabled func() bool `json:"-"`

InternalSchema *Schema `json:"-"`
Mapper Mapper `json:"-"`
Expand Down

0 comments on commit 2da1bd2

Please sign in to comment.