github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/caas/broker.go (about) 1 // Copyright 2017 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package caas 5 6 import ( 7 "fmt" 8 9 "github.com/juju/errors" 10 "github.com/juju/utils/set" 11 "github.com/juju/version" 12 core "k8s.io/api/core/v1" 13 14 "github.com/juju/juju/core/application" 15 "github.com/juju/juju/core/constraints" 16 "github.com/juju/juju/core/devices" 17 "github.com/juju/juju/core/status" 18 "github.com/juju/juju/core/watcher" 19 "github.com/juju/juju/environs" 20 "github.com/juju/juju/environs/context" 21 "github.com/juju/juju/network" 22 "github.com/juju/juju/storage" 23 ) 24 25 // ContainerEnvironProvider represents a computing and storage provider 26 // for a container runtime. 27 type ContainerEnvironProvider interface { 28 environs.EnvironProvider 29 30 // Open opens the broker and returns it. The configuration must 31 // have passed through PrepareConfig at some point in its lifecycle. 32 // 33 // Open should not perform any expensive operations, such as querying 34 // the cloud API, as it will be called frequently. 35 Open(args environs.OpenParams) (Broker, error) 36 37 // ParsePodSpec unmarshalls the given YAML pod spec. 38 ParsePodSpec(in string) (*PodSpec, error) 39 } 40 41 // RegisterContainerProvider is used for providers that we want to use for managing 'instances', 42 // but are not possible sources for 'juju bootstrap'. 43 func RegisterContainerProvider(name string, p ContainerEnvironProvider, alias ...string) (unregister func()) { 44 if err := environs.GlobalProviderRegistry().RegisterProvider(p, name, alias...); err != nil { 45 panic(fmt.Errorf("juju: %v", err)) 46 } 47 return func() { 48 environs.GlobalProviderRegistry().UnregisterProvider(name) 49 } 50 } 51 52 // New returns a new broker based on the provided configuration. 53 func New(args environs.OpenParams) (Broker, error) { 54 p, err := environs.Provider(args.Cloud.Type) 55 if err != nil { 56 return nil, errors.Trace(err) 57 } 58 return Open(p, args) 59 } 60 61 // Open creates a Broker instance and errors if the provider is not for 62 // a container substrate. 63 func Open(p environs.EnvironProvider, args environs.OpenParams) (Broker, error) { 64 if envProvider, ok := p.(ContainerEnvironProvider); !ok { 65 return nil, errors.NotValidf("container environ provider %T", p) 66 } else { 67 return envProvider.Open(args) 68 } 69 } 70 71 // NewContainerBrokerFunc returns a Container Broker. 72 type NewContainerBrokerFunc func(args environs.OpenParams) (Broker, error) 73 74 // StatusCallbackFunc represents a function that can be called to report a status. 75 type StatusCallbackFunc func(appName string, settableStatus status.Status, info string, data map[string]interface{}) error 76 77 // ServiceParams defines parameters used to create a service. 78 type ServiceParams struct { 79 // PodSpec is the spec used to configure a pod. 80 PodSpec *PodSpec 81 82 // ResourceTags is a set of tags to set on the created service. 83 ResourceTags map[string]string 84 85 // Placement defines node affinity rules. 86 Placement string 87 88 // Constraints is a set of constraints on 89 // the pod to create. 90 Constraints constraints.Value 91 92 // Filesystems is a set of parameters for filesystems that should be created. 93 Filesystems []storage.KubernetesFilesystemParams 94 95 // Devices is a set of parameters for Devices that is required. 96 Devices []devices.KubernetesDeviceParams 97 } 98 99 // Broker instances interact with the CAAS substrate. 100 type Broker interface { 101 // Provider returns the ContainerEnvironProvider that created this Broker. 102 Provider() ContainerEnvironProvider 103 104 // Destroy terminates all containers and other resources in this broker's namespace. 105 Destroy(context.ProviderCallContext) error 106 107 // Namespaces returns name names of the namespaces on the cluster. 108 Namespaces() ([]string, error) 109 110 // EnsureNamespace ensures this broker's namespace is created. 111 EnsureNamespace() error 112 113 // GetStorageClassName returns the name of a storage class with the specified 114 // labels, or else the cluster default storage class, or else a NotFound error. 115 GetStorageClassName(labels ...string) (string, error) 116 117 // EnsureOperator creates or updates an operator pod for running 118 // a charm for the specified application. 119 EnsureOperator(appName, agentPath string, config *OperatorConfig) error 120 121 // OperatorExists returns true if the operator for the specified 122 // application exists. 123 OperatorExists(appName string) (bool, error) 124 125 // DeleteOperator deletes the specified operator. 126 DeleteOperator(appName string) error 127 128 // EnsureService creates or updates a service for pods with the given params. 129 EnsureService(appName string, statusCallback StatusCallbackFunc, params *ServiceParams, numUnits int, config application.ConfigAttributes) error 130 131 // EnsureCustomResourceDefinition creates or updates a custom resource definition resource. 132 EnsureCustomResourceDefinition(appName string, podSpec *PodSpec) error 133 134 // Service returns the service for the specified application. 135 Service(appName string) (*Service, error) 136 137 // DeleteService deletes the specified service. 138 DeleteService(appName string) error 139 140 // ExposeService sets up external access to the specified service. 141 ExposeService(appName string, resourceTags map[string]string, config application.ConfigAttributes) error 142 143 // UnexposeService removes external access to the specified service. 144 UnexposeService(appName string) error 145 146 // WatchUnits returns a watcher which notifies when there 147 // are changes to units of the specified application. 148 WatchUnits(appName string) (watcher.NotifyWatcher, error) 149 150 // Units returns all units and any associated filesystems 151 // of the specified application. Filesystems are mounted 152 // via volumes bound to the unit. 153 Units(appName string) ([]Unit, error) 154 155 // WatchOperator returns a watcher which notifies when there 156 // are changes to the operator of the specified application. 157 WatchOperator(string) (watcher.NotifyWatcher, error) 158 159 // GetNamespace returns the namespace for the specified name or current namespace. 160 GetNamespace(name string) (*core.Namespace, error) 161 162 // Operator returns an Operator with current status and life details. 163 Operator(string) (*Operator, error) 164 165 // ListHostCloudRegions lists all the cloud regions that this cluster has worker nodes/instances running in. 166 ListHostCloudRegions() (set.Strings, error) 167 168 // NamespaceWatcher provides the API to watch caas namespace. 169 NamespaceWatcher 170 171 // ProviderRegistry is an interface for obtaining storage providers. 172 storage.ProviderRegistry 173 174 // InstancePrechecker provides a means of "prechecking" placement 175 // arguments before recording them in state. 176 environs.InstancePrechecker 177 178 // BootstrapEnviron defines methods for bootstraping a controller. 179 environs.BootstrapEnviron 180 181 // ResourceAdopter defines methods for adopting resources. 182 environs.ResourceAdopter 183 } 184 185 // NamespaceWatcher provides the API to watch caas namespace. 186 type NamespaceWatcher interface { 187 // WatchNamespace returns a watcher which notifies when there 188 // are changes to current namespace. 189 WatchNamespace() (watcher.NotifyWatcher, error) 190 } 191 192 // Service represents information about the status of a caas service entity. 193 type Service struct { 194 Id string 195 Addresses []network.Address 196 } 197 198 // FilesystemInfo represents information about a filesystem 199 // mounted by a unit. 200 type FilesystemInfo struct { 201 StorageName string 202 FilesystemId string 203 Size uint64 204 MountPoint string 205 ReadOnly bool 206 Status status.StatusInfo 207 Volume VolumeInfo 208 } 209 210 // VolumeInfo represents information about a volume 211 // mounted by a unit. 212 type VolumeInfo struct { 213 VolumeId string 214 Size uint64 215 Persistent bool 216 Status status.StatusInfo 217 } 218 219 // Unit represents information about the status of a "pod". 220 type Unit struct { 221 Id string 222 Address string 223 Ports []string 224 Dying bool 225 Status status.StatusInfo 226 FilesystemInfo []FilesystemInfo 227 } 228 229 // Operator represents information about the status of an "operator pod". 230 type Operator struct { 231 Id string 232 Dying bool 233 Status status.StatusInfo 234 } 235 236 // CharmStorageParams defines parameters used to create storage 237 // for operators to use for charm state. 238 type CharmStorageParams struct { 239 // Size is the minimum size of the filesystem in MiB. 240 Size uint64 241 242 // The provider type for this filesystem. 243 Provider storage.ProviderType 244 245 // Attributes is a set of provider-specific options for storage creation, 246 // as defined in a storage pool. 247 Attributes map[string]interface{} 248 249 // ResourceTags is a set of tags to set on the created filesystem, if the 250 // storage provider supports tags. 251 ResourceTags map[string]string 252 } 253 254 // OperatorConfig is the config to use when creating an operator. 255 type OperatorConfig struct { 256 // OperatorImagePath is the docker registry URL for the image. 257 OperatorImagePath string 258 259 // Version is the Juju version of the operator image. 260 Version version.Number 261 262 // CharmStorage defines parameters used to create storage 263 // for operators to use for charm state. 264 CharmStorage CharmStorageParams 265 266 // AgentConf is the contents of the agent.conf file. 267 AgentConf []byte 268 269 // ResourceTags is a set of tags to set on the operator pod. 270 ResourceTags map[string]string 271 }