k8srelay

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: Apache-2.0 Imports: 16 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 k8srelay is the in-cluster side of the Kubernetes relay: it decides which API requests may reach the API server, validates workload bodies, filters responses and enforces limits. It does no network setup of its own.

Index

Constants

View Source
const AgentNamespace = "levelrail-agent"

AgentNamespace is where the cluster agent runs; it is the only namespace the manifest creates.

Variables

This section is empty.

Functions

func FilterJSON

func FilterJSON(body []byte, forceSecret bool) ([]byte, error)

FilterJSON strips Secret data and redacts token-like fields from one JSON document. forceSecret treats the document as a Secret or SecretList even without a kind field. It fails closed: invalid JSON is an error.

func FilterText

func FilterText(s string) string

FilterText redacts token shapes from log output. This is best effort: it catches common shapes only and cannot promise logs hold no secrets.

func RenderManifest

func RenderManifest(in ManifestInput) (string, error)

RenderManifest returns the multi-document YAML the operator applies with their own kubectl.

Types

type Config

type Config struct {
	Namespaces  []string // namespaces the relay may read and (with Writes) mutate
	ClusterRead bool     // opt-in read of nodes, namespaces and cluster-wide ingress lists
	Writes      bool     // off means observe only

	// Workload write scope. Zero values are the strictest setting.
	ServiceAccount           string   // the only service account pods may name; empty refuses the field
	AllowServiceAccountToken bool     // permit automountServiceAccountToken=true
	AllowedSecrets           []string // secret names pods may reference
	AllowDockerConfig        bool     // permit writing registry credential secrets
	AllowServiceExposure     bool     // permit externalIPs, ExternalName, NodePort, LoadBalancer
	IngressAnnotations       []string // extra ingress annotations beyond the built-in list
}

Config scopes what the relay may touch. Everything not named here is denied.

func ConfigFromEnv

func ConfigFromEnv(getenv func(string) string) (Config, error)

ConfigFromEnv reads the scope flags the install manifest sets.

func (Config) Validate

func (c Config) Validate() error

Validate checks namespace names so a typo cannot widen scope.

type Decision

type Decision struct {
	Target Target
	Method string
	Verb   string
	Write  bool
	Stream bool
	Query  url.Values // sanitized, safe to forward
}

Decision is what a permitted request looks like after normalization.

type DenyError

type DenyError struct{ Rule string }

DenyError is returned for every refusal; Rule is a short stable code that callers may surface without echoing request content.

func (*DenyError) Error

func (e *DenyError) Error() string

type Handler

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

Handler enforces the policy and limits, forwards through an injected RoundTripper (the in-cluster client) and filters what comes back.

func NewHandler

func NewHandler(p *Policy, upstream http.RoundTripper, token TokenSource, baseURL string) (*Handler, error)

NewHandler builds a handler. baseURL is the API server address the RoundTripper resolves, for example the in-cluster service URL.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Limits

type Limits struct {
	MaxInflight       int
	MaxStreams        int
	BodyReadTimeout   time.Duration
	RPS               float64
	Burst             int
	MaxRequestBytes   int64
	MaxResponseBytes  int64
	RequestTimeout    time.Duration
	StreamIdleTimeout time.Duration
	StreamMaxLifetime time.Duration
	MaxLogTailLines   int
	MaxURLBytes       int
	MaxSelectorBytes  int
	MaxLineBytes      int
	Pod               PodLimits
}

Limits holds every numeric threshold the relay applies. Logic reads these values only; DefaultLimits is the single place defaults live.

func DefaultLimits

func DefaultLimits() Limits

DefaultLimits returns the seed values that settings and env override.

func LimitsFromEnv

func LimitsFromEnv(getenv func(string) string) (Limits, error)

LimitsFromEnv overlays APP_K8S_RELAY_* values on DefaultLimits. An unset or empty variable keeps the default; an unparsable or non-positive one is an error.

func (Limits) Validate

func (l Limits) Validate() error

Validate rejects zero or negative values so a bad setting fails loudly.

type ManifestInput

type ManifestInput struct {
	Cluster          string
	Image            string // must be pinned by digest
	ControlPlaneAddr string // host:port the agent dials
	CAFingerprint    string
	JoinToken        string
	Config           Config
}

ManifestInput is everything the install manifest needs. JoinToken is a one-time credential: callers print the manifest to the operator once and never log or store it.

func (ManifestInput) Validate

func (in ManifestInput) Validate() error

Validate refuses anything that could widen scope or smuggle YAML.

type PodLimits

type PodLimits struct {
	MaxContainers    int
	MaxEnvVars       int
	MaxVolumes       int
	MaxImageRefBytes int
}

PodLimits bounds workload bodies.

type Policy

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

Policy evaluates requests against a Config and Limits.

func NewPolicy

func NewPolicy(cfg Config, lim Limits) (*Policy, error)

NewPolicy validates inputs and builds a policy.

func (*Policy) Check

func (p *Policy) Check(r *http.Request) (Decision, error)

Check validates method, headers, path and query. Deny by default.

func (*Policy) ValidateBody

func (p *Policy) ValidateBody(d Decision, contentType string, body []byte) error

ValidateBody checks a write body against the decision: content type, strict JSON shape, namespace and name consistency, and per-resource safety rules.

type Target

type Target struct {
	NonResource bool // /version, /readyz
	Group       string
	Version     string
	Namespace   string
	Resource    string
	Name        string
	Subresource string
}

Target is a parsed, canonical API path.

func ParsePath

func ParsePath(p string) (Target, error)

ParsePath rejects every ambiguous form (encoding, dot segments, empty segments, odd characters, uppercase) before any matching happens.

func (Target) Path

func (t Target) Path() string

Path rebuilds the canonical path; ParsePath(t.Path()) round-trips.

type TokenSource

type TokenSource func() (string, error)

TokenSource returns the current service account token. It is called per request so rotated projected tokens are picked up.

Jump to

Keyboard shortcuts

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