Documentation
¶
Overview ¶
Package cloudguard is the one gate every AWS create goes through: caps, allowlists, kill switch and mandatory tags. It never deletes anything.
Index ¶
- Constants
- Variables
- func InjectTags(cfg Config, base map[string]string, m TagMeta) map[string]string
- func RunInstance(ctx context.Context, c *awsx.Client, g *Guard, in InstanceInput) (string, error)
- func TagKey(cfg Config, suffix string) string
- type CacheSpend
- type Config
- type CreateRequest
- type Decision
- type Expired
- type Guard
- type IdleConfig
- type InstanceInput
- type Orphan
- type OrphanReport
- type Plan
- type SpendSource
- type Suggestion
- type TagMeta
Constants ¶
const ( CodeKillSwitch = "kill_switch" CodeServiceDisabled = "service_disabled" CodePerCreateMax = "per_create_max" CodeMonthlyCap = "monthly_cap" CodeSpendUnknown = "spend_unknown" CodeInstanceType = "instance_type_not_allowed" CodeTagsMissing = "tags_missing" CodeAccountMissing = "account_missing" )
Refusal codes.
const ( KindEC2LowCPU = "ec2_low_cpu" KindEBSUnattached = "ebs_unattached" KindEIPFree = "eip_unassociated" KindALBNoRequests = "alb_no_requests" KindRDSStopped = "rds_stopped" KindRDSRestart = "rds_cluster_restart" ActionStop = "stop" ActionSnapshotDelete = "snapshot_delete" ActionRelease = "release" ActionDelete = "delete" ActionRestop = "restop" )
Idle suggestion kinds and actions.
Variables ¶
var ErrRefused = errors.New("cloudguard: create refused")
ErrRefused wraps every guard refusal of an EC2 create.
Functions ¶
func InjectTags ¶
InjectTags returns base plus the mandatory tags. Mandatory keys always win over caller values so a caller cannot drop or forge them.
func RunInstance ¶
RunInstance creates one EC2 instance. The guard decides first and the mandatory tags go on the RunInstances call itself, so the instance is never untagged.
Types ¶
type CacheSpend ¶
type CacheSpend struct {
Cache *cloudspend.Cache
// Fetch builds the provider fetch for one account.
Fetch func(account string) cloudspend.Fetch
}
CacheSpend reads month to date USD from the shared spend cache.
func (CacheSpend) MonthToDateUSD ¶
MonthToDateUSD implements SpendSource.
type Config ¶
type Config struct {
KillSwitch bool
// MonthlyCapUSD is the default cap per account; AccountCaps overrides it.
MonthlyCapUSD float64
AccountCaps map[string]float64
PerCreateMaxUSD float64
// InstanceTypes is the allowlist for creates that carry an instance type.
InstanceTypes []string
ServicesEnabled map[string]bool
// DefaultTTL feeds the expires tag; zero omits the tag.
DefaultTTL time.Duration
// TagPrefix defaults to the prefix of cloudspend.ProjectTagKey.
TagPrefix string
Now func() time.Time
}
Config holds the live guard settings. Zero values mean no limit, except ServicesEnabled and InstanceTypes, which fail closed.
type CreateRequest ¶
type CreateRequest struct {
Account string
Service string
Resource string
InstanceType string
EstimatedMonthlyUSD float64
Tags map[string]string
}
CreateRequest describes one create before it reaches AWS.
type Guard ¶
type Guard struct {
// contains filtered or unexported fields
}
Guard evaluates creates against the live Config.
func New ¶
func New(cfg func() Config, spend SpendSource) *Guard
New builds a guard. spend may be nil, which makes a set cap fail closed.
func (*Guard) Authorize ¶
Authorize runs the guard for one EC2 create and returns the full tag set to apply. A refusal carries the guard code and message so callers can answer with a clear 4xx.
type IdleConfig ¶
type IdleConfig struct {
CPUPercent float64
Days int
EBSGBMonthUSD float64
EIPMonthUSD float64
ALBMonthUSD float64
Now func() time.Time
}
IdleConfig carries the live idle thresholds and the unit prices behind the savings.
type InstanceInput ¶
type InstanceInput struct {
Account, Region, AMI, Type, Name, SubnetID string
EstimatedMonthlyUSD float64
Meta TagMeta
}
InstanceInput is one EC2 create request.
type OrphanReport ¶
OrphanReport lists candidates only. Nothing here is ever deleted.
func FindOrphans ¶
func FindOrphans(ctx context.Context, c *awsx.Client, region string, maxPages int) (OrphanReport, error)
FindOrphans reads unattached volumes, unassociated Elastic IPs and snapshots whose source volume is gone. The status-available filter and the self owner filter follow the EC2 DescribeVolumes and DescribeSnapshots docs.
type Plan ¶
type Plan struct {
DryRun bool
Items []Expired
// Unparsed lists managed ARNs whose expires tag was not a valid time.
Unparsed []string
}
Plan is what a sweep would delete. It is always a dry run.
type SpendSource ¶
SpendSource returns month to date USD spend for an account.
type Suggestion ¶
type Suggestion struct {
Kind, ID, Detail, Action string
// MonthlyUSD is the estimated saving, zero when no estimate exists.
MonthlyUSD float64
}
Suggestion is one idle resource. The control plane only reports it; it never acts.