mirror of
https://github.com/rancher/norman.git
synced 2025-09-07 18:20:17 +00:00
Add enable function and addFeature functions
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.
This commit is contained in:
@@ -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
|
||||
@@ -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 {
|
||||
@@ -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) {
|
||||
@@ -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 {
|
||||
}
|
||||
|
||||
@@ -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 {
|
||||
|
@@ -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 {
|
||||
|
@@ -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))
|
||||
@@ -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)
|
||||
}
|
||||
|
@@ -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:"-"`
|
||||
|
Reference in New Issue
Block a user