controller

package
v0.0.0-...-a216e23 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2016 License: Apache-2.0 Imports: 16 Imported by: 1

Documentation

Overview

Package controller implements a controller client providing basic operations against a controller endpoint through controller's HTTP API. Higher level scheduling and management should be built on top of that.

Index

Constants

This section is empty.

Variables

View Source
var (
	// StatusIndex represents the aggregated states of a unit.
	StatusIndex = []StatusContext{
		{
			FleetCurrent:  "inactive",
			FleetDesired:  "launched",
			SystemdActive: "active",
			SystemdSub:    "running",
			Aggregated:    StatusRunning,
		},
		{
			FleetCurrent:  "inactive",
			FleetDesired:  "*",
			SystemdActive: "*",
			SystemdSub:    "*",
			Aggregated:    StatusStopped,
		},
		{
			FleetCurrent:  "loaded|launched",
			FleetDesired:  "*",
			SystemdActive: "inactive",
			SystemdSub:    "*",
			Aggregated:    StatusStopped,
		},
		{
			FleetCurrent:  "loaded|launched",
			FleetDesired:  "*",
			SystemdActive: "failed",
			SystemdSub:    "*",
			Aggregated:    StatusFailed,
		},
		{
			FleetCurrent:  "loaded|launched",
			FleetDesired:  "*",
			SystemdActive: "activating",
			SystemdSub:    "*",
			Aggregated:    StatusStarting,
		},
		{
			FleetCurrent:  "loaded|launched",
			FleetDesired:  "*",
			SystemdActive: "deactivating",
			SystemdSub:    "*",
			Aggregated:    StatusStopping,
		},
		{
			FleetCurrent:  "loaded|launched",
			FleetDesired:  "*",
			SystemdActive: "active|reloading",
			SystemdSub:    "stop-sigterm|stop-post|stop",
			Aggregated:    StatusStopping,
		},
		{
			FleetCurrent:  "loaded|launched",
			FleetDesired:  "*",
			SystemdActive: "active|reloading",
			SystemdSub:    "auto-restart|launched|start-pre|start-pre|start-post|start|dead",
			Aggregated:    StatusStarting,
		},
		{
			FleetCurrent:  "loaded|launched",
			FleetDesired:  "*",
			SystemdActive: "active|reloading",
			SystemdSub:    "exited|running",
			Aggregated:    StatusRunning,
		},
	}
)

Functions

func IsAtInGroupNameError

func IsAtInGroupNameError(err error) bool

IsAtInGroupNameError returns true if the given error cause is atInGroupNameError.

func IsBadUnitPrefix

func IsBadUnitPrefix(err error) bool

IsBadUnitPrefix returns true if the given error cause is badUnitPrefixError.

func IsGroupsArePrefix

func IsGroupsArePrefix(err error) bool

IsGroupsArePrefix returns true if the given error cause is groupsArePrefixError.

func IsGroupsSameName

func IsGroupsSameName(err error) bool

IsGroupsSameName returns true if the given error cause is groupsSameNameError.

func IsInvalidArgument

func IsInvalidArgument(err error) bool

IsInvalidArgument checks whether the given error indicates a invalid argument to the operation that was performed.

func IsInvalidSubmitRequestNoSliceIDsGiven

func IsInvalidSubmitRequestNoSliceIDsGiven(err error) bool

IsInvalidSubmitRequestNoSliceIDsGiven returns true if the given error cause is invalidSubmitRequestNoSliceIDsGivenError.

func IsInvalidSubmitRequestSlicesGiven

func IsInvalidSubmitRequestSlicesGiven(err error) bool

IsInvalidSubmitRequestSlicesGiven returns true if the given error cause is invalidSubmitRequestSlicesGivenError.

func IsInvalidUnitStatus

func IsInvalidUnitStatus(err error) bool

IsInvalidUnitStatus checks whether the given error indicates the problem of an unexpected unit status response. In case you want to lookup the state of a unit that cannot be found on any machine or exists multiple times (what should never happen), an error that you can identify using this method is returned.

