update replaces the currently deployed version with a new one. If the objects already match then nothing is done.
(obj KubeObject, create bool, mapping *meta.RESTMapping)
| 141 | |
| 142 | // update replaces the currently deployed version with a new one. If the objects already match then nothing is done. |
| 143 | func (c *KubeCluster) update(obj KubeObject, create bool, mapping *meta.RESTMapping) (KubeObject, error) { |
| 144 | meta := obj.GetObjectMeta() |
| 145 | |
| 146 | deployed, err := c.get(meta.GetNamespace(), meta.GetName(), true, mapping) |
| 147 | if doesNotExist(err) && create { |
| 148 | return c.create(obj, mapping) |
| 149 | } else if err != nil { |
| 150 | return nil, err |
| 151 | } |
| 152 | |
| 153 | // TODO: need a better way to handle resource versioning |
| 154 | // set resource version on local to same as remote |
| 155 | deployedVersion := deployed.GetObjectMeta().GetResourceVersion() |
| 156 | meta.SetResourceVersion(deployedVersion) |
| 157 | |
| 158 | copyImmutables(deployed, obj) |
| 159 | |
| 160 | // if local matches deployed, do nothing |
| 161 | if kube.Semantic.DeepEqual(obj, deployed) { |
| 162 | return deployed, nil |
| 163 | } |
| 164 | |
| 165 | patch, err := diff(deployed, obj) |
| 166 | if err != nil { |
| 167 | return nil, fmt.Errorf("could not create diff: %v", err) |
| 168 | } |
| 169 | |
| 170 | req := c.Client.RESTClient.Patch(kube.StrategicMergePatchType). |
| 171 | Name(meta.GetName()). |
| 172 | Body(patch) |
| 173 | |
| 174 | setRequestObjectInfo(req, meta.GetNamespace(), mapping) |
| 175 | |
| 176 | runtimeObj, err := req.Do().Get() |
| 177 | if err != nil { |
| 178 | return nil, resourceError("update", meta.GetNamespace(), meta.GetName(), mapping, err) |
| 179 | } |
| 180 | |
| 181 | return AsKubeObject(runtimeObj) |
| 182 | } |
| 183 | |
| 184 | // Get retrieves an objects from a cluster using it's namespace name and API version. |
| 185 | func (c *KubeCluster) Get(kind, namespace, name string, export bool) (KubeObject, error) { |
no test coverage detected