Skip to content
Merged
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
4 changes: 0 additions & 4 deletions core/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ require (
github.com/go-jose/go-jose/v4 v4.1.4 // indirect
github.com/go-logr/logr v1.4.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-viper/mapstructure/v2 v2.5.0 // indirect
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
github.com/google/go-containerregistry v0.21.6 // indirect
github.com/google/go-intervals v0.0.2 // indirect
Expand Down Expand Up @@ -127,9 +126,6 @@ require (
github.com/vbauerster/cupwriter v0.0.4 // indirect
github.com/vbauerster/mpb/v8 v8.14.0 // indirect
github.com/xanzy/ssh-agent v0.3.3 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
github.com/zitadel/oidc/v3 v3.49.4 // indirect
github.com/zitadel/schema v1.3.2 // indirect
Expand Down
9 changes: 0 additions & 9 deletions core/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ github.com/go-logr/logr v1.4.4 h1:tG4xh9yMsRCAiodLVTxyrkzSZ9+o0L1Kg/+cPVcbP/8=
github.com/go-logr/logr v1.4.4/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-viper/mapstructure/v2 v2.5.0 h1:vM5IJoUAy3d7zRSVtIwQgBj7BiWtMPfmPEgAXnvj1Ro=
github.com/go-viper/mapstructure/v2 v2.5.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 h1:f+oWsMOmNPc8JmEHVZIycC7hBoQxHH9pNKQORJNozsQ=
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8/go.mod h1:wcDNUvekVysuuOpQKo3191zZyTpiI6se1N1ULghS0sw=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -312,13 +310,6 @@ github.com/vbauerster/mpb/v8 v8.14.0 h1:55SR80dptMfASxIG/oCEkBXgBhxeSu4GrVsjl16o
github.com/vbauerster/mpb/v8 v8.14.0/go.mod h1:HgpQPKfcWe3kbuGGPmi+jatHreMase5C3Fp5dpdAy0Q=
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb h1:zGWFAtiMcyryUHoUjUJX0/lt1H2+i2Ka2n+D3DImSNo=
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb/go.mod h1:N2zxlSyiKSe5eX1tZViRH5QA0qijqEDrYZiPEAiq3wU=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 h1:EzJWgHovont7NscjpAxXsDA8S8BMYve8Y5+7cuRE7R0=
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415/go.mod h1:GwrjFmJcFw6At/Gs6z4yjiIwzuJ1/+UwLxMQDVQXShQ=
github.com/xeipuuv/gojsonschema v1.2.0 h1:LhYJRs+L4fBtjZUfuSZIKGeVu0QRy8e5Xi7D17UxZ74=
github.com/xeipuuv/gojsonschema v1.2.0/go.mod h1:anYRn/JVcOK2ZgGU+IjEV4nwlhoK5sQluxsYJ78Id3Y=
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM=
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand Down
32 changes: 3 additions & 29 deletions core/pkg/container/cli/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@
package cli

import (
"fmt"
"strings"

"drassi.run/core/pkg/container/parser"
"drassi.run/core/pkg/container/types"
"github.com/google/shlex"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -69,8 +67,8 @@ func (fm *flagMapper) mapContainerSpec(flags *pflag.FlagSet, copts *containerOpt
Name: copts.name,
PullPolicy: copts.pull,
WorkingDir: copts.workingDir,
Environment: ConvertKVStringsToMap(copts.env.GetAllOrEmpty()),
Labels: ConvertKVStringsToMap(copts.labels.GetAllOrEmpty()),
Environment: parser.ConvertKVStringsToMap(copts.env.GetAllOrEmpty()),
Labels: parser.ConvertKVStringsToMap(copts.labels.GetAllOrEmpty()),
Annotations: copts.annotations.GetAll(),
}
spec := fm.Spec
Expand Down Expand Up @@ -104,27 +102,3 @@ func (fm *flagMapper) mapContainerSpec(flags *pflag.FlagSet, copts *containerOpt

return nil
}

// ConvertMapToKVString converts {"key":"value"} to ["key=value"]
func ConvertMapToKVString(m map[string]string) []string {
if len(m) == 0 {
return nil
}

r := make([]string, 0, len(m))
for k, v := range m {
r = append(r, fmt.Sprintf("%s=%s", k, v))
}
return r
}

// ConvertKVStringsToMap converts ["key=value"] to {"key":"value"}
func ConvertKVStringsToMap(values []string) map[string]string {
result := make(map[string]string, len(values))
for _, value := range values {
k, v, _ := strings.Cut(value, "=")
result[k] = v
}

return result
}
172 changes: 5 additions & 167 deletions core/pkg/container/cli/parse_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,11 @@
package cli

import (
"errors"
"fmt"
"net"
"net/netip"
"strconv"
"strings"

"drassi.run/core/pkg/container/parser"
"drassi.run/core/pkg/container/types"
)

Expand All @@ -39,7 +37,7 @@ func (fm *flagMapper) mapNetwork(copts *containerOptions) error {

func (fm *flagMapper) mapExposes(copts *containerOptions) error {
for _, opt := range copts.expose.GetAllOrEmpty() {
if p, length, err := ParseExpose(opt); err != nil {
if p, length, err := parser.ParseExpose(opt); err != nil {
return err
} else {
for i := range length {
Expand All @@ -53,7 +51,7 @@ func (fm *flagMapper) mapExposes(copts *containerOptions) error {

func (fm *flagMapper) mapPublish(copts *containerOptions) error {
for _, opt := range copts.publish.GetAllOrEmpty() {
if p, length, err := ParsePublish(opt); err != nil {
if p, length, err := parser.ParsePublish(opt); err != nil {
return err
} else {
for i := range length {
Expand Down Expand Up @@ -122,7 +120,7 @@ func (fm *flagMapper) mapEndpoints(copts *containerOptions) error {
}

func (fm *flagMapper) mapDNS(copts *containerOptions) error {
dns := &fm.Spec.DNS
dns := fm.Spec.DNS
if copts.dns.Len() > 0 {
servers := make([]netip.Addr, 0, copts.dns.Len())
for _, s := range copts.dns.GetAllOrEmpty() {
Expand All @@ -143,7 +141,7 @@ func (fm *flagMapper) mapDNS(copts *containerOptions) error {
dns.HostAdd = make(map[string][]string)
}
for _, h := range copts.extraHosts.GetAllOrEmpty() {
if host, ips, err := ParseHost(h); err != nil {
if host, ips, err := parser.ParseHost(h); err != nil {
return err
} else if exist, ok := dns.HostAdd[host]; ok {
dns.HostAdd[host] = append(exist, ips...)
Expand All @@ -154,163 +152,3 @@ func (fm *flagMapper) mapDNS(copts *containerOptions) error {

return nil
}

// ParseExpose parses user-provided exposed port definitions into types.Port format
// - [github.com/containers/podman/v5/pkg/specgenutil.CreateExpose]
func ParseExpose(str string) (*types.Port, uint16, error) {
remains, expose := str, new(types.Port)

if r, p, err := SplitProto(remains); err != nil {
return nil, 0, err
} else {
remains, expose.Protocol = r, p
}

if port, length, err := ParsePortRange(remains); err != nil {
return nil, 0, err
} else {
expose.Number = port
return expose, length, nil
}
}

// ParsePublish parses user-provided publish definitions into types.PortBinding format
// - [github.com/compose-spec/compose-go/v2/types.ParsePortConfig]
// - [github.com/docker/go-connections/nat.ParsePortSpec]
// - [github.com/containers/podman/v5/pkg/specgenutil.CreatePortBindings]
func ParsePublish(str string) (*types.PortBinding, uint16, error) {
remains := str
var hostIP, hostPort, containerPort, proto string

if r, p, err := SplitProto(remains); err != nil {
return nil, 0, err
} else {
remains, proto = r, p
}

if idx := strings.LastIndexByte(remains, ':'); idx != -1 {
remains, containerPort = remains[:idx], remains[idx+1:]
} else {
remains, containerPort = "", remains
}

if remains != "" {
if !strings.ContainsRune(remains, ':') {
remains, hostPort = "", remains
} else if host, port, err := net.SplitHostPort(remains); err != nil {
return nil, 0, fmt.Errorf("invalid publish: %s - %s", str, err)
} else {
remains, hostIP, hostPort = "", host, port
}
}

length := uint16(0)
publish := &types.PortBinding{
HostIP: hostIP,
Protocol: proto,
}

if port, portRange, err := ParsePortRange(containerPort); err != nil {
return nil, 0, err
} else {
publish.ContainerPort = port
length = portRange
}

if hostPort == "" {
return publish, length, nil
}

if port, portRange, err := ParsePortRange(hostPort); err != nil {
return nil, 0, err
} else {
publish.HostPort = port
if portRange > 1 {
if length > 1 && length != portRange {
return nil, 0, fmt.Errorf("invalid publish %q : port-range mismatch", str)
}
length = portRange
}
}

return publish, length, nil
}

var hostListSeparators = []string{"=", ":"}

// ParseHost parses user-provided additional host into hostname and list of IPs
// - [github.com/compose-spec/compose-go/v2/types.NewHostsList]
func ParseHost(s string) (string, []string, error) {
for _, sep := range hostListSeparators {
host, ip, ok := strings.Cut(s, sep)
if ok {
return host, strings.Split(ip, ","), nil
}
}

return "", nil, fmt.Errorf("invalid additional host, missing IP: %s", s)
}

func SplitProto(s string) (string, string, error) {
splits := strings.SplitN(s, "/", 3)
if len(splits) > 2 {
return "", "", fmt.Errorf("invalid protocol: %s - multiple protocols", s)
} else if len(splits) == 2 {
remains, proto := splits[0], splits[1]
if proto == "" {
proto = "tcp"
}
return remains, proto, nil
}
return s, "tcp", nil
}

// ParsePortRange parses specified string as a port-range
// - https://github.com/containers/podman/blob/v5.2.5/pkg/specgenutil/util.go#L216
// - [github.com/docker/go-connections/nat.ParsePortRange]
func ParsePortRange(portRange string) (uint16, uint16, error) {
var (
port string = portRange
endPort *string = nil
)

if splits := strings.SplitN(portRange, "-", 3); len(splits) > 2 {
return 0, 0, fmt.Errorf("invalid portRange: %s - too many parts", portRange)
} else if len(splits) == 2 {
port, endPort = splits[0], &splits[1]
}

var portNum uint16
if num, err := ParsePort(port); err != nil {
return 0, 0, err
} else {
portNum = num
}

if endPort != nil {
if num, err := ParsePort(*endPort); err != nil {
return 0, 0, err
} else if portNum >= num {
return 0, 0, fmt.Errorf("invalid portRange: %s - startPort >= endPort", portRange)
} else {
length := num - portNum + 1
return portNum, length, nil
}
}

return portNum, 1, nil
}

// ParsePort turn a single string into a valid U16 port.
// - https://github.com/containers/podman/blob/v5.2.5/pkg/specgenutil/util.go#L253-L262
// - [github.com/docker/go-connections/nat.ParsePort]
func ParsePort(port string) (uint16, error) {
num, err := strconv.ParseUint(port, 10, 16)
if err != nil {
if errors.Is(err, strconv.ErrRange) {
return 0, fmt.Errorf("invalid port: %s - must be in range [1, 65535]", port)
}
return 0, fmt.Errorf("invalid port: %s - %w", port, err)
}
return uint16(num), nil
}
8 changes: 4 additions & 4 deletions core/pkg/container/cli/parse_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (fm *flagMapper) mapResource(copts *containerOptions) error {
//// Applicable to all platforms
res.CPUShares = copts.cpuShares
res.CPUS = copts.cpus.String()
res.Memory = int64(copts.memory)
res.Memory = types.UnitBytes(copts.memory)
//// Applicable to Windows
res.CPUCount = copts.cpuCount
res.CPUPercent = float32(copts.cpuPercent) / 100.0
Expand All @@ -29,10 +29,10 @@ func (fm *flagMapper) mapResource(copts *containerOptions) error {
res.CPURTRuntime = copts.cpuRealtimeRuntime
res.CpusetCpus = copts.cpusetCpus
res.CpusetMems = copts.cpusetMems
res.MemReservation = int64(copts.memoryReservation)
res.MemSwapLimit = int64(copts.memorySwap)
res.MemReservation = types.UnitBytes(copts.memoryReservation)
res.MemSwapLimit = types.UnitBytes(copts.memorySwap)
res.MemSwappiness = copts.swappiness
res.ShmSize = int64(copts.shmSize)
res.ShmSize = types.UnitBytes(copts.shmSize)
res.OomKillDisable = copts.oomKillDisable
res.OomScoreAdj = int64(copts.oomScoreAdj)
res.PidsLimit = copts.pidsLimit
Expand Down
3 changes: 2 additions & 1 deletion core/pkg/container/cli/parse_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"fmt"
"time"

"drassi.run/core/pkg/container/parser"
"drassi.run/core/pkg/container/types"
"github.com/spf13/pflag"
)
Expand Down Expand Up @@ -40,7 +41,7 @@ func (fm *flagMapper) mapLogging(copts *containerOptions) error {

// https://github.com/docker/cli/blob/v29.7.2/cli/command/container/opts.go#L920-L926
driver := copts.loggingDriver
options := ConvertKVStringsToMap(copts.loggingOpts.GetAllOrEmpty())
options := parser.ConvertKVStringsToMap(copts.loggingOpts.GetAllOrEmpty())
if driver == "none" && len(options) > 0 {
return fmt.Errorf("invalid logging opts for driver %s", driver)
}
Expand Down
Loading
Loading