Skip to content

Commit

Permalink
Update resource.Schema to include ZeroList() (#280)
Browse files Browse the repository at this point in the history
Updated resource.Schema to include a ZeroList() function, and
resource.NewSimpleSchema to require an example zero list object.

This is a follow-up to
#277
  • Loading branch information
IfSentient authored May 13, 2024
1 parent 9ca9936 commit 9ec2f2b
Show file tree
Hide file tree
Showing 18 changed files with 118 additions and 68 deletions.
2 changes: 1 addition & 1 deletion codegen/templates/schema.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// schema is unexported to prevent accidental overwrites
var (
schema{{.Kind}} = resource.NewSimpleSchema("{{.Group}}", "{{.Version}}", &{{.Kind}}{}, resource.WithKind("{{.Kind}}"),
schema{{.Kind}} = resource.NewSimpleSchema("{{.Group}}", "{{.Version}}", &{{.Kind}}{}, &{{.Kind}}List{}, resource.WithKind("{{.Kind}}"),
resource.WithPlural("{{.Plural}}"), resource.WithScope(resource.{{.Scope}}Scope))
kind{{.Kind}} = resource.Kind{
Schema: schema{{.Kind}},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

// schema is unexported to prevent accidental overwrites
var (
schemaCustomKind = resource.NewSimpleSchema("custom.ext.grafana.com", "v0-0", &CustomKind{}, resource.WithKind("CustomKind"),
schemaCustomKind = resource.NewSimpleSchema("custom.ext.grafana.com", "v0-0", &CustomKind{}, &CustomKindList{}, resource.WithKind("CustomKind"),
resource.WithPlural("customkinds"), resource.WithScope(resource.NamespacedScope))
kindCustomKind = resource.Kind{
Schema: schemaCustomKind,
Expand Down
2 changes: 1 addition & 1 deletion examples/operator/opinionated/reconciler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func main() {
kubeConfig.APIPath = "/apis" // Don't know why this isn't set correctly by default, but it isn't

// Create a schema to use
schema := resource.NewSimpleSchema("example.grafana.com", "v1", &resource.TypedSpecObject[OpinionatedModel]{}, resource.WithKind("OpinionatedCustomResource"))
schema := resource.NewSimpleSchema("example.grafana.com", "v1", &resource.TypedSpecObject[OpinionatedModel]{}, &resource.TypedList[*resource.TypedSpecObject[OpinionatedModel]]{}, resource.WithKind("OpinionatedCustomResource"))
kind := resource.Kind{
Schema: schema,
Codecs: map[resource.KindEncoding]resource.Codec{resource.KindEncodingJSON: resource.NewJSONCodec()},
Expand Down
2 changes: 1 addition & 1 deletion examples/operator/opinionated/watcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func main() {
kubeConfig.APIPath = "/apis" // Don't know why this isn't set correctly by default, but it isn't

// Create a schema to use
schema := resource.NewSimpleSchema("example.grafana.com", "v1", &resource.TypedSpecObject[OpinionatedModel]{}, resource.WithKind("OpinionatedCustomResource"))
schema := resource.NewSimpleSchema("example.grafana.com", "v1", &resource.TypedSpecObject[OpinionatedModel]{}, &resource.TypedList[*resource.TypedSpecObject[OpinionatedModel]]{}, resource.WithKind("OpinionatedCustomResource"))
kind := resource.Kind{
Schema: schema,
Codecs: map[resource.KindEncoding]resource.Codec{resource.KindEncodingJSON: resource.NewJSONCodec()},
Expand Down
2 changes: 1 addition & 1 deletion examples/operator/simple/reconciler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func main() {
kubeConfig.APIPath = "/apis" // Don't know why this isn't set correctly by default, but it isn't

// Create a schema to use
schema := resource.NewSimpleSchema("example.grafana.com", "v1", &resource.TypedSpecObject[BasicModel]{}, resource.WithKind("BasicCustomResource"))
schema := resource.NewSimpleSchema("example.grafana.com", "v1", &resource.TypedSpecObject[BasicModel]{}, &resource.TypedList[*resource.TypedSpecObject[BasicModel]]{}, resource.WithKind("BasicCustomResource"))
kind := resource.Kind{
Schema: schema,
Codecs: map[resource.KindEncoding]resource.Codec{resource.KindEncodingJSON: resource.NewJSONCodec()},
Expand Down
2 changes: 1 addition & 1 deletion examples/operator/simple/watcher/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func main() {
kubeConfig.APIPath = "/apis" // Don't know why this isn't set correctly by default, but it isn't

// Create a schema to use
schema := resource.NewSimpleSchema("example.grafana.com", "v1", &resource.TypedSpecObject[BasicModel]{}, resource.WithKind("BasicCustomResource"))
schema := resource.NewSimpleSchema("example.grafana.com", "v1", &resource.TypedSpecObject[BasicModel]{}, &resource.TypedList[*resource.TypedSpecObject[BasicModel]]{}, resource.WithKind("BasicCustomResource"))
kind := resource.Kind{
Schema: schema,
Codecs: map[resource.KindEncoding]resource.Codec{resource.KindEncodingJSON: resource.NewJSONCodec()},
Expand Down
6 changes: 3 additions & 3 deletions examples/resource/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@ import (
// Schemas are defined here for clarity
var (
obj1Kind = resource.Kind{
Schema: resource.NewSimpleSchema("org.example.obj", "v1", &Obj1{}),
Schema: resource.NewSimpleSchema("org.example.obj", "v1", &Obj1{}, &resource.UntypedList{}),
Codecs: map[resource.KindEncoding]resource.Codec{resource.KindEncodingJSON: resource.NewJSONCodec()},
}
obj2Kind = resource.Kind{
Schema: resource.NewSimpleSchema("org.example.obj", "v1", &Obj2{}),
Schema: resource.NewSimpleSchema("org.example.obj", "v1", &Obj2{}, &resource.UntypedList{}),
Codecs: map[resource.KindEncoding]resource.Codec{resource.KindEncodingJSON: resource.NewJSONCodec()},
}
def1Kind = resource.Kind{
Schema: resource.NewSimpleSchema("org.example.def", "v1", &Def1{}),
Schema: resource.NewSimpleSchema("org.example.def", "v1", &Def1{}, &resource.UntypedList{}),
Codecs: map[resource.KindEncoding]resource.Codec{resource.KindEncodingJSON: resource.NewJSONCodec()},
}

Expand Down
2 changes: 1 addition & 1 deletion k8s/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

var (
testSchema = resource.NewSimpleSchema("group", "version", &resource.TypedSpecObject[testSpec]{}, resource.WithKind("test"))
testSchema = resource.NewSimpleSchema("group", "version", &resource.TypedSpecObject[testSpec]{}, &resource.TypedList[*resource.TypedSpecObject[testSpec]]{}, resource.WithKind("test"))
testKind = resource.Kind{
Schema: testSchema,
Codecs: map[resource.KindEncoding]resource.Codec{resource.KindEncodingJSON: resource.NewJSONCodec()},
Expand Down
39 changes: 39 additions & 0 deletions k8s/translation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,42 @@ func (tro *TestResourceObject) Copy() resource.Object {
func (tro *TestResourceObject) DeepCopyObject() runtime.Object {
return tro.Copy()
}

type TestResourceObjectList struct {
metav1.TypeMeta
metav1.ListMeta
Items []TestResourceObject
}

func (o *TestResourceObjectList) DeepCopyObject() runtime.Object {
return o.Copy()
}

func (o *TestResourceObjectList) Copy() resource.ListObject {
cpy := &TestResourceObjectList{
TypeMeta: o.TypeMeta,
Items: make([]TestResourceObject, len(o.Items)),
}
o.ListMeta.DeepCopyInto(&cpy.ListMeta)
for i := 0; i < len(o.Items); i++ {
if item, ok := o.Items[i].Copy().(*TestResourceObject); ok {
cpy.Items[i] = *item
}
}
return cpy
}

func (o *TestResourceObjectList) GetItems() []resource.Object {
items := make([]resource.Object, len(o.Items))
for i := 0; i < len(o.Items); i++ {
items[i] = &o.Items[i]
}
return items
}

func (o *TestResourceObjectList) SetItems(items []resource.Object) {
o.Items = make([]TestResourceObject, len(items))
for i := 0; i < len(items); i++ {
o.Items[i] = *items[i].(*TestResourceObject)
}
}
4 changes: 2 additions & 2 deletions k8s/webhooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ func (w *WebhookServer) HandleValidateHTTP(writer http.ResponseWriter, req *http
controller = tpl.controller
} else if w.DefaultValidatingController != nil {
// If we have a default controller, create a SimpleObject schema and use the default controller
schema.Schema = resource.NewSimpleSchema(admRev.Request.RequestKind.Group, admRev.Request.RequestKind.Version, &resource.TypedSpecObject[any]{}, resource.WithKind(admRev.Request.RequestKind.Kind))
schema.Schema = resource.NewSimpleSchema(admRev.Request.RequestKind.Group, admRev.Request.RequestKind.Version, &resource.TypedSpecObject[any]{}, &resource.TypedList[*resource.TypedSpecObject[any]]{}, resource.WithKind(admRev.Request.RequestKind.Kind))
schema.Codecs = map[resource.KindEncoding]resource.Codec{resource.KindEncodingJSON: resource.NewJSONCodec()}
controller = w.DefaultValidatingController
}
Expand Down Expand Up @@ -279,7 +279,7 @@ func (w *WebhookServer) HandleMutateHTTP(writer http.ResponseWriter, req *http.R
controller = tpl.controller
} else if w.DefaultMutatingController != nil {
// If we have a default controller, create a SimpleObject schema and use the default controller
schema.Schema = resource.NewSimpleSchema(admRev.Request.RequestKind.Group, admRev.Request.RequestKind.Version, &resource.TypedSpecObject[any]{}, resource.WithKind(admRev.Request.RequestKind.Kind))
schema.Schema = resource.NewSimpleSchema(admRev.Request.RequestKind.Group, admRev.Request.RequestKind.Version, &resource.TypedSpecObject[any]{}, &resource.TypedList[*resource.TypedSpecObject[any]]{}, resource.WithKind(admRev.Request.RequestKind.Kind))
schema.Codecs = map[resource.KindEncoding]resource.Codec{resource.KindEncodingJSON: resource.NewJSONCodec()}
controller = w.DefaultMutatingController
}
Expand Down
14 changes: 7 additions & 7 deletions k8s/webhooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ func TestWebhookServer_AddMutatingAdmissionController(t *testing.T) {
c2 := &testMutatingAdmissionController{}
c3 := &testMutatingAdmissionController{}
sch1 := resource.Kind{
Schema: resource.NewSimpleSchema("foo", "v1", &TestResourceObject{}, resource.WithKind("bar")),
Schema: resource.NewSimpleSchema("foo", "v1", &TestResourceObject{}, &TestResourceObjectList{}, resource.WithKind("bar")),
Codecs: map[resource.KindEncoding]resource.Codec{resource.KindEncodingJSON: resource.NewJSONCodec()},
}
sch2 := resource.Kind{
Schema: resource.NewSimpleSchema("bar", "v1", &TestResourceObject{}, resource.WithKind("foo")),
Schema: resource.NewSimpleSchema("bar", "v1", &TestResourceObject{}, &TestResourceObjectList{}, resource.WithKind("foo")),
Codecs: map[resource.KindEncoding]resource.Codec{resource.KindEncodingJSON: resource.NewJSONCodec()},
}

Expand Down Expand Up @@ -155,11 +155,11 @@ func TestWebhookServer_AddValidatingAdmissionController(t *testing.T) {
c2 := &testValidatingAdmissionController{}
c3 := &testValidatingAdmissionController{}
sch1 := resource.Kind{
Schema: resource.NewSimpleSchema("foo", "v1", &TestResourceObject{}, resource.WithKind("bar")),
Schema: resource.NewSimpleSchema("foo", "v1", &TestResourceObject{}, &TestResourceObjectList{}, resource.WithKind("bar")),
Codecs: map[resource.KindEncoding]resource.Codec{resource.KindEncodingJSON: resource.NewJSONCodec()},
}
sch2 := resource.Kind{
Schema: resource.NewSimpleSchema("bar", "v1", &TestResourceObject{}, resource.WithKind("foo")),
Schema: resource.NewSimpleSchema("bar", "v1", &TestResourceObject{}, &TestResourceObjectList{}, resource.WithKind("foo")),
Codecs: map[resource.KindEncoding]resource.Codec{resource.KindEncodingJSON: resource.NewJSONCodec()},
}

Expand Down Expand Up @@ -301,7 +301,7 @@ func TestWebhookServer_HandleMutateHTTP(t *testing.T) {
},
MutatingControllers: map[*resource.Kind]resource.MutatingAdmissionController{
&resource.Kind{
Schema: resource.NewSimpleSchema("foo", "v1", &TestResourceObject{}, resource.WithKind("bar")),
Schema: resource.NewSimpleSchema("foo", "v1", &TestResourceObject{}, &TestResourceObjectList{}, resource.WithKind("bar")),
Codecs: map[resource.KindEncoding]resource.Codec{resource.KindEncodingJSON: resource.NewJSONCodec()},
}: &testMutatingAdmissionController{
MutateFunc: func(ctx context.Context, request *resource.AdmissionRequest) (*resource.MutatingResponse, error) {
Expand All @@ -325,7 +325,7 @@ func TestWebhookServer_HandleMutateHTTP(t *testing.T) {
},
MutatingControllers: map[*resource.Kind]resource.MutatingAdmissionController{
&resource.Kind{
Schema: resource.NewSimpleSchema("foo", "v1", &TestResourceObject{}, resource.WithKind("bar")),
Schema: resource.NewSimpleSchema("foo", "v1", &TestResourceObject{}, &TestResourceObjectList{}, resource.WithKind("bar")),
Codecs: map[resource.KindEncoding]resource.Codec{resource.KindEncodingJSON: resource.NewJSONCodec()},
}: &testMutatingAdmissionController{
MutateFunc: func(ctx context.Context, request *resource.AdmissionRequest) (*resource.MutatingResponse, error) {
Expand Down Expand Up @@ -438,7 +438,7 @@ func TestWebhookServer_HandleValidateHTTP(t *testing.T) {
},
ValidatingControllers: map[*resource.Kind]resource.ValidatingAdmissionController{
&resource.Kind{
Schema: resource.NewSimpleSchema("foo", "v1", &TestResourceObject{}, resource.WithKind("bar")),
Schema: resource.NewSimpleSchema("foo", "v1", &TestResourceObject{}, &TestResourceObjectList{}, resource.WithKind("bar")),
Codecs: map[resource.KindEncoding]resource.Codec{resource.KindEncodingJSON: resource.NewJSONCodec()},
}: &testValidatingAdmissionController{
ValidateFunc: func(ctx context.Context, request *resource.AdmissionRequest) error {
Expand Down
14 changes: 7 additions & 7 deletions operator/opinionatedwatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (

func TestNewOpinionatedWatcher(t *testing.T) {
ex := &resource.TypedSpecObject[string]{}
schema := resource.NewSimpleSchema("group", "version", ex, resource.WithKind("my-crd"))
schema := resource.NewSimpleSchema("group", "version", ex, &resource.TypedList[*resource.TypedSpecObject[string]]{}, resource.WithKind("my-crd"))
client := &mockPatchClient{}

t.Run("nil args", func(t *testing.T) {
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestOpinionatedWatcher_Wrap(t *testing.T) {
}

t.Run("nil watcher", func(t *testing.T) {
w, err := NewOpinionatedWatcher(resource.NewSimpleSchema("", "", &resource.TypedSpecObject[string]{}), &mockPatchClient{})
w, err := NewOpinionatedWatcher(resource.NewSimpleSchema("", "", &resource.TypedSpecObject[string]{}, &resource.TypedList[*resource.TypedSpecObject[string]]{}), &mockPatchClient{})
assert.Nil(t, err)
w.Wrap(nil, false)
assert.Nil(t, w.AddFunc)
Expand All @@ -71,7 +71,7 @@ func TestOpinionatedWatcher_Wrap(t *testing.T) {
})

t.Run("syncToAdd=false", func(t *testing.T) {
w, err := NewOpinionatedWatcher(resource.NewSimpleSchema("", "", &resource.TypedSpecObject[string]{}), &mockPatchClient{})
w, err := NewOpinionatedWatcher(resource.NewSimpleSchema("", "", &resource.TypedSpecObject[string]{}, &resource.TypedList[*resource.TypedSpecObject[string]]{}), &mockPatchClient{})
assert.Nil(t, err)
w.Wrap(simple, false)
assert.NotNil(t, w.AddFunc)
Expand All @@ -81,7 +81,7 @@ func TestOpinionatedWatcher_Wrap(t *testing.T) {
})

t.Run("syncToAdd=true", func(t *testing.T) {
w, err := NewOpinionatedWatcher(resource.NewSimpleSchema("", "", &resource.TypedSpecObject[string]{}), &mockPatchClient{})
w, err := NewOpinionatedWatcher(resource.NewSimpleSchema("", "", &resource.TypedSpecObject[string]{}, &resource.TypedList[*resource.TypedSpecObject[string]]{}), &mockPatchClient{})
assert.Nil(t, err)
w.Wrap(simple, true)
assert.NotNil(t, w.AddFunc)
Expand All @@ -93,7 +93,7 @@ func TestOpinionatedWatcher_Wrap(t *testing.T) {

func TestOpinionatedWatcher_Add(t *testing.T) {
ex := &resource.TypedSpecObject[string]{}
schema := resource.NewSimpleSchema("group", "version", ex)
schema := resource.NewSimpleSchema("group", "version", ex, &resource.TypedList[*resource.TypedSpecObject[string]]{})
client := &mockPatchClient{}
o, err := NewOpinionatedWatcher(schema, client)
assert.Nil(t, err)
Expand Down Expand Up @@ -235,7 +235,7 @@ func TestOpinionatedWatcher_Add(t *testing.T) {

func TestOpinionatedWatcher_Update(t *testing.T) {
ex := &resource.TypedSpecObject[string]{}
schema := resource.NewSimpleSchema("group", "version", ex)
schema := resource.NewSimpleSchema("group", "version", ex, &resource.TypedList[*resource.TypedSpecObject[string]]{})
client := &mockPatchClient{}
o, err := NewOpinionatedWatcher(schema, client)
assert.Nil(t, err)
Expand Down Expand Up @@ -373,7 +373,7 @@ func TestOpinionatedWatcher_Update(t *testing.T) {

func TestOpinionatedWatcher_Delete(t *testing.T) {
ex := &resource.TypedSpecObject[string]{}
schema := resource.NewSimpleSchema("group", "version", ex)
schema := resource.NewSimpleSchema("group", "version", ex, &resource.TypedList[*resource.TypedSpecObject[string]]{})
client := &mockPatchClient{}
o, err := NewOpinionatedWatcher(schema, client)
assert.Nil(t, err)
Expand Down
2 changes: 1 addition & 1 deletion plugin/router/resourcegroup_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

var (
testResource = resource.Kind{
Schema: resource.NewSimpleSchema("test.resource", "v1", &Test{}, resource.WithKind("Test")),
Schema: resource.NewSimpleSchema("test.resource", "v1", &Test{}, &resource.UntypedList{}, resource.WithKind("Test")),
Codecs: map[resource.KindEncoding]resource.Codec{resource.KindEncodingJSON: resource.NewJSONCodec()},
}
testResourceGroup = &rg{[]resource.Kind{testResource}}
Expand Down
35 changes: 23 additions & 12 deletions resource/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ type Schema interface {
Plural() string
// ZeroValue returns the "zero-value", "default", or "empty" version of an Object of this Schema
ZeroValue() Object
// ZeroListValue returns a ListObject implementation which represents an empty (or appropriately "zero-value")
// version of the Schema's List.
ZeroListValue() ListObject
// Scope returns the scope of the schema object
Scope() SchemaScope
}
Expand All @@ -43,12 +46,13 @@ type SchemaGroup interface {
// though the easiest way to define a schema is via codegen.
// TODO: codegen info
type SimpleSchema struct {
group string
version string
kind string
plural string
scope SchemaScope
zero Object
group string
version string
kind string
plural string
scope SchemaScope
zero Object
zeroList ListObject
}

// Group returns the SimpleSchema's Group
Expand Down Expand Up @@ -82,6 +86,12 @@ func (s *SimpleSchema) ZeroValue() Object {
return s.zero.Copy()
}

// ZeroListValue returns a copy the SimpleSchema's zero-valued ListObject instance
// It can be used directly, as the returned interface is a copy.
func (s *SimpleSchema) ZeroListValue() ListObject {
return s.zeroList.Copy()
}

// SimpleSchemaGroup collects schemas with the same group and version
// Deprecated: Kinds are now favored over Schemas for usage. Use KindGroup instead.
type SimpleSchemaGroup struct {
Expand All @@ -97,8 +107,8 @@ func (g *SimpleSchemaGroup) Schemas() []Schema {

// AddSchema creates a new SimpleSchema with the SimpleSchemaGroup's group and version,
// adds it to the SimpleSchemaGroup, and returns the created SimpleSchema
func (g *SimpleSchemaGroup) AddSchema(zeroVal Object, opts ...SimpleSchemaOption) *SimpleSchema {
s := NewSimpleSchema(g.group, g.version, zeroVal, opts...)
func (g *SimpleSchemaGroup) AddSchema(zeroVal Object, zeroList ListObject, opts ...SimpleSchemaOption) *SimpleSchema {
s := NewSimpleSchema(g.group, g.version, zeroVal, zeroList, opts...)
g.schemas = append(g.schemas, s)
return s
}
Expand Down Expand Up @@ -130,11 +140,12 @@ func WithScope(scope SchemaScope) func(schema *SimpleSchema) {
}

// NewSimpleSchema returns a new SimpleSchema
func NewSimpleSchema(group, version string, zeroVal Object, opts ...SimpleSchemaOption) *SimpleSchema {
func NewSimpleSchema(group, version string, zeroVal Object, zeroList ListObject, opts ...SimpleSchemaOption) *SimpleSchema {
s := SimpleSchema{
group: group,
version: version,
zero: zeroVal,
group: group,
version: version,
zero: zeroVal,
zeroList: zeroList,
}
for _, opt := range opts {
opt(&s)
Expand Down
Loading

0 comments on commit 9ec2f2b

Please sign in to comment.