(op *createOperation)
| 81 | } |
| 82 | |
| 83 | func (o *ObjectPatcher) executeCreateOperation(op *createOperation) error { |
| 84 | if op.object == nil { |
| 85 | return fmt.Errorf("cannot create empty object") |
| 86 | } |
| 87 | |
| 88 | // Convert object from any. |
| 89 | object, err := toUnstructured(op.object) |
| 90 | if err != nil { |
| 91 | return err |
| 92 | } |
| 93 | |
| 94 | apiVersion := object.GetAPIVersion() |
| 95 | kind := object.GetKind() |
| 96 | |
| 97 | wrapErr := func(e error) error { |
| 98 | objectID := fmt.Sprintf("%s/%s/%s/%s", apiVersion, kind, object.GetNamespace(), object.GetName()) |
| 99 | return gerror.WithMessage(e, objectID) |
| 100 | } |
| 101 | |
| 102 | gvk, err := o.kubeClient.GroupVersionResource(apiVersion, kind) |
| 103 | if err != nil { |
| 104 | return wrapErr(err) |
| 105 | } |
| 106 | |
| 107 | log.Debug("Started Create API call") |
| 108 | _, err = o.kubeClient.Dynamic(). |
| 109 | Resource(gvk). |
| 110 | Namespace(object.GetNamespace()). |
| 111 | Create(context.TODO(), object, pkg.DefaultCreateOptions(), generateSubresources(op.subresource)...) |
| 112 | log.Debug("Finished Create API call") |
| 113 | |
| 114 | objectExists := errors.IsAlreadyExists(err) |
| 115 | |
| 116 | if objectExists && op.ignoreIfExists { |
| 117 | log.Debug("resource already exists, exiting without error") |
| 118 | return nil |
| 119 | } |
| 120 | |
| 121 | if objectExists && op.updateIfExists { |
| 122 | log.Debug("Object already exists, attempting to Update it with optimistic lock") |
| 123 | |
| 124 | return retry.RetryOnConflict(retry.DefaultBackoff, func() error { |
| 125 | log.Debug("Started Get API call") |
| 126 | existingObj, err := o.kubeClient.Dynamic(). |
| 127 | Resource(gvk). |
| 128 | Namespace(object.GetNamespace()). |
| 129 | Get(context.TODO(), object.GetName(), metav1.GetOptions{}, generateSubresources(op.subresource)...) |
| 130 | log.Debug("Finished Get API call") |
| 131 | if err != nil { |
| 132 | return wrapErr(err) |
| 133 | } |
| 134 | |
| 135 | objCopy := object.DeepCopy() |
| 136 | objCopy.SetResourceVersion(existingObj.GetResourceVersion()) |
| 137 | |
| 138 | log.Debug("Started Update API call") |
| 139 | _, err = o.kubeClient.Dynamic(). |
| 140 | Resource(gvk). |
no test coverage detected