core

package
v0.0.0-...-f82a63c Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoConfigurationAllowed is returned by AssetManager.CheckResourceAllowed()
	// when the user has given configuration, but the asset type in question does
	// not accept any configuration.
	ErrNoConfigurationAllowed = errors.New("no configuration allowed for this asset type")
	// ErrNoConfigurationProvided is returned by
	// AssetManager.CheckResourceAllowed() when the user has not given
	// configuration, but the asset type in question requires configuration.
	ErrNoConfigurationProvided = errors.New("type-specific configuration must be provided for this asset type")
)
View Source
var AssetManagerRegistry pluggable.Registry[AssetManager]

AssetManagerRegistry is a pluggable.Registry for AssetManager implementations.

Functions

func ApplyResourceSpecInto

func ApplyResourceSpecInto(res *db.Resource, spec castellum.Resource, existingResources map[db.AssetType]struct{}, cfg Config, team AssetManagerTeam) (errs errext.ErrorSet)

ApplyResourceSpecInto validates the configuration in the given resource specification (which can either come from the API or from a seed file) and applies it in-place into the given db.Resource record.

The `existingResources` argument shall contain all resources currently existing in the DB with the same scope UUID (including `res` itself, if it refers to a pre-existing resource).

For new resources, a fresh `res` shall be given that shall only be filled with an AssetType and ScopeUUID.

func CountStateTransition

func CountStateTransition(res db.Resource, assetUUID string, from, to castellum.OperationState)

CountStateTransition must be called whenever an operation changes to a different state.

func GetEligibleOperations

func GetEligibleOperations(res ResourceLogic, asset AssetStatus) map[castellum.OperationReason]uint64

GetEligibleOperations calculates which resizing operations the given asset (within the given resource) is eligible for. In the result, each key-value pair means that the asset has crossed the threshold `key` and thus should be resized to `value`.

func GetMultiUsagePercent

func GetMultiUsagePercent(size uint64, usage castellum.UsageValues) castellum.UsageValues

GetMultiUsagePercent is like GetUsagePercent, but converts multiple usage values at once.

func GetUsagePercent

func GetUsagePercent(size uint64, usage float64) float64

GetUsagePercent calculates `100 * usage / size`, but has additional logic for some corner cases like size = 0.

func Identifier

func Identifier(metric castellum.UsageMetric, format string) string

Identifier inserts the metric name into the given format string, but returns the empty string for SingularUsageMetric. It is used when building log messages.

Types

type AssetManager

type AssetManager interface {
	pluggable.Plugin

	// Init is called before all other interface methods, and can be used by the
	// AssetManager to receive a reference to the ProviderClient, as well as
	// perform any first-time initialization.
	//
	// The supplied ProviderClient should be stored inside the AssetManager
	// instance for later usage and/or used to query OpenStack capabilities.
	Init(provider ProviderClient) error

	// If this asset type is supported by this asset manager, return information
	// about it. Otherwise return nil.
	InfoForAssetType(assetType db.AssetType) *AssetTypeInfo

	// A non-nil return value makes the API deny any attempts to create a resource
	// with that scope and asset type with that error.
	//
	// This can perform multiple types of validations:
	//- allowing resources for some scopes, but not others (e.g. only projects
	//  with a specific marker)
	//- validating plugin-specific configuration in `configJSON`
	//- allowing resources depending on which other resources exist in the same
	// scope, by checking `existingResources`
	//
	// Simple implementations should return nil for empty `configJSON` and
	// `core.ErrNoConfigurationAllowed` otherwise.
	CheckResourceAllowed(assetType db.AssetType, scopeUUID string, configJSON string, existingResources map[db.AssetType]struct{}) error

	ListAssets(ctx context.Context, res db.Resource) ([]string, error)
	// The returned Outcome should be either Succeeded, Failed or Errored, but not Cancelled.
	// The returned error should be nil if and only if the outcome is Succeeded.
	SetAssetSize(res db.Resource, assetUUID string, oldSize, newSize uint64) (castellum.OperationOutcome, error)
	// previousStatus will be nil when this function is called for the first time
	// for the given asset.
	GetAssetStatus(ctx context.Context, res db.Resource, assetUUID string, previousStatus *AssetStatus) (AssetStatus, error)
}

AssetManager is the main modularization interface in Castellum. It provides a separation boundary between the plugins that implement the concrete behavior for specific asset types, and the core logic of Castellum. It is created by CreateAssetManagers() using AssetManagerRegistry.

type AssetManagerTeam

type AssetManagerTeam []AssetManager

AssetManagerTeam is the set of AssetManager instances that Castellum is using.

func CreateAssetManagers

func CreateAssetManagers(pluginTypeIDs []string, provider ProviderClient) (AssetManagerTeam, error)

CreateAssetManagers prepares a set of AssetManager instances for a single run of Castellum. The first argument is the list of IDs of all factories that shall be used to create asset managers.

func (AssetManagerTeam) ForAssetType

func (team AssetManagerTeam) ForAssetType(assetType db.AssetType) (AssetManager, AssetTypeInfo)

ForAssetType returns the asset manager for the given asset type, or nil if the asset type is not supported.

type AssetNotFoundError

type AssetNotFoundError struct {
	InnerError error
}

AssetNotFoundError is returned by AssetManager.GetAssetStatus() if the concerning asset can not be found in the respective backend.

func (AssetNotFoundError) Error

func (e AssetNotFoundError) Error() string

type AssetStatus

type AssetStatus struct {
	Size              uint64
	Usage             castellum.UsageValues
	StrictMinimumSize *uint64
	StrictMaximumSize *uint64
}

AssetStatus shows the current state of an asset.

