github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/environs/broker.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package environs 5 6 import ( 7 "github.com/juju/juju/cloudconfig/instancecfg" 8 "github.com/juju/juju/core/constraints" 9 "github.com/juju/juju/core/instance" 10 "github.com/juju/juju/core/lxdprofile" 11 corenetwork "github.com/juju/juju/core/network" 12 "github.com/juju/juju/core/status" 13 "github.com/juju/juju/environs/config" 14 "github.com/juju/juju/environs/context" 15 "github.com/juju/juju/environs/imagemetadata" 16 "github.com/juju/juju/environs/instances" 17 "github.com/juju/juju/storage" 18 "github.com/juju/juju/tools" 19 ) 20 21 // StatusCallbackFunc represents a function that can be called to report a status. 22 type StatusCallbackFunc func(settableStatus status.Status, info string, data map[string]interface{}) error 23 24 // StartInstanceParams holds parameters for the 25 // InstanceBroker.StartInstance method. 26 type StartInstanceParams struct { 27 // ControllerUUID is the uuid of the controller. 28 ControllerUUID string 29 30 // Constraints is a set of constraints on 31 // the kind of instance to create. 32 Constraints constraints.Value 33 34 // Tools is a list of tools that may be used 35 // to start a Juju agent on the machine. 36 Tools tools.List 37 38 // InstanceConfig describes the machine's configuration. 39 InstanceConfig *instancecfg.InstanceConfig 40 41 // Placement, if non-empty, contains an environment-specific 42 // placement directive that may be used to decide how the 43 // instance should be started. 44 Placement string 45 46 // AvailabilityZone, provides the name of the availability 47 // zone required to start the instance. 48 AvailabilityZone string 49 50 // RootDisk is a set of parameters for creating the root disk volume. 51 RootDisk *storage.VolumeParams 52 53 // Volumes is a set of parameters for volumes that should be created. 54 // 55 // StartInstance need not check the value of the Attachment field, 56 // as it is guaranteed that any volumes in this list are designated 57 // for attachment to the instance being started. 58 Volumes []storage.VolumeParams 59 60 // VolumeAttachments is a set of parameters for existing volumes that 61 // should be attached. If the StartInstance method does not attach the 62 // volumes, they will be attached by the storage provisioner once the 63 // machine has been created. The attachments are presented here to 64 // give the provider an opportunity for the volume attachments to 65 // influence the instance creation, e.g. by restricting the machine 66 // to specific availability zones. 67 VolumeAttachments []storage.VolumeAttachmentParams 68 69 // NetworkInfo is an optional list of network interface details, 70 // necessary to configure on the instance. 71 NetworkInfo corenetwork.InterfaceInfos 72 73 // SubnetsToZones is an optional collection of maps of provider-specific 74 // subnet IDs to a list of availability zone names each subnet is available 75 // in. 76 // It is populated when valid positive spaces constraints are present, 77 // or when the instance to be started will host an application with bound 78 // endpoints. 79 // The subnets present are derived from the union of 80 // constrained/bound spaces. 81 // Negative space constraints will have already been validated when 82 // retrieving `ProvisioningInfo`. 83 SubnetsToZones []map[corenetwork.Id][]string 84 85 // EndpointBindings holds the mapping between application endpoint names to 86 // provider-specific space IDs. It is populated when provisioning a machine 87 // to host a unit of an application with endpoint bindings. 88 EndpointBindings map[string]corenetwork.Id 89 90 // ImageMetadata is a collection of image metadata 91 // that may be used to start this instance. 92 ImageMetadata []*imagemetadata.ImageMetadata 93 94 // CleanupCallback is a callback to be used to clean up any residual 95 // status-reporting output from StatusCallback. 96 CleanupCallback func() error 97 98 // StatusCallback is a callback to be used by the instance to report 99 // changes in status. Its signature is consistent with other 100 // status-related functions to allow them to be used as callbacks. 101 StatusCallback StatusCallbackFunc 102 103 // Abort is a channel that will be closed to indicate that the command 104 // should be aborted. 105 Abort <-chan struct{} 106 107 // CharmLXDProfiles is a slice of names of lxd profiles to be used creating 108 // the LXD container, if specified and an LXD container. The profiles 109 // come from charms deployed on the machine. 110 CharmLXDProfiles []string 111 } 112 113 // StartInstanceResult holds the result of an 114 // InstanceBroker.StartInstance method call. 115 type StartInstanceResult struct { 116 // DisplayName is an optional human-readable string that's used 117 // for display purposes only. 118 DisplayName string 119 120 // Instance is an interface representing a cloud instance. 121 Instance instances.Instance 122 123 // Config holds the environment config to be used for any further 124 // operations, if the instance is for a controller. 125 Config *config.Config 126 127 // HardwareCharacteristics represents the hardware characteristics 128 // of the newly created instance. 129 Hardware *instance.HardwareCharacteristics 130 131 // NetworkInfo contains information about how to configure network 132 // interfaces on the instance. Depending on the provider, this 133 // might be the same StartInstanceParams.NetworkInfo or may be 134 // modified as needed. 135 NetworkInfo corenetwork.InterfaceInfos 136 137 // Volumes contains a list of volumes created, each one having the 138 // same Name as one of the VolumeParams in StartInstanceParams.Volumes. 139 // VolumeAttachment information is reported separately. 140 Volumes []storage.Volume 141 142 // VolumeAttachments contains a attachment-specific information about 143 // volumes that were attached to the started instance. 144 VolumeAttachments []storage.VolumeAttachment 145 } 146 147 // InstanceBroker defines methods for managing vm or container instances. 148 // TODO(wallyworld) - we want this in the environs/instance package but import loops 149 // stop that from being possible right now. 150 type InstanceBroker interface { 151 // StartInstance asks for a new instance to be created, associated with 152 // the provided config in machineConfig. The given config describes the 153 // juju state for the new instance to connect to. The config 154 // MachineNonce, which must be unique within an environment, is used by 155 // juju to protect against the consequences of multiple instances being 156 // started with the same machine id. 157 // 158 // Callers may attempt to distribute instances across a set of 159 // availability zones. If one zone fails, then the caller is expected 160 // to attempt in another zone. If the provider can determine that 161 // the StartInstanceParams can never be fulfilled in any zone, then 162 // it may return an error satisfying Is(err, ErrAvailabilityZoneIndependent). 163 StartInstance(ctx context.ProviderCallContext, args StartInstanceParams) (*StartInstanceResult, error) 164 165 // StopInstances shuts down the instances with the specified IDs. 166 // Unknown instance IDs are ignored, to enable idempotency. 167 StopInstances(context.ProviderCallContext, ...instance.Id) error 168 169 // AllInstances returns all instances currently known to the broker. 170 AllInstances(ctx context.ProviderCallContext) ([]instances.Instance, error) 171 172 // AllRunningInstances returns all running, available instances currently known to the broker. 173 AllRunningInstances(ctx context.ProviderCallContext) ([]instances.Instance, error) 174 } 175 176 // LXDProfiler defines an interface for dealing with lxd profiles used to 177 // deploy juju machines and containers. 178 type LXDProfiler interface { 179 // AssignLXDProfiles assigns the given profile names to the lxd instance 180 // provided. The slice of ProfilePosts provides details for adding to 181 // and removing profiles from the lxd server. 182 AssignLXDProfiles(instID string, profilesNames []string, profilePosts []lxdprofile.ProfilePost) ([]string, error) 183 184 // MaybeWriteLXDProfile writes the given LXDProfile to if not already there. 185 MaybeWriteLXDProfile(pName string, put lxdprofile.Profile) error 186 187 // LXDProfileNames returns all the profiles associated to a container name 188 LXDProfileNames(containerName string) ([]string, error) 189 }