func IsMixedSliceInstance

func IsMixedSliceInstance(err error) bool

IsMixedSliceInstance returns true if the given error cause is mixedSliceInstanceError.

func IsMultipleAtInUnitName

func IsMultipleAtInUnitName(err error) bool

IsMultipleAtInUnitName returns true if the given error cause is multipleAtInUnitNameError.

func IsNoUnitsInGroup

func IsNoUnitsInGroup(err error) bool

IsNoUnitsInGroup returns true if the given error cause is noUnitsInGroupError.

func IsUnitNotFound

func IsUnitNotFound(err error) bool

IsUnitNotFound checks whether the given error indicates the problem of an unit being found or not. In case you want to lookup the state of a group and no unit of it can be found on any machine, an error that you can identify using this method is returned.

func IsUnitSliceNotFound

func IsUnitSliceNotFound(err error) bool

IsUnitSliceNotFound checks whether the given error indicates the problem of an unit slice being found or not. In case you want to lookup the state of a group and some unit of this group cannot be found on any machine, an error that you can identify using this method is returned.

func IsUnitsAlreadyUpToDate

func IsUnitsAlreadyUpToDate(err error) bool

IsUnitsAlreadyUpToDate asserts unitsAlreadyUpToDate.

func IsUnitsSameName

func IsUnitsSameName(err error) bool

IsUnitsSameName returns true if the given error cause is unitsSameNameError.

func IsUpdateFailed

func IsUpdateFailed(err error) bool

IsUpdateFailed asserts updateFailedError.

func IsUpdateNotAllowed

func IsUpdateNotAllowed(err error) bool

IsUpdateNotAllowed asserts updateNotAllowedError.

func IsWaitTimeoutReached

func IsWaitTimeoutReached(err error) bool

IsWaitTimeoutReached asserts waitTimeoutReachedError.

func NewID

func NewID() string

NewID creates a new ID.

func StringsCountMoreThan

func StringsCountMoreThan(s []string, c string, n int) bool

StringsCountMoreThan returns true if any of the strings in s contain more than n occurrences of c, false otherwise.

func StringsHasPrefix

func StringsHasPrefix(s []string, p string) bool

StringsHasPrefix returns true if all of the strings have the given prefix, false otherwise.

func StringsHaveOrNot

func StringsHaveOrNot(s []string, c string) bool

StringsHaveOrNot returns true if all strings in s either have an occurrence of c, or do not have any occurrence of c. In another way, it returns false if only some strings in s have an occurrence of c.

func StringsSharePrefix

func StringsSharePrefix(s []string) bool

StringsSharePrefix returns true if any of the strings are prefixes of another, false otherwise.

func StringsUnique

func StringsUnique(s []string) bool

StringsUnique returns true if all strings in the list are unique, false otherwise.

func ValidateMultipleRequest

func ValidateMultipleRequest(requests []Request) (bool, error)

ValidateMultipleRequest takes a list of Requests, and returns whether they are valid together or not. If the requests are not valid, the error returned provides more details.

func ValidateRequest

func ValidateRequest(request Request) (bool, error)

ValidateRequest takes a Request, and returns whether it is valid or not. If the request is not valid, the error provides more details.

func ValidateSubmitRequest

func ValidateSubmitRequest(request Request) (bool, error)

ValidateSubmitRequest validates that the given request contains no SliceIDs. Otherwise it is identical to ValidateRequest().

Types

type Aggregator

type Aggregator struct {
	Logger logging.Logger
}

Aggregator provides methods for aggregating status.

func (Aggregator) AggregateStatus

func (a Aggregator) AggregateStatus(fc, fd, sa, ss string) (Status, error)

AggregateStatus aggregates the given fleet and systemd states to a Status known to Inago based on the StatusIndex.

fc: fleet current state
fd: fleet desired state
sa: systemd active state
ss: systemd sub state

func (Aggregator) UnitHasStatus

func (a Aggregator) UnitHasStatus(us fleet.UnitStatus, statuses ...Status) (bool, error)

