cosmos

package
v0.0.0-...-6b34a6b Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2024 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const CacheControllerName = "CosmosCache"

Variables

This section is empty.

Functions

func IntersectPods

func IntersectPods(coll *StatusCollection, pods []corev1.Pod)

IntersectPods removes all pods from the collection that are not in the given list.

func UpsertPod

func UpsertPod(coll *StatusCollection, pod *corev1.Pod)

UpsertPod updates the pod in the collection or adds an item to the collection if it does not exist. All operations are performed in-place.

Types

type CacheController

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

CacheController periodically polls pods for their CometBFT status and caches the result. The cache is a controller so it can watch CosmosFullNode objects to warm or invalidate the cache.

func NewCacheController

func NewCacheController(collector Collector, reader client.Reader, recorder record.EventRecorder) *CacheController

func (*CacheController) Close

func (c *CacheController) Close() error

Close stops all cache collecting and waits for all goroutines to exit.

func (*CacheController) Collect

func (c *CacheController) Collect(ctx context.Context, controller client.ObjectKey) StatusCollection

Collect returns a StatusCollection for the given controller. Only returns cached CometStatus.

func (*CacheController) Invalidate

func (c *CacheController) Invalidate(controller client.ObjectKey, pods []string)

Invalidate removes the given pods status from the cache.

func (*CacheController) Reconcile

func (*CacheController) SetupWithManager

func (c *CacheController) SetupWithManager(_ context.Context, mgr ctrl.Manager) error

SetupWithManager watches CosmosFullNode objects and starts cache collecting.

func (*CacheController) SyncedPods

func (c *CacheController) SyncedPods(ctx context.Context, controller client.ObjectKey) []*corev1.Pod

SyncedPods returns only the pods that are ready and in sync (i.e. caught up with chain tip).

type Collector

type Collector interface {
	Collect(ctx context.Context, pods []corev1.Pod) StatusCollection
}

type CometClient

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

CometClient knows how to make requests to the CometBFT (formerly Comet) RPC endpoints. This package uses a custom client because 1) parsing JSON is simple and 2) we prevent any dependency on CometBFT packages.

func NewCometClient

func NewCometClient(client *http.Client) *CometClient

func (*CometClient) Status

func (client *CometClient) Status(ctx context.Context, rpcHost string) (CometStatus, error)

Status finds the latest status.

type CometStatus

type CometStatus struct {
	JSONRPC string
	ID      int
	Result  struct {
		NodeInfo      NodeInfo
		SyncInfo      SyncInfo
		ValidatorInfo ValidatorInfo
	}
}

CometStatus is the common response from the /status RPC endpoint.

func (CometStatus) LatestBlockHeight

func (status CometStatus) LatestBlockHeight() uint64

LatestBlockHeight parses the latest block height string. If the string is malformed, returns 0.

type NodeInfo

type NodeInfo struct {
	ProtocolVersion struct {
		P2P   string `json:"p2p"`
		Block string `json:"block"`
		App   string `json:"app"`
	} `json:"protocol_version"`
	ID         string `json:"id"`
	ListenAddr string `json:"listen_addr"`
	Network    string `json:"network"`
	Version    string `json:"version"`
	Channels   string `json:"channels"`
	Moniker    string `json:"moniker"`
	Other      struct {
		TxIndex    string `json:"tx_index"`
		RPCAddress string `json:"rpc_address"`
	} `json:"other"`
}

type StatusCollection

type StatusCollection []StatusItem

StatusCollection is a list of pods and CometBFT status associated with the pod.

func (StatusCollection) Len

func (coll StatusCollection) Len() int

Len returns the number of items in the collection. Part of the sort.Interface implementation.

func (StatusCollection) Less

func (coll StatusCollection) Less(i, j int) bool

Less implements sort.Interface.

func (StatusCollection) Pods

func (coll StatusCollection) Pods() []*corev1.Pod

Pods returns all pods.

func (StatusCollection) Swap

func (coll StatusCollection) Swap(i, j int)

Swap implements sort.Interface.

func (StatusCollection) Synced

func (coll StatusCollection) Synced() StatusCollection

Synced returns all items that are caught up with the chain tip.

func (StatusCollection) SyncedPods

func (coll StatusCollection) SyncedPods() []*corev1.Pod

SyncedPods returns the pods that are caught up with the chain tip.

type StatusCollector

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

StatusCollector collects the CometBFT status of all pods owned by a controller.

func NewStatusCollector

func NewStatusCollector(comet Statuser, timeout time.Duration) *StatusCollector

NewStatusCollector returns a valid StatusCollector. Timeout is exposed here because it is important for good performance in reconcile loops, and reminds callers to set it.

func (StatusCollector) Collect

func (coll StatusCollector) Collect(ctx context.Context, pods []corev1.Pod) StatusCollection

Collect returns a StatusCollection for the given pods. Any non-nil error can be treated as transient and retried.

type StatusItem

type StatusItem struct {
	Pod    *corev1.Pod
	Status CometStatus
	TS     time.Time
	Err    error
}

StatusItem is a pod paired with its CometBFT status.

func (StatusItem) GetPod

func (status StatusItem) GetPod() *corev1.Pod

GetPod returns the pod.

func (StatusItem) GetStatus

func (status StatusItem) GetStatus() (CometStatus, error)

GetStatus returns the CometBFT status or an error if the status could not be fetched.

func (StatusItem) Timestamp

func (status StatusItem) Timestamp() time.Time

Timestamp returns the time when the CometBFT status was fetched.

type Statuser

type Statuser interface {
	Status(ctx context.Context, rpcHost string) (CometStatus, error)
}

Statuser calls the RPC status endpoint.

type SyncInfo

type SyncInfo struct {
	LatestBlockHash     string    `json:"latest_block_hash"`
	LatestAppHash       string    `json:"latest_app_hash"`
	LatestBlockHeight   string    `json:"latest_block_height"`
	LatestBlockTime     time.Time `json:"latest_block_time"`
	EarliestBlockHash   string    `json:"earliest_block_hash"`
	EarliestAppHash     string    `json:"earliest_app_hash"`
	EarliestBlockHeight string    `json:"earliest_block_height"`
	EarliestBlockTime   time.Time `json:"earliest_block_time"`
	CatchingUp          bool      `json:"catching_up"`
}

type ValidatorInfo

type ValidatorInfo struct {
	Address string `json:"address"`
	PubKey  struct {
		Type  string `json:"type"`
		Value string `json:"value"`
	} `json:"pub_key"`
	VotingPower string `json:"voting_power"`
}

Jump to

Keyboard shortcuts

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