Documentation
¶
Index ¶
- Constants
- Variables
- func SliceContains[T ~string](slice []T, key T) bool
- type ActionType
- type Event
- type Events
- type Rule
- type RulePath
- func (rp RulePath) FindAnchorPath(changePath string) string
- func (rp RulePath) IsAnchorPath() bool
- func (rp RulePath) IsExactPath() bool
- func (rp RulePath) Len() int
- func (rp RulePath) Matches(changePath string) bool
- func (rp RulePath) Path() string
- func (rp RulePath) Validate() error
- func (rp RulePath) WildcardCount() int
- type RulePathSegment
- type RuleType
- type ValidationError
Constants ¶
const ( // PathSeparator divides a rule's path into distinct segments. PathSeparator = "." // PathTerminator is used to prevent rule path from being matched with // longer change paths. PathTerminator = "!" // OptionPrefix marks the beginning of an option block. OptionPrefix = "{" // OptionSuffix marks the end of an option block. OptionSuffix = "}" // OptionSeparator divides individual options within an option block. OptionSeparator = "," // Wildcard represents a flexible match, allowing any segment in the // change path to be matched. Wildcard = "*" // Anchor signifies that the resulting change should be derived from // this specific path segment, rather than the segment where the change // was actually detected. Anchor = "@" )
RulePath operators are reserved characters with distinct meanings in rule paths. Currently, these characters are not allowed outside their intended context.
Note: Spaces are automatically stripped from rule paths upon creation.
Variables ¶
var ModifyRules = []Rule{ { Type: Warn, MatchChangeType: cmp.Modify, MatchPath: NewRulePath("hosts.*.mainResourcePoolPath"), Message: "Changing main resource pool location will trigger recreation of all resources bound to that resource pool, such as virtual machines and data disks.", }, { Type: Warn, MatchChangeType: cmp.Delete, MatchPath: NewRulePath("hosts.*.dataResourcePools.*"), Message: "Removing data resource pool will destroy all the data on that location.", }, { Type: Warn, MatchChangeType: cmp.Modify, MatchPath: NewRulePath("hosts.*.dataResourcePools.*.path"), Message: "Changing data resource pool location will trigger recreation of all resources bound to that resource pool, such as virtual machines and data disks", }, { Type: Allow, MatchChangeType: cmp.Any, MatchPath: NewRulePath("hosts.*.dataResourcePools.*"), }, { Type: Error, MatchChangeType: cmp.Any, MatchPath: NewRulePath("cluster.network"), Message: "Once the cluster is created, further changes to the network properties are not allowed. Such action may render the cluster unusable.", }, { Type: Error, MatchChangeType: cmp.Any, MatchPath: NewRulePath("cluster.nodeTemplate"), Message: "Once the cluster is created, further changes to the nodeTemplate properties are not allowed. Such action may render the cluster unusable.", }, { Type: Error, MatchChangeType: cmp.Delete, MatchPath: NewRulePath("cluster.nodes.{master, worker, loadBalancer}.instances.@"), Message: "To remove existing nodes run apply command with '--action scale' flag.", }, { Type: Error, MatchChangeType: cmp.Create, MatchPath: NewRulePath("cluster.nodes.{master, worker, loadBalancer}.instances.@"), Message: "To add new nodes run apply command with '--action scale' flag.", }, { Type: Error, MatchChangeType: cmp.Any, MatchPath: NewRulePath("cluster.nodes.{master, worker, loadBalancer}.default.{cpu, ram, mainDiskSize}"), Message: "Changing any default physical properties of nodes (cpu, ram, mainDiskSize) is not allowed. Such action may render the cluster unusable.", }, { Type: Error, MatchChangeType: cmp.Modify, MatchPath: NewRulePath("cluster.nodes.{master, worker, loadBalancer}.instances.@.{cpu, ram, mainDiskSize}"), Message: "Changing any physical properties of nodes (cpu, ram, mainDiskSize) is not allowed. Such action will recreate the node.", }, { Type: Error, MatchChangeType: cmp.Modify, MatchPath: NewRulePath("cluster.nodes.{master, worker, loadBalancer}.instances.@.{ip, mac}"), Message: "Changing IP or MAC address of the node is not allowed. Such action may render the cluster unusable.", }, { Type: Warn, MatchChangeType: cmp.Modify, MatchPath: NewRulePath("cluster.nodes.{master, worker}.instances.*.dataDisks.*"), Message: "Changing data disk properties, will recreate the disk (removing all of its content in the process).", }, { Type: Warn, MatchChangeType: cmp.Delete, MatchPath: NewRulePath("cluster.nodes.{master, worker}.instances.*.dataDisks.*"), Message: "One or more data disks will be removed.", }, { Type: Allow, MatchChangeType: cmp.Any, MatchPath: NewRulePath("cluster.nodes.loadBalancer.forwardPorts.*"), }, { Type: Error, MatchChangeType: cmp.Any, MatchPath: NewRulePath("cluster.nodes.loadBalancer.vip"), Message: "Once the cluster is created, changing virtual IP (VIP) is not allowed. Such action may render the cluster unusable.", }, { Type: Allow, MatchChangeType: cmp.Any, MatchPath: NewRulePath("cluster.nodes.{master, worker, loadBalancer}.instances.*"), }, { Type: Error, MatchChangeType: cmp.Any, MatchPath: NewRulePath("kubernetes.version"), Message: "Changing Kubernetes is allowed only when upgrading the cluster.\nTo upgrade the cluster run apply command with '--action upgrade' flag.", }, { Type: Allow, MatchChangeType: cmp.Any, MatchPath: NewRulePath("addons"), }, { Type: Error, MatchChangeType: cmp.Any, MatchPath: NewRulePath("@"), Message: "Change is not allowed.", }, }
var ScaleRules = []Rule{ { Type: Allow, MatchChangeType: cmp.Delete, MatchPath: NewRulePath("cluster.nodes.worker.instances.@"), ActionType: Action_ScaleDown, }, { Type: Allow, MatchChangeType: cmp.Create, MatchPath: NewRulePath("cluster.nodes.worker.instances.@"), ActionType: Action_ScaleUp, }, { Type: Allow, MatchChangeType: cmp.Delete, MatchPath: NewRulePath("cluster.nodes.loadBalancer.instances.@"), ActionType: Action_ScaleDown, }, { Type: Allow, MatchChangeType: cmp.Create, MatchPath: NewRulePath("cluster.nodes.loadBalancer.instances.@"), ActionType: Action_ScaleUp, }, { Type: Error, MatchChangeType: cmp.Create, MatchPath: NewRulePath("cluster.nodes.master.instances.@"), Message: "Currently, control plane cannot be scaled.", }, { Type: Allow, MatchChangeType: cmp.Delete, MatchPath: NewRulePath("cluster.nodes.master.instances.@"), Message: "Currently, control plane cannot be scaled.", }, { Type: Allow, MatchChangeType: cmp.Create, MatchPath: NewRulePath("hosts.@"), }, { Type: Allow, MatchChangeType: cmp.Delete, MatchPath: NewRulePath("hosts.@"), }, { Type: Error, MatchChangeType: cmp.Any, MatchPath: NewRulePath("*"), Message: "Change is not allowed. Scale action allows only addition and removal of worker and load balancer nodes.", }, }
var UpgradeRules = []Rule{ { Type: Allow, MatchChangeType: cmp.Modify, MatchPath: NewRulePath("kubernetes.version"), }, { Type: Error, MatchChangeType: cmp.Any, MatchPath: NewRulePath("@"), Message: "Change is not allowed. Upgrade action allows changing only 'kubernetes.version'.", }, }
Functions ¶
func SliceContains ¶
SliceContains checks whether a slice of strings contains a given key.
Types ¶
type ActionType ¶
type ActionType string
const ( Action_ScaleUp ActionType = "scale_up" Action_ScaleDown ActionType = "scale_down" )
type Event ¶
type Event struct {
Rule Rule
Change cmp.Change
// Paths of changes that matched the rule.
MatchedChangePaths []string
}
Event represents a detected change and its associated rule.
func GenerateEvents ¶
GenerateEvents evaluates the changes from the comparison tree against the provided rules and returns a list of corresponding events. Each event encapsulates a matched change and its associated rule. A single change can match at most one rule and thus produce at most one event. Note that provided rules are validated prior the event generation.
type Events ¶
type Events []Event
Events abstracts Event list in order to provide more advance operations on the list.
func (Events) Filter ¶
Filter iterates over the events and returns a new slice of events containing only those events that satisfy the provided filter function (return true).
func (Events) FilterByAction ¶
func (events Events) FilterByAction(t ActionType) Events
FilterByActionType returns a new slice containing only those events that match the provided ActionType.
func (Events) FilterByRuleType ¶
FilterByRuleType returns a new slice containing only those events that match the provided RuleType.
type Rule ¶
type Rule struct {
Type RuleType
MatchPath RulePath
MatchChangeType cmp.ChangeType
// Optional fields.
ActionType ActionType
Message string
}
Rule defines the conditions that trigger events based on the detected changes. It specifies the rule's type, the type and path of a change it observes. Other fields are optional and can be used for further event processing.
type RulePath ¶
type RulePath struct {
// contains filtered or unexported fields
}
RulePath represents a rule's path, broken down into individual segments. It provides utilities for matching and processing change paths.
func NewRulePath ¶
NewRulePath constructs a new rule path from a given string path. It parses the individual segments from the path and sets path's metadata.
func (RulePath) FindAnchorPath ¶
FindAnchorPath extracts the portion of the change path that corresponds to the anchor in the rule path. If no anchor exists in the rule path, it returns the entire change path.
func (RulePath) IsAnchorPath ¶
IsAnchorPath indicates whether the rule path contains an anchor segment.
func (RulePath) IsExactPath ¶
IsExactPath indicates if the rule path is terminated, ensuring an exact match.
func (RulePath) Matches ¶
Matches determines if the rule path matches with a given change path by comparing each segment of the rule path with the corresponding segment of the change path.
func (RulePath) Validate ¶
Validate ensures the rule path follows the expected format and constraints.
func (RulePath) WildcardCount ¶
WildcardCount returns the number of wildcard segments in the rule path.
type RulePathSegment ¶
type RulePathSegment struct {
// contains filtered or unexported fields
}
RulePathSegment represents a segment within a rule path. It can be a simple string, a wildcard, an anchor, or contain multiple options.
func NewRulePathSegment ¶
func NewRulePathSegment(path string) RulePathSegment
NewRulePathSegment constructs a new rule path segment from a given path. It parses segment options if they exist, and evaluates whether the segment is a wildcard or/and an anchor.
func (RulePathSegment) ContainsOption ¶
func (rps RulePathSegment) ContainsOption(o string) bool
ContainsOption checks if the rule path segment contains a given option.
func (RulePathSegment) IsAnchor ¶
func (rps RulePathSegment) IsAnchor() bool
IsAnchor checks if the rule path segment is an anchor.
func (RulePathSegment) IsWildcard ¶
func (rps RulePathSegment) IsWildcard() bool
IsWildcard checks if the rule path segment is a wildcard.
func (RulePathSegment) Matches ¶
func (rps RulePathSegment) Matches(changePathSeg string) bool
Matches checks if the rule path segment matches a given change path segment. A match occurs if the segments are identical, the rule path segment is a wildcard, or the change path segment matches any options in the rule segment.
func (RulePathSegment) Validate ¶
func (rps RulePathSegment) Validate() error
Validate checks the validity of a rule path segment and returns an error if invalid.
type RuleType ¶
type RuleType uint8
RuleType represents the priority or significance of a rule. A rule type with a higher value indicates greater importance. However, the 'Ignore' rule type is a special case and is always treated with the highest priority.
const ( // Allow is a normal rule type used for safe operations that do not // require any user confirmation. Allow RuleType = 0 // Warn is a rule type used for potentially dangerous operations that // should request user permission in order to proceed. Warn RuleType = 100 // Error is a rule type that should prevent any further actions. Error RuleType = 200 // Ignore is a rule type used for ignoring specific changes. Ignore RuleType = 255 )
type ValidationError ¶
type ValidationError struct {
// contains filtered or unexported fields
}
ValidationError represents an error that occurs during the validation of the rule related object. It contains a descriptive message about the nature of the validation error and the object that caused the error.
func NewValidationError ¶
func NewValidationError(o any, errFmt string, errArgs ...any) ValidationError
func (ValidationError) Error ¶
func (e ValidationError) Error() string