Skip to content

Commit

Permalink
feature: set quota id for a container
Browse files Browse the repository at this point in the history
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 <rudyflyzhang@gmail.com>
  • Loading branch information
rudyfly committed Apr 16, 2018
1 parent 150f947 commit f1ca332
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 12 deletions.
2 changes: 1 addition & 1 deletion apis/swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1786,7 +1786,7 @@ definitions:
type: "string"
QuotaID:
type: "string"
description: "set disk quota by specified quota id"
description: "set disk quota by specified quota id, if id < 0, it means pouchd alloc a unique quota id"

ContainerCreateResp:
description: "response returned by daemon when container create successfully"
Expand Down
2 changes: 1 addition & 1 deletion apis/types/container_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions cli/common_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func addCommonFlags(flagSet *pflag.FlagSet) *container {

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

// additional runtime spec annotations
flagSet.StringSliceVar(&c.specAnnotation, "annotation", nil, "Additional annotation for runtime")
Expand Down
2 changes: 2 additions & 0 deletions cli/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type container struct {
capDrop []string
IntelRdtL3Cbm string
diskQuota []string
quotaID string
oomScoreAdj int64
specAnnotation []string
cgroupParent string
Expand Down Expand Up @@ -180,6 +181,7 @@ func (c *container) config() (*types.ContainerCreateConfig, error) {
InitScript: c.initScript,
ExposedPorts: ports,
DiskQuota: diskQuota,
QuotaID: c.quotaID,
SpecAnnotation: specAnnotation,
},

Expand Down
52 changes: 43 additions & 9 deletions daemon/mgr/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -1497,17 +1497,47 @@ func (mgr *ContainerManager) parseBinds(ctx context.Context, meta *ContainerMeta
}

func (mgr *ContainerManager) setMountPointDiskQuota(ctx context.Context, c *ContainerMeta) error {
qid := 0
if c.Config.DiskQuota == nil {
return nil
}

var (
qid uint32
setQuotaID bool
)

if c.Config.QuotaID != "" {
var err error
qid, err = strconv.Atoi(c.Config.QuotaID)
id, err := strconv.Atoi(c.Config.QuotaID)
if err != nil {
return errors.Wrapf(err, "invalid argument, QuotaID: %s", c.Config.QuotaID)
}

// if QuotaID is < 0, it means pouchd alloc a unique quota id.
if id < 0 {
qid, err = quota.GetNextQuatoID()
if err != nil {
return errors.Wrap(err, "failed to get next quota id")
}

// update QuotaID
c.Config.QuotaID = strconv.Itoa(int(qid))
} else {
qid = uint32(id)
}
}

// parse diskquota regexe
if qid > 0 {
setQuotaID = true
}

// get rootfs quota
quotas := c.Config.DiskQuota
defaultQuota := quota.GetDefaultQuota(quotas)
if setQuotaID && defaultQuota == "" {
return fmt.Errorf("set quota id but have no set default quota size")
}

// parse diskquota regexe
res := make([]*quota.RegExp, 0)
for path, size := range quotas {
re := regexp.MustCompile(path)
Expand Down Expand Up @@ -1537,11 +1567,15 @@ func (mgr *ContainerManager) setMountPointDiskQuota(ctx context.Context, c *Cont
}
}

if matched {
err := quota.SetDiskQuota(mp.Source, quotas[mp.Destination], uint32(qid))
if err != nil {
return err
}
size := ""
if matched && !setQuotaID {
size = quotas[mp.Destination]
} else {
size = defaultQuota
}
err := quota.SetDiskQuota(mp.Source, size, qid)
if err != nil {
return err
}
}

Expand Down
19 changes: 19 additions & 0 deletions pkg/quota/quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,3 +120,22 @@ func SetFileAttrNoOutput(dir string, id uint32) {
func GetNextQuatoID() (uint32, error) {
return Gquota.GetNextQuatoID()
}

//GetDefaultQuota returns the default quota size.
func GetDefaultQuota(quotas map[string]string) string {
if quotas == nil {
return ""
}

quota, ok := quotas["/"]
if ok && quota != "" {
return quota
}

quota, ok = quotas[".*"]
if ok && quota != "" {
return quota
}

return ""
}
5 changes: 4 additions & 1 deletion pkg/quota/set_diskquota.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ func processSetQuotaReexec() {

basefs := os.Args[1]
size := os.Args[2]
id, _ := strconv.Atoi(os.Args[3])
id, err := strconv.Atoi(os.Args[3])
if err != nil {
return
}
qid = uint32(id)

logrus.Infof("set diskquota: %v", os.Args)
Expand Down

0 comments on commit f1ca332

Please sign in to comment.