Skip to content

Commit

Permalink
DRY up partition/store.go
Browse files Browse the repository at this point in the history
Signed-off-by: Silvio Moioli <[email protected]>
  • Loading branch information
moio committed Jun 4, 2024
1 parent 665ddd0 commit aed626e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 102 deletions.
18 changes: 9 additions & 9 deletions pkg/stores/partition/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func (s *Store) Delete(apiOp *types.APIRequest, schema *types.APISchema, id stri
if err != nil {
return types.APIObject{}, err
}
return toAPI(schema, obj, warnings), nil
return ToAPI(schema, obj, warnings), nil
}

// ByID looks up a single object by its ID.
Expand All @@ -124,7 +124,7 @@ func (s *Store) ByID(apiOp *types.APIRequest, schema *types.APISchema, id string
if err != nil {
return types.APIObject{}, err
}
return toAPI(schema, obj, warnings), nil
return ToAPI(schema, obj, warnings), nil
}

func (s *Store) listPartition(ctx context.Context, apiOp *types.APIRequest, schema *types.APISchema, partition Partition,
Expand Down Expand Up @@ -226,7 +226,7 @@ func (s *Store) List(apiOp *types.APIRequest, schema *types.APISchema) (types.AP

for _, item := range list {
item := item.DeepCopy()
result.Objects = append(result.Objects, toAPI(schema, item, nil))
result.Objects = append(result.Objects, ToAPI(schema, item, nil))
}

result.Pages = pages
Expand Down Expand Up @@ -266,7 +266,7 @@ func (s *Store) Create(apiOp *types.APIRequest, schema *types.APISchema, data ty
if err != nil {
return types.APIObject{}, err
}
return toAPI(schema, obj, warnings), nil
return ToAPI(schema, obj, warnings), nil
}

// Update updates a single object in the store.
Expand All @@ -280,7 +280,7 @@ func (s *Store) Update(apiOp *types.APIRequest, schema *types.APISchema, data ty
if err != nil {
return types.APIObject{}, err
}
return toAPI(schema, obj, warnings), nil
return ToAPI(schema, obj, warnings), nil
}

// Watch returns a channel of events for a list or resource.
Expand Down Expand Up @@ -310,7 +310,7 @@ func (s *Store) Watch(apiOp *types.APIRequest, schema *types.APISchema, wr types
return err
}
for i := range c {
response <- toAPIEvent(apiOp, schema, i)
response <- ToAPIEvent(apiOp, schema, i)
}
return nil
})
Expand All @@ -326,7 +326,7 @@ func (s *Store) Watch(apiOp *types.APIRequest, schema *types.APISchema, wr types
return response, nil
}

func toAPI(schema *types.APISchema, obj runtime.Object, warnings []types.Warning) types.APIObject {
func ToAPI(schema *types.APISchema, obj runtime.Object, warnings []types.Warning) types.APIObject {
if obj == nil || reflect.ValueOf(obj).IsNil() {
return types.APIObject{}
}
Expand Down Expand Up @@ -372,7 +372,7 @@ func moveToUnderscore(obj *unstructured.Unstructured) *unstructured.Unstructured
return obj
}

func toAPIEvent(apiOp *types.APIRequest, schema *types.APISchema, event watch.Event) types.APIEvent {
func ToAPIEvent(apiOp *types.APIRequest, schema *types.APISchema, event watch.Event) types.APIEvent {
name := types.ChangeAPIEvent
switch event.Type {
case watch.Deleted:
Expand All @@ -393,7 +393,7 @@ func toAPIEvent(apiOp *types.APIRequest, schema *types.APISchema, event watch.Ev
return apiEvent
}

apiEvent.Object = toAPI(schema, event.Object, nil)
apiEvent.Object = ToAPI(schema, event.Object, nil)

m, err := meta.Accessor(event.Object)
if err != nil {
Expand Down
102 changes: 9 additions & 93 deletions pkg/stores/partitionalpha/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,16 @@ package partitionalpha

import (
"context"
"fmt"
"reflect"

"github.com/rancher/apiserver/pkg/types"
"github.com/rancher/lasso/pkg/cache/sql/partition"
lassopartition "github.com/rancher/lasso/pkg/cache/sql/partition"
"github.com/rancher/steve/pkg/accesscontrol"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/watch"
"github.com/rancher/steve/pkg/stores/partition"
)

// Partitioner is an interface for interacting with partitions.
type Partitioner interface {
All(apiOp *types.APIRequest, schema *types.APISchema, verb, id string) ([]partition.Partition, error)
All(apiOp *types.APIRequest, schema *types.APISchema, verb, id string) ([]lassopartition.Partition, error)
Store() UnstructuredStore
}

Expand Down Expand Up @@ -54,7 +48,7 @@ func (s *Store) Delete(apiOp *types.APIRequest, schema *types.APISchema, id stri
if err != nil {
return types.APIObject{}, err
}
return toAPI(schema, obj, warnings), nil
return partition.ToAPI(schema, obj, warnings), nil
}

// ByID looks up a single object by its ID.
Expand All @@ -65,7 +59,7 @@ func (s *Store) ByID(apiOp *types.APIRequest, schema *types.APISchema, id string
if err != nil {
return types.APIObject{}, err
}
return toAPI(schema, obj, warnings), nil
return partition.ToAPI(schema, obj, warnings), nil
}

// List returns a list of objects across all applicable partitions.
Expand All @@ -91,7 +85,7 @@ func (s *Store) List(apiOp *types.APIRequest, schema *types.APISchema) (types.AP

for _, item := range list {
item := item.DeepCopy()
result.Objects = append(result.Objects, toAPI(schema, item, nil))
result.Objects = append(result.Objects, partition.ToAPI(schema, item, nil))
}

result.Revision = ""
Expand All @@ -107,7 +101,7 @@ func (s *Store) Create(apiOp *types.APIRequest, schema *types.APISchema, data ty
if err != nil {
return types.APIObject{}, err
}
return toAPI(schema, obj, warnings), nil
return partition.ToAPI(schema, obj, warnings), nil
}

// Update updates a single object in the store.
Expand All @@ -118,7 +112,7 @@ func (s *Store) Update(apiOp *types.APIRequest, schema *types.APISchema, data ty
if err != nil {
return types.APIObject{}, err
}
return toAPI(schema, obj, warnings), nil
return partition.ToAPI(schema, obj, warnings), nil
}

// Watch returns a channel of events for a list or resource.
Expand All @@ -140,87 +134,9 @@ func (s *Store) Watch(apiOp *types.APIRequest, schema *types.APISchema, wr types
defer close(response)

for i := range c {
response <- toAPIEvent(schema, i)
response <- partition.ToAPIEvent(nil, schema, i)
}
}()

return response, nil
}

func toAPI(schema *types.APISchema, obj runtime.Object, warnings []types.Warning) types.APIObject {
if obj == nil || reflect.ValueOf(obj).IsNil() {
return types.APIObject{}
}

if unstr, ok := obj.(*unstructured.Unstructured); ok {
obj = moveToUnderscore(unstr)
}

apiObject := types.APIObject{
Type: schema.ID,
Object: obj,
}

m, err := meta.Accessor(obj)
if err != nil {
return apiObject
}

id := m.GetName()
ns := m.GetNamespace()
if ns != "" {
id = fmt.Sprintf("%s/%s", ns, id)
}

apiObject.ID = id
apiObject.Warnings = warnings
return apiObject
}

func moveToUnderscore(obj *unstructured.Unstructured) *unstructured.Unstructured {
if obj == nil {
return nil
}

for k := range types.ReservedFields {
v, ok := obj.Object[k]
if ok {
delete(obj.Object, k)
obj.Object["_"+k] = v
}
}

return obj
}

func toAPIEvent(schema *types.APISchema, event watch.Event) types.APIEvent {
name := types.ChangeAPIEvent
switch event.Type {
case watch.Deleted:
name = types.RemoveAPIEvent
case watch.Added:
name = types.CreateAPIEvent
case watch.Error:
name = "resource.error"
}

apiEvent := types.APIEvent{
Name: name,
}

if event.Type == watch.Error {
status, _ := event.Object.(*metav1.Status)
apiEvent.Error = fmt.Errorf(status.Message)
return apiEvent
}

apiEvent.Object = toAPI(schema, event.Object, nil)

m, err := meta.Accessor(event.Object)
if err != nil {
return apiEvent
}

apiEvent.Revision = m.GetResourceVersion()
return apiEvent
}

0 comments on commit aed626e

Please sign in to comment.