Skip to content

Commit

Permalink
kubelet: Fix fs quota monitoring on volumes
Browse files Browse the repository at this point in the history
Signed-off-by: Paco Xu <paco.xu@daocloud.io>
  • Loading branch information
pacoxu committed Feb 3, 2023
1 parent 43e6bb0 commit a38b256
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
4 changes: 2 additions & 2 deletions pkg/volume/util/fsquota/project.go
Expand Up @@ -190,13 +190,13 @@ func addDirToProject(path string, id common.QuotaID, list *projectsList) (common
idMap := make(map[common.QuotaID]bool)
for _, project := range list.projects {
if project.data == path {
if id != project.id {
if id != common.BadQuotaID && id != project.id {
return common.BadQuotaID, false, fmt.Errorf("attempt to reassign project ID for %s", path)
}
// Trying to reassign a directory to the project it's
// already in. Maybe this should be an error, but for
// now treat it as an idempotent operation
return id, false, nil
return project.id, false, nil
}
idMap[project.id] = true
}
Expand Down
20 changes: 16 additions & 4 deletions pkg/volume/util/fsquota/quota_linux.go
Expand Up @@ -38,6 +38,9 @@ import (
// Pod -> ID
var podQuotaMap = make(map[types.UID]common.QuotaID)

// Pod -> External Pod UID
var podUidMap = make(map[types.UID]types.UID)

// Dir -> ID (for convenience)
var dirQuotaMap = make(map[string]common.QuotaID)

Expand Down Expand Up @@ -304,7 +307,7 @@ func SupportsQuotas(m mount.Interface, path string) (bool, error) {
// AssignQuota chooses the quota ID based on the pod UID and path.
// If the pod UID is identical to another one known, it may (but presently
// doesn't) choose the same quota ID as other volumes in the pod.
func AssignQuota(m mount.Interface, path string, poduid types.UID, bytes *resource.Quantity) error { //nolint:staticcheck
func AssignQuota(m mount.Interface, path string, externalPodUid types.UID, bytes *resource.Quantity) error { //nolint:staticcheck
if bytes == nil {
return fmt.Errorf("attempting to assign null quota to %s", path)
}
Expand All @@ -319,15 +322,23 @@ func AssignQuota(m mount.Interface, path string, poduid types.UID, bytes *resour
// volumes in a pod, we can simply remove this line of code.
// If and when we decide permanently that we're going to adopt
// one quota per volume, we can rip all of the pod code out.
volumeuid := types.UID(uuid.NewUUID())
if pod, ok := dirPodMap[path]; ok && pod != volumeuid {
return fmt.Errorf("requesting quota on existing directory %s but different pod %s %s", path, pod, volumeuid)
var volumeuid types.UID
if internalPodUid, ok := dirPodMap[path]; ok {
if podUidMap[internalPodUid] != externalPodUid {
return fmt.Errorf("requesting quota on existing directory %s but different pod %s %s", path, podUidMap[internalPodUid], externalPodUid)
}
volumeuid = internalPodUid
} else {
volumeuid = types.UID(uuid.NewUUID())
}
oid, ok := podQuotaMap[volumeuid]
if ok {
if quotaSizeMap[oid] != ibytes {
return fmt.Errorf("requesting quota of different size: old %v new %v", quotaSizeMap[oid], bytes)
}
if _, ok := dirPodMap[path]; ok {
return nil
}
} else {
oid = common.BadQuotaID
}
Expand All @@ -347,6 +358,7 @@ func AssignQuota(m mount.Interface, path string, poduid types.UID, bytes *resour
podQuotaMap[volumeuid] = id
dirQuotaMap[path] = id
dirPodMap[path] = volumeuid
podUidMap[volumeuid] = externalPodUid
podDirCountMap[volumeuid]++
klog.V(4).Infof("Assigning quota ID %d (%d) to %s", id, ibytes, path)
return nil
Expand Down

0 comments on commit a38b256

Please sign in to comment.