The restore path took any Job standing under a RestoreJob's Job name as
its own, so a RestoreJob re-created under a used name met the completed
Job of the earlier one before the garbage collector removed it and was
marked Succeeded although nothing was restored for it.
The restore path now refuses such a Job the way the backup path does:
the Job of an earlier RestoreJob of the same name is waited out, with
the Ready condition naming it, and any other Job holding the name
fails the RestoreJob.
Assisted-by: LLM
Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
A Job standing under a BackupJob's Job name was taken as its own. A
BackupJob re-created under a used name, as a Plan does when the
BackupJob of a schedule slot is deleted within the slot's starting
deadline, met the completed Job of the earlier one before the garbage
collector removed it, was marked Succeeded without uploading anything,
and recorded a Backup for an archive nobody uploaded for it.
A Job controlled by an earlier BackupJob of the same name is now
waited out, with the Ready condition naming it; any other Job holding
the name fails the BackupJob, since nothing will remove it.
Assisted-by: LLM
Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
Deleting a Backup taken with the Altinity strategy released its
finalizer at once and left the archive in the bucket. On the system
bucket nothing else prunes it, so every ClickHouse backup stayed until
someone called the sidecar by hand.
Deleting such a Backup now runs a one-shot Job that asks the
application's sidecar to delete the archive the Backup records, and
holds the finalizer until the Job completes. The driver creates the
Backup with the finalizer already set, so a Backup deleted before the
reconciler first sees it still gets its archive deleted. An archive
that is already gone counts as deleted. The sidecar refuses a delete
while another operation runs, and cannot be reached while the
ClickHouse Pod restarts, so the Job waits both out within its
deadline; any other refusal fails the Job, which is reported as an
ArtifactCleanupFailed Warning Event carrying the reason it failed.
Each failure doubles the wait before the next cleanup Job, from 30
seconds up to 30 minutes, so a delete the sidecar refuses for good,
such as one made with a wrong password, does not cost a Job, its Pod
and a Warning Event every few seconds. The count of failures and the
time the next Job may start are kept on the Backup, because the Backup
controller does not watch Jobs; a start time further ahead than the
longest wait, from a skewed clock or a hand edit, is logged and
ignored. A Job the namespace's Job quota refuses is retried once a
slot frees up rather than released, since the Backup would be gone by
then and nothing would delete the archive.
The Job is built by the controller, not rendered from the strategy
template, because the shipped and the example templates run a restore
for every mode other than backup. Only the image, how to pull it, and
the node selector, tolerations, affinity and priority class come from
the template, rendered against the application and the Backup as for
the strategy's own Pods, so the cleanup Pod can be scheduled where
they are. The strategy is read only when a Job has to be created, so a
running delete outlives it. The Job also waits for the projected
credentials to reach the sidecar, as the backup and restore Jobs do: a
sidecar still serving the previous bucket answers "not found", and the
delete would count as done.
When the delete cannot run, the Backup is released with an
ArtifactNotDeleted Warning Event naming the archive left behind: the
namespace is terminating, the strategy, the application or its API
Secret is gone, the application has left the system bucket, the
platform's system bucket is no longer the one the Backup records (also
when it changes while the delete runs), another Job holds the cleanup
Job name, or the API server refuses the Job. An archive named without
its BackupJob's UID is released the same way: such a name comes from a
run that crossed the controller upgrade, and another run started in
the same second may share it. The
backups.cozystack.io/skip-artifact-cleanup annotation releases the
Backup without starting a delete. An application on its own bucket
keeps the archive's lifecycle, as with MongoDB.
The restore scenario and the chart comments told tenants to delete an
archive with DELETE /backup/<name>/remote, which clickhouse-backup 2.7
answers with 404; they now name POST /backup/delete/remote/<name>.
Co-authored-by: Yan Bondarenko <202671653+yankawai@users.noreply.github.com>
Assisted-by: LLM
Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
Signed-off-by: Yan Bondarenko <202671653+yankawai@users.noreply.github.com>
A ClickHouse application writes its archives either to the system
bucket, which nothing prunes, or to its own bucket, whose lifecycle is
the tenant's. A Backup did not say which applied, so nothing deciding
the fate of the archive when the Backup goes could tell them apart.
The application's useSystemBucket is read when the backup Job is
created, stamped on the Job and copied onto the Backup from there,
because the application may switch before the controller sees the Job
complete. It is the setting, not an observation of the bucket: the
sidecar uploads to the bucket it was last restarted with, so a switch
made just before the backup can be recorded while the sidecar still
writes to the previous bucket. A Job created before the stamp existed
falls back to the application as it reads then.
Assisted-by: LLM
Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
Remove helpers and constants nothing references, escape format
characters in test literals, and take the simplifications staticcheck
suggests where they read the same. Error strings no longer start with a
capital: they keep their meaning, with the kind or component name moved
out of first position. Callers match errors by type, not text; the one
test that matches text, for Kafka, keeps the phrase it looks for.
Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>
main pins the archive name a backup Pod keeps across a wait for
late bucket credentials, against the controller's archive helper.
That helper now names a run's archive with part of the BackupJob's
UID, so the merged test has to give its BackupJob one.
Assisted-by: LLM
Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
The archive name is part of every object key, a key is capped at 1024
bytes, and the legacy s3PathOverride prefix has no length limit. The
full UID made the name 37 characters longer than before, so a prefix
that fit the old name pushed the manifest key past the cap and failed
the backup.
The run's name now drops "clickhouse-" and the "T" and "Z" of the
timestamp and spends the room on the first 12 hex digits of the UID,
so it is exactly as long as the old name for every application.
Assisted-by: LLM
Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
The archive a backup Job carries was found as a substring of its Pod
spec. The name without the BackupJob's UID starts the name with it,
and a strategy may append to either, so a Job that wrote a longer
archive could get the shorter name recorded on its Backup, and a
restore of that Backup would read another archive.
Assisted-by: LLM
Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
A backup Job created before archive names carried the BackupJob's UID
writes its archive under the name without it, and the Job is not
re-rendered when the controller is upgraded. Matching only the new
name left that Backup with no archive recorded, so its restore fell
back to guessing by timestamp.
The name without the UID is matched second, because it is a prefix of
the one with it.
Assisted-by: LLM
Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
An archive was named from the application and the BackupJob's start
time, which has one-second resolution. Two runs of one application
started in the same second, as two Plans on the same schedule are,
wrote the same archive. The strategy takes a finished upload of its
archive as an earlier attempt of its own run, so the later run could
succeed on the other run's upload and record an archive missing any
table created since.
The BackupJob's UID now ends the name. It goes after the timestamp so
names keep sorting chronologically, and the restore of a Backup that
records no archive, which accepts only a bare timestamp after the
release prefix, never picks one of these.
Assisted-by: LLM
Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
A Backup outlives the BackupJob that made it, so a BackupJob created
again under a name already used meets the earlier run's Backup when its
own Job completes. That happens by hand, and from a Plan reconciled
again inside its starting deadline, since a Plan names each BackupJob
after its schedule slot. The Altinity driver took the earlier Backup as
its result: the BackupJob turned Succeeded with a BackupRef recording
the earlier run's archive, the archive this run wrote was recorded on
nothing, and restoring what the BackupJob reported brought back the
older data.
An existing Backup is now taken as this run's only when it records this
run's archive, which is what an earlier reconcile of the same run
leaves when its status write is lost. Any other fails the BackupJob,
naming both archives, and leaves the earlier Backup untouched.
Assisted-by: LLM
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
The hold sets Ready=False/WaitingForCredentials with a countdown, and
nothing removed it once the Job was created, so a backup or restore
that ran for minutes kept saying it was still waiting. The condition is
dropped when the Job runs; a Ready condition set for another reason is
left alone.
Assisted-by: LLM
Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
The projector rewrites the credentials-updated-at stamp only when the
projected content changes, so a stamp the hold cannot use stays put.
An unreadable one failed every reconcile with no condition on the job,
and one far in the future held each job again on every reconcile; in
both cases every ClickHouse backup and restore in the namespace stopped
for good. Such a stamp is now logged and ignored. A stamp only slightly
ahead, as a skewed clock writes it, still waits, and that wait ends.
Assisted-by: LLM
Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
The clickhouse-backup sidecar now reads the projected credentials from a
mounted Secret, which kubelet rewrites only after its sync period,
about 75 seconds after the change in practice. The first BackupJob
projects the Secret and started the strategy Job a second later, so its
attempts raced the volume update and the backup could still fail on a
sidecar without credentials.
The projector records when the projected content last changed, and the
ClickHouse driver holds back creating a backup or restore Job until two
minutes have passed since, with a WaitingForCredentials condition. An
unchanged re-projection keeps the time, so only the first projection
and a rotation cause a wait, and a Job already running is never held.
Assisted-by: LLM
Signed-off-by: Alexey Artamonov <aleksei.artamonov@aenix.io>
The Altinity strategy named each archive inside its Pod with date -u
and recorded nothing on the Backup, so a restore could only guess: it
listed the remote archives under the application's prefix and took the
newest. A RestoreJob of an older Backup therefore restored whichever
backup had run last.
The controller now names the archive before it creates the backup Job,
from the BackupJob's persisted StartedAt, so every reconcile and every
retried Pod of the Job writes the same one. The name is recorded in the
Backup's driverMetadata next to the strategy parameters, and a restore
renders it into the strategy Pod, which restores exactly that archive
and fails, naming it, when remote storage no longer holds it. The name
is recorded only when the Job's Pod was given it, so a Job created by
an earlier controller, or a strategy that names its own archives,
leaves the Backup without one instead of pointing it at an archive
that does not exist.
Because a retried Pod reuses the name, it waits on an attempt the
sidecar is still running instead of being refused with 423, and asks
for resume, so a local or partial remote backup an earlier attempt left
is completed rather than refused as existing.
Backups taken before names were recorded stay restorable: the Pod picks
the newest archive whose name is stamped no later than the Backup's
takenAt, which is set only after the backup Job finished, and logs a
warning that the choice is a best effort.
Assisted-by: LLM
Signed-off-by: Myasnikov Daniil <myasnikovdaniil2001@gmail.com>
Run go fix over every first-party module: interface{} becomes any,
hand-written loops become slices, maps, min/max and range-over-int,
errors.As becomes errors.AsType, pointer helpers become new(expr) and
are inlined at their call sites, and so on. Every rewrite keeps
behaviour.
Three rewrites stay out. embedlit flattens embedded-field literals
such as ObjectMeta, syntax only Go 1.27 accepts, which would break
backports to release branches still on Go 1.26. omitzero drops
omitempty from struct fields, and openapi-gen reads omitempty as
optional, so metadata, spec and status would turn required in the
published schemas. And errorsastype rewrote an errors.As whose
target, apierrors.APIStatus, does not implement error, which
errors.AsType rejects at compile time.
Assisted-by: LLM
Signed-off-by: Aleksei Sviridkin <f@lex.la>