Skip to content

Commit 3d77778

Browse files
Merge pull request #1081 from ggaaooppeenngg/gaopeng/replace-range-map
Refactor enum map range to slice range
2 parents d9fec4c + c5393da commit 3d77778

File tree

2 files changed

+33
-23
lines changed

2 files changed

+33
-23
lines changed

libcontainer/factory_linux.go

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -223,17 +223,21 @@ func (l *LinuxFactory) Type() string {
223223
// This is a low level implementation detail of the reexec and should not be consumed externally
224224
func (l *LinuxFactory) StartInitialization() (err error) {
225225
var pipefd, rootfd int
226-
for k, v := range map[string]*int{
227-
"_LIBCONTAINER_INITPIPE": &pipefd,
228-
"_LIBCONTAINER_STATEDIR": &rootfd,
226+
for _, pair := range []struct {
227+
k string
228+
v *int
229+
}{
230+
{"_LIBCONTAINER_INITPIPE", &pipefd},
231+
{"_LIBCONTAINER_STATEDIR", &rootfd},
229232
} {
230-
s := os.Getenv(k)
233+
234+
s := os.Getenv(pair.k)
231235

232236
i, err := strconv.Atoi(s)
233237
if err != nil {
234-
return fmt.Errorf("unable to convert %s=%s to int", k, s)
238+
return fmt.Errorf("unable to convert %s=%s to int", pair.k, s)
235239
}
236-
*v = i
240+
*pair.v = i
237241
}
238242
var (
239243
pipe = os.NewFile(uintptr(pipefd), "pipe")

update.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -155,33 +155,39 @@ other options are ignored.
155155
r.CPU.Mems = &val
156156
}
157157

158-
for opt, dest := range map[string]*uint64{
159-
"cpu-period": r.CPU.Period,
160-
"cpu-quota": r.CPU.Quota,
161-
"cpu-share": r.CPU.Shares,
158+
for _, pair := range []struct {
159+
opt string
160+
dest *uint64
161+
}{
162+
163+
{"cpu-period", r.CPU.Period},
164+
{"cpu-quota", r.CPU.Quota},
165+
{"cpu-share", r.CPU.Shares},
162166
} {
163-
if val := context.String(opt); val != "" {
167+
if val := context.String(pair.opt); val != "" {
164168
var err error
165-
*dest, err = strconv.ParseUint(val, 10, 64)
169+
*pair.dest, err = strconv.ParseUint(val, 10, 64)
166170
if err != nil {
167-
return fmt.Errorf("invalid value for %s: %s", opt, err)
171+
return fmt.Errorf("invalid value for %s: %s", pair.opt, err)
168172
}
169173
}
170174
}
171-
172-
for opt, dest := range map[string]*uint64{
173-
"kernel-memory": r.Memory.Kernel,
174-
"kernel-memory-tcp": r.Memory.KernelTCP,
175-
"memory": r.Memory.Limit,
176-
"memory-reservation": r.Memory.Reservation,
177-
"memory-swap": r.Memory.Swap,
175+
for _, pair := range []struct {
176+
opt string
177+
dest *uint64
178+
}{
179+
{"kernel-memory", r.Memory.Kernel},
180+
{"kernel-memory-tcp", r.Memory.KernelTCP},
181+
{"memory", r.Memory.Limit},
182+
{"memory-reservation", r.Memory.Reservation},
183+
{"memory-swap", r.Memory.Swap},
178184
} {
179-
if val := context.String(opt); val != "" {
185+
if val := context.String(pair.opt); val != "" {
180186
v, err := units.RAMInBytes(val)
181187
if err != nil {
182-
return fmt.Errorf("invalid value for %s: %s", opt, err)
188+
return fmt.Errorf("invalid value for %s: %s", pair.opt, err)
183189
}
184-
*dest = uint64(v)
190+
*pair.dest = uint64(v)
185191
}
186192
}
187193
}

0 commit comments

Comments
 (0)