Skip to content

Commit 1008648

Browse files
authored
Merge pull request #132 from hasbro17/haseeb/add-action-update
action: add update
2 parents 8a2ae59 + 6ba8348 commit 1008648

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

pkg/sdk/action/api.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,35 @@ func Create(object sdkTypes.Object) (err error) {
5353
}
5454
return nil
5555
}
56+
57+
// Update updates the provided object on the server and updates the arg
58+
// "object" with the result from the server(UID, resourceVersion, etc).
59+
// Returns an error if the object’s TypeMeta(Kind, APIVersion) or ObjectMeta(Name, Namespace) is missing or incorrect.
60+
// Can also return an api error from the server
61+
// e.g Conflict https://github.com/kubernetes/apimachinery/blob/master/pkg/api/errors/errors.go#L428
62+
func Update(object sdkTypes.Object) (err error) {
63+
_, namespace, err := k8sutil.GetNameAndNamespace(object)
64+
if err != nil {
65+
return err
66+
}
67+
gvk := object.GetObjectKind().GroupVersionKind()
68+
apiVersion, kind := gvk.ToAPIVersionAndKind()
69+
70+
resourceClient, _, err := k8sclient.GetResourceClient(apiVersion, kind, namespace)
71+
if err != nil {
72+
return fmt.Errorf("failed to get resource client: %v", err)
73+
}
74+
75+
unstructObj := k8sutil.UnstructuredFromRuntimeObject(object)
76+
unstructObj, err = resourceClient.Update(unstructObj)
77+
if err != nil {
78+
return err
79+
}
80+
81+
// Update the arg object with the result
82+
err = k8sutil.UnstructuredIntoRuntimeObject(unstructObj, object)
83+
if err != nil {
84+
return fmt.Errorf("failed to unmarshal the retrieved data: %v", err)
85+
}
86+
return nil
87+
}

0 commit comments

Comments
 (0)