From aed626ebfcdaadc996bf2719dd04cda8ddbf10b6 Mon Sep 17 00:00:00 2001 From: Silvio Moioli Date: Tue, 4 Jun 2024 12:38:04 +0200 Subject: [PATCH] DRY up partition/store.go Signed-off-by: Silvio Moioli --- pkg/stores/partition/store.go | 18 ++--- pkg/stores/partitionalpha/store.go | 102 +++-------------------------- 2 files changed, 18 insertions(+), 102 deletions(-) diff --git a/pkg/stores/partition/store.go b/pkg/stores/partition/store.go index f42b65e60..b09d86564 100644 --- a/pkg/stores/partition/store.go +++ b/pkg/stores/partition/store.go @@ -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. @@ -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, @@ -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 @@ -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. @@ -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. @@ -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 }) @@ -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{} } @@ -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: @@ -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 { diff --git a/pkg/stores/partitionalpha/store.go b/pkg/stores/partitionalpha/store.go index 160f86a5e..7cfd0eee4 100644 --- a/pkg/stores/partitionalpha/store.go +++ b/pkg/stores/partitionalpha/store.go @@ -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 } @@ -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. @@ -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. @@ -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 = "" @@ -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. @@ -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. @@ -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 -}