Skip to content

Commit

Permalink
add --init and --entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
turtletowerz committed Oct 27, 2024
1 parent 807f3c4 commit 3aaf61e
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 20 deletions.
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ done in a separate Github Action.


### Current Unsupported Flags
- `--entrypoint`
- `--gpus`
- `--init`
- `--mount`
- `--tmpfs`
- `--ip`
- `--ip6`
- `--network-alias`
Expand Down
9 changes: 7 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,22 @@ func parseFromJSON(cli *client.Client, ct *types.ContainerJSON) ([]string, error
optSlice[string]{ct.HostConfig.Binds, nil, "-v "},
optSlice[string]{ct.HostConfig.VolumesFrom, nil, "--volumes-from="},
opt[string]{ct.HostConfig.VolumeDriver, "", "--volume-driver="},
optFunc[map[string]string]{ct.HostConfig.Tmpfs, handleTmpFS},

// Misc popular options
opt[string]{ct.Config.WorkingDir, imgdata.Config.WorkingDir, "--workdir="},
opt[string]{ct.HostConfig.LogConfig.Type, "json-file", "--log-driver="},
optMap{ct.HostConfig.LogConfig.Config, "--log-opt "},
optFunc[*labels]{&labels{ct.Config.Labels, imgdata.Config.Labels}, handleLabels},
optFunc[*capabilities]{&capabilities{ct.HostConfig.CapAdd, ct.HostConfig.CapDrop}, handleCapabilities},
optFunc[twoOf[map[string]string]]{twoOf[map[string]string]{ct.Config.Labels, imgdata.Config.Labels, "--label="}, handleLabels},
optFunc[twoOf[[]string]]{twoOf[[]string]{ct.HostConfig.CapAdd, ct.HostConfig.CapDrop, ""}, handleCapabilities},
opt[bool]{ct.HostConfig.ReadonlyRootfs, false, "--read-only"},
optSlice[string]{ct.HostConfig.SecurityOpt, nil, "--security-opt="},
optMap{ct.HostConfig.StorageOpt, "--storage-opt "},
optMap{ct.HostConfig.Sysctls, "--sysctl "},

// Commands
optFunc[twoOf[[]string]]{twoOf[[]string]{ct.Config.Entrypoint, imgdata.Config.Entrypoint, "--entrypoint="}, handleCommand},

// Networking stuff
// TODO: Put hostname, MAC address and other network settings behind an optional network flag?
optFunc[*types.ContainerJSON]{ct, handlePorts},
Expand Down Expand Up @@ -115,6 +119,7 @@ func parseFromJSON(cli *client.Client, ct *types.ContainerJSON) ([]string, error
optPtr[int64]{ct.HostConfig.PidsLimit, -1, "--pids-limit="},
optSlice[string]{ct.HostConfig.GroupAdd, nil, "--group-add "},
optSlice[*container.Ulimit]{ct.HostConfig.Ulimits, nil, "--ulimit "},
optPtr[bool]{ct.HostConfig.Init, false, "--init"},

opt[uint16]{ct.HostConfig.BlkioWeight, 0, "--blkio-weight="},
optSlice[*blkiodev.WeightDevice]{ct.HostConfig.BlkioWeightDevice, nil, "--blkio-weight-device="},
Expand Down
47 changes: 32 additions & 15 deletions parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,22 @@ func (o optFunc[T]) Values() []string {
return o.f(o.v)
}

type capabilities struct {
add []string
drop []string
type twoOf[T any] struct {
first T
second T
name string
}

func handleCapabilities(cap *capabilities) (ret []string) {
func handleCapabilities(cap twoOf[[]string]) (ret []string) {
defaults := caps.DefaultCapabilities()

for _, c := range cap.add {
for _, c := range cap.first {
if !slices.Contains(defaults, "CAP_"+c) {
ret = append(ret, "--cap-add=CAP_"+c)
}
}

for _, c := range cap.drop {
for _, c := range cap.second {
if slices.Contains(defaults, "CAP_"+c) {
ret = append(ret, "--cap-drop=CAP_"+c)
}
Expand Down Expand Up @@ -170,15 +171,10 @@ func handleDevices(devices []container.DeviceMapping) (ret []string) {
return
}

type labels struct {
ctlabels map[string]string
imglabels map[string]string
}

func handleLabels(l *labels) (ret []string) {
for k, v := range l.ctlabels {
if iv, ok := l.imglabels[k]; !ok || v != iv {
ret = append(ret, "--label='"+k+"="+v+"'")
func handleLabels(l twoOf[map[string]string]) (ret []string) {
for k, v := range l.first {
if iv, ok := l.second[k]; !ok || v != iv {
ret = append(ret, l.name+"'"+k+"="+v+"'")
}
}
return
Expand Down Expand Up @@ -251,3 +247,24 @@ func handlePorts(ctdata *types.ContainerJSON) (ret []string) {

return
}

// https://github.com/moby/moby/blob/27.x/integration/internal/container/ops.go#L138
func handleTmpFS(tmpfs map[string]string) (ret []string) {
for k, v := range tmpfs {
ret = append(ret, "--tmpfs="+k+":"+v)
}
return
}

func handleCommand(cmds twoOf[[]string]) []string {
if len(cmds.first) == 0 {
return nil
}

if slices.Compare(cmds.first, cmds.second) == 0 {
return nil
}

// TODO: Escape quotes
return []string{cmds.name + strconv.Quote(strings.Join(cmds.first, " "))}
}

0 comments on commit 3aaf61e

Please sign in to comment.