serviceregistration

package
v1.6.112 Latest Latest
Warning

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

Go to latest
Published: Aug 13, 2023 License: MPL-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAddress

func GetAddress(
	address,
	addressMode,
	portLabel string,
	networks structs.Networks,
	driverNet *drivers.DriverNetwork,
	ports structs.AllocatedPorts,
	netStatus *structs.AllocNetworkStatus,
) (string, int, error)

GetAddress returns the IP (or custom advertise address) and port to use for a service or check registration. If no port label is specified (an empty value) and no custom address is specified, zero values are returned because no address could be resolved.

func MakeAllocServiceID

func MakeAllocServiceID(allocID, taskName string, service *structs.Service) string

MakeAllocServiceID creates a unique ID for identifying an alloc service in a service registration provider. Both Nomad and Consul solutions use the same ID format to provide consistency.

Example Service ID: _nomad-task-b4e61df9-b095-d64e-f241-23860da1375f-redis-http-http

Types

type AllocRegistration

type AllocRegistration struct {
	// Tasks maps the name of a task to its registered services and checks.
	Tasks map[string]*ServiceRegistrations
}

AllocRegistration holds the status of services registered for a particular allocations by task.

func (*AllocRegistration) Copy

Copy performs a deep copy of the AllocRegistration object.

func (*AllocRegistration) NumChecks

func (a *AllocRegistration) NumChecks() int

NumChecks returns the number of registered checks from both task AND group services. Group services are prefixed with "group-".

func (*AllocRegistration) NumServices

func (a *AllocRegistration) NumServices() int

NumServices returns the number of registered task AND group services. Group services are prefixed with "group-".

type CheckStatusGetter

type CheckStatusGetter interface {
	// Get returns a map from CheckID -> (minimal) CheckStatus
	Get() (map[string]string, error)
}

CheckStatusGetter is implemented per-provider.

type CheckWatcher

type CheckWatcher interface {
	// Run the CheckWatcher. Maintains a background process to continuously
	// monitor active checks. Must be called before Watch or Unwatch. Must be
	// called as a goroutine.
	Run(ctx context.Context)

	// Watch the given check. If the check status enters a failing state, the
	// task associated with the check will be restarted according to its check_restart
	// policy via wr.
	Watch(allocID, taskName, checkID string, check *structs.ServiceCheck, wr WorkloadRestarter)

	// Unwatch will cause the CheckWatcher to no longer monitor the check of given checkID.
	Unwatch(checkID string)
}

A CheckWatcher watches for check failures and restarts tasks according to their check_restart policy.

type Handler

type Handler interface {

	// RegisterWorkload adds all service entries and checks to the backend
	// provider. Whilst callers attempt to ensure WorkloadServices.Services is
	// not empty before calling this function, implementations should also
	// perform this.
	RegisterWorkload(workload *WorkloadServices) error

	// RemoveWorkload all service entries and checks from the backend provider
	// that are found within the passed WorkloadServices object. Whilst callers
	// attempt to ensure WorkloadServices.Services is not empty before calling
	// this function, implementations should also perform this.
	RemoveWorkload(workload *WorkloadServices)

	// UpdateWorkload removes workload as specified by the old parameter, and
	// adds workload as specified by the new parameter. Callers do not perform
	// any deduplication on both objects, it is therefore the responsibility of
	// the implementation.
	UpdateWorkload(old, newTask *WorkloadServices) error

	// AllocRegistrations returns the registrations for the given allocation.
	AllocRegistrations(allocID string) (*AllocRegistration, error)

	// UpdateTTL is used to update the TTL of an individual service
	// registration check.
	UpdateTTL(id, namespace, output, status string) error
}

Handler is the interface the Nomad Client uses to register, update and remove services and checks from service registration providers. Currently, Consul and Nomad are supported providers.

When utilising Consul, the ACL "service:write" is required. It supports all functionality and is the OG/GOAT.

