Skip to content

Commit 7cf077c

Browse files
committed
common/pkg: remove additional cgroups2 conditionals w/o error check
Also inline some struct element values with constant values Signed-off-by: Lokesh Mandvekar <[email protected]>
1 parent 0176dc0 commit 7cf077c

File tree

3 files changed

+12
-53
lines changed

3 files changed

+12
-53
lines changed

common/pkg/cgroups/cgroups_linux_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func TestResources(t *testing.T) {
8989
}
9090

9191
// test CPU Quota adjustment.
92-
u, _, b, _, _, _ := resourcesToProps(&resources, true)
92+
u, _, b, _, _, _ := resourcesToProps(&resources)
9393

9494
val, ok := u["CPUQuotaPerSecUSec"]
9595
if !ok {

common/pkg/cgroups/systemd_linux.go

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,11 @@ func systemdCreate(resources *cgroups.Resources, path string, c *systemdDbus.Con
3232
systemdDbus.PropDescription("cgroup " + name),
3333
systemdDbus.PropWants(slice),
3434
}
35-
var ioString string
36-
v2, _ := IsCgroup2UnifiedMode()
37-
if v2 {
38-
ioString = "IOAccounting"
39-
} else {
40-
ioString = "BlockIOAccounting"
41-
}
4235
pMap := map[string]bool{
4336
"DefaultDependencies": false,
4437
"MemoryAccounting": true,
4538
"CPUAccounting": true,
46-
ioString: true,
39+
"IOAccounting": true,
4740
}
4841
if i == 0 {
4942
pMap["Delegate"] = true
@@ -57,7 +50,7 @@ func systemdCreate(resources *cgroups.Resources, path string, c *systemdDbus.Con
5750
properties = append(properties, p)
5851
}
5952

60-
uMap, sMap, bMap, iMap, structMap, err := resourcesToProps(resources, v2)
53+
uMap, sMap, bMap, iMap, structMap, err := resourcesToProps(resources)
6154
if err != nil {
6255
lastError = err
6356
continue
@@ -150,7 +143,7 @@ func systemdDestroyConn(path string, c *systemdDbus.Conn) error {
150143
return nil
151144
}
152145

153-
func resourcesToProps(res *cgroups.Resources, v2 bool) (map[string]uint64, map[string]string, map[string][]byte, map[string]int64, map[string][]BlkioDev, error) {
146+
func resourcesToProps(res *cgroups.Resources) (map[string]uint64, map[string]string, map[string][]byte, map[string]int64, map[string][]BlkioDev, error) {
154147
bMap := make(map[string][]byte)
155148
// this array is not used but will be once more resource limits are added
156149
sMap := make(map[string]string)
@@ -176,13 +169,8 @@ func resourcesToProps(res *cgroups.Resources, v2 bool) (map[string]uint64, map[s
176169

177170
if res.CpuShares != 0 {
178171
// convert from shares to weight. weight only supports 1-10000
179-
v2, _ := IsCgroup2UnifiedMode()
180-
if v2 {
181-
wt := (1 + ((res.CpuShares-2)*9999)/262142)
182-
uMap["CPUWeight"] = wt
183-
} else {
184-
uMap["CPUShares"] = res.CpuShares
185-
}
172+
wt := (1 + ((res.CpuShares-2)*9999)/262142)
173+
uMap["CPUWeight"] = wt
186174
}
187175

188176
// CPUSet
@@ -212,21 +200,15 @@ func resourcesToProps(res *cgroups.Resources, v2 bool) (map[string]uint64, map[s
212200
case res.Memory == -1 || res.MemorySwap == -1:
213201
swap := -1
214202
uMap["MemorySwapMax"] = uint64(swap)
215-
case v2:
203+
default:
216204
// swap max = swap (limit + swap limit) - limit
217205
uMap["MemorySwapMax"] = uint64(res.MemorySwap - res.Memory)
218-
default:
219-
uMap["MemorySwapMax"] = uint64(res.MemorySwap)
220206
}
221207
}
222208

223209
// Blkio
224210
if res.BlkioWeight > 0 {
225-
if v2 {
226-
uMap["IOWeight"] = uint64(res.BlkioWeight)
227-
} else {
228-
uMap["BlockIOWeight"] = uint64(res.BlkioWeight)
229-
}
211+
uMap["IOWeight"] = uint64(res.BlkioWeight)
230212
}
231213

232214
// systemd requires the paths to be in the form /dev/{block, char}/major:minor
@@ -238,11 +220,7 @@ func resourcesToProps(res *cgroups.Resources, v2 bool) (map[string]uint64, map[s
238220
Device: fmt.Sprintf("/dev/block/%d:%d", entry.Major, entry.Minor),
239221
Bytes: entry.Rate,
240222
}
241-
if v2 {
242-
structMap["IOReadBandwidthMax"] = append(structMap["IOReadBandwidthMax"], newThrottle)
243-
} else {
244-
structMap["BlockIOReadBandwidth"] = append(structMap["BlockIOReadBandwidth"], newThrottle)
245-
}
223+
structMap["IOReadBandwidthMax"] = append(structMap["IOReadBandwidthMax"], newThrottle)
246224
}
247225
}
248226

@@ -252,11 +230,7 @@ func resourcesToProps(res *cgroups.Resources, v2 bool) (map[string]uint64, map[s
252230
Device: fmt.Sprintf("/dev/block/%d:%d", entry.Major, entry.Minor),
253231
Bytes: entry.Rate,
254232
}
255-
if v2 {
256-
structMap["IOWriteBandwidthMax"] = append(structMap["IOWriteBandwidthMax"], newThrottle)
257-
} else {
258-
structMap["BlockIOWriteBandwidth"] = append(structMap["BlockIOWriteBandwidth"], newThrottle)
259-
}
233+
structMap["IOWriteBandwidthMax"] = append(structMap["IOWriteBandwidthMax"], newThrottle)
260234
}
261235
}
262236

@@ -266,11 +240,7 @@ func resourcesToProps(res *cgroups.Resources, v2 bool) (map[string]uint64, map[s
266240
Device: fmt.Sprintf("/dev/block/%d:%d", entry.Major, entry.Minor),
267241
Bytes: uint64(entry.Weight),
268242
}
269-
if v2 {
270-
structMap["IODeviceWeight"] = append(structMap["IODeviceWeight"], newWeight)
271-
} else {
272-
structMap["BlockIODeviceWeight"] = append(structMap["BlockIODeviceWeight"], newWeight)
273-
}
243+
structMap["IODeviceWeight"] = append(structMap["IODeviceWeight"], newWeight)
274244
}
275245
}
276246

common/pkg/config/default.go

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"go.podman.io/common/internal/attributedstring"
1616
nettypes "go.podman.io/common/libnetwork/types"
1717
"go.podman.io/common/pkg/apparmor"
18-
"go.podman.io/common/pkg/cgroups"
1918
"go.podman.io/storage/pkg/fileutils"
2019
"go.podman.io/storage/pkg/homedir"
2120
"go.podman.io/storage/pkg/unshare"
@@ -230,17 +229,12 @@ func defaultConfig() (*Config, error) {
230229
}
231230
}
232231

233-
cgroupNS := "host"
234-
if cgroup2, _ := cgroups.IsCgroup2UnifiedMode(); cgroup2 {
235-
cgroupNS = "private"
236-
}
237-
238232
return &Config{
239233
Containers: ContainersConfig{
240234
Annotations: attributedstring.Slice{},
241235
ApparmorProfile: DefaultApparmorProfile,
242236
BaseHostsFile: "",
243-
CgroupNS: cgroupNS,
237+
CgroupNS: "private",
244238
Cgroups: getDefaultCgroupsMode(),
245239
DNSOptions: attributedstring.Slice{},
246240
DNSSearches: attributedstring.Slice{},
@@ -652,12 +646,7 @@ func (c *Config) PidsLimit() int64 {
652646
if c.Engine.CgroupManager != SystemdCgroupsManager {
653647
return 0
654648
}
655-
cgroup2, _ := cgroups.IsCgroup2UnifiedMode()
656-
if !cgroup2 {
657-
return 0
658-
}
659649
}
660-
661650
return c.Containers.PidsLimit
662651
}
663652

0 commit comments

Comments
 (0)