rings

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2024 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package rings provides logic to work with the four rings of a cluster

Index

Constants

View Source
const (
	// UnspecifiedRingID is the zero value of RingID and not considered
	// valid.
	UnspecifiedRingID RingID = iota
	RingZeroID               // RingZeroID is the RingID for RingZero (backbone)
	RingOneID                // RingOneID is the RingID for RingOne (local zone)
	RingTwoID                // RingTwoID is the RingID for RingTwo (region services)
	RingThreeID              // RingThreeID is the RingID for RingThree (region cluster pods)

	// RingMax indicates the highest [Ring] identifier
	RingMax = RingThreeID

	// RegionMax indicates the highest number that can be used for a [RegionID].
	RegionMax = (1 << 4) - 1
	// ZoneMax indicates the highest number that can be used for a [ZoneID].
	ZoneMax = (1 << 4) - 1
	// NodeMax indicates the highest number that can be used for a [NodeID].
	NodeMax = (1 << 12) - 2
	// NodeZeroMax indicates the highest number that can be used for a [NodeID]
	// when its a gateway connected to Ring 0 (backbone).
	NodeZeroMax = (1 << 8) - 2

	// RingZeroBits indicates the size of the prefix on the ring 0 (backbone) network.
	RingZeroBits = 16
	// RingOneBits indicates the size of the prefix on the ring 1 (lan) network.
	RingOneBits = 20
	// RingTwoBits indicates the size of the prefix on the ring 2 (services) network
	// of all kubernetes clusters.
	RingTwoBits = 20
	// RingThreeBits indicates the size of the prefix on the ring 3 (pods) network
	// of the kubernetes cluster of a region.
	RingThreeBits = 12
)

Variables

This section is empty.

Functions

func AddrFrom4

func AddrFrom4(a, b, c, d uint) netip.Addr

AddrFrom4 assembles an IPv4 address for 4 numbers. each number is truncated to 8-bits.

func AddrFromU32

func AddrFromU32(v uint32) netip.Addr

AddrFromU32 converts a 32bit value into an IPv4 address.

func AddrToU32

func AddrToU32(addr netip.Addr) (v uint32, ok bool)

AddrToU32 converts a valid IPv4 address into it's 32bit numeric representation.

func DecodeAddress added in v0.8.3

func DecodeAddress[T ~uint | NodeID](addr netip.Addr) (RingID, RegionID, ZoneID, T)

DecodeAddress extracts ring address fields from a given 10.0.0.0/8 address.

revive:disable:function-result-limit

func DecodeRingOneAddress added in v0.8.3

func DecodeRingOneAddress(addr netip.Addr) (RegionID, ZoneID, NodeID, bool)

DecodeRingOneAddress attempts to extract region, zone and node identifiers from a given ring 1 address.

revive:disable:function-result-limit

func DecodeRingZeroAddress added in v0.8.3

func DecodeRingZeroAddress(addr netip.Addr) (RegionID, ZoneID, NodeID, bool)

DecodeRingZeroAddress attempts to extract region, zone and node identifiers from a given ring 0 address.

revive:disable:function-result-limit

func ErrOutOfRange

func ErrOutOfRange[T ~int | ~uint32](value T, field string) error

ErrOutOfRange is an error indicating the value of a field is out of range.

func PrefixToRange

func PrefixToRange(subnet netip.Prefix) (from, to netip.Addr, ok bool)

PrefixToRange returns the beginning and end of a netip.Prefix (aka CIDR or subnet).

func RingOneAddress

func RingOneAddress(region RegionID, zone ZoneID, node NodeID) (addr netip.Addr, err error)

RingOneAddress returns a Ring 1 address for a particular node.

A ring 1 address is `10.(region_id).(zone_id << 4).(node_id)/20` but the node_id can take up to 12 bits.

func RingOnePrefix

func RingOnePrefix(region RegionID, zone ZoneID) (cidr netip.Prefix, err error)

RingOnePrefix represents a (virtual) local network of a zone.

Ring 1 is `10.(region_id).(zone_id << 4).(node_id)/20` network grouped under what would be Ring 2 for region_id 0. There are 12 bits worth of nodes but nodes under 255 are special as they also get a slot on Ring 0.

