Documentation
¶
Overview ¶
Package gc finds what `provr gc` should clean up: stopped hosts nobody uses, idle hosts the reaper missed, expired previews, leftover tailnet devices, detached disks, and resources it can only report (unreachable running hosts, instances without an owner).
This package only detects. Every rule is a function that takes the listed state and returns Findings; Scan lists the state for one project and runs the rules in the spec's order. Detection never stops, deletes or brings anything down; applying the planned Actions is a separate step.
Rules 4 and 7 never plan a mutation: a broken tailnet must not be able to make gc stop or delete working hosts, so they carry the command for the user to run (Action report-only).
Index ¶
- Constants
- func CronLine(bin string) string
- func CronText(bin string, lookup LookPathFunc) string
- type Action
- type Deps
- type Disk
- type Finding
- func Dedupe(fs []Finding) []Finding
- func DetachedDisks(disks []Disk) []Finding
- func ExpiredPreviews(list preview.ListResult) ([]Finding, error)
- func IdleHosts(ctx context.Context, r execx.Runner, sshBinary string, hosts []core.Host, ...) ([]Finding, error)
- func Scan(ctx context.Context, d Deps, p config.Project) ([]Finding, error)
- func StaleDevices(devices []api.Device, hostnames, previewIDs []string, now time.Time) []Finding
- func StoppedHosts(hosts []core.Host, deleteAfter time.Duration, now time.Time) []Finding
- func UnownedInstances(insts []Instance, gcpProject string) []Finding
- func UnreachableHosts(hosts []core.Host, gcpProject string, now time.Time) []Finding
- type Instance
- type Loader
- type LookPathFunc
- type Report
- type Result
Constants ¶
const ( KindHost = "host" KindPreview = "preview" KindDevice = "tailnet-device" KindDisk = "disk" KindInstance = "instance" )
Resource kinds of a Finding.
const ( RuleStoppedHost = 1 RuleIdleHost = 2 RuleExpired = 3 RuleUnreachable = 4 RuleDevice = 5 RuleDisk = 6 RuleUnowned = 7 )
Rule numbers and names, in the order Scan runs them.
const ( // ResultPlanned: a dry run left the mutating action undone. ResultPlanned = "planned" // ResultOK: the action succeeded. ResultOK = "ok" // ResultFailed: the action failed; Result.Error says why. ResultFailed = "failed" // ResultReported: a report-only finding; nothing was or will be done. ResultReported = "reported" // ResultSkippedAttached: the disk got a user between scan and delete. ResultSkippedAttached = "skipped: attached" )
Result strings of an applied or planned Finding.
const DiskPrefix = "provr-"
DiskPrefix is the name prefix of provr's disks.
const RecommendedMarker = "# recommended"
RecommendedMarker tags the one crontab line provr recommends.
Variables ¶
This section is empty.
Functions ¶
func CronLine ¶
CronLine returns the hourly crontab entry for the provr binary at bin. It uses /bin/sh syntax (cron runs commands with sh): `.` instead of `source` and $HOME instead of `~`, which cron does not expand. The binary is an absolute path because cron's PATH is minimal; the env file it sources sets PATH for the tools gc runs (see CronText).
func CronText ¶
func CronText(bin string, lookup LookPathFunc) string
CronText is what `provr gc --print-cron` prints. It never installs anything and holds no secret values, only placeholders. The env file also exports a PATH computed with lookup (nil means exec.LookPath) so gc's tools resolve under cron's minimal PATH.
Types ¶
type Action ¶
type Action string
Action is what gc plans to do about a Finding.
const ( ActionDeleteHost Action = "delete-host" ActionStopHost Action = "stop-host" ActionDownPreview Action = "down-preview" ActionDeleteDev Action = "delete-device" ActionDeleteDisk Action = "delete-disk" ActionReportOnly Action = "report-only" )
Planned actions. ActionReportOnly findings are never applied; they carry the command the user can run (Finding.Command).
type Deps ¶
type Deps struct {
// Provider lists the project's hosts.
Provider core.HostProvider
// Runner runs ssh (rule 2). SSHBinary defaults to "ssh".
Runner execx.Runner
SSHBinary string
// Gcloud is the impersonated gcloud wrapper used to list instances and
// disks (rules 6 and 7); its Project is the GCP project.
Gcloud setup.Gcloud
// Devices lists the tailnet devices (rule 5).
Devices *api.Client
// ListPreviews lists the project's previews with their expiry, normally
// preview.Service.List. It is a func (not the *Service) so tests do not
// have to script every ssh call the service makes.
ListPreviews func(ctx context.Context) (preview.ListResult, error)
// Down removes one preview by id through the same path as `provr down`,
// normally preview.Service.Down. Only Apply uses it.
Down func(ctx context.Context, id string) error
// Now is the clock.
Now func() time.Time
}
Deps are the collaborators Scan reads the state through.
type Disk ¶
type Disk struct {
Name string `json:"name"`
Zone string `json:"zone"`
// Users are the URLs of the instances using the disk.
Users []string `json:"users"`
}
Disk is a persistent disk as listed for rule 6.
type Finding ¶
type Finding struct {
// Rule is the rule number (1-7) and RuleName its short name.
Rule int `json:"rule"`
RuleName string `json:"rule_name"`
// Kind is the resource kind (Kind* constants) and ID its identifier: the
// host or instance name, preview id, tailnet device id or disk name.
Kind string `json:"kind"`
ID string `json:"id"`
// Name is the device's short name; empty for other kinds.
Name string `json:"name,omitempty"`
// Zone is the cloud zone of a host, instance or disk.
Zone string `json:"zone,omitempty"`
// HostID is the host a preview runs on.
HostID string `json:"host_id,omitempty"`
// Reason says why the rule matched, for the report.
Reason string `json:"reason"`
// Action is the planned action.
Action Action `json:"action"`
// Command is the recommended command of a report-only finding.
Command string `json:"command,omitempty"`
// Project is the project whose scan found it. Set only by RunAll, so the
// single-project report is unchanged.
Project string `json:"project,omitempty"`
// contains filtered or unexported fields
}
Finding is one resource a rule matched and what gc plans to do about it.
func Dedupe ¶
Dedupe drops findings that repeat an earlier one's (kind, id, zone), keeping the first. Rules 6 and 7 are not project-scoped, so scans of several projects can report the same disk or instance.
func DetachedDisks ¶
DetachedDisks is rule 6: disks named provr-* that no instance uses are planned for deletion.
func ExpiredPreviews ¶
func ExpiredPreviews(list preview.ListResult) ([]Finding, error)
ExpiredPreviews is rule 3: previews that preview.Service.List marks expired are planned to be taken down (the same path as `provr down`). Expiry is read the way `provr ls` reads it, from the container labels. A running host the listing could not query is returned in the error.
func IdleHosts ¶
func IdleHosts(ctx context.Context, r execx.Runner, sshBinary string, hosts []core.Host, idleStop time.Duration, now time.Time) ([]Finding, error)
IdleHosts is rule 2: a reachable running host with no provr=managed containers whose activity marker is older than idleStop + 15 min is planned to be stopped (the reaper should have done it). Each host is probed over Tailscale SSH through r; a failed probe skips that host and is returned in the error, joined with the others.
func Scan ¶
Scan lists the state of one project and returns the findings of rules 1-7 in that order. It makes no mutating call. A failure to list one kind of state skips only the rules that need it; every such failure is joined into the returned error, and the findings of the other rules are still returned.
func StaleDevices ¶
StaleDevices is rule 5: tailnet devices tagged tag:provr that are not connected and were last seen more than an hour ago are planned for deletion, unless their short name is the tailnet hostname of an existing host (hostnames) or the id of a running preview (previewIDs). A device with no LastSeen is skipped, since its age is unknown.
func StoppedHosts ¶
StoppedHosts is rule 1: a stopped host whose StoppedAt is older than deleteAfter is planned for deletion. When the stop time is the host's creation time (the provider recorded no stop), the reason says so and the choice is logged.
func UnownedInstances ¶
UnownedInstances is rule 7: instances labeled provr=managed that lack provr-project or provr-owner are only reported, with the delete command.
func UnreachableHosts ¶
UnreachableHosts is rule 4: a running host that does not answer over SSH and has been running for more than 15 minutes is only reported, with the gcloud command to stop it. gc never stops or deletes it itself, because a broken tailnet would otherwise wipe working hosts.
type Instance ¶
type Instance struct {
Name string `json:"name"`
Zone string `json:"zone"`
Labels map[string]string `json:"labels"`
}
Instance is a GCE instance as listed for rule 7.
type Loader ¶
Loader loads the provr.yaml at configPath and builds the Deps gc works through for it.
type LookPathFunc ¶
LookPathFunc resolves a tool name to its absolute path (exec.LookPath shape).
type Report ¶
type Report struct {
DryRun bool `json:"dry_run"`
Project string `json:"project"`
Findings []Result `json:"findings"`
Errors []string `json:"errors"`
// Projects and Skipped are set by `gc --all` only: the provr.yaml paths
// scanned, and a note for each recorded entry that was skipped.
Projects []string `json:"projects,omitempty"`
Skipped []string `json:"skipped,omitempty"`
}
Report is the outcome of one gc run. Its JSON shape is the `--json` output:
{"dry_run": bool, "project": string,
"findings": [{"rule": int, "rule_name": string, "kind": string,
"id": string, "name"?: string, "zone"?: string,
"host_id"?: string, "reason": string, "action": string,
"command"?: string, "result": string, "error"?: string}],
"errors": [string]}
result is "planned" (dry run), "ok", "failed", "reported" (report-only) or "skipped: attached". errors holds the partial-scan failures. With `gc --all` project is "" and each finding carries its "project"; "projects" lists the scanned provr.yaml paths and "skipped" the entries whose file is gone.
func Run ¶
Run scans one project and applies (or, with dryRun, plans) the findings. Scan's partial-failure errors go into the Report; they do not stop apply.
func RunAll ¶
RunAll is `gc --all`: it scans every recorded project (provr.yaml path to project name, as state.Store.Projects returns it) in path order, merges the findings, drops duplicates across projects (rules 6 and 7 are not project scoped) and applies each finding with the Deps of the project that found it. The result is one Report.
An entry whose provr.yaml no longer exists is skipped with a note in Report.Skipped; that is not a failure. An entry that cannot be loaded for another reason is an error in Report.Errors, and the other projects go on.
func (Report) FailureCount ¶
FailureCount is the number of failed actions plus scan errors.
type Result ¶
Result is a Finding with what happened to it. Its JSON is the Finding's fields plus "result" and, on failure, "error".