Add revision to list

This commit is contained in:
Darren Shepherd
2019-08-12 13:00:39 -07:00
parent 01395a0ace
commit 488fc4a6e3
5 changed files with 35 additions and 4 deletions

View File

@@ -11,7 +11,9 @@ func QueryFilter(opts *types.QueryOptions, schema *types.Schema, data types.APIO
if opts == nil {
opts = &types.QueryOptions{}
}
return ApplyQueryOptions(opts, schema, data)
result := ApplyQueryOptions(opts, schema, data)
result.ListRevision = data.ListRevision
return result
}
func ApplyQueryOptions(options *types.QueryOptions, schema *types.Schema, data types.APIObject) types.APIObject {

View File

@@ -35,11 +35,15 @@ func (j *EncodingResponseWriter) Body(apiOp *types.APIRequest, writer io.Writer,
}
func (j *EncodingResponseWriter) VersionBody(apiOp *types.APIRequest, writer io.Writer, obj interface{}) error {
var output interface{}
var (
output interface{}
revision string
)
builder := builder.NewBuilder(apiOp)
if apiObject, ok := obj.(types.APIObject); ok {
obj = apiObject.Raw()
revision = apiObject.ListRevision
}
switch v := obj.(type) {
@@ -59,6 +63,10 @@ func (j *EncodingResponseWriter) VersionBody(apiOp *types.APIRequest, writer io.
output = v
}
if list, ok := output.(*types.GenericCollection); ok && revision != "" {
list.Revision = revision
}
if output != nil {
return j.Encoder(writer, output)
}

View File

@@ -1,6 +1,7 @@
package proxy
import (
"strconv"
"sync"
errors2 "github.com/pkg/errors"
@@ -51,12 +52,24 @@ func (s *Store) byID(apiOp *types.APIRequest, schema *types.Schema, id string) (
return s.singleResult(apiOp, schema, resp)
}
func max(old int, newInt string) int {
v, err := strconv.Atoi(newInt)
if err != nil {
return old
}
if v > old {
return v
}
return old
}
func (s *Store) List(apiOp *types.APIRequest, schema *types.Schema, opt *types.QueryOptions) (types.APIObject, error) {
resultList := &unstructured.UnstructuredList{}
var (
errGroup errgroup.Group
mux sync.Mutex
revision int
)
if len(apiOp.Namespaces) <= 1 {
@@ -69,6 +82,7 @@ func (s *Store) List(apiOp *types.APIRequest, schema *types.Schema, opt *types.Q
if err != nil {
return types.APIObject{}, err
}
revision = max(revision, resultList.GetResourceVersion())
} else {
allNS := apiOp.Namespaces
for _, ns := range allNS {
@@ -81,6 +95,7 @@ func (s *Store) List(apiOp *types.APIRequest, schema *types.Schema, opt *types.Q
mux.Lock()
resultList.Items = append(resultList.Items, list.Items...)
revision = max(revision, list.GetResourceVersion())
mux.Unlock()
return nil
@@ -96,7 +111,11 @@ func (s *Store) List(apiOp *types.APIRequest, schema *types.Schema, opt *types.Q
result = append(result, s.fromInternal(apiOp, schema, obj.Object))
}
return types.ToAPI(result), nil
apiObject := types.ToAPI(result)
if revision > 0 {
apiObject.ListRevision = strconv.Itoa(revision)
}
return apiObject, nil
}
func (s *Store) listNamespace(namespace string, apiOp types.APIRequest, schema *types.Schema) (*unstructured.UnstructuredList, error) {

View File

@@ -214,7 +214,8 @@ type APIEvent struct {
}
type APIObject struct {
Object interface{} `json:",inline"`
ListRevision string `json:"-"`
Object interface{} `json:",inline"`
}
func ToAPI(data interface{}) APIObject {

View File

@@ -13,6 +13,7 @@ type Collection struct {
Sort *Sort `json:"sort,omitempty"`
Filters map[string][]Condition `json:"filters,omitempty"`
ResourceType string `json:"resourceType"`
Revision string `json:"revision,omitempty"`
}
type GenericCollection struct {