Skip to content

Commit

Permalink
Removed unnecessary update to terraform plan while destroying
Browse files Browse the repository at this point in the history
grafana-agent-k8s.
  • Loading branch information
chanchiwai-ray committed Jan 29, 2024
1 parent d3359e6 commit 9202b14
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 46 deletions.
39 changes: 5 additions & 34 deletions sunbeam-python/sunbeam/plugins/observability/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -499,60 +499,33 @@ class RemoveGrafanaAgentK8sStep(BaseStep, JujuStepHelper):

def __init__(
self,
plugin: "ObservabilityPlugin",
tfhelper: TerraformHelper,
tfhelper_cos: TerraformHelper,
jhelper: JujuHelper,
tfhelper: TerraformHelper,
):
super().__init__("Remove Grafana Agent k8s", "Removing Grafana Agent k8s")
self.tfhelper = tfhelper
self.tfhelper_cos = tfhelper_cos
self.jhelper = jhelper
self.tfhelper = tfhelper
self.model = OPENSTACK_MODEL
self.read_config = lambda: plugin.get_plugin_info().get(
"grafana-agent-k8s-config", {}
)
self.update_config = lambda c: plugin.update_plugin_info(
{"grafana-agent-k8s-config": c}
)

def run(self, status: Optional[Status] = None) -> Result:
"""Execute configuration using terraform."""
app = "grafana-agent-k8s"
cos_backend = self.tfhelper_cos.backend
cos_backend_config = self.tfhelper_cos.backend_config()
try:
config = self.read_config()
except ConfigItemNotFoundException as e:
LOG.exception("Failed removing %s: unable to read config", app)
return Result(ResultType.FAILED, str(e))

tfvars = {
"cos-state-backend": cos_backend,
"cos-state-config": cos_backend_config,
"grafana-agent-k8s-channel": GRAFANA_AGENT_K8S_CHANNEL,
"model": self.model,
}
config.update(tfvars)
self.update_config(config)
self.tfhelper.write_tfvars(tfvars)
try:
self.tfhelper.destroy()
except TerraformException as e:
LOG.exception("Error destroying %s", app)
return Result(ResultType.FAILED, str(e))

apps = [app]
try:
run_sync(
self.jhelper.wait_application_gone(
apps,
[app],
self.model,
timeout=OBSERVABILITY_DEPLOY_TIMEOUT,
)
)
except TimeoutException as e:
LOG.debug("Failed to destroy %s", apps, exc_info=True)
LOG.debug("Failed to destroy %s", app, exc_info=True)
return Result(ResultType.FAILED, str(e))

return Result(ResultType.COMPLETED)
Expand Down Expand Up @@ -695,9 +668,7 @@ def run_disable_plans(self):

grafana_agent_k8s_plan = [
TerraformInitStep(tfhelper_grafana_agent_k8s),
RemoveGrafanaAgentK8sStep(
self, tfhelper_grafana_agent_k8s, tfhelper_cos, jhelper
),
RemoveGrafanaAgentK8sStep(jhelper, tfhelper_grafana_agent_k8s),
]

run_plan(grafana_agent_k8s_plan, console)
Expand Down
15 changes: 3 additions & 12 deletions sunbeam-python/tests/unit/sunbeam/plugins/test_observability.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,9 @@ def test_run_waiting_timed_out(

class TestRemoveGrafanaAgentK8sStep:
def test_run(self, cclient, jhelper, tfhelper, observabilityplugin):
step = observability_plugin.RemoveGrafanaAgentK8sStep(
observabilityplugin, tfhelper, tfhelper, jhelper
)
step = observability_plugin.RemoveGrafanaAgentK8sStep(jhelper, tfhelper)
result = step.run()

tfhelper.write_tfvars.assert_called_once()
tfhelper.destroy.assert_called_once()
jhelper.wait_application_gone.assert_called_once()
assert result.result_type == ResultType.COMPLETED
Expand All @@ -297,12 +294,9 @@ def test_run_tf_destroy_failed(
):
tfhelper.destroy.side_effect = TerraformException("destroy failed...")

step = observability_plugin.RemoveGrafanaAgentK8sStep(
observabilityplugin, tfhelper, tfhelper, jhelper
)
step = observability_plugin.RemoveGrafanaAgentK8sStep(jhelper, tfhelper)
result = step.run()

tfhelper.write_tfvars.assert_called_once()
tfhelper.destroy.assert_called_once()
jhelper.wait_application_gone.assert_not_called()
assert result.result_type == ResultType.FAILED
Expand All @@ -313,12 +307,9 @@ def test_run_waiting_timed_out(
):
jhelper.wait_application_gone.side_effect = TimeoutException("timed out")

step = observability_plugin.RemoveGrafanaAgentK8sStep(
observabilityplugin, tfhelper, tfhelper, jhelper
)
step = observability_plugin.RemoveGrafanaAgentK8sStep(jhelper, tfhelper)
result = step.run()

tfhelper.write_tfvars.assert_called_once()
tfhelper.destroy.assert_called_once()
jhelper.wait_application_gone.assert_called_once()
assert result.result_type == ResultType.FAILED
Expand Down

0 comments on commit 9202b14

Please sign in to comment.