UnitHasStatus determines if a given unit's status is effectivly equal to a set of given statuses. This method provides status mapping of AggregateStatus and compares the result with the given set of statuses.

type Config

type Config struct {
	Fleet fleet.Fleet

	TaskService task.Service

	// WaitCount represents the amount of times a desired status is required to
	// be seen to interpret it as final. E.g. when WaitCount is 3 and you start a
	// group, all statuses of units of that group need to be seen as "running" 3
	// times in a row.
	WaitCount int

	// WaitSleep represents the time to sleep between status-check cycles.
	WaitSleep time.Duration

	// WaitTimeout represents the maximum time to wait to reach a certain
	// status. When the desired status was not reached within the given period of
	// time, the wait ends.
	WaitTimeout time.Duration

	// Logger provides an initialised logger.
	Logger logging.Logger
}

Config provides all necessary and injectable configurations for a new controller.

func DefaultConfig

func DefaultConfig() Config

DefaultConfig provides a set of configurations with default values by best effort.

type Controller

type Controller interface {
	ExtendWithExistingSliceIDs(req Request) (Request, error)

	// GroupNeedsUpdate checks if the given group should be updated or not. To
	// make a decision the unit content of each unit of each slice is compared
	// using its unit hash. As soon as one unit hash differs, or a unit cannot be
	// found, Inago assumes the whole group slice to be "dirty" and returns true
	// having the group slices removed from the given req that are up to date,
	// otherwise false, leaving the req as it is.
	GroupNeedsUpdate(ctx context.Context, req Request) (Request, bool, error)

	// Submit schedules a group on the configured fleet cluster. This is done by
	// setting the state of the units in the group to loaded.
	// If req.DesiredSlices is positive, new random (non conflicting) SliceIDs will be generated.
	// Otherwise the given req.SliceIDs will be used. Only one of those options can be used.
	Submit(ctx context.Context, req Request) (*task.Task, error)

	// Start starts a group on the configured fleet cluster. This is done by
	// setting the state of the units in the group to launched.
	Start(ctx context.Context, req Request) (*task.Task, error)

	// Stop stops a group on the configured fleet cluster. This is done by
	// setting the state of the units in the group to loaded.
	Stop(ctx context.Context, req Request) (*task.Task, error)

	// Destroy delets a group on the configured fleet cluster. This is done by
	// setting the state of the units in the group to inactive.
	Destroy(ctx context.Context, req Request) (*task.Task, error)

	// GetStatus fetches the current status of a group. If the unit cannot be
	// found, an error that you can identify using IsUnitNotFound is returned.
	GetStatus(ctx context.Context, req Request) ([]fleet.UnitStatus, error)

	// WaitForStatus waits for a group to reach the given status.
	WaitForStatus(ctx context.Context, req Request, closer <-chan struct{}, desiredStatuses ...Status) error

	// WaitForTask waits for the given task to reach a final status. Once the
	// given task has reached the final status, the final task representation is
	// returned.
	WaitForTask(ctx context.Context, taskID string, closer <-chan struct{}) (*task.Task, error)

	// Update updates the given group on best effort with respect to the given
	// opts. The given req identifies the group to update. The given options
	// define the strategy used to update the given group. See also
	// UpdateOptions.
	Update(ctx context.Context, req Request, opts UpdateOptions) (*task.Task, error)
}

Controller defines the interface a controller needs to implement to provide operations for groups of unit files against a fleet cluster.

func NewController

func NewController(config Config) Controller

NewController creates a new Controller that is configured with the given settings.

newConfig := controller.DefaultConfig()
newConfig.Fleet = myCustomFleetClient
newController := controller.NewController(newConfig)

type Request

type Request struct {
	RequestConfig

	// Units represents a list of unit files that is supposed to be extended
	// using the provided slice IDs.
	Units []Unit

	// DesiredSlices defines the number of random sliceIDs that should be generated
	// when submitting new groups.
	DesiredSlices int
}

Request represents a controller request. This is used to process some action on the controller.

func NewRequest

func NewRequest(config RequestConfig) Request

NewRequest returns a Request, given a RequestConfig.