When utilising Nomad, the client secret ID is used for authorisation. It currently supports service registrations only.

type ServiceRegistration

type ServiceRegistration struct {
	// serviceID and checkIDs are internal fields that track just the IDs of the
	// services/checks registered in Consul. It is used to materialize the other
	// fields when queried.
	ServiceID string
	CheckIDs  map[string]struct{} // todo: use a Set?

	// CheckOnUpdate is a map of checkIDs and the associated OnUpdate value
	// from the ServiceCheck It is used to determine how a reported checks
	// status should be evaluated.
	CheckOnUpdate map[string]string

	// Service is the AgentService registered in Consul.
	Service *api.AgentService

	// Checks is the status of the registered checks.
	Checks []*api.AgentCheck
}

ServiceRegistration holds the status of a registered Consul Service and its Checks.

type ServiceRegistrations

type ServiceRegistrations struct {
	// Services maps service_id -> service registration
	Services map[string]*ServiceRegistration
}

ServiceRegistrations holds the status of services registered for a particular task or task group.

type UniversalCheckWatcher

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

UniversalCheckWatcher is an implementation of CheckWatcher capable of watching checks in the Nomad or Consul service providers.

func NewCheckWatcher

func NewCheckWatcher(logger hclog.Logger, getter CheckStatusGetter) *UniversalCheckWatcher

func (*UniversalCheckWatcher) Run

func (*UniversalCheckWatcher) Unwatch

func (w *UniversalCheckWatcher) Unwatch(checkID string)

Unwatch a check.

func (*UniversalCheckWatcher) Watch

func (w *UniversalCheckWatcher) Watch(allocID, taskName, checkID string, check *structs.ServiceCheck, wr WorkloadRestarter)

Watch a check and restart its task if unhealthy.

type WorkloadRestarter

type WorkloadRestarter interface {
	Restart(ctx context.Context, event *structs.TaskEvent, failure bool) error
}

WorkloadRestarter allows the checkWatcher to restart tasks or entire task groups.

type WorkloadServices

type WorkloadServices struct {
	AllocInfo structs.AllocInfo

	// Canary indicates whether, or not the allocation is a canary. This is
	// used to build the correct tags mapping.
	Canary bool

	// ProviderNamespace is the provider namespace in which services will be
	// registered, if the provider supports this functionality.
	ProviderNamespace string

	// Restarter allows restarting the task or task group depending on the
	// check_restart blocks.
	Restarter WorkloadRestarter

	// Services and checks to register for the task.
	Services []*structs.Service

	// Networks from the task's resources block.
	// TODO: remove and use Ports
	Networks structs.Networks

	// NetworkStatus from alloc if network namespace is created.
	// Can be nil.
	NetworkStatus *structs.AllocNetworkStatus

	// AllocatedPorts is the list of port mappings.
	Ports structs.AllocatedPorts

	// DriverExec is the script executor for the task's driver. For group
	// services this is nil and script execution is managed by a tasklet in the
	// taskrunner script_check_hook.
	DriverExec interfaces.ScriptExecutor

	// DriverNetwork is the network specified by the driver and may be nil.
	DriverNetwork *drivers.DriverNetwork
}

WorkloadServices describes services defined in either a Task or TaskGroup that need to be syncronized with a service registration provider.

func (*WorkloadServices) Copy

func (ws *WorkloadServices) Copy() *WorkloadServices

Copy method for easing tests.

func (*WorkloadServices) Name

func (ws *WorkloadServices) Name() string

func (*WorkloadServices) RegistrationProvider

func (ws *WorkloadServices) RegistrationProvider() string

RegistrationProvider identifies the service registration provider for the WorkloadServices.

Directories

Path Synopsis
Package nsd provides Nomad service registration and therefore discovery capabilities for Nomad clients.
Package nsd provides Nomad service registration and therefore discovery capabilities for Nomad clients.

Jump to

Keyboard shortcuts

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