func RingThreePrefix

func RingThreePrefix(region RegionID) (subnet netip.Prefix, err error)

RingThreePrefix returns the subnet corresponding to the pods of a cluster.

Ring 3 is a `10.(region_id << 4).0.0/12` network

func RingTwoPrefix

func RingTwoPrefix(region RegionID) (cidr netip.Prefix, err error)

RingTwoPrefix represents the services of a cluster

Ring 2 subnets are of the form `10.(region_id).0.0/20`, using the address space that would belong to the ring 3 region_id 0.

func RingZeroAddress

func RingZeroAddress(region RegionID, zone ZoneID, node NodeID) (addr netip.Addr, err error)

RingZeroAddress returns a Ring 0 address for a particular node.

A ring 0 address looks like 10.0.(region_id << 4 + zone_id).(node_id)/20

func RingZeroPrefix

func RingZeroPrefix(region RegionID, zone ZoneID) (cidr netip.Prefix, err error)

RingZeroPrefix represents the backbone that connects gateways of the different Ring 1 networks.

The ring 0 network corresponds to what would be ring 2 for region_id 0. 10.0.0.0-10.0.255.255

func UnsafeRingOneAddress added in v0.8.7

func UnsafeRingOneAddress(region RegionID, zone ZoneID, node NodeID) netip.Addr

UnsafeRingOneAddress is equivalent ot RingOneAddress but without validating the input.

func UnsafeRingThreeAddress added in v0.8.7

func UnsafeRingThreeAddress(region RegionID, n uint) netip.Addr

UnsafeRingThreeAddress is equivalent ot RingThreeAddress but without validating the input.

func UnsafeRingTwoAddress added in v0.8.7

func UnsafeRingTwoAddress(region RegionID, n uint) netip.Addr

UnsafeRingTwoAddress is equivalent ot RingTwoAddress but without validating the input.

func UnsafeRingZeroAddress added in v0.8.7

func UnsafeRingZeroAddress(region RegionID, zone ZoneID, node NodeID) netip.Addr

UnsafeRingZeroAddress is equivalent ot RingZeroAddress but without validating the input.

Types

type NodeID

type NodeID int

NodeID is the identifier of a machine within a zone of a region, valid between 1 and NodeMax, but between 1 and NodeZeroMax if it will be a zone gateway.

func (NodeID) String added in v0.8.2

func (n NodeID) String() string

func (NodeID) Valid

func (n NodeID) Valid() bool

Valid tells a NodeID is within the valid range.

func (NodeID) ValidZero

func (n NodeID) ValidZero() bool

ValidZero tells a NodeID is within the valid range for a gateway.

type RegionID

type RegionID int

RegionID is the identifier of a region, valid between 1 and RegionMax.

func DecodeRingThreeAddress added in v0.8.3

func DecodeRingThreeAddress(addr netip.Addr) (RegionID, uint, bool)

DecodeRingThreeAddress attempts to extract region and unique identifier for a kubernetes pod from a given ring 3 address.

func DecodeRingTwoAddress added in v0.8.3

func DecodeRingTwoAddress(addr netip.Addr) (RegionID, uint, bool)

DecodeRingTwoAddress attempts to extract region and unique identifier for a kubernetes service from a given ring 2 address.

func (RegionID) String added in v0.8.2

func (n RegionID) String() string

func (RegionID) Valid

func (n RegionID) Valid() bool

Valid tells a RegionID is within the valid range.

type Ring added in v0.8.3

type Ring interface {
	ID() RingID
}

A Ring identifies what ring an address belongs to

type RingID added in v0.8.3

type RingID int

RingID identifies a Ring

func (RingID) String added in v0.8.3

func (n RingID) String() string

func (RingID) Valid added in v0.8.3

func (n RingID) Valid() bool

Valid tells a RingID is within the valid range.

type ZoneID

type ZoneID int

ZoneID is the identifier of a zone within a region, valid between 1 and ZoneMax.

func (ZoneID) String added in v0.8.2

func (n ZoneID) String() string

func (ZoneID) Valid

func (n ZoneID) Valid() bool

Valid tells a ZoneID is within the valid range.

Jump to

Keyboard shortcuts

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