Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use replace_resource only if resource already exists #111

Merged
merged 6 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions oper8/deploy_manager/openshift_deploy_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -920,8 +920,10 @@ def _apply(self, resource_definition, method: DeployMethod):
# If the resource requires a replace operation then use put. Otherwise use
# server side apply
if (
req_replace or method is DeployMethod.REPLACE
) and method != DeployMethod.UPDATE:
(req_replace or method is DeployMethod.REPLACE)
and method != DeployMethod.UPDATE
and current != {}
):
apply_res = self._replace_resource(
resource_definition,
)
Expand Down
7 changes: 4 additions & 3 deletions oper8/test_helpers/kub_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -791,9 +791,9 @@ def current_state_put(self, api_endpoint, api_version, kind, body, is_status=Fal
content.update({"status": body.get("status", {})})
updated_content = content
else:
if "status" in content:
del content["status"]
updated_content = content
if "status" in body:
del body["status"]
updated_content = body
log.debug3(
"Updating [%s/%s/%s/%s] with body: %s",
namespace,
Expand Down Expand Up @@ -832,6 +832,7 @@ def current_state_patch(self, api_endpoint, api_version, kind, body):
name=name,
)
log.debug3("Current Content: %s", content)
log.debug3("Update body: %s", body)

# If the content has a status code, unpack it
status_code = 200
Expand Down
21 changes: 18 additions & 3 deletions tests/deploy_manager/test_openshift_deploy_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,17 @@ def test_deploy_method_resource():
"""
start_cluster_state = {"test": {"Foo": {"foo.bar.com/v1": {}}}}
end_cluster_state = {"test": {"Foo": {"foo.bar.com/v1": {"bar": {}}}}}
replace_apply_resource = {
"kind": "Foo",
"apiVersion": "foo.bar.com/v1",
"metadata": {"name": "bar", "namespace": "test"},
"spec": {"some": "key_1"},
}
end_apply_resource = {
"kind": "Foo",
"apiVersion": "foo.bar.com/v1",
"metadata": {"name": "bar", "namespace": "test"},
"spec": {"some": "key"},
"spec": {"some": "key_2"},
}
dm = setup_testable_manager(cluster_state=start_cluster_state)
dm._requires_replace = lambda *args, **kwargs: True
Expand All @@ -222,21 +228,30 @@ def track_apply(resource_definition):

dm._apply_resource = track_apply

# Use apply instead of replace when first deploying
success, changed = dm.deploy(
resource_definitions=make_obj_states(end_cluster_state),
method=DeployMethod.REPLACE,
)
assert success
assert changed
assert len(replace_called) == 1 and len(apply_called) == 0
assert len(replace_called) == 0 and len(apply_called) == 1

success, changed = dm.deploy(
resource_definitions=[end_apply_resource], method=DeployMethod.UPDATE
resource_definitions=[replace_apply_resource],
method=DeployMethod.REPLACE,
)
assert success
assert changed
assert len(replace_called) == 1 and len(apply_called) == 1

success, changed = dm.deploy(
resource_definitions=[end_apply_resource], method=DeployMethod.UPDATE
)
assert success
assert changed
assert len(replace_called) == 1 and len(apply_called) == 2


def test_deploy_method_update_resource():
"""Make sure that deploying a new instance of an existing resource type to
Expand Down
Loading