environments

package
v1.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 31, 2026 License: Apache-2.0 Imports: 4 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package environments resolves the environment axis from namespace names.

The multi-cluster model assumed environment maps to a kubeconfig context. A real cluster disproved that: it held several environments side by side, distinguished by a namespace prefix, sharing one context and one set of credentials (see docs/04-multi-cluster.md and issue #39). This package makes environment a dimension that can come from a namespace pattern.

Index

Constants

View Source
const (
	SegEnv     = "env"
	SegSystem  = "system"
	SegService = "service"
)

Segment names the resolver treats specially. A template is free to use other names, but these three drive the environment axis and app identity.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// Pattern is the namespace template, e.g. "{env}-{system}-{service}".
	Pattern string
	// Systems anchors the system segment. Recommended when environments are
	// dynamic, since new environments then need no reconfiguration.
	Systems []string
	// Environments anchors the environment segment. Use when systems vary but
	// the environment set is stable.
	Environments []string
}

Config configures a Resolver.

type Decomposition

type Decomposition struct {
	// Tenant is true when the namespace matched the pattern and any
	// constraints. A platform namespace (kube-system, argocd, a vendor
	// component) is not a tenant and has no environment.
	Tenant bool

	Environment string
	System      string
	Service     string

	// Segments holds every captured placeholder, so a template with names
	// beyond env/system/service still exposes them.
	Segments map[string]string
}

Decomposition is what the resolver made of one namespace.

func (Decomposition) Identity

func (d Decomposition) Identity() string

Identity is how an app is matched across environments: everything except the environment segment. Two namespaces that agree on identity but differ on environment are the same service in two places.

type Environment

type Environment struct {
	Name   string
	Risk   Risk
	Write  WritePolicy
	Color  string
	Hazard bool // render hazard hatching, high-risk environments only
}

Environment is a named tier with a risk level and a write policy.

func Classify

func Classify(name string) Environment

Classify infers an environment from a name.

The name is split into tokens on common separators and each token's trailing digits are stripped, so "qa1" and "sandbox3" classify the same as "qa" and "sandbox". That matters because real environments are frequently numbered.

An unmatched name becomes unclassified and inherits prod-strength guardrails: high risk, writes denied, hazard on. Treating the unknown as dangerous is the safe direction to be wrong in.

func WithName

func WithName(name string) Environment

WithName returns a copy classified as name, preserving nothing else. It is a convenience for turning an inferred environment segment into an Environment.

type EnvironmentCandidate

type EnvironmentCandidate struct {
	Name     string
	Services int // distinct identities seen under this environment segment
}

EnvironmentCandidate is a proposed environment, with evidence.

type Pattern

type Pattern struct {
	// contains filtered or unexported fields
}

Pattern decomposes a namespace name into named segments from a template such as "{env}-{system}-{service}".

Two rules make the common conventions work:

  • A non-final placeholder matches one separator-free segment. The final placeholder captures the remainder, because service names contain the separator ("notification-aws-ses-adapter" is one service, not three).
  • A template segment can be a literal instead of a placeholder. Anchoring one segment is what separates real tenant namespaces from platform ones that happen to share the same shape: a structural rule alone matches "aws-load-balancer-controller" as if it were a tenant service, which it is not.

func ParsePattern

func ParsePattern(template string) (*Pattern, error)

ParsePattern compiles a namespace template.

The template must contain at least one placeholder, and placeholder names must be unique so a decomposition is unambiguous.

func (*Pattern) Match

func (p *Pattern) Match(namespace string) (map[string]string, bool)

Match decomposes a namespace, returning the segment values keyed by placeholder name. The bool is false when the namespace does not fit, which is the normal answer for a platform namespace.

func (*Pattern) Names

func (p *Pattern) Names() []string

Names returns the placeholder names in template order.

func (*Pattern) String

func (p *Pattern) String() string

String returns the original template.

type Resolver

type Resolver struct {
	// contains filtered or unexported fields
}

Resolver decomposes namespaces and decides tenant versus platform.

func NewResolver

func NewResolver(cfg Config) (*Resolver, error)

NewResolver builds a resolver from config.

func (*Resolver) Decompose

func (r *Resolver) Decompose(namespace string) Decomposition

Decompose classifies one namespace.

func (*Resolver) InferEnvironments

func (r *Resolver) InferEnvironments(namespaces []string) []EnvironmentCandidate

InferEnvironments proposes environment names from a set of namespaces.

It is a suggestion, never applied automatically. Structurally, "team-a-*" and "team-b-*" are indistinguishable from environment prefixes while meaning something entirely different, so a human confirms before these become an axis. The ranking by shared-service count is the signal: real environments run largely the same set of services, teams do not.

type Risk

type Risk int

Risk drives how much friction a write requires. Unknown risk is treated as high, because assuming an unfamiliar environment is safe is the dangerous default.

const (
	RiskUnknown Risk = iota
	RiskLow
	RiskMedium
	RiskHigh
)

func (Risk) String

func (r Risk) String() string

type WritePolicy

type WritePolicy int

WritePolicy is how destructive actions are gated in an environment.

const (
	// WriteDeny blocks writes. The default for anything not proven safe.
	WriteDeny WritePolicy = iota
	// WriteConfirm allows writes after a typed confirmation.
	WriteConfirm
	// WriteAllow permits writes directly.
	WriteAllow
	// WriteBreakGlass unlocks writes for a limited window with a stated reason.
	WriteBreakGlass
)

func (WritePolicy) String

func (w WritePolicy) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL