Documentation
¶
Index ¶
- func DelItemFrom(graph Graph, item ItemRef, path SubGraphPath) bool
- func PutItemInto(graph Graph, item Item, state ItemState, path SubGraphPath) bool
- type Dependency
- type DependencyAttributes
- type DotExporter
- type Edge
- type EdgeIterator
- type Graph
- type GraphIterator
- type GraphR
- type InitArgs
- type Item
- type ItemIterator
- type ItemRef
- type ItemState
- type ItemWithState
- type Iterator
- type SubGraphPath
- func (p SubGraphPath) Append(elems ...string) SubGraphPath
- func (p SubGraphPath) Compare(p2 SubGraphPath) int
- func (p SubGraphPath) Concatenate(p2 SubGraphPath) SubGraphPath
- func (p SubGraphPath) IsPrefixOf(p2 SubGraphPath) bool
- func (p SubGraphPath) Len() int
- func (p SubGraphPath) TrimPrefix(prefix SubGraphPath) SubGraphPath
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DelItemFrom ¶
func DelItemFrom(graph Graph, item ItemRef, path SubGraphPath) bool
DelItemFrom is a helper which allows to remove item from the selected subgraph. Returns true if the path refers to an existing subgraph and the item existed and was successfully removed, false otherwise.
func PutItemInto ¶
func PutItemInto(graph Graph, item Item, state ItemState, path SubGraphPath) bool
PutItemInto is a helper which allows to add or move (and update) item into the selected subgraph. Returns true if the path refers to an existing subgraph and the item was successfully put, false otherwise.
Types ¶
type Dependency ¶
type Dependency struct { // RequiredItem references item which must be already created. RequiredItem ItemRef // MustSatisfy : used if the required item must not only exist but also satisfy // a certain condition. For example, a network route may depend on a specific network // interface to exist and also to have a specific IP address assigned. MustSatisfy can // check for the presence of the IP address. // This function may get called quite often (by Reconciler) so keep it lightweight. MustSatisfy func(Item) bool // Description : optional description of the dependency. Description string // Attributes : some additional attributes that may be helpful in special cases // to further describe a dependency. Attributes DependencyAttributes }
Dependency which is considered satisfied if RequiredItem is already created and MustSatisfy returns true for that item or is nil.
type DependencyAttributes ¶
type DependencyAttributes struct { // RecreateWhenModified : items that have this dependency should be recreated whenever // the required item changes (through Modify). RecreateWhenModified bool // AutoDeletedByExternal : items that have this dependency are automatically/externally // deleted (by other agents or by the managed system itself) whenever the required // *external* item is deleted. If the required item is not external (Item.External() // returns false), this dependency attribute should be ignored. AutoDeletedByExternal bool }
DependencyAttributes : some additional attributes that may be helpful in special cases to further describe a dependency.
type DotExporter ¶
type DotExporter struct { // CheckDeps : enable this option to have the dependencies checked // and edges colored accordingly (black vs. red). CheckDeps bool // contains filtered or unexported fields }
DotExporter exports dependency graph into DOT 1. It provides two methods:
- Export(graph GraphR): export a single graph into DOT
- ExportTransition(src, dst GraphR): export graph "src" into DOT just like with Export(), but additionally also describe what is out-of-sync and will need a state transition to match the graph "dst". For example, all items present in "dst" but missing in "src" are also included, but with a lowered saturation for node fillcolor and with a grey border.
func (*DotExporter) Export ¶
func (e *DotExporter) Export(graph GraphR) (dot string, err error)
Export returns DOT description of the graph content. This can be visualized with Graphviz and used for troubleshooting/presentation purposes.
func (*DotExporter) ExportTransition ¶
func (e *DotExporter) ExportTransition(src, dst GraphR) (dot string, err error)
ExportTransition exports graph "src" into DOT just like with Export(), but additionally also describes what is out-of-sync and will need a state transition to match the graph "dst".
type Edge ¶
type Edge struct { FromItem ItemRef ToItem ItemRef // Dependency represented by this edge. Dependency Dependency }
Edge represents a directed edge of a dependency graph.
type EdgeIterator ¶
type EdgeIterator interface { Iterator // Edge returns the current Edge from the iterator. Edge() Edge }
EdgeIterator iterates outgoing or incoming edges of an item. The order of edges is undefined.
type Graph ¶
type Graph interface { GraphR // SetDescription updates description assigned to the (sub)graph. SetDescription(string) // PutItem adds or moves (and updates) item into this (sub)graph. // Function also adds or updates ItemState stored alongside the item. PutItem(item Item, state ItemState) // DelItem deletes an existing item from this (sub)graph. // Returns true if the item existed and was actually deleted. DelItem(ItemRef) bool // PutSubGraph adds a new subgraph into this graph or updates an existing // subgraph. This refers to a direct child of this graph, cannot add/update // a nested subgraphs. PutSubGraph(Graph) // DelSubGraph deletes existing subgraph. This refers to a direct child of this // graph, cannot delete a nested subgraph. // Returns true if the subgraph existed and was actually deleted. // It is an error to try to use a subgraph after it was deleted (can't be used // even as a separate graph anymore). DelSubGraph(name string) bool // EditSubGraph elevates read-only subgraph handle to read-write access. // Panics if the given graph is not actually a subgraph (direct or nested) // of this graph. EditSubGraph(GraphR) Graph // EditParentGraph returns read-write handle to a (direct) parent graph // of this subgraph. // Return nil if the graph is not a subgraph. EditParentGraph() Graph // PutPrivateData allows the user to store any data with the graph. // Graph does not do anything with these data. // Retrieve with GraphR.PrivateData(). PutPrivateData(interface{}) }
Graph is a dependency graph. The main use-case is to represent configuration items (network interfaces, routes, volumes, etc.) or any managed stateful objects (incl. processes, containers, files, etc.) as graph nodes (here called items instead) and their dependencies as directed graph edges. For more information please see README.md.
func GetGraphRoot ¶
GetGraphRoot is a simple helper which returns the top-most parent graph for a given (sub)graph.
func GetSubGraph ¶
func GetSubGraph(graph Graph, path SubGraphPath) Graph
GetSubGraph is a simple helper which allows to obtain subgraph by a relative path (which is for example returned by GraphR.Item()).
type GraphIterator ¶
type GraphIterator interface { Iterator // SubGraph returns the current subgraph from the iterator. SubGraph() GraphR }
GraphIterator iterates subgraphs of a graph. The order of subgraphs is undefined.
type GraphR ¶
type GraphR interface { // Name assigned to the (sub)graph. Name() string // Description assigned to the (sub)graph. Description() string // Item returns an item from the graph, incl. state data stored alongside it. // The function will look for the item also inside all the subgraphs // (both direct and nested). If found, it will also return a path leading // to the subgraph with the item. // To obtain reference to the subgraph, use GetSubGraph(). Item(ItemRef) (item Item, state ItemState, path SubGraphPath, found bool) // Items returns an iterator for items inside this graph. // If inclSubGraphs is set to true, the iteration will include items // from subgraphs (both direct and nested). Items(inclSubGraphs bool) ItemIterator // DiffItems returns references to items that differ between this and the other // graph. Two respective item instances are considered different if Item.Equal(other) // returns false, or if their location wrt. subgraphs is different. // Item state data are not compared. // A returned reference may refer to an item present in this graph but not present // in the other graph and vice versa. // otherGraph is allowed to be nil - in that case references to all items in this // graph will be returned. // Complexity is O(V). DiffItems(otherGraph GraphR) []ItemRef // SubGraph returns a read-only handle to a (direct, not nested) subgraph. // Returns nil if subgraph with such name is not present. SubGraph(name string) GraphR // SubGraphs returns an iterator for (direct) subgraphs of this graph. SubGraphs() GraphIterator // SubGraph returns a read-only handle to the (direct) parent graph. // Return nil if the graph is not a subgraph. ParentGraph() GraphR // ItemAsSubGraph allows to view an item (that may or may not exist) // as a single-item subgraph. // This is useful if you need a common interface for a subgraph and an item. ItemAsSubGraph(ItemRef) GraphR // OutgoingEdges returns iterator for all outgoing edges of the given item, // as determined by item dependencies. // Item can be also from a subgraph (direct or nested). OutgoingEdges(ItemRef) EdgeIterator // OutgoingEdges returns iterator for all incoming edges of the given item, // as determined by dependencies of other items. // Item can be also from a subgraph (direct or nested). IncomingEdges(ItemRef) EdgeIterator // DetectCycle checks if the graph contains a cycle (which it should not, // dependency graph is supposed to be DAG) and the first one found is returned // as a list of references to items inside the cycle (with the order of the cycle). // Complexity is O(V+E). DetectCycle() []ItemRef // PrivateData returns whatever custom data has the user stored with the graph. PrivateData() interface{} }
GraphR : Read-only access to a dependency graph.
func GetGraphRootR ¶
GetGraphRootR is a read-only variant for GetGraphRoot.
func GetSubGraphR ¶
func GetSubGraphR(graph GraphR, path SubGraphPath) GraphR
GetSubGraphR is a read-only variant for GetSubGraph.
type InitArgs ¶
type InitArgs struct { // Name of the graph. Name string // Description for the graph. Description string // ItemsWithState : items inside the graph with state data attached. ItemsWithState []ItemWithState // Items : items inside the graph without state data attached. // Use this instead of ItemsWithState to avoid passing ItemState as nil. // This makes the code shorter and easier to read. // But do not put the same Item into both Items and ItemsWithState. Items []Item // List of subgraphs directly under this graph. Subgraphs []InitArgs // PrivateData for the user of the graph to store anything. PrivateData interface{} }
InitArgs : input arguments to use with the (sub)graph constructor New().
type Item ¶
type Item interface { // Name should return a unique string identifier for the item instance. // It is required for the name to be unique only within item instances of the // same type (see Type()). A globally unique item identifier is therefore // a combination of the item type and the item name. Name() string // Label is an optional alternative name that does not have to be unique. // It is only used in the graph visualization as the label for the graph node // that represents the item. If empty string is returned, Item.Name() is used // for labeling instead. Label() string // Type should return the name of the item type. // This is something like reflect.TypeOf(item).Name(), but potentially much more // human-readable. // For example, type could be "Linux bridge". Type() string // Equal compares this and the other item instance (of the same type and name) // for equivalency. For the purposes of state reconciliation (see libs/reconciler), // Equal determines if the current and the new intended state of an item is equal, // or if a state transition is needed. // Note that if two items are equal, their dependencies should be the same! Equal(Item) bool // External should return true for items which are not managed (created/modified/deleted) // by the caller/owner. This could be used for items created by other management agents // or to represent system notifications (e.g. interface link is up). // For reconciliation, the presence of external items in the graph is used only for // dependency purposes (e.g. create A only after another microservice created B). External() bool // String should return a human-readable description of the item instance. // (e.g. a network interface configuration) String() string // Dependencies returns a list of all dependencies that have to be satisfied before // the item can be created (i.e. dependencies in the returned list are AND-ed). // Should be empty for external item (see Item.External()). Dependencies() []Dependency }
Item is something that can be created, modified and deleted, essentially a stateful object. This could be for example a network interface, volume instance, configuration file, etc. In this dependency graph, each item instance makes one graph node. Beware that items are stored inside the graph and their content should not change in any other way than through the Graph APIs. It is recommended to implement the Item interface with *value* receivers (or alternatively pass *copied* item values to the graph).
type ItemIterator ¶
type ItemIterator interface { Iterator // Item returns the current Item from the iterator. Item() (Item, ItemState) }
ItemIterator iterates items of a graph. Items are ordered lexicographically first by subgraphs (in DFS order) and secondly by item references.
type ItemRef ¶
ItemRef is used to uniquely reference item inside the graph.
type ItemState ¶
type ItemState interface { // String should return a human-readable description of the item state. String() string // IsCreated should return true if the item is actually created. // Return false to model a scenario such as item not being created due to // a missing dependency, or failing to get created, etc. IsCreated() bool // WithError should return non-nil error if the last state transition // for this item failed. The error should describe why the item is in a failed // state. WithError() error // InTransition should return true if an item state transition is in progress. InTransition() bool }
ItemState should store state information for an item instance. This can be used for state reconciliation purposes for example. It is used by the Reconciler (see libs/reconciler). Beware that items are stored inside the graph and their content should not change in any other way than through the Graph APIs. It is recommended to implement the ItemState interface with *value* receivers (or alternatively pass *copied* values to the graph).
type ItemWithState ¶
ItemWithState just wraps item with its state data together under one struct. Only used with InitArgs.
type Iterator ¶
type Iterator interface { // Next advances the iterator and returns whether the next call // to the Item()/Edge()/... method will return a non-nil value. // Next should be called prior to any call to the iterator's // item retrieval method after the iterator has been obtained or reset. Next() bool // Len returns the number of items remaining in the iterator. Len() int // Reset returns the iterator to its start position. Reset() }
Iterator : a common iterator interface. Note that it is undefined what happens if the iterated set is changed during iteration! Do not add/remove item during iteration.
type SubGraphPath ¶
type SubGraphPath struct {
// contains filtered or unexported fields
}
SubGraphPath is a relative path from a graph to one of its subgraphs (direct or a nested one).
func NewSubGraphPath ¶
func NewSubGraphPath(subGraphNames ...string) SubGraphPath
NewSubGraphPath is a constructor for SubGraphPath. The path is built by listing the names of subgraphs, each being a child of the previous one, leading to a destination subgraph (the last entry).
func (SubGraphPath) Append ¶
func (p SubGraphPath) Append(elems ...string) SubGraphPath
Append creates a *new* path with added elements at the end.
func (SubGraphPath) Compare ¶
func (p SubGraphPath) Compare(p2 SubGraphPath) int
Compare returns an integer comparing two paths lexicographically. The result will be 0 if p==p2, -1 if p < p2, and +1 if p > p2. This allows you to have an ordering for a list of subgraph paths.
func (SubGraphPath) Concatenate ¶
func (p SubGraphPath) Concatenate(p2 SubGraphPath) SubGraphPath
Concatenate creates a *new* path by concatenating this path with another path.
func (SubGraphPath) IsPrefixOf ¶
func (p SubGraphPath) IsPrefixOf(p2 SubGraphPath) bool
IsPrefixOf returns true if this path is prefix of the other path.
func (SubGraphPath) Len ¶
func (p SubGraphPath) Len() int
Len returns the path length (the number of nested subgraphs along the way).
func (SubGraphPath) TrimPrefix ¶
func (p SubGraphPath) TrimPrefix(prefix SubGraphPath) SubGraphPath
TrimPrefix returns a *new* SubGraphPath which has the given prefix removed from this path.