github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/caas/application.go (about)

     1  // Copyright 2020 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package caas
     5  
     6  import (
     7  	"context"
     8  
     9  	"github.com/juju/version/v2"
    10  	core "k8s.io/api/core/v1"
    11  
    12  	"github.com/juju/juju/core/constraints"
    13  	"github.com/juju/juju/core/devices"
    14  	"github.com/juju/juju/core/resources"
    15  	"github.com/juju/juju/core/watcher"
    16  	"github.com/juju/juju/storage"
    17  )
    18  
    19  //go:generate go run go.uber.org/mock/mockgen -package mocks -destination mocks/application_mock.go github.com/juju/juju/caas Application
    20  
    21  // Application is for interacting with the CAAS substrate.
    22  type Application interface {
    23  	Ensure(config ApplicationConfig) error
    24  	Exists() (DeploymentState, error)
    25  	Delete() error
    26  	Watch() (watcher.NotifyWatcher, error)
    27  	WatchReplicas() (watcher.NotifyWatcher, error)
    28  
    29  	// ApplicationPodSpec returns the pod spec needed to run the application workload.
    30  	ApplicationPodSpec(config ApplicationConfig) (*core.PodSpec, error)
    31  
    32  	// Scale scales the Application's unit to the value specified. Scale must
    33  	// be >= 0. Application units will be removed or added to meet the scale
    34  	// defined.
    35  	Scale(int) error
    36  
    37  	// Trust sets up the role on the application's service account to
    38  	// give full access to the cluster.
    39  	Trust(bool) error
    40  
    41  	State() (ApplicationState, error)
    42  
    43  	// Units of the application fetched from kubernetes by matching pod labels.
    44  	Units() ([]Unit, error)
    45  
    46  	UnitsToRemove(context.Context, int) ([]string, error)
    47  
    48  	// Service returns the service associated with the application.
    49  	Service() (*Service, error)
    50  
    51  	ServiceInterface
    52  }
    53  
    54  // ServicePort represents service ports mapping from service to units.
    55  type ServicePort struct {
    56  	Name       string `json:"name"`
    57  	Port       int    `json:"port"`
    58  	TargetPort int    `json:"target-port"`
    59  	Protocol   string `json:"protocol"`
    60  }
    61  
    62  // ServiceParam defines parameters for an UpdateService request.
    63  type ServiceParam struct {
    64  	Type  string        `json:"type"`
    65  	Ports []ServicePort `json:"ports"`
    66  }
    67  
    68  // ServiceInterface provides the API to get/set service.
    69  type ServiceInterface interface {
    70  	// UpdateService updates the default service with specific service type and port mappings.
    71  	UpdateService(ServiceParam) error
    72  
    73  	UpdatePorts(ports []ServicePort, updateContainerPorts bool) error
    74  }
    75  
    76  // ApplicationState represents the application state.
    77  type ApplicationState struct {
    78  	DesiredReplicas int
    79  	Replicas        []string
    80  }
    81  
    82  // ApplicationConfig is the config passed to the application units.
    83  type ApplicationConfig struct {
    84  	// AgentVersion is the Juju version of the agent image.
    85  	AgentVersion version.Number
    86  
    87  	// AgentImagePath is the docker registry URL for the charm container.
    88  	AgentImagePath string
    89  
    90  	// CharmBaseImagePath is the docker registry URL for the workload containers to run pebble.
    91  	CharmBaseImagePath string
    92  
    93  	// IsPrivateImageRepo indicates if the images repositories are private or not.
    94  	// If they are, we need to set the image pull secret.
    95  	IsPrivateImageRepo bool
    96  
    97  	// CharmModifiedVersion is a monotonically incrementing version number
    98  	// that represents the version of the charm and resources with regards to
    99  	// this application. The CAAS provider will pass this to the uniter worker
   100  	// to ensure the container infrastructure matches the charm.
   101  	CharmModifiedVersion int
   102  
   103  	// Containers is the list of containers that make up the container (excluding uniter and init containers).
   104  	Containers map[string]ContainerConfig
   105  
   106  	// ExistingContainers is a list of names for containers which will be added
   107  	// to the application pod spec outside the ApplicationPodSpec method.
   108  	// These containers will be added to the JUJU_CONTAINER_NAMES env variable
   109  	// in the charm container, but we will not create new container specs for
   110  	// them, as they are assumed to already exist.
   111  	ExistingContainers []string
   112  
   113  	// IntroductionSecret
   114  	IntroductionSecret string
   115  	// ControllerAddresses is a comma separated list of controller addresses.
   116  	// TODO: Use model-operator service instead for introduction, so controller addresses can change
   117  	// without having to update deployed application.
   118  	ControllerAddresses string
   119  	// ControllerCertBundle is a PEM cert bundle for talking to the Juju controller.
   120  	ControllerCertBundle string
   121  
   122  	// ResourceTags is a set of tags to set on the operator pod.
   123  	ResourceTags map[string]string
   124  
   125  	// Constraints is a set of constraints on
   126  	// the pod to create.
   127  	Constraints constraints.Value
   128  
   129  	// Filesystems is a set of parameters for filesystems that should be created.
   130  	Filesystems []storage.KubernetesFilesystemParams
   131  
   132  	// Devices is a set of parameters for Devices that is required.
   133  	Devices []devices.KubernetesDeviceParams
   134  
   135  	// Trust is set to true to give the application cloud access.
   136  	Trust bool
   137  
   138  	// InitialScale is used to provide the initial desired scale of the application.
   139  	// After the application is created, InitialScale has no effect.
   140  	InitialScale int
   141  
   142  	// CharmUser controls what user the charm/unit agent runs as.
   143  	CharmUser RunAs
   144  }
   145  
   146  // ContainerConfig describes a container that is deployed alonside the uniter/charm container.
   147  type ContainerConfig struct {
   148  	// Name of the container.
   149  	Name string
   150  
   151  	// Image used to create the container.
   152  	Image resources.DockerImageDetails
   153  
   154  	// Mounts to storage that are to be provided within this container.
   155  	Mounts []MountConfig
   156  
   157  	// Uid to run as. Default to 0 or root.
   158  	Uid int
   159  
   160  	// Gid to run as. Default to 0 or root.
   161  	Gid int
   162  }
   163  
   164  // MountConfig describes a storage that should be mounted to a container.
   165  type MountConfig struct {
   166  	// StorageName is the name of the storage as specified in the charm.
   167  	StorageName string
   168  
   169  	// Path is the mount point inside the container.
   170  	Path string
   171  }
   172  
   173  // RunAs defines which user to run a certain process as.
   174  type RunAs string
   175  
   176  const (
   177  	RunAsRoot    RunAs = "root"
   178  	RunAsSudoer  RunAs = "sudoer"
   179  	RunAsNonRoot RunAs = "non-root"
   180  )