Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: provide an example of typed resource type #368

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 27 additions & 45 deletions pkg/controller/conformance/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ func (ctrl *IntToStrController) Inputs() []controller.Input {
return []controller.Input{
{
Namespace: ctrl.SourceNamespace,
Type: IntResourceType,
Type: IntResourceType.Naked(),
Kind: controller.InputStrong,
},
{
Namespace: ctrl.TargetNamespace,
Type: StrResourceType,
Type: StrResourceType.Naked(),
Kind: controller.InputDestroyReady,
},
}
Expand All @@ -48,7 +48,7 @@ func (ctrl *IntToStrController) Inputs() []controller.Input {
func (ctrl *IntToStrController) Outputs() []controller.Output {
return []controller.Output{
{
Type: StrResourceType,
Type: StrResourceType.Naked(),
Kind: controller.OutputExclusive,
},
}
Expand All @@ -58,7 +58,7 @@ func (ctrl *IntToStrController) Outputs() []controller.Output {
//
//nolint:gocognit
func (ctrl *IntToStrController) Run(ctx context.Context, r controller.Runtime, _ *zap.Logger) error {
sourceMd := resource.NewMetadata(ctrl.SourceNamespace, IntResourceType, "", resource.VersionUndefined)
sourceMd := safe.NewTaggedMD(ctrl.SourceNamespace, IntResourceType, "", resource.VersionUndefined)

for {
select {
Expand All @@ -67,10 +67,7 @@ func (ctrl *IntToStrController) Run(ctx context.Context, r controller.Runtime, _
case <-r.EventCh():
}

intList, err := safe.ReaderList[interface {
IntegerResource
resource.Resource
}](ctx, r, sourceMd)
intList, err := safe.ReaderListByMD(ctx, r, sourceMd)
if err != nil {
return fmt.Errorf("error listing objects: %w", err)
}
Expand Down Expand Up @@ -154,7 +151,7 @@ func (ctrl *StrToSentenceController) Run(ctx context.Context, r controller.Runti
if err := r.UpdateInputs([]controller.Input{
{
Namespace: ctrl.SourceNamespace,
Type: StrResourceType,
Type: StrResourceType.Naked(),
Kind: controller.InputStrong,
},
{
Expand All @@ -166,7 +163,7 @@ func (ctrl *StrToSentenceController) Run(ctx context.Context, r controller.Runti
return fmt.Errorf("error setting up dependencies: %w", err)
}

sourceMd := resource.NewMetadata(ctrl.SourceNamespace, StrResourceType, "", resource.VersionUndefined)
sourceMd := safe.NewTaggedMD(ctrl.SourceNamespace, StrResourceType, "", resource.VersionUndefined)

for {
select {
Expand All @@ -175,10 +172,7 @@ func (ctrl *StrToSentenceController) Run(ctx context.Context, r controller.Runti
case <-r.EventCh():
}

strList, err := safe.ReaderList[interface {
StringResource
resource.Resource
}](ctx, r, sourceMd)
strList, err := safe.ReaderListByMD(ctx, r, sourceMd)
if err != nil {
return fmt.Errorf("error listing objects: %w", err)
}
Expand Down Expand Up @@ -250,7 +244,7 @@ func (ctrl *SumController) Inputs() []controller.Input {
func (ctrl *SumController) Outputs() []controller.Output {
return []controller.Output{
{
Type: IntResourceType,
Type: IntResourceType.Naked(),
Kind: controller.OutputShared,
},
}
Expand All @@ -261,14 +255,14 @@ func (ctrl *SumController) Run(ctx context.Context, r controller.Runtime, _ *zap
if err := r.UpdateInputs([]controller.Input{
{
Namespace: ctrl.SourceNamespace,
Type: IntResourceType,
Type: IntResourceType.Naked(),
Kind: controller.InputWeak,
},
}); err != nil {
return fmt.Errorf("error setting up dependencies: %w", err)
}

sourceMd := resource.NewMetadata(ctrl.SourceNamespace, IntResourceType, "", resource.VersionUndefined)
sourceMd := safe.NewTaggedMD(ctrl.SourceNamespace, IntResourceType, "", resource.VersionUndefined)

for {
select {
Expand All @@ -277,10 +271,7 @@ func (ctrl *SumController) Run(ctx context.Context, r controller.Runtime, _ *zap
case <-r.EventCh():
}

intList, err := safe.ReaderList[interface {
IntegerResource
resource.Resource
}](ctx, r, sourceMd, state.WithLabelQuery(resource.RawLabelQuery(ctrl.SourceLabelQuery)))
intList, err := safe.ReaderListByMD(ctx, r, sourceMd, state.WithLabelQuery(resource.RawLabelQuery(ctrl.SourceLabelQuery)))
if err != nil {
return fmt.Errorf("error listing objects: %w", err)
}
Expand Down Expand Up @@ -325,7 +316,7 @@ func (ctrl *FailingController) Inputs() []controller.Input {
func (ctrl *FailingController) Outputs() []controller.Output {
return []controller.Output{
{
Type: IntResourceType,
Type: IntResourceType.Naked(),
Kind: controller.OutputExclusive,
},
}
Expand Down Expand Up @@ -372,7 +363,7 @@ func (ctrl *IntDoublerController) Inputs() []controller.Input {
return []controller.Input{
{
Namespace: ctrl.SourceNamespace,
Type: IntResourceType,
Type: IntResourceType.Naked(),
Kind: controller.InputStrong,
},
}
Expand All @@ -382,15 +373,15 @@ func (ctrl *IntDoublerController) Inputs() []controller.Input {
func (ctrl *IntDoublerController) Outputs() []controller.Output {
return []controller.Output{
{
Type: IntResourceType,
Type: IntResourceType.Naked(),
Kind: controller.OutputShared,
},
}
}

// Run implements controller.Controller interface.
func (ctrl *IntDoublerController) Run(ctx context.Context, r controller.Runtime, _ *zap.Logger) error {
sourceMd := resource.NewMetadata(ctrl.SourceNamespace, IntResourceType, "", resource.VersionUndefined)
sourceMd := safe.NewTaggedMD(ctrl.SourceNamespace, IntResourceType, "", resource.VersionUndefined)

for {
select {
Expand All @@ -401,10 +392,7 @@ func (ctrl *IntDoublerController) Run(ctx context.Context, r controller.Runtime,

r.StartTrackingOutputs()

intList, err := safe.ReaderList[interface {
IntegerResource
resource.Resource
}](ctx, r, sourceMd)
intList, err := safe.ReaderListByMD(ctx, r, sourceMd)
if err != nil {
return fmt.Errorf("error listing objects: %w", err)
}
Expand All @@ -423,7 +411,7 @@ func (ctrl *IntDoublerController) Run(ctx context.Context, r controller.Runtime,
}
}

if err = r.CleanupOutputs(ctx, resource.NewMetadata(ctrl.TargetNamespace, IntResourceType, "", resource.VersionUndefined)); err != nil {
if err = r.CleanupOutputs(ctx, safe.NewTaggedMD(ctrl.TargetNamespace, IntResourceType, "", resource.VersionUndefined)); err != nil {
return fmt.Errorf("error cleaning up outputs: %w", err)
}
}
Expand All @@ -445,7 +433,7 @@ func (ctrl *ModifyWithResultController) Inputs() []controller.Input {
return []controller.Input{
{
Namespace: ctrl.SourceNamespace,
Type: StrResourceType,
Type: StrResourceType.Naked(),
Kind: controller.InputStrong,
},
}
Expand All @@ -455,15 +443,15 @@ func (ctrl *ModifyWithResultController) Inputs() []controller.Input {
func (ctrl *ModifyWithResultController) Outputs() []controller.Output {
return []controller.Output{
{
Type: StrResourceType,
Type: StrResourceType.Naked(),
Kind: controller.OutputExclusive,
},
}
}

// Run implements controller.Controller interface.
func (ctrl *ModifyWithResultController) Run(ctx context.Context, r controller.Runtime, _ *zap.Logger) error {
sourceMd := resource.NewMetadata(ctrl.SourceNamespace, StrResourceType, "", resource.VersionUndefined)
sourceMd := safe.NewTaggedMD(ctrl.SourceNamespace, StrResourceType, "", resource.VersionUndefined)

for {
select {
Expand All @@ -472,10 +460,7 @@ func (ctrl *ModifyWithResultController) Run(ctx context.Context, r controller.Ru
case <-r.EventCh():
}

strList, err := safe.ReaderList[interface {
StringResource
resource.Resource
}](ctx, r, sourceMd)
strList, err := safe.ReaderListByMD(ctx, r, sourceMd)
if err != nil {
return fmt.Errorf("error listing objects: %w", err)
}
Expand Down Expand Up @@ -535,12 +520,12 @@ func (ctrl *MetricsController) Inputs() []controller.Input {
return []controller.Input{
{
Namespace: ctrl.SourceNamespace,
Type: IntResourceType,
Type: IntResourceType.Naked(),
Kind: controller.InputStrong,
},
{
Namespace: ctrl.TargetNamespace,
Type: StrResourceType,
Type: StrResourceType.Naked(),
Kind: controller.InputDestroyReady,
},
}
Expand All @@ -550,15 +535,15 @@ func (ctrl *MetricsController) Inputs() []controller.Input {
func (ctrl *MetricsController) Outputs() []controller.Output {
return []controller.Output{
{
Type: StrResourceType,
Type: StrResourceType.Naked(),
Kind: controller.OutputExclusive,
},
}
}

// Run implements controller.Controller interface.
func (ctrl *MetricsController) Run(ctx context.Context, r controller.Runtime, _ *zap.Logger) error {
sourceMd := resource.NewMetadata(ctrl.SourceNamespace, IntResourceType, "", resource.VersionUndefined)
sourceMd := safe.NewTaggedMD(ctrl.SourceNamespace, IntResourceType, "", resource.VersionUndefined)

for {
select {
Expand All @@ -567,10 +552,7 @@ func (ctrl *MetricsController) Run(ctx context.Context, r controller.Runtime, _
case <-r.EventCh():
}

intList, err := safe.ReaderList[interface {
IntegerResource
resource.Resource
}](ctx, r, sourceMd)
intList, err := safe.ReaderListByMD(ctx, r, sourceMd)
if err != nil {
return fmt.Errorf("error listing objects: %w", err)
}
Expand Down
4 changes: 3 additions & 1 deletion pkg/controller/conformance/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

package conformance

import "github.com/cosi-project/runtime/pkg/resource"
import (
"github.com/cosi-project/runtime/pkg/resource"
)

// Resource represents some T value.
type Resource[T any, S Spec[T], SS SpecPtr[T, S]] struct {
Expand Down
9 changes: 5 additions & 4 deletions pkg/controller/conformance/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"encoding/binary"

"github.com/cosi-project/runtime/pkg/resource"
"github.com/cosi-project/runtime/pkg/safe"
)

// IntegerResource is implemented by resources holding ints.
Expand All @@ -23,14 +24,14 @@ type StringResource interface {
}

// IntResourceType is the type of IntResource.
const IntResourceType = resource.Type("test/int")
const IntResourceType = safe.TaggedType[*IntResource]("test/int")

// IntResource represents some integer value.
type IntResource = Resource[int, intSpec, *intSpec]

// NewIntResource creates new IntResource.
func NewIntResource(ns resource.Namespace, id resource.ID, value int) *IntResource {
return NewResource[int, intSpec, *intSpec](resource.NewMetadata(ns, IntResourceType, id, resource.VersionUndefined), value)
return NewResource[int, intSpec, *intSpec](safe.NewTaggedMD(ns, IntResourceType, id, resource.VersionUndefined).Naked(), value)
}

type intSpec struct{ ValueGetSet[int] }
Expand All @@ -48,14 +49,14 @@ func (is intSpec) MarshalProto() ([]byte, error) {
}

// StrResourceType is the type of StrResource.
const StrResourceType = resource.Type("test/str")
const StrResourceType = safe.TaggedType[*StrResource]("test/str")

// StrResource represents some string value.
type StrResource = Resource[string, strSpec, *strSpec]

// NewStrResource creates new StrResource.
func NewStrResource(ns resource.Namespace, id resource.ID, value string) *StrResource {
return NewResource[string, strSpec, *strSpec](resource.NewMetadata(ns, StrResourceType, id, resource.VersionUndefined), value)
return NewResource[string, strSpec, *strSpec](resource.NewMetadata(ns, StrResourceType.Naked(), id, resource.VersionUndefined), value)
}

type strSpec struct{ ValueGetSet[string] }
Expand Down
18 changes: 9 additions & 9 deletions pkg/controller/conformance/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ func (suite *RuntimeSuite) assertIntObjects(ids []string, values []int) retry.Re
typ := IntResourceType

return func() error {
items, err := suite.State.List(suite.ctx, resource.NewMetadata(ns, typ, "", resource.VersionUndefined))
items, err := safe.StateListByMD(suite.ctx, suite.State, safe.NewTaggedMD(ns, typ, "", resource.VersionUndefined))
if err != nil {
return err
}

if len(items.Items) != len(ids) {
return retry.ExpectedErrorf("expected %d objects, got %d", len(ids), len(items.Items))
if items.Len() != len(ids) {
return retry.ExpectedErrorf("expected %d objects, got %d", len(ids), items.Len())
}

for i, id := range ids {
Expand Down Expand Up @@ -186,13 +186,13 @@ func (suite *RuntimeSuite) TestIntToStrControllers() {
suite.Assert().NoError(suite.State.Create(suite.ctx, NewIntResource("default", "two", 2)))

suite.Assert().NoError(retry.Constant(10*time.Second, retry.WithUnits(10*time.Millisecond)).
Retry(suite.assertStrObjects("default", StrResourceType, []string{"one", "two"}, []string{"1", "2"})))
Retry(suite.assertStrObjects("default", StrResourceType.Naked(), []string{"one", "two"}, []string{"1", "2"})))

three := NewIntResource("default", "three", 3)
suite.Assert().NoError(suite.State.Create(suite.ctx, three))

suite.Assert().NoError(retry.Constant(10*time.Second, retry.WithUnits(10*time.Millisecond)).
Retry(suite.assertStrObjects("default", StrResourceType, []string{"one", "two", "three"}, []string{"1", "2", "3"})))
Retry(suite.assertStrObjects("default", StrResourceType.Naked(), []string{"one", "two", "three"}, []string{"1", "2", "3"})))

type integerResource interface {
IntegerResource
Expand All @@ -207,7 +207,7 @@ func (suite *RuntimeSuite) TestIntToStrControllers() {
suite.Assert().NoError(err)

suite.Assert().NoError(retry.Constant(10*time.Second, retry.WithUnits(10*time.Millisecond)).
Retry(suite.assertStrObjects("default", StrResourceType, []string{"one", "two", "three"}, []string{"1", "2", "33"})))
Retry(suite.assertStrObjects("default", StrResourceType.Naked(), []string{"one", "two", "three"}, []string{"1", "2", "33"})))

ready, err := suite.State.Teardown(suite.ctx, three.Metadata())
suite.Assert().NoError(err)
Expand All @@ -217,7 +217,7 @@ func (suite *RuntimeSuite) TestIntToStrControllers() {
suite.Assert().NoError(err)

suite.Assert().NoError(retry.Constant(10*time.Second, retry.WithUnits(10*time.Millisecond)).
Retry(suite.assertStrObjects("default", StrResourceType, []string{"one", "two"}, []string{"1", "2"})))
Retry(suite.assertStrObjects("default", StrResourceType.Naked(), []string{"one", "two"}, []string{"1", "2"})))

suite.Assert().NoError(suite.State.Destroy(suite.ctx, three.Metadata()))
}
Expand Down Expand Up @@ -474,7 +474,7 @@ func (suite *RuntimeSuite) TestModifyWithResultController() {
suite.Require().NoError(suite.State.Create(suite.ctx, NewStrResource(srcNS, "id", "val-1")))

suite.Assert().NoError(retry.Constant(10*time.Second, retry.WithUnits(10*time.Millisecond)).Retry(
suite.assertStrObjects(targetNS, StrResourceType,
suite.assertStrObjects(targetNS, StrResourceType.Naked(),
[]string{"id-out", "id-out-modify-result"},
[]string{"val-1-modified", "val-1-valid"},
),
Expand All @@ -490,7 +490,7 @@ func (suite *RuntimeSuite) TestModifyWithResultController() {
suite.Require().NoError(err)

suite.Assert().NoError(retry.Constant(10*time.Second, retry.WithUnits(10*time.Millisecond)).Retry(
suite.assertStrObjects(targetNS, StrResourceType,
suite.assertStrObjects(targetNS, StrResourceType.Naked(),
[]string{"id-out", "id-out-modify-result"},
[]string{"val-2-modified", "val-2-valid"},
),
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/protobuf/protobuf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ type ProtobufConformanceSuite struct {
}

func TestProtobufConformance(t *testing.T) {
require.NoError(t, protobuf.RegisterResource(conformance.IntResourceType, &conformance.IntResource{}))
require.NoError(t, protobuf.RegisterResource(conformance.StrResourceType, &conformance.StrResource{}))
require.NoError(t, protobuf.RegisterResource(conformance.IntResourceType.Naked(), &conformance.IntResource{}))
require.NoError(t, protobuf.RegisterResource(conformance.StrResourceType.Naked(), &conformance.StrResource{}))
require.NoError(t, protobuf.RegisterResource(conformance.SentenceResourceType, &conformance.SentenceResource{}))

suite := &ProtobufConformanceSuite{
Expand Down
Loading