From b217c142e46b7383a42eec1c7b15f4e66d05bd4a Mon Sep 17 00:00:00 2001 From: Cezar Rata Date: Fri, 6 Sep 2024 18:44:39 +0000 Subject: [PATCH] feat: add container create options to support sagemaker Signed-off-by: Cezar Rata --- api/handlers/container/create.go | 46 +++++++++++++++++++++------ api/handlers/container/create_test.go | 1 + api/types/container_types.go | 33 +++++++++++++------ 3 files changed, 61 insertions(+), 19 deletions(-) diff --git a/api/handlers/container/create.go b/api/handlers/container/create.go index de87e7e..c59ae84 100644 --- a/api/handlers/container/create.go +++ b/api/handlers/container/create.go @@ -20,6 +20,7 @@ import ( "github.com/runfinch/finch-daemon/api/response" "github.com/runfinch/finch-daemon/api/types" "github.com/runfinch/finch-daemon/pkg/errdefs" + "github.com/runfinch/finch-daemon/pkg/utility/maputility" ) type containerCreateResponse struct { @@ -44,6 +45,14 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) { } // defaults + rp := req.HostConfig.RestartPolicy + restart := "no" // Docker API default. + if rp.Name != "" { + restart = rp.Name + if rp.MaximumRetryCount > 0 { + restart = fmt.Sprintf("%s:%d", restart, rp.MaximumRetryCount) + } + } stopSignal := "SIGTERM" // nerdctl default. if req.StopSignal != "" { stopSignal = req.StopSignal @@ -56,6 +65,15 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) { if req.HostConfig.Memory > 0 { memory = fmt.Sprint(req.HostConfig.Memory) } + lc := req.HostConfig.LogConfig + logDriver := "json-file" // Docker API default + if lc.Type != "" { + logDriver = lc.Type + } + logOpt := []string{} + if len(lc.Config) > 0 { + logOpt = maputility.Flatten(lc.Config, maputility.KeyEqualsValueFormat) + } // Volumes: // nerdctl expects volumes to be a list of bind mounts or individual user created volumes. @@ -111,8 +129,8 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) { Interactive: false, // TODO: update this after attach supports STDIN TTY: false, // TODO: update this after attach supports STDIN Detach: true, // TODO: current implementation of create does not support AttachStdin, AttachStdout, and AttachStderr flags - Restart: "no", // Docker API default. - Rm: req.HostConfig.AutoRemove, // Automatically remove container upon exit + Restart: restart, // Restart policy to apply when a container exits. + Rm: req.HostConfig.AutoRemove, // Automatically remove container upon exit. Pull: "missing", // nerdctl default. StopSignal: stopSignal, StopTimeout: stopTimeout, @@ -127,11 +145,12 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) { // #endregion // #region for resource flags - Memory: memory, // memory limit (in bytes) - CPUQuota: -1, // nerdctl default. - MemorySwappiness64: -1, // nerdctl default. - PidsLimit: -1, // nerdctl default. - Cgroupns: defaults.CgroupnsMode(), // nerdctl default. + CPUShares: uint64(req.HostConfig.CPUShares), // CPU shares (relative weight) + Memory: memory, // memory limit (in bytes) + CPUQuota: -1, // nerdctl default. + MemorySwappiness64: -1, // nerdctl default. + PidsLimit: -1, // nerdctl default. + Cgroupns: defaults.CgroupnsMode(), // nerdctl default. // #endregion // #region for user flags @@ -166,7 +185,8 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) { // #endregion // #region for logging flags - LogDriver: "json-file", // nerdctl default. + LogDriver: logDriver, // logging driver for the container + LogOpt: logOpt, // logging driver specific options // #endregion // #region for image pull and verify options @@ -190,10 +210,16 @@ func (h *handler) create(w http.ResponseWriter, r *http.Request) { if networkMode == "" || networkMode == "default" { networkMode = "bridge" } + dnsOpt := []string{} + if req.HostConfig.DNSOptions != nil { + dnsOpt = req.HostConfig.DNSOptions + } netOpt := ncTypes.NetworkOptions{ Hostname: req.Hostname, - NetworkSlice: []string{networkMode}, // TODO: Set to none if "NetworkDisabled" is true in request - DNSResolvConfOptions: []string{}, // nerdctl default. + NetworkSlice: []string{networkMode}, // TODO: Set to none if "NetworkDisabled" is true in request + DNSServers: req.HostConfig.DNS, // Custom DNS lookup servers. + DNSResolvConfOptions: dnsOpt, // DNS options. + DNSSearchDomains: req.HostConfig.DNSSearch, // Custom DNS search domains. PortMappings: portMappings, } diff --git a/api/handlers/container/create_test.go b/api/handlers/container/create_test.go index 5a09f5a..68f5085 100644 --- a/api/handlers/container/create_test.go +++ b/api/handlers/container/create_test.go @@ -493,6 +493,7 @@ func getDefaultCreateOpt(conf config.Config) types.ContainerCreateOptions { // #region for logging flags LogDriver: "json-file", // nerdctl default. + LogOpt: []string{}, // #endregion // #region for image pull and verify types diff --git a/api/types/container_types.go b/api/types/container_types.go index 4c8beb7..7090117 100644 --- a/api/types/container_types.go +++ b/api/types/container_types.go @@ -59,11 +59,11 @@ type ContainerHostConfig struct { // Applicable to all platforms Binds []string // List of volume bindings for this container // TODO: ContainerIDFile string // File (path) where the containerId is written - // TODO: LogConfig LogConfig // Configuration of the logs for this container - NetworkMode string // Network mode to use for the container - PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host - // TODO: RestartPolicy RestartPolicy // Restart policy to be used for the container - AutoRemove bool // Automatically remove container when it exits + LogConfig LogConfig // Configuration of the logs for this container + NetworkMode string // Network mode to use for the container + PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host + RestartPolicy RestartPolicy // Restart policy to be used for the container + AutoRemove bool // Automatically remove container when it exits // TODO: VolumeDriver string // Name of the volume driver used to mount volumes // TODO: VolumesFrom []string // List of volumes to take from other container // TODO: ConsoleSize [2]uint // Initial console size (height,width) @@ -73,9 +73,9 @@ type ContainerHostConfig struct { CapAdd []string // List of kernel capabilities to add to the container // TODO: CapDrop strslice.StrSlice // List of kernel capabilities to remove from the container // TODO: CgroupnsMode CgroupnsMode // Cgroup namespace mode to use for the container - // TODO: DNS []string `json:"Dns"` // List of DNS server to lookup - // TODO: DNSOptions []string `json:"DnsOptions"` // List of DNSOption to look for - // TODO: DNSSearch []string `json:"DnsSearch"` // List of DNSSearch to look for + DNS []string `json:"Dns"` // List of DNS server to lookup + DNSOptions []string `json:"DnsOptions"` // List of DNSOption to look for + DNSSearch []string `json:"DnsSearch"` // List of DNSSearch to look for // TODO: ExtraHosts []string // List of extra hosts // TODO: GroupAdd []string // List of additional groups that the container process will run as // TODO: IpcMode IpcMode // IPC namespace to use for the container @@ -99,7 +99,8 @@ type ContainerHostConfig struct { // TODO: Isolation Isolation // Isolation technology of the container (e.g. default, hyperv) // Contains container's resources (cgroups, ulimits) - Memory int64 // Memory limit (in bytes) + CPUShares int64 `json:"CpuShares"` // CPU shares (relative weight vs. other containers) + Memory int64 // Memory limit (in bytes) // TODO: Resources // Mounts specs used by the container @@ -115,6 +116,20 @@ type ContainerHostConfig struct { // TODO: Init *bool `json:",omitempty"` } +// LogConfig represents the logging configuration of the container. +// From https://github.com/moby/moby/blob/v24.0.2/api/types/container/hostconfig.go#L319-L323 +type LogConfig struct { + Type string + Config map[string]string +} + +// RestartPolicy represents the restart policies of the container. +// From https://github.com/moby/moby/blob/v24.0.2/api/types/container/hostconfig.go#L272-L276 +type RestartPolicy struct { + Name string + MaximumRetryCount int +} + type ContainerCreateRequest struct { ContainerConfig HostConfig ContainerHostConfig