Skip to content

refactor: share the env status read across providers - #273

Open
ianaya89 wants to merge 1 commit into
masterfrom
refactor/share-env-status-read
Open

ianaya89 wants to merge 1 commit into
masterfrom
refactor/share-env-status-read

Conversation

@ianaya89

Copy link
Copy Markdown
Member

Six status data sources — aws, gcp, azure, hcloud, k8s and aws_hosted — each carried the same ~105 line Read: fetch, nil check, short-circuit on the applied spec revision, build a poll closure, wait, re-fetch, nil check, set state. Only the client method, the response field and the model type differed.

The copies had already drifted:

  • "env status matchs spec" in azure, gcp, hcloud and k8s; aws and aws_hosted spelled it correctly
  • the // Re-fetch to populate the model comment existed in two of the six

Both are the kind of thing that only shows up as a diff between siblings nobody diffs.

Shape

common.ReadEnvStatus owns the flow. Each provider supplies one refresh closure that fetches the status, writes it into its model and returns what the wait needs (AppliedSpecRevision, Errors, Found). WaitForSpecRevision becomes unexported and takes that same closure, which drops the PollFunc type and stops threading the env name through it.

Per-provider Read goes from ~105 lines to ~45, and what is left is the binding — client method, response field, model — with no control flow.

Also collapses the DISCONNECTED branch in the wait, whose two arms both did blockingErrors = append(...) then continue:

if (e.Code == "DISCONNECTED" || e.Code == "K8S_DISCONNECTED") && result.AppliedSpecRevision == 0 {
    ...
    continue
}
blockingErrors = append(blockingErrors, e)

Behaviour

Unchanged, with one exception worth flagging: the read timeout is now parsed before the short-circuit rather than only on the waiting path, so a malformed timeouts block is reported in both cases instead of only when a wait happens.

Tests

env_status/common had no tests; the flow now has 11, covering no target, target already met, refresh error, not found, refresh-after-wait, timeout, DISCONNECTED/K8S_DISCONNECTED before first provision, disconnect after provisioning, a plain provisioning error, and a blocking error arriving alongside a non-blocking disconnect.

The six status data sources each carried the same ~105 line Read: fetch, nil
check, short-circuit on the applied spec revision, build a poll closure, wait,
re-fetch, nil check, set state. Only the client method, response field and model
type differed, and the copies had already drifted apart.

Move the flow into common.ReadEnvStatus, driven by one refresh closure per
provider that fetches the status, writes it into the model and reports what the
wait needs. WaitForSpecRevision becomes unexported and takes that closure, so
the env name is no longer threaded through it.

Also collapses the DISCONNECTED branch, whose two arms both appended to
blockingErrors and continued.

No behaviour change, except that the read timeout is now parsed before the
short-circuit rather than only on the waiting path, so a malformed timeouts
block is reported either way.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant