Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
214 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
api-aggregation-lib/pkg/registry/hello.zeng.dev/foo/etcd.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package foo | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apiserver/pkg/registry/generic" | ||
genericregistry "k8s.io/apiserver/pkg/registry/generic/registry" | ||
|
||
hellov1 "github.com/phosae/x-kubernetes/api/hello.zeng.dev/v1" | ||
) | ||
|
||
// NewREST returns a RESTStorage object that will work against API services. | ||
func NewREST(scheme *runtime.Scheme, optsGetter generic.RESTOptionsGetter) (*genericregistry.Store, error) { | ||
strategy := NewStrategy(scheme) | ||
|
||
store := &genericregistry.Store{ | ||
NewFunc: func() runtime.Object { return &hellov1.Foo{} }, | ||
NewListFunc: func() runtime.Object { return &hellov1.FooList{} }, | ||
PredicateFunc: MatchFoo, | ||
DefaultQualifiedResource: hellov1.Resource("foos"), | ||
SingularQualifiedResource: hellov1.Resource("foos"), | ||
|
||
CreateStrategy: strategy, | ||
UpdateStrategy: strategy, | ||
DeleteStrategy: strategy, | ||
TableConvertor: strategy, | ||
} | ||
options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: GetAttrs} | ||
if err := store.CompleteWithOptions(options); err != nil { | ||
return nil, err | ||
} | ||
return store, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
api-aggregation-lib/pkg/registry/hello.zeng.dev/foo/strategy.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package foo | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/fields" | ||
"k8s.io/apimachinery/pkg/labels" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/util/validation/field" | ||
"k8s.io/apiserver/pkg/registry/generic" | ||
"k8s.io/apiserver/pkg/storage" | ||
"k8s.io/apiserver/pkg/storage/names" | ||
|
||
hellov1 "github.com/phosae/x-kubernetes/api/hello.zeng.dev/v1" | ||
) | ||
|
||
// NewStrategy creates and returns a fooStrategy instance | ||
func NewStrategy(typer runtime.ObjectTyper) fooStrategy { | ||
return fooStrategy{typer, names.SimpleNameGenerator} | ||
} | ||
|
||
// GetAttrs returns labels.Set, fields.Set, and error in case the given runtime.Object is not a Fischer | ||
func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) { | ||
apiserver, ok := obj.(*hellov1.Foo) | ||
if !ok { | ||
return nil, nil, fmt.Errorf("given object is not a Fischer") | ||
} | ||
return labels.Set(apiserver.ObjectMeta.Labels), SelectableFields(apiserver), nil | ||
} | ||
|
||
// MatchFoo is the filter used by the generic etcd backend to watch events | ||
// from etcd to clients of the apiserver only interested in specific labels/fields. | ||
func MatchFoo(label labels.Selector, field fields.Selector) storage.SelectionPredicate { | ||
return storage.SelectionPredicate{ | ||
Label: label, | ||
Field: field, | ||
GetAttrs: GetAttrs, | ||
} | ||
} | ||
|
||
// SelectableFields returns a field set that represents the object. | ||
func SelectableFields(obj *hellov1.Foo) fields.Set { | ||
return generic.ObjectMetaFieldsSet(&obj.ObjectMeta, true) | ||
} | ||
|
||
type fooStrategy struct { | ||
runtime.ObjectTyper | ||
names.NameGenerator | ||
} | ||
|
||
func (fooStrategy) NamespaceScoped() bool { | ||
return true | ||
} | ||
|
||
func (fooStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) { | ||
} | ||
|
||
func (fooStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) { | ||
} | ||
|
||
func (fooStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList { | ||
_ = obj.(*hellov1.Foo) | ||
return nil | ||
} | ||
|
||
// WarningsOnCreate returns warnings for the creation of the given object. | ||
func (fooStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { return nil } | ||
|
||
func (fooStrategy) AllowCreateOnUpdate() bool { | ||
return false | ||
} | ||
|
||
func (fooStrategy) AllowUnconditionalUpdate() bool { | ||
return false | ||
} | ||
|
||
func (fooStrategy) Canonicalize(obj runtime.Object) { | ||
} | ||
|
||
func (fooStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList { | ||
return field.ErrorList{} | ||
} | ||
|
||
// WarningsOnUpdate returns warnings for the given update. | ||
func (fooStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { | ||
return nil | ||
} | ||
|
||
func (fooStrategy) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) { | ||
var table metav1.Table | ||
|
||
table.ColumnDefinitions = []metav1.TableColumnDefinition{ | ||
{Name: "Name", Type: "string", Format: "name", Description: metav1.ObjectMeta{}.SwaggerDoc()["name"]}, | ||
{Name: "Age", Type: "string", Description: metav1.ObjectMeta{}.SwaggerDoc()["creationTimestamp"]}, | ||
{Name: "Message", Type: "string", Format: "message", Description: "foo message"}, | ||
{Name: "Message1", Type: "string", Format: "message1", Description: "foo message plus"}, | ||
} | ||
|
||
switch t := object.(type) { | ||
case *hellov1.Foo: | ||
table.ResourceVersion = t.ResourceVersion | ||
addFoosToTable(&table, *t) | ||
case *hellov1.FooList: | ||
table.ResourceVersion = t.ResourceVersion | ||
table.Continue = t.Continue | ||
addFoosToTable(&table, t.Items...) | ||
default: | ||
} | ||
|
||
return &table, nil | ||
} |