forked from moby/buildkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathattr.go
146 lines (135 loc) · 3.17 KB
/
attr.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package dockerui
import (
"net"
"strconv"
"strings"
"time"
"github.com/containerd/platforms"
"github.com/docker/go-units"
"github.com/moby/buildkit/client/llb"
"github.com/moby/buildkit/solver/pb"
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/tonistiigi/go-csvvalue"
)
func parsePlatforms(v string) ([]ocispecs.Platform, error) {
var pp []ocispecs.Platform
for _, v := range strings.Split(v, ",") {
p, err := platforms.Parse(v)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse target platform %s", v)
}
pp = append(pp, platforms.Normalize(p))
}
return pp, nil
}
func parseResolveMode(v string) (llb.ResolveMode, error) {
switch v {
case pb.AttrImageResolveModeDefault, "":
return llb.ResolveModeDefault, nil
case pb.AttrImageResolveModeForcePull:
return llb.ResolveModeForcePull, nil
case pb.AttrImageResolveModePreferLocal:
return llb.ResolveModePreferLocal, nil
default:
return 0, errors.Errorf("invalid image-resolve-mode: %s", v)
}
}
func parseExtraHosts(v string) ([]llb.HostIP, error) {
if v == "" {
return nil, nil
}
out := make([]llb.HostIP, 0)
fields, err := csvvalue.Fields(v, nil)
if err != nil {
return nil, err
}
for _, field := range fields {
key, val, ok := strings.Cut(strings.ToLower(field), "=")
if !ok {
return nil, errors.Errorf("invalid key-value pair %s", field)
}
ip := net.ParseIP(val)
if ip == nil {
return nil, errors.Errorf("failed to parse IP %s", val)
}
out = append(out, llb.HostIP{Host: key, IP: ip})
}
return out, nil
}
func parseShmSize(v string) (int64, error) {
if len(v) == 0 {
return 0, nil
}
kb, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return 0, err
}
return kb, nil
}
func parseUlimits(v string) ([]*pb.Ulimit, error) {
if v == "" {
return nil, nil
}
out := make([]*pb.Ulimit, 0)
fields, err := csvvalue.Fields(v, nil)
if err != nil {
return nil, err
}
for _, field := range fields {
ulimit, err := units.ParseUlimit(field)
if err != nil {
return nil, err
}
out = append(out, &pb.Ulimit{
Name: ulimit.Name,
Soft: ulimit.Soft,
Hard: ulimit.Hard,
})
}
return out, nil
}
func parseNetMode(v string) (pb.NetMode, error) {
if v == "" {
return llb.NetModeSandbox, nil
}
switch v {
case "none":
return llb.NetModeNone, nil
case "host":
return llb.NetModeHost, nil
case "sandbox":
return llb.NetModeSandbox, nil
default:
return 0, errors.Errorf("invalid netmode %s", v)
}
}
func parseSourceDateEpoch(v string) (*time.Time, error) {
if v == "" {
return nil, nil
}
sde, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return nil, errors.Wrapf(err, "invalid SOURCE_DATE_EPOCH: %s", v)
}
tm := time.Unix(sde, 0).UTC()
return &tm, nil
}
func parseLocalSessionIDs(opt map[string]string) map[string]string {
m := map[string]string{}
for k, v := range opt {
if strings.HasPrefix(k, localSessionIDPrefix) {
m[strings.TrimPrefix(k, localSessionIDPrefix)] = v
}
}
return m
}
func filter(opt map[string]string, key string) map[string]string {
m := map[string]string{}
for k, v := range opt {
if strings.HasPrefix(k, key) {
m[strings.TrimPrefix(k, key)] = v
}
}
return m
}