Skip to content

Add host options dirty #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pkg/cmd/container/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ func Create(ctx context.Context, client *containerd.Client, args []string, netMa
return nil, generateRemoveStateDirFunc(ctx, id, internalLabels), err
}
internalLabels.logURI = logConfig.LogURI
internalLabels.logConfig = logConfig

restartOpts, err := generateRestartOpts(ctx, client, options.Restart, logConfig.LogURI, options.InRun)
if err != nil {
Expand Down Expand Up @@ -647,7 +648,8 @@ type internalLabels struct {
// log
logURI string
// a label to check whether the --rm option is specified.
rm string
rm string
logConfig logging.LogConfig
}

// WithInternalLabels sets the internal labels for a container.
Expand Down Expand Up @@ -678,6 +680,11 @@ func withInternalLabels(internalLabels internalLabels) (containerd.NewContainerO
}
if internalLabels.logURI != "" {
m[labels.LogURI] = internalLabels.logURI
logConfigJSON, err := json.Marshal(internalLabels.logConfig)
if err != nil {
return nil, err
}
m[labels.LogConfig] = string(logConfigJSON)
}
if len(internalLabels.anonVolumes) > 0 {
anonVolumeJSON, err := json.Marshal(internalLabels.anonVolumes)
Expand Down
59 changes: 58 additions & 1 deletion pkg/inspecttypes/dockercompat/dockercompat.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ type ImageMetadata struct {
LastTagTime time.Time `json:",omitempty"`
}

type LogConfig struct {
Type string
Config loggerLogConfig
}

type loggerLogConfig struct {
Driver string `json:"driver"`
Opts map[string]string `json:"opts,omitempty"`
LogURI string `json:"-"`
Address string `json:"address"`
}

// Container mimics a `docker container inspect` object.
// From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L340-L374
type Container struct {
Expand All @@ -116,7 +128,7 @@ type Container struct {
// TODO: ProcessLabel string
AppArmorProfile string
// TODO: ExecIDs []string
// TODO: HostConfig *container.HostConfig
HostConfig *HostConfig
// TODO: GraphDriver GraphDriverData
SizeRw *int64 `json:",omitempty"`
SizeRootFs *int64 `json:",omitempty"`
Expand All @@ -126,6 +138,15 @@ type Container struct {
NetworkSettings *NetworkSettings
}

// From https://github.com/moby/moby/blob/8dbd90ec00daa26dc45d7da2431c965dec99e8b4/api/types/container/host_config.go#L391
// HostConfig the non-portable Config structure of a container.
type HostConfig struct {
ExtraHosts []string // List of extra hosts
PortBindings nat.PortMap // Port mapping between the exposed port (container) and the host
LogConfig LogConfig // Configuration of the logs for this container

}

// From https://github.com/moby/moby/blob/v20.10.1/api/types/types.go#L416-L427
// MountPoint represents a mount point configuration inside the container.
// This is used for reporting the mountpoints in use by a container.
Expand Down Expand Up @@ -282,6 +303,32 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
c.Mounts = mounts
}

c.HostConfig = new(HostConfig)
if nedctlExtraHosts := n.Labels[labels.ExtraHosts]; nedctlExtraHosts != "" {
c.HostConfig.ExtraHosts = parseExtraHosts(nedctlExtraHosts)
}

if nerdctlLoguri := n.Labels[labels.LogURI]; nerdctlLoguri != "" {
c.HostConfig.LogConfig.Type = nerdctlLoguri
// c.HostConfig.LogConfig.Config = map[string]string{}
}
if logConfigJSON, ok := n.Labels[labels.LogConfig]; ok {
var logConfig loggerLogConfig
err := json.Unmarshal([]byte(logConfigJSON), &logConfig)
if err != nil {
return nil, fmt.Errorf("failed to unmarshal log config: %v", err)
}

// Assign the parsed LogConfig to c.HostConfig.LogConfig
c.HostConfig.LogConfig.Config = logConfig
} else {
// If LogConfig label is not present, set default values
c.HostConfig.LogConfig.Config = loggerLogConfig{
Driver: "json-file",
Opts: make(map[string]string),
}
}

cs := new(ContainerState)
cs.Restarting = n.Labels[restart.StatusLabel] == string(containerd.Running)
cs.Error = n.Labels[labels.Error]
Expand All @@ -308,6 +355,7 @@ func ContainerFromNative(n *native.Container) (*Container, error) {
return nil, err
}
c.NetworkSettings = nSettings
c.HostConfig.PortBindings = *nSettings.Ports
}
c.State = cs
c.Config = &Config{
Expand Down Expand Up @@ -491,6 +539,15 @@ func convertToNatPort(portMappings []cni.PortMapping) (*nat.PortMap, error) {
return &portMap, nil
}

func parseExtraHosts(extraHostsJSON string) []string {
var extraHosts []string
if err := json.Unmarshal([]byte(extraHostsJSON), &extraHosts); err != nil {
// Handle error or return empty slice
return []string{}
}
return extraHosts
}

type IPAMConfig struct {
Subnet string `json:"Subnet,omitempty"`
Gateway string `json:"Gateway,omitempty"`
Expand Down
3 changes: 3 additions & 0 deletions pkg/labels/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@ const (
// (like "nerdctl/default-network=true" or "nerdctl/default-network=false")
NerdctlDefaultNetwork = Prefix + "default-network"

// LogConfig defines the loggin configuration passed to the container
LogConfig = Prefix + "log-config"

// ContainerAutoRemove is to check whether the --rm option is specified.
ContainerAutoRemove = Prefix + "auto-remove"
)
Loading