github.com/mirantis/virtlet@v1.5.2-0.20191204181327-1659b8a48e9b/pkg/metadata/types/types.go (about)

     1  /*
     2  Copyright 2018 Mirantis
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package types
    18  
    19  import (
    20  	"github.com/Mirantis/virtlet/pkg/network"
    21  	"github.com/Mirantis/virtlet/pkg/utils"
    22  )
    23  
    24  const (
    25  	blockVolumeNsUUID = "593c763a-381c-4736-8c7d-20cff5278e76"
    26  )
    27  
    28  // PodSandboxState specifies the state of the sandbox
    29  type PodSandboxState int32
    30  
    31  const (
    32  	// PodSandboxState_SANDBOX_READY specifies that the pod is ready.
    33  	PodSandboxState_SANDBOX_READY PodSandboxState = 0
    34  	// PodSandboxState_SANDBOX_READY specifies that the pod is not ready.
    35  	// This includes errors during RunPodSandbox.
    36  	PodSandboxState_SANDBOX_NOTREADY PodSandboxState = 1
    37  )
    38  
    39  // Protocol specifies the protocol for a port mapping.
    40  type Protocol int32
    41  
    42  const (
    43  	// Protocol_TCP specifies TCP protocol.
    44  	Protocol_TCP Protocol = 0
    45  	// Protocol_TCP specifies UDP protocol.
    46  	Protocol_UDP Protocol = 1
    47  )
    48  
    49  // ContainerState specifies the state of a container
    50  type ContainerState int32
    51  
    52  const (
    53  	// ContainerState_CONTAINER_CREATED means that the container is just created.
    54  	ContainerState_CONTAINER_CREATED ContainerState = 0
    55  	// ContainerState_CONTAINER_CREATED means that the container is running.
    56  	ContainerState_CONTAINER_RUNNING ContainerState = 1
    57  	// ContainerState_CONTAINER_CREATED means that the container has exited.
    58  	ContainerState_CONTAINER_EXITED ContainerState = 2
    59  	// ContainerState_CONTAINER_CREATED means that the container state is not known.
    60  	ContainerState_CONTAINER_UNKNOWN ContainerState = 3
    61  )
    62  
    63  // PodSandboxInfo contains metadata information about pod sandbox instance
    64  type PodSandboxInfo struct {
    65  	// Pod ID.
    66  	PodID string
    67  	// Sandbox configuration information.
    68  	Config *PodSandboxConfig
    69  	// Creation timestamp.
    70  	CreatedAt int64
    71  	// Sandbox state.
    72  	State PodSandboxState
    73  	// Sandbox network state.
    74  	ContainerSideNetwork *network.ContainerSideNetwork
    75  }
    76  
    77  // ContainerInfo contains metadata information about container instance
    78  type ContainerInfo struct {
    79  	// Container ID
    80  	Id string
    81  	// Container name
    82  	Name string
    83  	// Container creation timestamp
    84  	CreatedAt int64
    85  	// Container startup timestamp
    86  	StartedAt int64
    87  	// Current state of the container
    88  	State ContainerState
    89  	// Container configuration
    90  	Config VMConfig
    91  }
    92  
    93  // VMStats contains cpu/memory/disk usage for VM.
    94  type VMStats struct {
    95  	// ContainerID holds identifier of container for which these statistics
    96  	// were collected
    97  	ContainerID string
    98  	// Name holds name of the container
    99  	Name string
   100  	// Timestatmp holds an unix timestamp (including nanoseconds)
   101  	// for stats collection
   102  	Timestamp int64
   103  	// CpuUsage in nano seconds per cpu
   104  	CpuUsage uint64
   105  	// MemoryUsage is expected to contain the amount of working set memory
   106  	// in bytes what in our case will be returned using RSS value
   107  	MemoryUsage uint64
   108  	// FsBytes represents current size of rootfs in bytes
   109  	FsBytes uint64
   110  }
   111  
   112  // NamespaceOption provides options for Linux namespaces.
   113  type NamespaceOption struct {
   114  	// If set, use the host's network namespace.
   115  	HostNetwork bool
   116  	// If set, use the host's PID namespace.
   117  	HostPid bool
   118  	// If set, use the host's IPC namespace.
   119  	HostIpc bool
   120  }
   121  
   122  // PodSandboxFilter is used to filter a list of PodSandboxes.
   123  // All those fields are combined with 'AND'
   124  type PodSandboxFilter struct {
   125  	// ID of the sandbox.
   126  	Id string
   127  	// State of the sandbox.
   128  	State *PodSandboxState
   129  	// LabelSelector to select matches.
   130  	// Only api.MatchLabels is supported for now and the requirements
   131  	// are ANDed. MatchExpressions is not supported yet.
   132  	LabelSelector map[string]string
   133  }
   134  
   135  // DNSConfig specifies the DNS servers and search domains of a sandbox.
   136  type DNSConfig struct {
   137  	// List of DNS servers of the cluster.
   138  	Servers []string
   139  	// List of DNS search domains of the cluster.
   140  	Searches []string
   141  	// List of DNS options. See https://linux.die.net/man/5/resolv.conf
   142  	// for all available options.
   143  	Options []string
   144  }
   145  
   146  // PortMapping specifies the port mapping configurations of a sandbox.
   147  type PortMapping struct {
   148  	// Protocol of the port mapping.
   149  	Protocol Protocol
   150  	// Port number within the container. Default: 0 (not specified).
   151  	ContainerPort int32
   152  	// Port number on the host. Default: 0 (not specified).
   153  	HostPort int32
   154  	// Host IP.
   155  	HostIp string
   156  }
   157  
   158  // PodSandboxConfig holds all the required and optional fields for creating a
   159  // sandbox.
   160  type PodSandboxConfig struct {
   161  	// Pod name of the sandbox.
   162  	Name string
   163  	// Pod UID of the sandbox.
   164  	Uid string
   165  	// Pod namespace of the sandbox.
   166  	Namespace string
   167  	// Attempt number of creating the sandbox. Default: 0.
   168  	Attempt uint32
   169  	// Hostname of the sandbox.
   170  	Hostname string
   171  	// Path to the directory on the host in which container log files are
   172  	// stored.
   173  	LogDirectory string
   174  	// DNS config for the sandbox.
   175  	DnsConfig *DNSConfig
   176  	// Port mappings for the sandbox.
   177  	PortMappings []*PortMapping
   178  	// Key-value pairs that may be used to scope and select individual resources.
   179  	Labels map[string]string
   180  	// Unstructured key-value map that may be set by the kubelet to store and
   181  	// retrieve arbitrary metadata. This will include any annotations set on a
   182  	// pod through the Kubernetes API.
   183  	Annotations map[string]string
   184  	// Optional configurations specific to Linux hosts.
   185  	CgroupParent string
   186  }
   187  
   188  // VMKeyValue denotes a key-value pair.
   189  type VMKeyValue struct {
   190  	// Key contains the key part of the pair.
   191  	Key string
   192  	// Value contains the value part of the pair.
   193  	Value string
   194  }
   195  
   196  // VMMount denotes a host directory corresponding to a volume which is
   197  // to be mounted inside the VM.
   198  type VMMount struct {
   199  	// ContainerPath specifies the mount path in the container namespace.
   200  	ContainerPath string
   201  	// HostPath specifies the mount path in the host namespace.
   202  	HostPath string
   203  	// If set, the mount is read-only.
   204  	Readonly bool
   205  }
   206  
   207  // VMVolumeDevice denotes a raw block device mapping within a VM which
   208  // is used for block PVs.
   209  type VMVolumeDevice struct {
   210  	// DevicePath specifies the path to the device inside the VM.
   211  	DevicePath string
   212  	// HostPath specifies the mount path in the host namespace.
   213  	HostPath string
   214  }
   215  
   216  // IsRoot returns true if this volume device should be used for a
   217  // persistent root filesystem, that is, its DevicePath is "/"
   218  func (d VMVolumeDevice) IsRoot() bool {
   219  	return d.DevicePath == "/"
   220  }
   221  
   222  // UUID returns an uuid that uniquely identifies the block device on
   223  // the host.
   224  func (dev VMVolumeDevice) UUID() string {
   225  	return utils.NewUUID5(blockVolumeNsUUID, dev.HostPath)
   226  }
   227  
   228  // VMConfig contains the information needed to start create a VM
   229  // TODO: use this struct to store VM metadata.
   230  type VMConfig struct {
   231  	// Id of the containing pod sandbox.
   232  	PodSandboxID string
   233  	// Name of the containing pod sandbox.
   234  	PodName string
   235  	// Namespace of the containing pod sandbox.
   236  	PodNamespace string
   237  	// Name of the container (VM).
   238  	Name string
   239  	// Image to use for the VM.
   240  	Image string
   241  	// Attempt is the number of container creation attempts before this one.
   242  	Attempt uint32
   243  	// Memory limit in bytes. Default: 0 (not specified).
   244  	MemoryLimitInBytes int64
   245  	// CPU shares (relative weight vs. other containers). Default: 0 (not specified).
   246  	CPUShares int64
   247  	// CPU CFS (Completely Fair Scheduler) period. Default: 0 (not specified).
   248  	CPUPeriod int64
   249  	// CPU CFS (Completely Fair Scheduler) quota. Default: 0 (not specified).
   250  	CPUQuota int64
   251  	// Annotations for the containing pod.
   252  	PodAnnotations map[string]string
   253  	// Annotations for the container.
   254  	ContainerAnnotations map[string]string
   255  	// Labels for the container.
   256  	ContainerLabels map[string]string
   257  	// Parsed representation of pod annotations. Populated by LoadAnnotations() call.
   258  	ParsedAnnotations *VirtletAnnotations
   259  	// Domain UUID (set by the CreateContainer).
   260  	// TODO: this field should be moved to VMStatus
   261  	DomainUUID string
   262  	// Environment variables to set in the VM.
   263  	Environment []VMKeyValue
   264  	// Host directories corresponding to the volumes which are to.
   265  	// be mounted inside the VM
   266  	Mounts []VMMount
   267  	// Host block devices that should be made available inside the VM.
   268  	// This is used for block PVs.
   269  	VolumeDevices []VMVolumeDevice
   270  	// ContainerSideNetwork stores info about container side network configuration.
   271  	ContainerSideNetwork *network.ContainerSideNetwork
   272  	// Path to the directory on the host in which container log files are
   273  	// stored.
   274  	LogDirectory string
   275  	// Path relative to LogDirectory for container to store the
   276  	// log (STDOUT and STDERR) on the host.
   277  	LogPath string
   278  }
   279  
   280  // RootVolumeDevice returns the volume device that should be used for
   281  // a persistent root filesystem, that is, its DevicePath is "/"
   282  func (c *VMConfig) RootVolumeDevice() *VMVolumeDevice {
   283  	for n := range c.VolumeDevices {
   284  		dev := &c.VolumeDevices[n]
   285  		if dev.IsRoot() {
   286  			return dev
   287  		}
   288  	}
   289  	return nil
   290  }
   291  
   292  // LoadAnnotations parses pod annotations in the VM config an
   293  // populates the ParsedAnnotations field.
   294  func (c *VMConfig) LoadAnnotations() error {
   295  	ann, err := loadAnnotations(c.PodNamespace, c.PodAnnotations)
   296  	if err != nil {
   297  		return err
   298  	}
   299  	c.ParsedAnnotations = ann
   300  	return nil
   301  }
   302  
   303  // ContainerFilter is used to filter containers.
   304  // All those fields are combined with 'AND'
   305  type ContainerFilter struct {
   306  	// ID of the container.
   307  	Id string
   308  	// State of the container.
   309  	State *ContainerState
   310  	// ID of the PodSandbox.
   311  	PodSandboxID string
   312  	// LabelSelector to select matches.
   313  	// Only api.MatchLabels is supported for now and the requirements
   314  	// are ANDed. MatchExpressions is not supported yet.
   315  	LabelSelector map[string]string
   316  }
   317  
   318  // VMStatsFilter is used to filter set of container stats
   319  // All those fields are combined with 'AND'
   320  type VMStatsFilter struct {
   321  	// ID holds of the container.
   322  	Id string
   323  	// PodSandboxID holds id of podsandbox.
   324  	PodSandboxID string
   325  	// LabelSelector to select matches. Requirementes should be ANDed.
   326  	// Match Expressions is not supported.
   327  	LabelSelector map[string]string
   328  }
   329  
   330  // FilesystemStats contains info about filesystem mountpoint and
   331  // space/inodes used by images on it
   332  type FilesystemStats struct {
   333  	// Mountpoint denotes the filesystem mount point
   334  	Mountpoint string
   335  	// UsedBytes is the number of bytes used by images
   336  	UsedBytes uint64
   337  	// UsedInodes is the number of inodes used by images
   338  	UsedInodes uint64
   339  }