// ExtractList returns obj's Items element as an array of runtime.Objects. // Returns an error if obj is not a List type (does not have an Items member). func ExtractList(obj runtime.Object) ([]runtime.Object, error) { itemsPtr, err := GetItemsPtr(obj) if err != nil { return nil, err } items, err := conversion.EnforcePtr(itemsPtr) if err != nil { return nil, err } list := make([]runtime.Object, items.Len()) for i := range list { raw := items.Index(i) switch item := raw.Interface().(type) { case runtime.RawExtension: switch { case item.Object != nil: list[i] = item.Object case item.Raw != nil: // TODO: Set ContentEncoding and ContentType correctly. list[i] = &runtime.Unknown{Raw: item.Raw} default: list[i] = nil } case runtime.Object: list[i] = item default: var found bool if list[i], found = raw.Addr().Interface().(runtime.Object); !found { return nil, fmt.Errorf("%v: item[%v]: Expected object, got %#v(%s)", obj, i, raw.Interface(), raw.Kind()) } } } return list, nil }
func (h *etcdHelper) extractObj(response *etcd.Response, inErr error, objPtr runtime.Object, ignoreNotFound, prevNode bool) (body string, node *etcd.Node, err error) { if response != nil { if prevNode { node = response.PrevNode } else { node = response.Node } } if inErr != nil || node == nil || len(node.Value) == 0 { if ignoreNotFound { v, err := conversion.EnforcePtr(objPtr) if err != nil { return "", nil, err } v.Set(reflect.Zero(v.Type())) return "", nil, nil } else if inErr != nil { return "", nil, inErr } return "", nil, fmt.Errorf("unable to locate a value on the response: %#v", response) } body = node.Value out, gvk, err := h.codec.Decode([]byte(body), nil, objPtr) if err != nil { return body, nil, err } if out != objPtr { return body, nil, fmt.Errorf("unable to decode object %s into %v", gvk.String(), reflect.TypeOf(objPtr)) } // being unable to set the version does not prevent the object from being extracted _ = h.versioner.UpdateObject(objPtr, node.ModifiedIndex) return body, node, err }
// Implements storage.Interface. func (c *Cacher) List(ctx context.Context, key string, resourceVersion string, pred SelectionPredicate, listObj runtime.Object) error { if resourceVersion == "" { // If resourceVersion is not specified, serve it from underlying // storage (for backward compatibility). return c.storage.List(ctx, key, resourceVersion, pred, listObj) } // If resourceVersion is specified, serve it from cache. // It's guaranteed that the returned value is at least that // fresh as the given resourceVersion. listRV, err := ParseListResourceVersion(resourceVersion) if err != nil { return err } trace := util.NewTrace(fmt.Sprintf("cacher %v: List", c.objectType.String())) defer trace.LogIfLong(500 * time.Millisecond) c.ready.wait() trace.Step("Ready") // List elements with at least 'listRV' from cache. listPtr, err := meta.GetItemsPtr(listObj) if err != nil { return err } listVal, err := conversion.EnforcePtr(listPtr) if err != nil || listVal.Kind() != reflect.Slice { return fmt.Errorf("need a pointer to slice, got %v", listVal.Kind()) } filter := filterFunction(key, pred) objs, readResourceVersion, err := c.watchCache.WaitUntilFreshAndList(listRV, trace) if err != nil { return err } trace.Step(fmt.Sprintf("Listed %d items from cache", len(objs))) if len(objs) > listVal.Cap() && pred.Label.Empty() && pred.Field.Empty() { // Resize the slice appropriately, since we already know that none // of the elements will be filtered out. listVal.Set(reflect.MakeSlice(reflect.SliceOf(c.objectType.Elem()), 0, len(objs))) trace.Step("Resized result") } for _, obj := range objs { elem, ok := obj.(*storeElement) if !ok { return fmt.Errorf("non *storeElement returned from storage: %v", obj) } if filter(elem.Key, elem.Object) { listVal.Set(reflect.Append(listVal, reflect.ValueOf(elem.Object).Elem())) } } trace.Step(fmt.Sprintf("Filtered %d items", listVal.Len())) if c.versioner != nil { if err := c.versioner.UpdateList(listObj, readResourceVersion); err != nil { return err } } return nil }
// SetZeroValue would set the object of objPtr to zero value of its type. func SetZeroValue(objPtr Object) error { v, err := conversion.EnforcePtr(objPtr) if err != nil { return err } v.Set(reflect.Zero(v.Type())) return nil }
// ListMetaFor returns a pointer to a provided object's ListMeta, // or an error if the object does not have that pointer. // TODO: allow runtime.Unknown to extract this object // TODO: Remove this function and use meta.ObjectMetaAccessor() instead. func ListMetaFor(obj runtime.Object) (*ListMeta, error) { v, err := conversion.EnforcePtr(obj) if err != nil { return nil, err } var meta *ListMeta err = runtime.FieldPtr(v, "ListMeta", &meta) return meta, err }
// Implements storage.Interface. func (h *etcdHelper) Delete(ctx context.Context, key string, out runtime.Object, preconditions *storage.Preconditions) error { if ctx == nil { glog.Errorf("Context is nil") } key = path.Join(h.pathPrefix, key) v, err := conversion.EnforcePtr(out) if err != nil { panic("unable to convert output object to pointer") } if preconditions == nil { startTime := time.Now() response, err := h.etcdKeysAPI.Delete(ctx, key, nil) metrics.RecordEtcdRequestLatency("delete", getTypeName(out), startTime) if !etcdutil.IsEtcdNotFound(err) { // if the object that existed prior to the delete is returned by etcd, update the out object. if err != nil || response.PrevNode != nil { _, _, err = h.extractObj(response, err, out, false, true) } } return toStorageErr(err, key, 0) } // Check the preconditions match. obj := reflect.New(v.Type()).Interface().(runtime.Object) for { _, node, res, err := h.bodyAndExtractObj(ctx, key, obj, false) if err != nil { return toStorageErr(err, key, 0) } if err := checkPreconditions(key, preconditions, obj); err != nil { return toStorageErr(err, key, 0) } index := uint64(0) if node != nil { index = node.ModifiedIndex } else if res != nil { index = res.Index } opt := etcd.DeleteOptions{PrevIndex: index} startTime := time.Now() response, err := h.etcdKeysAPI.Delete(ctx, key, &opt) metrics.RecordEtcdRequestLatency("delete", getTypeName(out), startTime) if etcdutil.IsEtcdTestFailed(err) { glog.Infof("deletion of %s failed because of a conflict, going to retry", key) } else { if !etcdutil.IsEtcdNotFound(err) { // if the object that existed prior to the delete is returned by etcd, update the out object. if err != nil || response.PrevNode != nil { _, _, err = h.extractObj(response, err, out, false, true) } } return toStorageErr(err, key, 0) } } }
// Implements storage.Interface. func (c *Cacher) GetToList(ctx context.Context, key string, resourceVersion string, pred SelectionPredicate, listObj runtime.Object) error { if resourceVersion == "" { // If resourceVersion is not specified, serve it from underlying // storage (for backward compatibility). return c.storage.GetToList(ctx, key, resourceVersion, pred, listObj) } // If resourceVersion is specified, serve it from cache. // It's guaranteed that the returned value is at least that // fresh as the given resourceVersion. listRV, err := ParseListResourceVersion(resourceVersion) if err != nil { return err } trace := util.NewTrace(fmt.Sprintf("cacher %v: List", c.objectType.String())) defer trace.LogIfLong(500 * time.Millisecond) c.ready.wait() trace.Step("Ready") // List elements with at least 'listRV' from cache. listPtr, err := meta.GetItemsPtr(listObj) if err != nil { return err } listVal, err := conversion.EnforcePtr(listPtr) if err != nil || listVal.Kind() != reflect.Slice { return fmt.Errorf("need a pointer to slice, got %v", listVal.Kind()) } filter := filterFunction(key, pred) obj, exists, readResourceVersion, err := c.watchCache.WaitUntilFreshAndGet(listRV, key, trace) if err != nil { return err } trace.Step("Got from cache") if exists { elem, ok := obj.(*storeElement) if !ok { return fmt.Errorf("non *storeElement returned from storage: %v", obj) } if filter(elem.Key, elem.Object) { listVal.Set(reflect.Append(listVal, reflect.ValueOf(elem.Object).Elem())) } } if c.versioner != nil { if err := c.versioner.UpdateList(listObj, readResourceVersion); err != nil { return err } } return nil }
// Delete implements storage.Interface.Delete. func (s *store) Delete(ctx context.Context, key string, out runtime.Object, precondtions *storage.Preconditions) error { v, err := conversion.EnforcePtr(out) if err != nil { panic("unable to convert output object to pointer") } key = path.Join(s.pathPrefix, key) if precondtions == nil { return s.unconditionalDelete(ctx, key, out) } return s.conditionalDelete(ctx, key, out, v, precondtions) }
// decode decodes value of bytes into object. It will also set the object resource version to rev. // On success, objPtr would be set to the object. func decode(codec runtime.Codec, versioner storage.Versioner, value []byte, objPtr runtime.Object, rev int64) error { if _, err := conversion.EnforcePtr(objPtr); err != nil { panic("unable to convert output object to pointer") } _, _, err := codec.Decode(value, nil, objPtr) if err != nil { return err } // being unable to set the version does not prevent the object from being extracted versioner.UpdateObject(objPtr, uint64(rev)) return nil }
func (s *Scheme) IsUnversioned(obj Object) (bool, bool) { v, err := conversion.EnforcePtr(obj) if err != nil { return false, false } t := v.Type() if _, ok := s.typeToGVK[t]; !ok { return false, false } _, ok := s.unversionedTypes[t] return ok, true }
// SetList sets the given list object's Items member have the elements given in // objects. // Returns an error if list is not a List type (does not have an Items member), // or if any of the objects are not of the right type. func SetList(list runtime.Object, objects []runtime.Object) error { itemsPtr, err := GetItemsPtr(list) if err != nil { return err } items, err := conversion.EnforcePtr(itemsPtr) if err != nil { return err } if items.Type() == objectSliceType { items.Set(reflect.ValueOf(objects)) return nil } slice := reflect.MakeSlice(items.Type(), len(objects), len(objects)) for i := range objects { dest := slice.Index(i) // check to see if you're directly assignable if reflect.TypeOf(objects[i]).AssignableTo(dest.Type()) { dest.Set(reflect.ValueOf(objects[i])) continue } src, err := conversion.EnforcePtr(objects[i]) if err != nil { return err } if src.Type().AssignableTo(dest.Type()) { dest.Set(src) } else if src.Type().ConvertibleTo(dest.Type()) { dest.Set(src.Convert(dest.Type())) } else { return fmt.Errorf("item[%d]: can't assign or convert %v into %v", i, src.Type(), dest.Type()) } } items.Set(slice) return nil }
// ObjectKinds returns all possible group,version,kind of the go object, true if the // object is considered unversioned, or an error if it's not a pointer or is unregistered. func (s *Scheme) ObjectKinds(obj Object) ([]schema.GroupVersionKind, bool, error) { v, err := conversion.EnforcePtr(obj) if err != nil { return nil, false, err } t := v.Type() gvks, ok := s.typeToGVK[t] if !ok { return nil, false, NewNotRegisteredErr(schema.GroupVersionKind{}, t) } _, unversionedType := s.unversionedTypes[t] return gvks, unversionedType, nil }
// EachListItem invokes fn on each runtime.Object in the list. Any error immediately terminates // the loop. func EachListItem(obj runtime.Object, fn func(runtime.Object) error) error { // TODO: Change to an interface call? itemsPtr, err := GetItemsPtr(obj) if err != nil { return err } items, err := conversion.EnforcePtr(itemsPtr) if err != nil { return err } len := items.Len() if len == 0 { return nil } takeAddr := false if elemType := items.Type().Elem(); elemType.Kind() != reflect.Ptr && elemType.Kind() != reflect.Interface { if !items.Index(0).CanAddr() { return fmt.Errorf("unable to take address of items in %T for EachListItem", obj) } takeAddr = true } for i := 0; i < len; i++ { raw := items.Index(i) if takeAddr { raw = raw.Addr() } switch item := raw.Interface().(type) { case *runtime.RawExtension: if err := fn(item.Object); err != nil { return err } case runtime.Object: if err := fn(item); err != nil { return err } default: obj, ok := item.(runtime.Object) if !ok { return fmt.Errorf("%v: item[%v]: Expected object, got %#v(%s)", obj, i, raw.Interface(), raw.Kind()) } if err := fn(obj); err != nil { return err } } } return nil }
func listToItems(listObj runtime.Object) ([]runtime.Object, error) { v, err := conversion.EnforcePtr(listObj) if err != nil { return nil, fmt.Errorf("unexpected error: %v", err) } items := v.FieldByName("Items") if !items.IsValid() { return nil, fmt.Errorf("unexpected Items field in %v", listObj) } if items.Type().Kind() != reflect.Slice { return nil, fmt.Errorf("unexpected Items field type: %v", items.Type().Kind()) } result := make([]runtime.Object, items.Len()) for i := 0; i < items.Len(); i++ { result[i] = items.Index(i).Addr().Interface().(runtime.Object) } return result, nil }
// decodeList decodes a list of values into a list of objects, with resource version set to corresponding rev. // On success, ListPtr would be set to the list of objects. func decodeList(elems []*elemForDecode, filter storage.FilterFunc, ListPtr interface{}, codec runtime.Codec, versioner storage.Versioner) error { v, err := conversion.EnforcePtr(ListPtr) if err != nil || v.Kind() != reflect.Slice { panic("need ptr to slice") } for _, elem := range elems { obj, _, err := codec.Decode(elem.data, nil, reflect.New(v.Type().Elem()).Interface().(runtime.Object)) if err != nil { return err } // being unable to set the version does not prevent the object from being extracted versioner.UpdateObject(obj, elem.rev) if filter(obj) { v.Set(reflect.Append(v, reflect.ValueOf(obj).Elem())) } } return nil }
// Field puts the value of fieldName, which must be a member of v, into dest, // which must be a variable to which this field's value can be assigned. func Field(v reflect.Value, fieldName string, dest interface{}) error { field := v.FieldByName(fieldName) if !field.IsValid() { return fmt.Errorf("couldn't find %v field in %#v", fieldName, v.Interface()) } destValue, err := conversion.EnforcePtr(dest) if err != nil { return err } if field.Type().AssignableTo(destValue.Type()) { destValue.Set(field) return nil } if field.Type().ConvertibleTo(destValue.Type()) { destValue.Set(field.Convert(destValue.Type())) return nil } return fmt.Errorf("couldn't assign/convert %v to %v", field.Type(), destValue.Type()) }
// Implements storage.Interface. func (c *Cacher) Get(ctx context.Context, key string, resourceVersion string, objPtr runtime.Object, ignoreNotFound bool) error { if resourceVersion == "" { // If resourceVersion is not specified, serve it from underlying // storage (for backward compatibility). return c.storage.Get(ctx, key, resourceVersion, objPtr, ignoreNotFound) } // If resourceVersion is specified, serve it from cache. // It's guaranteed that the returned value is at least that // fresh as the given resourceVersion. getRV, err := ParseListResourceVersion(resourceVersion) if err != nil { return err } // Do not create a trace - it's not for free and there are tons // of Get requests. We can add it if it will be really needed. c.ready.wait() objVal, err := conversion.EnforcePtr(objPtr) if err != nil { return err } obj, exists, readResourceVersion, err := c.watchCache.WaitUntilFreshAndGet(getRV, key, nil) if err != nil { return err } if exists { elem, ok := obj.(*storeElement) if !ok { return fmt.Errorf("non *storeElement returned from storage: %v", obj) } objVal.Set(reflect.ValueOf(elem.Object).Elem()) } else { objVal.Set(reflect.Zero(objVal.Type())) if !ignoreNotFound { return NewKeyNotFoundError(key, int64(readResourceVersion)) } } return nil }
// addObjectParams converts a runtime.Object into a set of go-restful Param() definitions on the route. // The object must be a pointer to a struct; only fields at the top level of the struct that are not // themselves interfaces or structs are used; only fields with a json tag that is non empty (the standard // Go JSON behavior for omitting a field) become query parameters. The name of the query parameter is // the JSON field name. If a description struct tag is set on the field, that description is used on the // query parameter. In essence, it converts a standard JSON top level object into a query param schema. func addObjectParams(ws *restful.WebService, route *restful.RouteBuilder, obj interface{}) error { sv, err := conversion.EnforcePtr(obj) if err != nil { return err } st := sv.Type() switch st.Kind() { case reflect.Struct: for i := 0; i < st.NumField(); i++ { name := st.Field(i).Name sf, ok := st.FieldByName(name) if !ok { continue } switch sf.Type.Kind() { case reflect.Interface, reflect.Struct: case reflect.Ptr: // TODO: This is a hack to let metav1.Time through. This needs to be fixed in a more generic way eventually. bug #36191 if (sf.Type.Elem().Kind() == reflect.Interface || sf.Type.Elem().Kind() == reflect.Struct) && strings.TrimPrefix(sf.Type.String(), "*") != "metav1.Time" { continue } fallthrough default: jsonTag := sf.Tag.Get("json") if len(jsonTag) == 0 { continue } jsonName := strings.SplitN(jsonTag, ",", 2)[0] if len(jsonName) == 0 { continue } var desc string if docable, ok := obj.(documentable); ok { desc = docable.SwaggerDoc()[jsonName] } route.Param(ws.QueryParameter(jsonName, desc).DataType(typeToJSON(sf.Type.String()))) } } } return nil }
// decodeNodeList walks the tree of each node in the list and decodes into the specified object func (h *etcdHelper) decodeNodeList(nodes []*etcd.Node, filter storage.FilterFunc, slicePtr interface{}) error { trace := util.NewTrace("decodeNodeList " + getTypeName(slicePtr)) defer trace.LogIfLong(400 * time.Millisecond) v, err := conversion.EnforcePtr(slicePtr) if err != nil || v.Kind() != reflect.Slice { // This should not happen at runtime. panic("need ptr to slice") } for _, node := range nodes { if node.Dir { trace.Step("Decoding dir " + node.Key + " START") if err := h.decodeNodeList(node.Nodes, filter, slicePtr); err != nil { return err } trace.Step("Decoding dir " + node.Key + " END") continue } if obj, found := h.getFromCache(node.ModifiedIndex, filter); found { // obj != nil iff it matches the filter function. if obj != nil { v.Set(reflect.Append(v, reflect.ValueOf(obj).Elem())) } } else { obj, _, err := h.codec.Decode([]byte(node.Value), nil, reflect.New(v.Type().Elem()).Interface().(runtime.Object)) if err != nil { return err } // being unable to set the version does not prevent the object from being extracted _ = h.versioner.UpdateObject(obj, node.ModifiedIndex) if filter(obj) { v.Set(reflect.Append(v, reflect.ValueOf(obj).Elem())) } if node.ModifiedIndex != 0 { h.addToCache(node.ModifiedIndex, obj) } } } trace.Step(fmt.Sprintf("Decoded %v nodes", len(nodes))) return nil }
// TypeAccessor returns an interface that allows retrieving and modifying the APIVersion // and Kind of an in-memory internal object. // TODO: this interface is used to test code that does not have ObjectMeta or ListMeta // in round tripping (objects which can use apiVersion/kind, but do not fit the Kube // api conventions). func TypeAccessor(obj interface{}) (Type, error) { if typed, ok := obj.(runtime.Object); ok { return objectAccessor{typed}, nil } v, err := conversion.EnforcePtr(obj) if err != nil { return nil, err } t := v.Type() if v.Kind() != reflect.Struct { return nil, fmt.Errorf("expected struct, but got %v: %v (%#v)", v.Kind(), t, v.Interface()) } typeMeta := v.FieldByName("TypeMeta") if !typeMeta.IsValid() { return nil, fmt.Errorf("struct %v lacks embedded TypeMeta type", t) } a := &genericAccessor{} if err := extractFromTypeMeta(typeMeta, a); err != nil { return nil, fmt.Errorf("unable to find type fields on %#v: %v", typeMeta, err) } return a, nil }
// GetItemsPtr returns a pointer to the list object's Items member. // If 'list' doesn't have an Items member, it's not really a list type // and an error will be returned. // This function will either return a pointer to a slice, or an error, but not both. func GetItemsPtr(list runtime.Object) (interface{}, error) { v, err := conversion.EnforcePtr(list) if err != nil { return nil, err } items := v.FieldByName("Items") if !items.IsValid() { return nil, fmt.Errorf("no Items field in %#v", list) } switch items.Kind() { case reflect.Interface, reflect.Ptr: target := reflect.TypeOf(items.Interface()).Elem() if target.Kind() != reflect.Slice { return nil, fmt.Errorf("items: Expected slice, got %s", target.Kind()) } return items.Interface(), nil case reflect.Slice: return items.Addr().Interface(), nil default: return nil, fmt.Errorf("items: Expected slice, got %s", items.Kind()) } }
// Implements storage.Interface. func (h *etcdHelper) Create(ctx context.Context, key string, obj, out runtime.Object, ttl uint64) error { trace := util.NewTrace("etcdHelper::Create " + getTypeName(obj)) defer trace.LogIfLong(250 * time.Millisecond) if ctx == nil { glog.Errorf("Context is nil") } key = path.Join(h.pathPrefix, key) data, err := runtime.Encode(h.codec, obj) trace.Step("Object encoded") if err != nil { return err } if version, err := h.versioner.ObjectResourceVersion(obj); err == nil && version != 0 { return errors.New("resourceVersion may not be set on objects to be created") } trace.Step("Version checked") startTime := time.Now() opts := etcd.SetOptions{ TTL: time.Duration(ttl) * time.Second, PrevExist: etcd.PrevNoExist, } response, err := h.etcdKeysAPI.Set(ctx, key, string(data), &opts) trace.Step("Object created") metrics.RecordEtcdRequestLatency("create", getTypeName(obj), startTime) if err != nil { return toStorageErr(err, key, 0) } if out != nil { if _, err := conversion.EnforcePtr(out); err != nil { panic("unable to convert output object to pointer") } _, _, err = h.extractObj(response, err, out, false, false) } return err }
// GuaranteedUpdate implements storage.Interface.GuaranteedUpdate. func (s *store) GuaranteedUpdate( ctx context.Context, key string, out runtime.Object, ignoreNotFound bool, precondtions *storage.Preconditions, tryUpdate storage.UpdateFunc, suggestion ...runtime.Object) error { trace := util.NewTrace(fmt.Sprintf("GuaranteedUpdate etcd3: %s", reflect.TypeOf(out).String())) defer trace.LogIfLong(500 * time.Millisecond) v, err := conversion.EnforcePtr(out) if err != nil { panic("unable to convert output object to pointer") } key = path.Join(s.pathPrefix, key) var origState *objState if len(suggestion) == 1 && suggestion[0] != nil { origState, err = s.getStateFromObject(suggestion[0]) if err != nil { return err } } else { getResp, err := s.client.KV.Get(ctx, key, s.getOps...) if err != nil { return err } origState, err = s.getState(getResp, key, v, ignoreNotFound) if err != nil { return err } } trace.Step("initial value restored") for { if err := checkPreconditions(key, precondtions, origState.obj); err != nil { return err } ret, ttl, err := s.updateState(origState, tryUpdate) if err != nil { return err } data, err := runtime.Encode(s.codec, ret) if err != nil { return err } if bytes.Equal(data, origState.data) { return decode(s.codec, s.versioner, origState.data, out, origState.rev) } opts, err := s.ttlOpts(ctx, int64(ttl)) if err != nil { return err } trace.Step("Transaction prepared") txnResp, err := s.client.KV.Txn(ctx).If( clientv3.Compare(clientv3.ModRevision(key), "=", origState.rev), ).Then( clientv3.OpPut(key, string(data), opts...), ).Else( clientv3.OpGet(key), ).Commit() if err != nil { return err } trace.Step("Transaction committed") if !txnResp.Succeeded { getResp := (*clientv3.GetResponse)(txnResp.Responses[0].GetResponseRange()) glog.V(4).Infof("GuaranteedUpdate of %s failed because of a conflict, going to retry", key) origState, err = s.getState(getResp, key, v, ignoreNotFound) if err != nil { return err } trace.Step("Retry value restored") continue } putResp := txnResp.Responses[0].GetResponsePut() return decode(s.codec, s.versioner, data, out, putResp.Header.Revision) } }
// Implements storage.Interface. func (h *etcdHelper) GuaranteedUpdate( ctx context.Context, key string, ptrToType runtime.Object, ignoreNotFound bool, preconditions *storage.Preconditions, tryUpdate storage.UpdateFunc, _ ...runtime.Object) error { // Ignore the suggestion about current object. if ctx == nil { glog.Errorf("Context is nil") } v, err := conversion.EnforcePtr(ptrToType) if err != nil { // Panic is appropriate, because this is a programming error. panic("need ptr to type") } key = path.Join(h.pathPrefix, key) for { obj := reflect.New(v.Type()).Interface().(runtime.Object) origBody, node, res, err := h.bodyAndExtractObj(ctx, key, obj, ignoreNotFound) if err != nil { return toStorageErr(err, key, 0) } if err := checkPreconditions(key, preconditions, obj); err != nil { return toStorageErr(err, key, 0) } meta := storage.ResponseMeta{} if node != nil { meta.TTL = node.TTL meta.ResourceVersion = node.ModifiedIndex } // Get the object to be written by calling tryUpdate. ret, newTTL, err := tryUpdate(obj, meta) if err != nil { return toStorageErr(err, key, 0) } index := uint64(0) ttl := uint64(0) if node != nil { index = node.ModifiedIndex if node.TTL != 0 { ttl = uint64(node.TTL) } if node.Expiration != nil && ttl == 0 { ttl = 1 } } else if res != nil { index = res.Index } if newTTL != nil { if ttl != 0 && *newTTL == 0 { // TODO: remove this after we have verified this is no longer an issue glog.V(4).Infof("GuaranteedUpdate is clearing TTL for %q, may not be intentional", key) } ttl = *newTTL } // Since update object may have a resourceVersion set, we need to clear it here. if err := h.versioner.UpdateObject(ret, 0); err != nil { return errors.New("resourceVersion cannot be set on objects store in etcd") } data, err := runtime.Encode(h.codec, ret) if err != nil { return err } // First time this key has been used, try creating new value. if index == 0 { startTime := time.Now() opts := etcd.SetOptions{ TTL: time.Duration(ttl) * time.Second, PrevExist: etcd.PrevNoExist, } response, err := h.etcdKeysAPI.Set(ctx, key, string(data), &opts) metrics.RecordEtcdRequestLatency("create", getTypeName(ptrToType), startTime) if etcdutil.IsEtcdNodeExist(err) { continue } _, _, err = h.extractObj(response, err, ptrToType, false, false) return toStorageErr(err, key, 0) } if string(data) == origBody { // If we don't send an update, we simply return the currently existing // version of the object. _, _, err := h.extractObj(res, nil, ptrToType, ignoreNotFound, false) return err } startTime := time.Now() // Swap origBody with data, if origBody is the latest etcd data. opts := etcd.SetOptions{ PrevIndex: index, TTL: time.Duration(ttl) * time.Second, } response, err := h.etcdKeysAPI.Set(ctx, key, string(data), &opts) metrics.RecordEtcdRequestLatency("compareAndSwap", getTypeName(ptrToType), startTime) if etcdutil.IsEtcdTestFailed(err) { // Try again. continue } _, _, err = h.extractObj(response, err, ptrToType, false, false) return toStorageErr(err, key, int64(index)) } }