func (Request) ExtendSlices

func (r Request) ExtendSlices() (Request, error)

ExtendSlices extends unit files with respect to the given slice IDs. Having slice IDs "1" and "2" and having unit files "foo@.service" and "bar@.service" results in the following extended unit files.

foo@1.service
bar@1.service
foo@2.service
bar@2.service

type RequestConfig

type RequestConfig struct {
	// Group represents the plain group name without any slice expression.
	Group string

	// SliceIDs contains the IDs to create. IDs can be "1", "first", "whatever",
	// "5", etc..
	SliceIDs []string
}

RequestConfig represents the configuration of a Request.

func DefaultRequestConfig

func DefaultRequestConfig() RequestConfig

DefaultRequestConfig returns a RequestConfig by best effort.

type Status

type Status string

Status represents the current status of a unit.

var (
	// StatusFailed represents a unit being failed.
	StatusFailed Status = "failed"

	// StatusNotFound represents a unit not being found.
	StatusNotFound Status = "not-found"

	// StatusRunning represents a unit running.
	StatusRunning Status = "running"

	// StatusStarting represents a unit starting.
	StatusStarting Status = "starting"

	// StatusStopped represents a unit that has stopped.
	StatusStopped Status = "stopped"

	// StatusStopping represents a unit stopping.
	StatusStopping Status = "stopping"
)

type StatusContext

type StatusContext struct {
	FleetCurrent  string
	FleetDesired  string
	SystemdActive string
	SystemdSub    string
	Aggregated    Status
}

StatusContext represents a units status from fleet and systemd.

type Unit

type Unit struct {
	// Name is something like "appd@.service". It needs to be extended using the
	// slice ID before submitting to fleet.
	Name string

	// Content represents normal systemd unit file content.
	Content string
}

Unit represents a systemd unit file.

type UnitStatusList

type UnitStatusList []fleet.UnitStatus

UnitStatusList represents a list of UnitStatus.

func (UnitStatusList) Group

func (usl UnitStatusList) Group() (UnitStatusList, error)

Group returns a shortened version of usl where equal status are replaced by one UnitStatus where the Name is replaced with "*".

type UpdateOptions

type UpdateOptions struct {
	// MaxGrowth represents the number of groups allowed to be added at a given
	// time. No more than MaxGrowth groups will be added at the same time during
	// the update process.
	MaxGrowth int

	// MinAlive represents the number of groups required to stay healthy during
	// the update process. No more than MinAlive groups will be removed at the
	// same time during the update process.
	MinAlive int

	// ReadySecs represents the number of seconds required to wait before ending
	// the update process of one group and starting the update process of another
	// group. This is basically a cool down where the update process sleeps
	// before updating the next group.
	ReadySecs int
}

UpdateOptions represents the options defining the strategy of an update process. Lets have a look at how the update process of 3 group slices would look like using the given options.

MaxGrowth    1
MinAlive     2
ReadySecs    30

@1 (running)  ->  @1 (stopped/destroyed)
@2 (running)  ->  @2 (running)            ->  @2 (stopped/destroyed)
@3 (running)  ->  @3 (running)            ->  @3 (running)            ->  @3 (stopped/destroyed)
              ->  @1 (submitted/running)  ->  @1 (running)            ->  @1 (running)            ->  @1 (running)
                                          ->  @2 (submitted/running)  ->  @2 (running)            ->  @2 (running)
                                                                      ->  @3 (submitted/running)  ->  @3 (running)

type ValidationError

type ValidationError struct {
	CausingErrors []error
}

ValidationError capsules validation errors into one error struct. It is returned when the validation fails. CausingErrors contains all errors that occurred, while validating the request.

func (*ValidationError) Add

func (e *ValidationError) Add(err error)

Add adds the given error to the list of validation errors

func (*ValidationError) Contains

func (e *ValidationError) Contains(checker func(error) bool) bool

Contains returns true if the given error is present in the ValidationError

func (ValidationError) Error

func (e ValidationError) Error() string

Jump to

Keyboard shortcuts

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