From b80039523f49e7bfd335031ac82208f1bbf6bd40 Mon Sep 17 00:00:00 2001 From: Darren Shepherd Date: Fri, 29 Dec 2017 19:14:59 -0700 Subject: [PATCH] Fix update --- store/crd/crd_store.go | 2 +- store/proxy/proxy_store.go | 56 ++++++++++++++++++++++++++++++++++---- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/store/crd/crd_store.go b/store/crd/crd_store.go index efe451de..f00fcaf7 100644 --- a/store/crd/crd_store.go +++ b/store/crd/crd_store.go @@ -119,7 +119,7 @@ func (c *Store) AddSchemas(ctx context.Context, schemas ...*types.Schema) error } for schema, crd := range schemaStatus { - c.schemaStores[key(schema)] = proxy.NewProxyStore(c.k8sClient, + c.schemaStores[key(schema)] = proxy.NewProxyStoreForCRD(c.k8sClient, []string{"apis"}, crd.Spec.Group, crd.Spec.Version, diff --git a/store/proxy/proxy_store.go b/store/proxy/proxy_store.go index 48601b8b..567d80bd 100644 --- a/store/proxy/proxy_store.go +++ b/store/proxy/proxy_store.go @@ -37,12 +37,33 @@ type Store struct { kind string resourcePlural string authContext map[string]string + supportPatch bool } func NewProxyStore(k8sClient rest.Interface, prefix []string, group, version, kind, resourcePlural string) types.Store { return &errorStore{ Store: &Store{ + supportPatch: true, + k8sClient: k8sClient, + prefix: prefix, + group: group, + version: version, + kind: kind, + resourcePlural: resourcePlural, + authContext: map[string]string{ + "apiGroup": group, + "resource": resourcePlural, + }, + }, + } +} + +func NewProxyStoreForCRD(k8sClient rest.Interface, + prefix []string, group, version, kind, resourcePlural string) types.Store { + return &errorStore{ + Store: &Store{ + supportPatch: false, k8sClient: k8sClient, prefix: prefix, group: group, @@ -217,15 +238,40 @@ func (p *Store) toInternal(mapper types.Mapper, data map[string]interface{}) { } func (p *Store) Update(apiContext *types.APIContext, schema *types.Schema, data map[string]interface{}, id string) (map[string]interface{}, error) { - p.toInternal(schema.Mapper, data) + if p.supportPatch { + p.toInternal(schema.Mapper, data) + namespace, id := splitID(id) + + req := p.common(namespace, p.k8sClient.Patch(patchtype.StrategicMergePatchType)). + Body(&unstructured.Unstructured{ + Object: data, + }). + Name(id). + SetHeader("Content-Type", string(patchtype.StrategicMergePatchType)) + + _, result, err := p.singleResult(apiContext, schema, req) + return result, err + } + + resourceVersion, existing, err := p.byID(apiContext, schema, id) + if err != nil { + return data, nil + } + + for k, v := range data { + existing[k] = v + } + + p.toInternal(schema.Mapper, existing) namespace, id := splitID(id) - req := p.common(namespace, p.k8sClient.Patch(patchtype.StrategicMergePatchType)). + values.PutValue(existing, resourceVersion, "metadata", "resourceVersion") + + req := p.common(namespace, p.k8sClient.Put()). Body(&unstructured.Unstructured{ - Object: data, + Object: existing, }). - Name(id). - SetHeader("Content-Type", string(patchtype.StrategicMergePatchType)) + Name(id) _, result, err := p.singleResult(apiContext, schema, req) return result, err