Skip to content

Commit e09c6db

Browse files
committed
feature: set quota id for a container
Specified quota id for a container, if id < 0, it means pouchd alloc a unique quota id. If specified quota id, it will ignore the quota's regular expression, but the volume will keep its size as before. Signed-off-by: Rudy Zhang <[email protected]>
1 parent f3d3d24 commit e09c6db

File tree

4 files changed

+61
-9
lines changed

4 files changed

+61
-9
lines changed

cli/common_flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func addCommonFlags(flagSet *pflag.FlagSet) *container {
9292

9393
// disk quota
9494
flagSet.StringSliceVar(&c.diskQuota, "disk-quota", nil, "Set disk quota for container")
95+
flagSet.StringVar(&c.quotaID, "quota-id", "", "Specified quota id, if id < 0, it means pouchd alloc a unique quota id")
9596

9697
// additional runtime spec annotations
9798
flagSet.StringSliceVar(&c.specAnnotation, "annotation", nil, "Additional annotation for runtime")

cli/container.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ type container struct {
6262
capDrop []string
6363
IntelRdtL3Cbm string
6464
diskQuota []string
65+
quotaID string
6566
oomScoreAdj int64
6667
specAnnotation []string
6768
cgroupParent string
@@ -180,6 +181,7 @@ func (c *container) config() (*types.ContainerCreateConfig, error) {
180181
InitScript: c.initScript,
181182
ExposedPorts: ports,
182183
DiskQuota: diskQuota,
184+
QuotaID: c.quotaID,
183185
SpecAnnotation: specAnnotation,
184186
},
185187

daemon/mgr/container.go

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1497,17 +1497,43 @@ func (mgr *ContainerManager) parseBinds(ctx context.Context, meta *ContainerMeta
14971497
}
14981498

14991499
func (mgr *ContainerManager) setMountPointDiskQuota(ctx context.Context, c *ContainerMeta) error {
1500-
qid := 0
1500+
var (
1501+
qid uint32
1502+
setQuotaID bool
1503+
)
1504+
15011505
if c.Config.QuotaID != "" {
1502-
var err error
1503-
qid, err = strconv.Atoi(c.Config.QuotaID)
1506+
id, err := strconv.Atoi(c.Config.QuotaID)
15041507
if err != nil {
15051508
return errors.Wrapf(err, "invalid argument, QuotaID: %s", c.Config.QuotaID)
15061509
}
1510+
1511+
// if QuotaID is < 0, it means pouchd alloc a unique quota id.
1512+
if id < 0 {
1513+
qid, err = quota.GetNextQuatoID()
1514+
if err != nil {
1515+
return errors.Wrap(err, "failed to get next quota id")
1516+
}
1517+
1518+
// update QuotaID
1519+
c.Config.QuotaID = strconv.Itoa(int(qid))
1520+
} else {
1521+
qid = uint32(id)
1522+
}
15071523
}
15081524

1509-
// parse diskquota regexe
1525+
if qid > 0 {
1526+
setQuotaID = true
1527+
}
1528+
1529+
// get rootfs quota
15101530
quotas := c.Config.DiskQuota
1531+
defaultQuota := quota.GetDefaultQuota(quotas)
1532+
if setQuotaID && defaultQuota == "" {
1533+
return fmt.Errorf("set quota id but have no set quota size")
1534+
}
1535+
1536+
// parse diskquota regexe
15111537
res := make([]*quota.RegExp, 0)
15121538
for path, size := range quotas {
15131539
re := regexp.MustCompile(path)
@@ -1537,11 +1563,15 @@ func (mgr *ContainerManager) setMountPointDiskQuota(ctx context.Context, c *Cont
15371563
}
15381564
}
15391565

1540-
if matched {
1541-
err := quota.SetDiskQuota(mp.Source, quotas[mp.Destination], uint32(qid))
1542-
if err != nil {
1543-
return err
1544-
}
1566+
size := ""
1567+
if matched && !setQuotaID {
1568+
size = quotas[mp.Destination]
1569+
} else {
1570+
size = defaultQuota
1571+
}
1572+
err := quota.SetDiskQuota(mp.Source, size, qid)
1573+
if err != nil {
1574+
return err
15451575
}
15461576
}
15471577

pkg/quota/quota.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,22 @@ func SetFileAttrNoOutput(dir string, id uint32) {
120120
func GetNextQuatoID() (uint32, error) {
121121
return Gquota.GetNextQuatoID()
122122
}
123+
124+
//GetDefaultQuota returns the default quota.
125+
func GetDefaultQuota(quotas map[string]string) string {
126+
if quotas == nil {
127+
return ""
128+
}
129+
130+
quota, ok := quotas["/"]
131+
if ok && quota != "" {
132+
return quota
133+
}
134+
135+
quota, ok = quotas[".*"]
136+
if ok && quota != "" {
137+
return quota
138+
}
139+
140+
return ""
141+
}

0 commit comments

Comments
 (0)