generated from ansible-collections/collection_template
-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
k8s - add support for Server Side apply (#260)
k8s - add support for Server Side apply SUMMARY Server side apply is now support for k8s module with this Pull request. The feature is not yet released on kubernetes-client, once this is done, we can merge this pull request. closes #87 ISSUE TYPE Feature Pull Request COMPONENT NAME k8s ADDITIONAL INFORMATION Reviewed-by: Mike Graves <[email protected]> Reviewed-by: None <None> Reviewed-by: None <None>
- Loading branch information
Showing
5 changed files
with
213 additions
and
2 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
changelogs/fragments/260-k8s-add-support-for-server_side_apply.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
minor_changes: | ||
- k8s - add support for server_side_apply. (https://github.com/ansible-collections/kubernetes.core/issues/87). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,19 +114,32 @@ def apply_patch(actual, desired): | |
return actual, dict_merge(desired, annotate(desired)) | ||
|
||
|
||
def apply_object(resource, definition): | ||
def apply_object(resource, definition, server_side=False): | ||
try: | ||
actual = resource.get( | ||
name=definition["metadata"]["name"], | ||
namespace=definition["metadata"].get("namespace"), | ||
) | ||
if server_side: | ||
return actual, None | ||
except NotFoundError: | ||
return None, dict_merge(definition, annotate(definition)) | ||
return apply_patch(actual.to_dict(), definition) | ||
|
||
|
||
def k8s_apply(resource, definition, **kwargs): | ||
existing, desired = apply_object(resource, definition) | ||
server_side = kwargs.get("server_side", False) | ||
if server_side: | ||
body = json.dumps(definition).encode() | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
gravesm
Member
|
||
# server_side_apply is forces content_type to 'application/apply-patch+yaml' | ||
return resource.server_side_apply( | ||
body=body, | ||
name=definition["metadata"]["name"], | ||
namespace=definition["metadata"].get("namespace"), | ||
force_conflicts=kwargs.get("force_conflicts"), | ||
field_manager=kwargs.get("field_manager"), | ||
This comment has been minimized.
Sorry, something went wrong.
flybyray
|
||
) | ||
if not existing: | ||
return resource.create( | ||
body=desired, namespace=definition["metadata"].get("namespace"), **kwargs | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
why
encode()
?body
's type isbytes
.