-
Notifications
You must be signed in to change notification settings - Fork 949
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: make all quota implementation logic clear
Signed-off-by: Allen Sun <[email protected]>
- Loading branch information
1 parent
e42ceba
commit 55b50ef
Showing
12 changed files
with
610 additions
and
378 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package mgr | ||
|
||
import ( | ||
"regexp" | ||
"strconv" | ||
|
||
"github.com/alibaba/pouch/storage/quota" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// GetQuotaID generate a valid quota ID according to the input one. | ||
// If quotaID < 0, allocate a unique ID via quota driver; | ||
// If quotaID = 0, do not set disk quota; | ||
// Otherwise, return the input one. | ||
// | ||
// first return parameter is quota ID in string; | ||
// second is quota ID in uint32; | ||
// third is an error instance. | ||
func getQuotaID(quotaID string) (string, uint32, error) { | ||
var ( | ||
id int | ||
qid uint32 | ||
err error | ||
) | ||
|
||
if quotaID == "" { | ||
return quotaID, uint32(0), nil | ||
} | ||
|
||
id, err = strconv.Atoi(quotaID) | ||
if err != nil { | ||
return quotaID, uint32(0), errors.Wrapf(err, "invalid argument, quotaID: %s", quotaID) | ||
} | ||
|
||
qid = uint32(id) | ||
if id > 0 { | ||
return quotaID, uint32(0), nil | ||
} | ||
|
||
// if QuotaID is <= 0, it means pouchd alloc a unique quota id. | ||
qid, err = quota.GetNextQuotaID() | ||
if err != nil { | ||
return quotaID, qid, errors.Wrap(err, "failed to get next quota id") | ||
} | ||
quotaID = strconv.Itoa(int(qid)) | ||
|
||
return quotaID, qid, nil | ||
} | ||
|
||
func parseDiskQuota(diskQuotas map[string]string) []*quota.RegExp { | ||
regExps := make(*quota.RegExp, 0) | ||
for path, size := range quotas { | ||
re := regexp.MustCompile(path) | ||
regExps = append(res, "a.RegExp{Pattern: re, Path: path, Size: size}) | ||
} | ||
return regExps | ||
} | ||
|
||
func (mgr *ContainerManager) setDiskQuotaForContainer(mounts []*types.MountPoint, quotas [string]string) error{ | ||
for _, mp := range mounts { | ||
// skip volume mount or replace mode mount | ||
if mp.Replace != "" || mp.Source == "" || mp.Destination == "" { | ||
logrus.Debugf("skip volume mount or replace mode mount") | ||
continue | ||
} | ||
|
||
if mp.Name != "" { | ||
v, err := mgr.VolumeMgr.Get(ctx, mp.Name) | ||
if err != nil { | ||
logrus.Warnf("failed to get volume %s when updating disk quota of container %s: %v", mp.Name, c.ID, err) | ||
continue | ||
} | ||
|
||
if v.Size() != "" { | ||
logrus.Debugf("skip volume %s with size", mp.Name) | ||
continue | ||
} | ||
} | ||
|
||
// skip non-directory path. | ||
if fd, err := os.Stat(mp.Source); err != nil || !fd.IsDir() { | ||
logrus.Debugf("skip non-directory path: %s", mp.Source) | ||
continue | ||
} | ||
|
||
matched := false | ||
for _, re := range diskRegExps { | ||
findStr := re.Pattern.FindString(mp.Destination) | ||
if findStr == mp.Destination { | ||
quotas[mp.Destination] = re.Size | ||
matched = true | ||
if re.Path != ".*" { | ||
break | ||
} | ||
} | ||
} | ||
|
||
size := "" | ||
if matched && !setQuotaID { | ||
size = quotas[mp.Destination] | ||
} else { | ||
size = defaultQuota | ||
} | ||
if err := quota.SetDiskQuota(mp.Source, size, qid); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// +build linux | ||
|
||
package system | ||
|
||
import ( | ||
"syscall" | ||
|
||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// GetDevID returns device id via syscall according to the input directory. | ||
func GetDevID(dir string) (uint64, error) { | ||
var st syscall.Stat_t | ||
if err := syscall.Stat(dir, &st); err != nil { | ||
logrus.Warnf("failed to get device id of dir %s: %v", dir, err) | ||
return 0, err | ||
} | ||
return st.Dev, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
// +build linux | ||
|
||
package system | ||
|
||
import ( | ||
|
Oops, something went wrong.