It is returned by AssetManager.GetAssetStatus(). The semantics of all fields match their equivalently-named counterparts in the db.Asset type.

func StatusOfAsset

func StatusOfAsset(asset db.Asset) AssetStatus

StatusOfAsset converts an Asset into just its AssetStatus.

This is used when passing an Asset from a high-level workflow function into a low-level logic function. Low-level functions explicitly take only the AssetStatus to avoid accidental dependencies on non-logic attributes like timestamps, UUIDs or error message strings.

type AssetTypeInfo

type AssetTypeInfo struct {
	AssetType    db.AssetType
	UsageMetrics []castellum.UsageMetric
}

AssetTypeInfo describes an AssetType supported by an AssetManager.

func (AssetTypeInfo) MakeZeroUsageValues

func (info AssetTypeInfo) MakeZeroUsageValues() castellum.UsageValues

MakeZeroUsageValues is a convenience function to instantiate an all-zero UsageValues for this AssetType.

type CachedDomain

type CachedDomain struct {
	Name string
}

CachedDomain contains cached information about a Keystone domain.

type CachedProject

type CachedProject struct {
	Name     string
	DomainID string
}

CachedProject contains cached information about a Keystone project.

type Config

type Config struct {
	MaxAssetSizeRules []MaxAssetSizeRule `yaml:"max_asset_sizes"`
	ProjectSeeds      []ProjectSeed      `yaml:"project_seeds"`
}

Config contains everything that we found in the configuration file.

func LoadConfig

func LoadConfig(configPath string) (Config, error)

LoadConfig loads the configuration file from the given path.

func (Config) IsSeededResource

func (c Config) IsSeededResource(project CachedProject, domain CachedDomain, assetType db.AssetType) bool

IsSeededResource returns true if the config contains a seed for this resource. This is used by the API to reject PUT/DELETE requests to seeded resources.

func (Config) MaxAssetSizeFor

func (c Config) MaxAssetSizeFor(assetType db.AssetType, scopeUUID string) (result *uint64)

MaxAssetSizeFor computes the highest permissible max_size value for this asset type. If no constraints apply, nil is returned.

type MaxAssetSizeRule

type MaxAssetSizeRule struct {
	AssetTypeRx regexpext.BoundedRegexp `yaml:"asset_type"`
	ScopeUUID   string                  `yaml:"scope_uuid"` // leave empty to have the rule apply to all scopes
	Value       uint64                  `yaml:"value"`
}

MaxAssetSizeRule appears in type Config.

type ProjectScope

type ProjectScope struct {
	// The ID of the project to scope into.
	ID string
	// Before scoping into the project, assign these roles to ourselves.
	RoleNames []string
}

ProjectScope defines the scope into which ProviderClient.ProjectScopedClient() will authenticate.

type ProjectSeed

type ProjectSeed struct {
	ProjectName             string                              `yaml:"project_name"`
	DomainName              string                              `yaml:"domain_name"`
	Resources               map[db.AssetType]castellum.Resource `yaml:"resources"`
	DisabledResourceRegexps []regexpext.BoundedRegexp           `yaml:"disabled_resources"`
}

ProjectSeed appears in type Seed.

func (ProjectSeed) ForbidsResource

func (s ProjectSeed) ForbidsResource(assetType db.AssetType) bool

type ProviderClient

type ProviderClient interface {
	// CloudAdminClient returns a service client in the provider client's default scope.
	// The argument is a function like `openstack.NewIdentityV3`.
	CloudAdminClient(factory ServiceClientFactory) (*gophercloud.ServiceClient, error)
	// ProjectScopedClient authenticates into the specified project scope.
	ProjectScopedClient(scope ProjectScope) (*gophercloud.ProviderClient, gophercloud.EndpointOpts, error)

	// GetAuthResult has the same semantics as gophercloud.ProviderClient.GetAuthResult.
	GetAuthResult() gophercloud.AuthResult
	// GetProject queries the given project ID in Keystone, unless it is already cached.
	// When the project does not exist, nil is returned instead of an error.
	GetProject(projectID string) (*CachedProject, error)
	// GetDomain queries the given domain ID in Keystone, unless it is already cached.
	// When the project does not exist, nil is returned instead of an error.
	GetDomain(domainID string) (*CachedDomain, error)

	// FindProjectID searches for a project with the given name and domain name.
	// When the project does not exist, "" is returned instead of an error.
	FindProjectID(projectName, projectDomainName string) (string, error)
}

ProviderClient is an interface for an internal type that wraps gophercloud.ProviderClient to provide caching and rescoping. It is only provided as an interface to enable substitution of a mock for tests.

func NewProviderClient

NewProviderClient constructs a new ProviderClient instance.

type ResourceLogic

type ResourceLogic struct {
	UsageMetrics             []castellum.UsageMetric
	LowThresholdPercent      castellum.UsageValues
	HighThresholdPercent     castellum.UsageValues
	CriticalThresholdPercent castellum.UsageValues

	SizeStepPercent float64
	SingleStep      bool

	MinimumSize     *uint64
	MaximumSize     *uint64
	MinimumFreeSize *uint64
}

ResourceLogic contains all the attributes from `db.Resource` that pertain to the calculation of resize actions for assets within a given resource.

It does not include anything that is not needed for that calculation, in order to avoid accidental dependencies on things like timing information, UUIDs or error message strings.

func LogicOfResource

func LogicOfResource(res db.Resource, info AssetTypeInfo) ResourceLogic

LogicOfResource converts a Resource into just its ResourceLogic.

type ServiceClientFactory

ServiceClientFactory is a typedef that appears in type ProviderClient.

Jump to

Keyboard shortcuts

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